From 028bdcca60ca69897ea2131e4047c607ec354a3e Mon Sep 17 00:00:00 2001 From: Hrushikesh Date: Thu, 18 Apr 2024 00:58:24 +0530 Subject: [PATCH] Add glyphs to fix Hash collision with bigint and string (#2512) 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. --- packages/fast-stable-stringify/src/__tests__/index-test.ts | 5 ++++- packages/fast-stable-stringify/src/index.ts | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/fast-stable-stringify/src/__tests__/index-test.ts b/packages/fast-stable-stringify/src/__tests__/index-test.ts index 19264bae30f4..0759913ed408 100644 --- a/packages/fast-stable-stringify/src/__tests__/index-test.ts +++ b/packages/fast-stable-stringify/src/__tests__/index-test.ts @@ -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]}'); }); }); diff --git a/packages/fast-stable-stringify/src/index.ts b/packages/fast-stable-stringify/src/index.ts index fd2b9bed55f8..ea3a4c3cd9ff 100644 --- a/packages/fast-stable-stringify/src/index.ts +++ b/packages/fast-stable-stringify/src/index.ts @@ -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: