Add stringify util (#956)

* Add stringify util

* stringify space param

* Update packages/util/src/stringify.ts
This commit is contained in:
Jaco
2021-04-22 12:14:31 +02:00
committed by GitHub
parent a136d938df
commit 4ca010cc99
6 changed files with 56 additions and 13 deletions
+1
View File
@@ -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';
+4 -2
View File
@@ -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;
}
+3 -11
View File
@@ -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, F extends (...args: any[]) => T> (fn: F, { getInstanceId = defaultGetId }: Options = {}): Memoized<F> {
const cache: Record<string, Record<string, T>> = {};
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, F extends (...args: any[]) => 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])) {
+32
View File
@@ -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"}}');
});
});
+15
View File
@@ -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
);
}