Skip to content

Commit

Permalink
Add glyphs to fix Hash collision with bigint and string (#2512)
Browse files Browse the repository at this point in the history
Support BigInt and added more tests. Putting Glyph outside the quote avoid collisions. 

```
s(100n) // "100"n
s({foo: 100n}) // {"foo":"100"n}
```

Closes #2496.
  • Loading branch information
Hrushi20 authored Apr 17, 2024
1 parent 291830b commit 028bdcc
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/fast-stable-stringify/src/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ describe('fastStableStringify', function () {
expect(stringify(value)).toBe(jsonStableStringify(value));
});
it('hashes bigints', function () {
expect(stringify(200n)).toMatch('"200n"');
expect(stringify(200n)).toMatch('200n');
expect(stringify({ foo: 100n, goo: '100n' })).toMatch('{"foo":100n,"goo":"100n"}');
expect(stringify({ age: BigInt(100n), name: 'Hrushi' })).toMatch('{"age":100n,"name":"Hrushi"}');
expect(stringify({ age: [BigInt(100n), BigInt(200n), BigInt(300n)] })).toMatch('{"age":[100n,200n,300n]}');
});
});
2 changes: 1 addition & 1 deletion packages/fast-stable-stringify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function stringify(val: unknown, isArrayProp: boolean) {
case 'undefined':
return isArrayProp ? null : undefined;
case 'bigint':
return JSON.stringify(val.toString() + 'n');
return `${val.toString()}n`;
case 'string':
return JSON.stringify(val);
default:
Expand Down

0 comments on commit 028bdcc

Please sign in to comment.