Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/large-integer-real.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"plist": patch
---

Emit large integer-valued numbers (`|x| >= 1e21`) as `<real>` instead of `<integer>`. `Number.prototype.toString()` renders them in exponential notation (`(1e21).toString() === "1e+21"`), which the `<integer>` parse path truncated at the `e`, silently corrupting the value on a `build` → `parse` round-trip.
3 changes: 2 additions & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ function walk_obj(next: unknown, next_child: xmlbuilder.XMLElement): void {
}
}
} else if (typeof next === 'number') {
const tag_type = next % 1 === 0 ? 'integer' : 'real';
const tag_type =
Number.isInteger(next) && Math.abs(next) < 1e21 ? 'integer' : 'real';
next_child.ele(tag_type).txt(next.toString());
} else if (typeof next === 'bigint') {
next_child.ele('integer').txt(next.toString());
Expand Down
6 changes: 6 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ describe('parse()', () => {
const parsed = parseFixture('<integer>14</integer>');
expect(parsed).toBe(14);
});

it('should round-trip integer-valued numbers >= 1e21', () => {
expect(parse(build(1e21))).toBe(1e21);
expect(parse(build(-1e21))).toBe(-1e21);
expect(parse(build(1.23e30))).toBe(1.23e30);
});
});

describe('real', () => {
Expand Down