diff --git a/src/index.ts b/src/index.ts index 0e73994..cb60c8c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -161,6 +161,10 @@ export const replacer = function replacer(options: Options) { return value; } + if (typeof value === 'bigint') { + return `_bigint_${value.toString()}`; + } + if (typeof value === 'string') { if (dateFormat.test(value)) { if (!options.allowDate) { @@ -348,6 +352,10 @@ export const reviver = function reviver(options: Options) { return NaN; } + if (typeof value === 'string' && value.startsWith('_bigint_') && typeof BigInt === 'function') { + return BigInt(value.replace('_bigint_', '')); + } + return value; }; }; diff --git a/test/common/index.test.js b/test/common/index.test.js index c2ccd58..de88c94 100644 --- a/test/common/index.test.js +++ b/test/common/index.test.js @@ -230,6 +230,16 @@ const tests = ({ stringify, parse }) => { expect(parsed).toMatchObject(data); }); + test('check BigInt value', () => { + const data = { LotOfFruits: BigInt('123456789123456789123456789123456789') }; + + const stringified = stringify(data); + const parsed = parse(stringified); + + expect(stringified).toEqual('{"LotOfFruits":"_bigint_123456789123456789123456789123456789"}'); + expect(parsed).toMatchObject(data); + }); + test('check undefined value', () => { const data = { undefinedFruit: undefined }; diff --git a/tsconfig.json b/tsconfig.json index fb5f94d..beddd04 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,12 @@ { "compilerOptions": { "target": "es5", - "lib": ["es2015", "es2017", "dom"], + "lib": [ + "es2015", + "es2017", + "dom", + "es2020.bigint" + ], "module": "commonjs", "declaration": true, "removeComments": true,