diff --git a/CHANGELOG.md b/CHANGELOG.md index 42cd0fb0c..c613c4c36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Changes: +- Added `stringify` util to correctly handle `JSON.stringify` with `BigInt` values - Additional input asserts for non-convertable values in `u8aToU8a` - Adapt `@polkadot/x-global` to return `globalThis` as available - Minor readability cleanups diff --git a/packages/util/src/index.ts b/packages/util/src/index.ts index 9975c062c..8b419ccd7 100644 --- a/packages/util/src/index.ts +++ b/packages/util/src/index.ts @@ -22,5 +22,6 @@ export * from './memoize'; export * from './number'; export * from './promisify'; export * from './string'; +export * from './stringify'; export * from './u8a'; export * from './versionDetect'; diff --git a/packages/util/src/is/jsonObject.ts b/packages/util/src/is/jsonObject.ts index e6314a5b6..c08dbcf8d 100644 --- a/packages/util/src/is/jsonObject.ts +++ b/packages/util/src/is/jsonObject.ts @@ -1,6 +1,8 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { stringify } from '../stringify'; + interface ObjectIndexed { // eslint-disable-next-line @typescript-eslint/no-explicit-any [index: string]: any; @@ -32,13 +34,13 @@ interface ObjectIndexed { */ export function isJsonObject (value: unknown): value is ObjectIndexed { const str = typeof value !== 'string' - ? JSON.stringify(value) + ? stringify(value) : value; try { const obj = JSON.parse(str) as unknown; - return (typeof obj === 'object' && obj !== null); + return typeof obj === 'object' && obj !== null; } catch (e) { return false; } diff --git a/packages/util/src/memoize.ts b/packages/util/src/memoize.ts index bb544791c..663ef3003 100644 --- a/packages/util/src/memoize.ts +++ b/packages/util/src/memoize.ts @@ -3,8 +3,8 @@ import type { Memoized } from './types'; -import { isBigInt } from './is/bigInt'; import { isUndefined } from './is/undefined'; +import { stringify } from './stringify'; interface Options { getInstanceId?: () => string; @@ -14,20 +14,12 @@ function defaultGetId (): string { return 'none'; } -function normalize (args: unknown[]): string { - return JSON.stringify(args, (_, value: unknown) => - isBigInt(value) - ? value.toString() - : value - ); -} - // eslint-disable-next-line @typescript-eslint/no-explicit-any export function memoize T> (fn: F, { getInstanceId = defaultGetId }: Options = {}): Memoized { const cache: Record> = {}; const memoized = (...args: unknown[]): T => { - const stringParams = normalize(args); + const stringParams = stringify(args); const instanceId = getInstanceId(); if (!cache[instanceId]) { @@ -42,7 +34,7 @@ export function memoize T> (fn: F, { getInstan }; memoized.unmemoize = (...args: unknown[]): void => { - const stringParams = normalize(args); + const stringParams = stringify(args); const instanceId = getInstanceId(); if (cache[instanceId] && !isUndefined(cache[instanceId][stringParams])) { diff --git a/packages/util/src/stringify.spec.ts b/packages/util/src/stringify.spec.ts new file mode 100644 index 000000000..c66633b7c --- /dev/null +++ b/packages/util/src/stringify.spec.ts @@ -0,0 +1,32 @@ +// Copyright 2017-2021 @polkadot/util authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import BN from 'bn.js'; + +import { stringify } from './stringify'; + +describe('stringify', (): void => { + it('stringifies a BigInt', (): void => { + expect( + stringify(1234n) + ).toEqual('"1234"'); + }); + + it('stringifies an array of values', (): void => { + expect( + stringify(['a', 1, 2n, new BN(3)]) + ).toEqual('["a",1,"2","3"]'); + }); + + it('stringifies an array of values (with spaces)', (): void => { + expect( + stringify(['a', 1, 2n, new BN(3)], 2) + ).toEqual('[\n "a",\n 1,\n "2",\n "3"\n]'); + }); + + it('stringifies a nested object', (): void => { + expect( + stringify({ a: 'a', b: 1, c: { d: 2n, e: new BN(3) } }) + ).toEqual('{"a":"a","b":1,"c":{"d":"2","e":"3"}}'); + }); +}); diff --git a/packages/util/src/stringify.ts b/packages/util/src/stringify.ts new file mode 100644 index 000000000..8c37a0851 --- /dev/null +++ b/packages/util/src/stringify.ts @@ -0,0 +1,15 @@ +// Copyright 2017-2021 @polkadot/util authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { isBigInt } from './is/bigInt'; + +export function stringify (args: unknown, space?: string | number): string { + return JSON.stringify( + args, + (_, value: unknown) => + isBigInt(value) + ? value.toString() + : value, + space + ); +}