1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.isJsonObject = void 0;
|
4 | const stringify_js_1 = require("../stringify.js");
|
5 | /**
|
6 | * @name isJsonObject
|
7 | * @summary Tests for a valid JSON `object`.
|
8 | * @description
|
9 | * Checks to see if the input value is a valid JSON object.
|
10 | * It returns false if the input is JSON parsable, but not an Javascript object.
|
11 | * @example
|
12 | * <BR>
|
13 | *
|
14 | * ```javascript
|
15 | * import { isJsonObject } from '@polkadot/util';
|
16 | *
|
17 | * isJsonObject({}); // => true
|
18 | * isJsonObject({
|
19 | * "Test": "1234",
|
20 | * "NestedTest": {
|
21 | * "Test": "5678"
|
22 | * }
|
23 | * }); // => true
|
24 | * isJsonObject(1234); // JSON parsable, but not an object => false
|
25 | * isJsonObject(null); // JSON parsable, but not an object => false
|
26 | * isJsonObject('not an object'); // => false
|
27 | * ```
|
28 | */
|
29 | function isJsonObject(value) {
|
30 | const str = typeof value !== 'string'
|
31 | ? (0, stringify_js_1.stringify)(value)
|
32 | : value;
|
33 | try {
|
34 | const obj = JSON.parse(str);
|
35 | return typeof obj === 'object' && obj !== null;
|
36 | }
|
37 | catch {
|
38 | return false;
|
39 | }
|
40 | }
|
41 | exports.isJsonObject = isJsonObject;
|