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
+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;
}