@crikey/json v0.0.2
@crikey/json
JSON types and utility functions
See @crikey/json for full documentation.
API
Types
Contains types used to represent JSON and JSON equivalents.
Primary types:
Json- A pure JSON type. Contained values may not be undefined.Jsonish- A JSON type which allows for contained values to be undefined.
Guards
Contains typescript guards for identifying type information.
is_undefinedreturns true if value is undefinedis_nullreturns true if value is nullis_booleanreturns true if value is a booleanis_integerreturns true if value is an integeris_safe_integerreturns true if value is a safe integeris_index_numberreturns true if value is a positive integeris_index_stringreturns true if value is the string equivalent of a positive integeris_index_number_or_stringreturns true if eitheris_index_numberoris_index_stringis trueis_numberreturns true if value is a numberis_finite_numberreturns true if value is a finite numberis_stringreturns true if value is a stringis_primitivereturns true if value is a json primitiveis_encodable_primitivereturns true if value is an encodable primitiveis_objectreturns true if value is an object (but not an array)is_arrayreturns true if value is an arrayis_containerreturns true if eitheris_objectoris_arrayis trueis_jsonreturns true if a shallow check of value confirms a json typeis_json_deepreturns true if a deep check of value confirms every contained value is a json typeis_encodable_jsonreturns true if a shallow check of value confirms it is an encodable json typeis_encodable_json_deepreturns true if a deep check of value confirms every contained value is an encodable json typeis_equal_deepreturns true if a deep comparison of values proves equality
Access
object_has_keyreturns true if object has an ownProperty with the given nameobject_has_defined_keyreturns true if object has an ownProperty with the given name whose value is not undefinedobject_memberreturns the value of the given ownPropertyobject_entriesreturns all ownProperty entries of valueobject_defined_entriesreturns all ownProperty entries of value whose values are not undefinedobject_keysreturns all ownProperty keys of valueobject_defined_keysreturns all ownProperty keys of value whose values are not undefinedarray_has_indexreturns true if array has the given indexarray_elementreturns the value of the given indexcontainer_has_key_or_indexreturns true if value has the given index/keycontainer_has_defined_key_or_indexreturns true if value has the given index/key, and it's value is not undefinedcontainer_itemreturn the value of the given key/index
Traversal
Provides methods for the following activities:
traverse_has | traverse_json.has,traverse_has | traverse_jsonish.hasreturns true if the given path existstraverse_get | traverse_json.get,traverse_get | traverse_jsonish.getreturns the value resulting from traversing the given pathtraverse_json_set | traverse_json.set,traverse_jsonish_set | traverse_jsonish.settraverses the given path, creating objects and arrays as necessary, and sets the leaf node valuetraverse_json_update | traverse_json.update,traverse_jsonish_update | traverse_jsonish.updatetraverses the given path, creating objects and arrays as necessary, and updates the leaf node valuetraverse_delete | traverse_json.delete,traverse_delete | traverse_jsonish.deletetraverses the given path, deleting the leaf node if it exists
Utilities
cloneReturns a deep clone of the given value Json or Jsonish valueparse_indexParses the given encoded_index into an index, or undefined if it is invalidparse_index_stringReturns the given encoded_index parsed into a numerical index, or undefined if the string does not represent a valid indexobject_assignSafe version of Object.assign which doesn't risk polluting the result object via__proto__
Installation
# pnpm
$ pnpm add @crikey/json
# npm
$ npm add @crikey/json
# yarn
$ yarn add @crikey/jsonExample Usage
Example Guards
const value: any = {a: NaN};
console.log(is_json(value)); // true
console.log(is_json_deep(value)); // true
console.log(is_encodable_json_deep(value)); // false - NaN is not a valid JSON value and will be cooerced into a nullconst value: any = {a: undefined};
console.log(is_json(value)); // true
console.log(is_json_deep(value)); // false - json does not support undefined values
console.log(is_jsonish_deep(value)); // true - jsonish does support undefined valuesExample Access
const parent = {x: 1};
const child = {y: 1};
child.__proto__ = parent;
console.log('x' in child); // true
console.log(object_has_key(child, 'x')); // false
console.log(object_has_key(child, 'y')); // true
console.log(object_keys(child)); // ['y']
console.log(object_entries(child)); // [['y', 2]]import {array_has_index} from "./array_has_index";
const value = ['a', 'b', 'c'];
console.log(array_has_index(value, 0)); // true
console.log(array_has_index(value, 3)); // false
console.log(array_element(value, 1)); // 'b'Example Traverse
import {traverse_get} from "./traverse_get";
import {traverse_json} from "./traverse_json";
const value = {foo: ['bar', 'baz']};
console.log(traverse_json.get(value, ['foo', 0])); // 'bar'
console.log(traverse_json.get(value, ['foo', 1])); // 'baz'
console.log(traverse_json.get(value, ['fah'])); // undefined
console.log(traverse_json.set(value, ['fah'], 1)); // {foo: ['bar', 'baz'], fah: 1}Example Util
const value = 'my value';
const type: JsonTypeEnum = get_type(value);
console.log(type); // 'string'const value = {a: 1, b: undefined, c:[]};
const cloned = clone(value);
console.log(cloned); // {a: 1, b: undefined, c:[]}import {parse_index} from "./parse_index";
console.log(parse_index(1)); // 1
console.log(parse_index(1.1)); // undefined
console.log(parse_index('1')); // 1
console.log(parse_index('1.1')); // undefined
console.log(parse_index_string('1')); // 1
console.log(parse_index_string('1.1')); // undefinedconst unsafe = Object.assign({}, { ['__proto__']: { x: 1 }});
const safe = object_assign({}, { ['__proto__']: { x: 1 }});
console.log(unsafe); // {}
console.log(safe); // { ['__proto__']: { x: 1 }}