Skip to content

Commit

Permalink
fix: correctly serialize negative numbers
Browse files Browse the repository at this point in the history
This commit fixes a bug where negative numbers were not correctly serialized, because the value parser confused "references" (which is where special negative tokens for special values exist), and "values" (where negative numbers are just negative numbers). The parser now correctly distinguishes between the two.
  • Loading branch information
lucacasonato committed Dec 11, 2024
1 parent 65d61bd commit 34bd028
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 326 deletions.
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"@marvinh-test/fresh-island": "jsr:@marvinh-test/fresh-island@^0.0.1",
"linkedom": "npm:linkedom@^0.16.11",
"@std/async": "jsr:@std/async@1",
"@std/expect": "jsr:@std/expect@1"
"@std/expect": "jsr:@std/expect@1",
"@std/testing": "jsr:@std/testing@1"
},
"compilerOptions": {
"lib": ["dom", "dom.asynciterable", "deno.ns", "deno.unstable"],
Expand Down
63 changes: 60 additions & 3 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions src/jsonify/__snapshots__/round_trip_test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
export const snapshot = {};

snapshot[`round trip - undefined 1`] = `"-1"`;

snapshot[`round trip - null 1`] = `"-2"`;

snapshot[`round trip - 1 1`] = `"[1]"`;

snapshot[`round trip - 2 1`] = `"[2]"`;

snapshot[`round trip - -2 1`] = `"[-2]"`;

snapshot[`round trip - Infinity 1`] = `"-4"`;

snapshot[`round trip - -Infinity 1`] = `"-5"`;

snapshot[`round trip - 0 1`] = `"[0]"`;

snapshot[`round trip - -0 1`] = `"-6"`;

snapshot[`round trip - NaN 1`] = `"-3"`;

snapshot[`round trip - 0 2`] = `"[0]"`;

snapshot[`round trip - true 1`] = `"[true]"`;

snapshot[`round trip - false 1`] = `"[false]"`;

snapshot[`round trip - 'abc' 1`] = `'["abc"]'`;

snapshot[`round trip - 1n 1`] = `'[["BigInt","1"]]'`;

snapshot[`round trip - -1n 1`] = `'[["BigInt","-1"]]'`;

snapshot[`round trip - [ 1, 2, 3 ] 1`] = `"[[1,2,3],1,2,3]"`;

snapshot[`round trip - [ null, undefined, -2 ] 1`] = `"[[-2,-1,1],-2]"`;

snapshot[`round trip - { a: 1, b: null, c: -2 } 1`] = `'[{"a":1,"b":-2,"c":2},1,-2]'`;

snapshot[`round trip - Uint8Array(3) [ 1, 2, 3 ] 1`] = `'[["Uint8Array","AQID"]]'`;

snapshot[`round trip - 1990-05-31T00:00:00.000Z 1`] = `'[["Date","1990-05-31T00:00:00.000Z"]]'`;

snapshot[`round trip - Map(2) { 1 => null, undefined => -2 } 1`] = `'[["Map",[1,-2,-1,2]],1,-2]'`;

snapshot[`round trip - Set(5) { 1, 2, null, -2, NaN } 1`] = `'[["Set",[1,2,-2,3,-3]],1,2,-2]'`;

snapshot[`round trip - [ 1, <1 empty item>, 3 ] 1`] = `"[[1,-7,2],1,3]"`;

snapshot[`round trip - /foo["]/ 1`] = `'[["RegExp","foo[\\\\"]", ""]]'`;

snapshot[`round trip - { a: { foo: 123 }, b: [ { foo: 123 }, { foo: 123 } ] } 1`] = `'[{"a":1,"b":3},{"foo":2},123,[1,1]]'`;

snapshot[`round trip - <ref *1> { a: 1, b: [Circular *1] } 1`] = `'[{"a":1,"b":0},1]'`;
Loading

0 comments on commit 34bd028

Please sign in to comment.