UNPKG

@itwin/core-bentley

Version:

Bentley JavaScript core components

157 lines 6.69 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Json */ Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonUtils = void 0; /** Utility functions for converting from JSON objects, with default values. * @public */ var JsonUtils; (function (JsonUtils) { /** Get a value as a boolean. * @param json the input JSON object * @param defaultVal default value if json cannot be converted to boolean * @returns the value of json as a boolean, or default value */ function asBool(json, defaultVal = false) { return isNullOrUndefined(json) ? defaultVal : !!json; } JsonUtils.asBool = asBool; /** Get a value as an integer. * @param json the input JSON object * @param defaultVal default value if json cannot be converted to integer * @returns the value of json as an integer, or default value */ function asInt(json, defaultVal = 0) { return (typeof json === "number") ? Math.trunc(json) : defaultVal; } JsonUtils.asInt = asInt; /** Get a value as a double. * @param json the input JSON object * @param defaultVal default value if json cannot be converted to double * @returns the value of json as a double, or default value */ function asDouble(json, defaultVal = 0) { return (typeof json === "number") ? json : defaultVal; } JsonUtils.asDouble = asDouble; /** Get a value as a string. * @param json the input JSON object * @param defaultVal default value if json cannot be converted to string * @returns the value of json as a string, or default value */ function asString(json, defaultVal = "") { return isNullOrUndefined(json) ? defaultVal : json.toString(); } JsonUtils.asString = asString; /** Get a value as an array. * @param json the input JSON object * @returns the input JSON object if it is an array, otherwise undefined */ function asArray(json) { return Array.isArray(json) ? json : undefined; } JsonUtils.asArray = asArray; /** Get a value as an object. * @param json the input JSON object * @returns the input JSON object if it is an object, otherwise undefined */ function asObject(json) { return "object" === typeof json ? json : undefined; } JsonUtils.asObject = asObject; /** Set or remove a number on a json object, given a key name, a value, and a default value. Sets `json[key] = val` if val is *not* equal to the default, * otherwise `delete json[key]`. This is used to omit values from JSON strings that are of known defaults. * @param json the JSON object to affect * @param key the name of the member to set or remove * @param val the value to set * @param defaultVal the default value. */ function setOrRemoveNumber(json, key, val, defaultVal) { if (val === defaultVal) delete json[key]; else json[key] = val; } JsonUtils.setOrRemoveNumber = setOrRemoveNumber; /** Set or remove a boolean on a json object, given a key name, a value, and a default value. Sets `json[key] = val` if val is *not* equal to the default, * otherwise `delete json[key]`. This is used to omit values from JSON strings that are of known defaults. * @param json the JSON object to affect * @param key the name of the member to set or remove * @param val the value to set * @param defaultVal the default value. */ function setOrRemoveBoolean(json, key, val, defaultVal) { if (val === defaultVal) delete json[key]; else json[key] = val; } JsonUtils.setOrRemoveBoolean = setOrRemoveBoolean; /** Returns `true` if `json` is a non-null object. */ function isObject(json) { return json !== null && "object" === typeof json; } JsonUtils.isObject = isObject; /** Determine if a Javascript object is equivalent to `{}`. * @param json The JSON object to test. * @returns true if `json` is an Object with no keys. */ function isEmptyObject(json) { return isObject(json) && 0 === Object.keys(json).length; } JsonUtils.isEmptyObject = isEmptyObject; /** Determine if the input is undefined or an empty Javascript object. * @param json The JSON object to test. * @returns true if `json` is undefined or is an Object with no keys (equivalent to `{}`). */ function isEmptyObjectOrUndefined(json) { return undefined === json || isEmptyObject(json); } JsonUtils.isEmptyObjectOrUndefined = isEmptyObjectOrUndefined; function isNullOrUndefined(json) { return null === json || undefined === json; } /** Determine if the input is a non-empty Javascript object. * @param value The value to test. * @returns true if `value` is an Object with at least one key. */ function isNonEmptyObject(value) { return !isEmptyObjectOrUndefined(value); } JsonUtils.isNonEmptyObject = isNonEmptyObject; /** * Convert the input object into a "pure" JavaScript object, with only instances of "object" or primitives in the returned value. * Works recursively for object members, and over arrays entries. Calls "toJSON" on any members that implement it. */ function toObject(val) { if (typeof val === "boolean" || typeof val === "number" || typeof val === "string") return val; if (typeof val !== "object") return undefined; // See if the object has toJSON() function defined. if (typeof val.toJSON !== "undefined") return toObject(val.toJSON()); // if it's an array, convert each member. if (Array.isArray(val)) { const arr = new Array(val.length); val.forEach((el, i) => arr[i] = toObject(el)); return arr; } // Convert each property const out = {}; Object.getOwnPropertyNames(val).forEach((prop) => { const transformVal = toObject(val[prop]); if (transformVal !== undefined) out[prop] = transformVal; }); return out; } JsonUtils.toObject = toObject; })(JsonUtils || (exports.JsonUtils = JsonUtils = {})); //# sourceMappingURL=JsonUtils.js.map