From 2602a43eabea2da734983f0678af59d59c874d85 Mon Sep 17 00:00:00 2001 From: Stefanie Doll Date: Tue, 12 Mar 2019 11:58:11 +0100 Subject: [PATCH] Parsing function for JSON objects (#359) * Added parsin function for JSON objects * Re-defined wording * code formatting/ new lines --- packages/util/src/is/index.ts | 1 + packages/util/src/is/jsonObject.spec.ts | 50 +++++++++++++++++++++++++ packages/util/src/is/jsonObject.ts | 47 +++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 packages/util/src/is/jsonObject.spec.ts create mode 100644 packages/util/src/is/jsonObject.ts diff --git a/packages/util/src/is/index.ts b/packages/util/src/is/index.ts index 9e94f842b..0db1214fb 100644 --- a/packages/util/src/is/index.ts +++ b/packages/util/src/is/index.ts @@ -14,6 +14,7 @@ export { default as isFunction } from './function'; export { default as isHex } from './hex'; export { default as isInstanceOf } from './instanceOf'; export { default as isIp } from './ip'; +export { default as isJsonObject } from './jsonObject'; export { default as isNull } from './null'; export { default as isNumber } from './number'; export { default as isObject } from './object'; diff --git a/packages/util/src/is/jsonObject.spec.ts b/packages/util/src/is/jsonObject.spec.ts new file mode 100644 index 000000000..c31c97bf3 --- /dev/null +++ b/packages/util/src/is/jsonObject.spec.ts @@ -0,0 +1,50 @@ +// Copyright 2017-2019 @polkadot/util authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { isJsonObject } from './index'; + +const jsonObject = `{ + "Test": "1234", + "NestedTest": { + "Test": "5678" + } + }`; + +describe('isJsonObject', () => { + it('returns true on empty objects', () => { + expect( + isJsonObject({}) + ).toEqual(true); + }); + + it('returns true on JSON objects', () => { + expect( + isJsonObject(jsonObject) + ).toEqual(true); + }); + + it('returns false on JSON parsable value typeof number', () => { + expect( + isJsonObject(1234) + ).toEqual(false); + }); + + it('returns false on JSON parsable value null', () => { + expect( + isJsonObject(null) + ).toEqual(false); + }); + + it('returns false on invalid objects', () => { + expect( + isJsonObject('notAnObject') + ).toEqual(false); + }); + + it('returns false on invalid JSON', () => { + expect( + isJsonObject(`{"abc", "def"}`) + ).toEqual(false); + }); +}); diff --git a/packages/util/src/is/jsonObject.ts b/packages/util/src/is/jsonObject.ts new file mode 100644 index 000000000..b7a20296d --- /dev/null +++ b/packages/util/src/is/jsonObject.ts @@ -0,0 +1,47 @@ +// Copyright 2017-2019 @polkadot/util authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +type ObjectIndexed = { + [index: string]: any +}; + +/** + * @name isJsonObject + * @summary Tests for a valid JSON `object`. + * @description + * Checks to see if the input value is a valid JSON object. + * It returns false if the input is JSON parsable, but not an Javascript object. + * @example + *
+ * + * ```javascript + * import { isJsonObject } from '@polkadot/util'; + * + * isJsonObject({}); // => true + * isJsonObject({ + * "Test": "1234", + * "NestedTest": { + * "Test": "5678" + * } + * }); // => true + * isJsonObject(1234); // JSON parsable, but not an object => false + * isJsonObject(null); // JSON parsable, but not an object => false + * isJsonObject('not an object'); // => false + * ``` + */ + +export default function isJsonObject (value: any): value is ObjectIndexed { + + value = typeof value !== 'string' + ? JSON.stringify(value) + : value; + + try { + value = JSON.parse(value); + + return (typeof value === 'object' && value !== null); + } catch (e) { + return false; + } +}