UNPKG

@ng-formworks/core

Version:

Angular ng-formworks - JSON Schema Form builder core

417 lines (416 loc) 18.5 kB
import * as i0 from "@angular/core"; /** * 'JsonPointer' class * * Some utilities for using JSON Pointers with JSON objects * https://tools.ietf.org/html/rfc6901 * * get, getCopy, getFirst, set, setCopy, insert, insertCopy, remove, has, dict, * forEachDeep, forEachDeepCopy, escape, unescape, parse, compile, toKey, * isJsonPointer, isSubPointer, toIndexedPointer, toGenericPointer, * toControlPointer, toSchemaPointer, toDataPointer, parseObjectPath * * Some functions based on manuelstofer's json-pointer utilities * https://github.com/manuelstofer/json-pointer */ export type Pointer = string | string[]; export declare class JsonPointer { /** * 'get' function * * Uses a JSON Pointer to retrieve a value from an object. * * // { object } object - Object to get value from * // { Pointer } pointer - JSON Pointer (string or array) * // { number = 0 } startSlice - Zero-based index of first Pointer key to use * // { number } endSlice - Zero-based index of last Pointer key to use * // { boolean = false } getBoolean - Return only true or false? * // { boolean = false } errors - Show error if not found? * // { object } - Located value (or true or false if getBoolean = true) */ static get(object: any, pointer: any, startSlice?: number, endSlice?: number, getBoolean?: boolean, errors?: boolean): any; private static logErrors; /** * Evaluates conditional expression in form of `model.<property>==<value>` or * `model.<property>!=<value>` where the first one means that the value must match to be * shown in a form, while the former shows the property only when the property value is not * set, or does not equal the given value. * * // { subObject } subObject - an object containing the data values of properties * // { key } key - the key from the for loop in a form of `<property>==<value>` * * Returns the object with two properties. The property passed informs whether * the expression evaluated successfully and the property key returns either the same * key if it is not contained inside the subObject or the key of the property if it is contained. */ static evaluateExpression(subObject: Object, key: any): any; /** * Performs the actual evaluation on the given expression with given values and keys. * // { cleanedValue } cleanedValue - the given valued cleaned of quotes if it had any * // { subObject } subObject - the object with properties values * // { keysAndExpression } keysAndExpression - an object holding the expressions with */ private static performExpressionOnValue; private static doComparisonByExpressionType; /** * Does the checks when the parsed key is actually no a property inside subObject. * That would mean that the equal comparison makes no sense and thus the negative result * is returned, and the not equal comparison is not necessary because it doesn't equal * obviously. Returns null when the given key is a real property inside the subObject. * // { subObject } subObject - the object with properties values * // { keysAndExpression } keysAndExpression - an object holding the expressions with * the associated keys. */ private static doOwnCheckResult; /** * Does the basic checks and tries to parse an expression and a pair * of key and value. * // { key } key - the original for loop created value containing key and value in one string * // { subObject } subObject - the object with properties values */ private static parseKeysAndExpression; private static keyOrSubObjEmpty; /** * 'getCopy' function * * Uses a JSON Pointer to deeply clone a value from an object. * * // { object } object - Object to get value from * // { Pointer } pointer - JSON Pointer (string or array) * // { number = 0 } startSlice - Zero-based index of first Pointer key to use * // { number } endSlice - Zero-based index of last Pointer key to use * // { boolean = false } getBoolean - Return only true or false? * // { boolean = false } errors - Show error if not found? * // { object } - Located value (or true or false if getBoolean = true) */ static getCopy(object: any, pointer: any, startSlice?: number, endSlice?: number, getBoolean?: boolean, errors?: boolean): any; /** * 'getFirst' function * * Takes an array of JSON Pointers and objects, * checks each object for a value specified by the pointer, * and returns the first value found. * * // { [object, pointer][] } items - Array of objects and pointers to check * // { any = null } defaultValue - Value to return if nothing found * // { boolean = false } getCopy - Return a copy instead? * // - First value found */ static getFirst(items: any, defaultValue?: any, getCopy?: boolean): any; /** * 'getFirstCopy' function * * Similar to getFirst, but always returns a copy. * * // { [object, pointer][] } items - Array of objects and pointers to check * // { any = null } defaultValue - Value to return if nothing found * // - Copy of first value found */ static getFirstCopy(items: any, defaultValue?: any): any; /** * 'set' function * * Uses a JSON Pointer to set a value on an object. * Also creates any missing sub objects or arrays to contain that value. * * If the optional fourth parameter is TRUE and the inner-most container * is an array, the function will insert the value as a new item at the * specified location in the array, rather than overwriting the existing * value (if any) at that location. * * So set([1, 2, 3], '/1', 4) => [1, 4, 3] * and * So set([1, 2, 3], '/1', 4, true) => [1, 4, 2, 3] * * // { object } object - The object to set value in * // { Pointer } pointer - The JSON Pointer (string or array) * // value - The new value to set * // { boolean } insert - insert value? * // { object } - The original object, modified with the set value */ static set(object: any, pointer: any, value: any, insert?: boolean): any; /** * 'setCopy' function * * Copies an object and uses a JSON Pointer to set a value on the copy. * Also creates any missing sub objects or arrays to contain that value. * * If the optional fourth parameter is TRUE and the inner-most container * is an array, the function will insert the value as a new item at the * specified location in the array, rather than overwriting the existing value. * * // { object } object - The object to copy and set value in * // { Pointer } pointer - The JSON Pointer (string or array) * // value - The value to set * // { boolean } insert - insert value? * // { object } - The new object with the set value */ static setCopy(object: any, pointer: any, value: any, insert?: boolean): any; /** * 'insert' function * * Calls 'set' with insert = TRUE * * // { object } object - object to insert value in * // { Pointer } pointer - JSON Pointer (string or array) * // value - value to insert * // { object } */ static insert(object: any, pointer: any, value: any): any; /** * 'insertCopy' function * * Calls 'setCopy' with insert = TRUE * * // { object } object - object to insert value in * // { Pointer } pointer - JSON Pointer (string or array) * // value - value to insert * // { object } */ static insertCopy(object: any, pointer: any, value: any): any; /** * 'remove' function * * Uses a JSON Pointer to remove a key and its attribute from an object * * // { object } object - object to delete attribute from * // { Pointer } pointer - JSON Pointer (string or array) * // { object } */ static remove(object: any, pointer: any): any; /** * 'has' function * * Tests if an object has a value at the location specified by a JSON Pointer * * // { object } object - object to chek for value * // { Pointer } pointer - JSON Pointer (string or array) * // { boolean } */ static has(object: any, pointer: any): any; /** * 'dict' function * * Returns a (pointer -> value) dictionary for an object * * // { object } object - The object to create a dictionary from * // { object } - The resulting dictionary object */ static dict(object: any): any; /** * 'forEachDeep' function * * Iterates over own enumerable properties of an object or items in an array * and invokes an iteratee function for each key/value or index/value pair. * By default, iterates over items within objects and arrays after calling * the iteratee function on the containing object or array itself. * * The iteratee is invoked with three arguments: (value, pointer, rootObject), * where pointer is a JSON pointer indicating the location of the current * value within the root object, and rootObject is the root object initially * submitted to th function. * * If a third optional parameter 'bottomUp' is set to TRUE, the iterator * function will be called on sub-objects and arrays after being * called on their contents, rather than before, which is the default. * * This function can also optionally be called directly on a sub-object by * including optional 4th and 5th parameterss to specify the initial * root object and pointer. * * // { object } object - the initial object or array * // { (v: any, p?: string, o?: any) => any } function - iteratee function * // { boolean = false } bottomUp - optional, set to TRUE to reverse direction * // { object = object } rootObject - optional, root object or array * // { string = '' } pointer - optional, JSON Pointer to object within rootObject * // { object } - The modified object */ static forEachDeep(object: any, fn?: (v: any, p?: string, o?: any) => any, bottomUp?: boolean, pointer?: string, rootObject?: any): void; /** * 'forEachDeepCopy' function * * Similar to forEachDeep, but returns a copy of the original object, with * the same keys and indexes, but with values replaced with the result of * the iteratee function. * * // { object } object - the initial object or array * // { (v: any, k?: string, o?: any, p?: any) => any } function - iteratee function * // { boolean = false } bottomUp - optional, set to TRUE to reverse direction * // { object = object } rootObject - optional, root object or array * // { string = '' } pointer - optional, JSON Pointer to object within rootObject * // { object } - The copied object */ static forEachDeepCopy(object: any, fn?: (v: any, p?: string, o?: any) => any, bottomUp?: boolean, pointer?: string, rootObject?: any): any; /** * 'escape' function * * Escapes a string reference key * * // { string } key - string key to escape * // { string } - escaped key */ static escape(key: any): any; /** * 'unescape' function * * Unescapes a string reference key * * // { string } key - string key to unescape * // { string } - unescaped key */ static unescape(key: any): any; /** * 'parse' function * * Converts a string JSON Pointer into a array of keys * (if input is already an an array of keys, it is returned unchanged) * * // { Pointer } pointer - JSON Pointer (string or array) * // { boolean = false } errors - Show error if invalid pointer? * // { string[] } - JSON Pointer array of keys */ static parse(pointer: any, errors?: boolean): any[]; /** * 'compile' function * * Converts an array of keys into a JSON Pointer string * (if input is already a string, it is normalized and returned) * * The optional second parameter is a default which will replace any empty keys. * * // { Pointer } pointer - JSON Pointer (string or array) * // { string | number = '' } defaultValue - Default value * // { boolean = false } errors - Show error if invalid pointer? * // { string } - JSON Pointer string */ static compile(pointer: any, defaultValue?: string, errors?: boolean): any; /** * 'toKey' function * * Extracts name of the final key from a JSON Pointer. * * // { Pointer } pointer - JSON Pointer (string or array) * // { boolean = false } errors - Show error if invalid pointer? * // { string } - the extracted key */ static toKey(pointer: any, errors?: boolean): any; /** * 'isJsonPointer' function * * Checks a string or array value to determine if it is a valid JSON Pointer. * Returns true if a string is empty, or starts with '/' or '#/'. * Returns true if an array contains only string values. * * // value - value to check * // { boolean } - true if value is a valid JSON Pointer, otherwise false */ static isJsonPointer(value: any): any; /** * 'isSubPointer' function * * Checks whether one JSON Pointer is a subset of another. * * // { Pointer } shortPointer - potential subset JSON Pointer * // { Pointer } longPointer - potential superset JSON Pointer * // { boolean = false } trueIfMatching - return true if pointers match? * // { boolean = false } errors - Show error if invalid pointer? * // { boolean } - true if shortPointer is a subset of longPointer, false if not */ static isSubPointer(shortPointer: any, longPointer: any, trueIfMatching?: boolean, errors?: boolean): boolean; /** * 'toIndexedPointer' function * * Merges an array of numeric indexes and a generic pointer to create an * indexed pointer for a specific item. * * For example, merging the generic pointer '/foo/-/bar/-/baz' and * the array [4, 2] would result in the indexed pointer '/foo/4/bar/2/baz' * * * // { Pointer } genericPointer - The generic pointer * // { number[] } indexArray - The array of numeric indexes * // { Map<string, number> } arrayMap - An optional array map * // { string } - The merged pointer with indexes */ static toIndexedPointer(genericPointer: any, indexArray: any, arrayMap?: Map<string, number>): any; /** * 'toGenericPointer' function * * Compares an indexed pointer to an array map and removes list array * indexes (but leaves tuple arrray indexes and all object keys, including * numeric keys) to create a generic pointer. * * For example, using the indexed pointer '/foo/1/bar/2/baz/3' and * the arrayMap [['/foo', 0], ['/foo/-/bar', 3], ['/foo/-/bar/-/baz', 0]] * would result in the generic pointer '/foo/-/bar/2/baz/-' * Using the indexed pointer '/foo/1/bar/4/baz/3' and the same arrayMap * would result in the generic pointer '/foo/-/bar/-/baz/-' * (the bar array has 3 tuple items, so index 2 is retained, but 4 is removed) * * The structure of the arrayMap is: [['path to array', number of tuple items]...] * * * // { Pointer } indexedPointer - The indexed pointer (array or string) * // { Map<string, number> } arrayMap - The optional array map (for preserving tuple indexes) * // { string } - The generic pointer with indexes removed */ static toGenericPointer(indexedPointer: any, arrayMap?: Map<string, number>): any; /** * 'toControlPointer' function * * Accepts a JSON Pointer for a data object and returns a JSON Pointer for the * matching control in an Angular FormGroup. * * // { Pointer } dataPointer - JSON Pointer (string or array) to a data object * // { FormGroup } formGroup - Angular FormGroup to get value from * // { boolean = false } controlMustExist - Only return if control exists? * // { Pointer } - JSON Pointer (string) to the formGroup object */ static toControlPointer(dataPointer: any, formGroup: any, controlMustExist?: boolean): any; /** * 'toSchemaPointer' function * * Accepts a JSON Pointer to a value inside a data object and a JSON schema * for that object. * * Returns a Pointer to the sub-schema for the value inside the object's schema. * * // { Pointer } dataPointer - JSON Pointer (string or array) to an object * // schema - JSON schema for the object * // { Pointer } - JSON Pointer (string) to the object's schema */ static toSchemaPointer(dataPointer: any, schema: any): any; /** * 'toDataPointer' function * * Accepts a JSON Pointer to a sub-schema inside a JSON schema and the schema. * * If possible, returns a generic Pointer to the corresponding value inside * the data object described by the JSON schema. * * Returns null if the sub-schema is in an ambiguous location (such as * definitions or additionalProperties) where the corresponding value * location cannot be determined. * * // { Pointer } schemaPointer - JSON Pointer (string or array) to a JSON schema * // schema - the JSON schema * // { boolean = false } errors - Show errors? * // { Pointer } - JSON Pointer (string) to the value in the data object */ static toDataPointer(schemaPointer: any, schema: any, errors?: boolean): any; /** * 'parseObjectPath' function * * Parses a JavaScript object path into an array of keys, which * can then be passed to compile() to convert into a string JSON Pointer. * * Based on mike-marcacci's excellent objectpath parse function: * https://github.com/mike-marcacci/objectpath * * // { Pointer } path - The object path to parse * // { string[] } - The resulting array of keys */ static parseObjectPath(path: any): any[]; static ɵfac: i0.ɵɵFactoryDeclaration<JsonPointer, never>; static ɵprov: i0.ɵɵInjectableDeclaration<JsonPointer>; }