ObjectUtil
Provides common object manipulation utilities including depth traversal, obtaining accessors, safely setting values / equality tests, and validation.
Support for typhonjs-plugin-manager is enabled.
Static Method Summary
Static Public Methods | ||
public static |
depthTraverse(data: object | Array, func: function, modify: boolean): * Performs a naive depth traversal of an object / array. |
|
public static |
getAccessorList(data: object): Array Returns a list of accessor keys by traversing the given object. |
|
public static |
safeAccess(data: object, accessor: string, defaultValue: *): * Provides a way to safely access an objects data / entries given an accessor string which describes the entries to walk. |
|
public static |
safeBatchSet(data: object, accessors: Array<string>, value: object | *, operation: string, defaultAccessValue: object | *, createMissing: boolean) Provides a way to safely batch set an objects data / entries given an array of accessor strings which describe the entries to walk. |
|
public static |
Compares a source object and values of entries against a target object. |
|
public static |
safeSet(data: object, accessor: string, value: *, operation: string, createMissing: boolean): boolean Provides a way to safely set an objects data / entries given an accessor string which describes the entries to walk. |
|
public static |
safeSetAll(data: object, accessorValues: object<string, *>, operation: string, createMissing: boolean) Performs bulk setting of values to the given data object. |
|
public static |
Performs bulk validation of data given an object, |
|
public static |
validateArray(data: *, accessor: *, objectPattern2: {"type": *, "expected": *, "message": *, "required": *, "error": *}, dataName: string): boolean |
|
public static |
validateEntry(data: *, accessor: *, objectPattern2: {"type": *, "expected": *, "message": *, "required": *, "error": *}, dataName: string): boolean |
|
public static |
validateEntryOrArray(data: object, accessor: string, entry: ValidationEntry, dataName: string): boolean Dispatches validation of data entry to string or array validation depending on data entry type. |
Static Public Methods
public static depthTraverse(data: object | Array, func: function, modify: boolean): * source
Performs a naive depth traversal of an object / array. The data structure must not have circular references. The result of the callback function is used to modify in place the given data.
Return:
* |
public static getAccessorList(data: object): Array source
Returns a list of accessor keys by traversing the given object.
Params:
Name | Type | Attribute | Description |
data | object | An object to traverse for accessor keys. |
public static safeAccess(data: object, accessor: string, defaultValue: *): * source
Provides a way to safely access an objects data / entries given an accessor string which describes the
entries to walk. To access deeper entries into the object format the accessor string with .
between entries
to walk.
Return:
* |
public static safeBatchSet(data: object, accessors: Array<string>, value: object | *, operation: string, defaultAccessValue: object | *, createMissing: boolean) source
Provides a way to safely batch set an objects data / entries given an array of accessor strings which describe the
entries to walk. To access deeper entries into the object format the accessor string with .
between entries
to walk. If value is an object the accessor will be used to access a target value from value
which is
subsequently set to data
by the given operation. If value
is not an object it will be used as the target
value to set across all accessors.
Params:
Name | Type | Attribute | Description |
data | object | An object to access entry data. |
|
accessors | Array<string> | A string describing the entries to access. |
|
value | object | * | A new value to set if an entry for accessor is found. |
|
operation | string |
|
Operation to perform including: 'add', 'div', 'mult', 'set', 'set-undefined', 'sub'. |
defaultAccessValue | object | * |
|
A new value to set if an entry for accessor is found. |
createMissing | boolean |
|
If true missing accessor entries will be created as objects automatically. |
public static safeEqual(source: object, target: object): boolean source
Compares a source object and values of entries against a target object. If the entries in the source object match
the target object then true
is returned otherwise false
. If either object is undefined or null then false
is returned.
public static safeSet(data: object, accessor: string, value: *, operation: string, createMissing: boolean): boolean source
Provides a way to safely set an objects data / entries given an accessor string which describes the
entries to walk. To access deeper entries into the object format the accessor string with .
between entries
to walk.
Params:
Name | Type | Attribute | Description |
data | object | An object to access entry data. |
|
accessor | string | A string describing the entries to access. |
|
value | * | A new value to set if an entry for accessor is found. |
|
operation | string |
|
Operation to perform including: 'add', 'div', 'mult', 'set', 'set-undefined', 'sub'. |
createMissing | boolean |
|
If true missing accessor entries will be created as objects automatically. |
public static safeSetAll(data: object, accessorValues: object<string, *>, operation: string, createMissing: boolean) source
Performs bulk setting of values to the given data object.
Params:
Name | Type | Attribute | Description |
data | object | The data object to set data. |
|
accessorValues | object<string, *> | Object of accessor keys to values to set. |
|
operation | string |
|
Operation to perform including: 'add', 'div', 'mult', 'set', 'sub';
default ( |
createMissing | boolean |
|
If true missing accessor entries will be created as objects automatically. |
public static validate(data: object, validationData: object<string, ValidationEntry>, dataName: string): boolean source
Performs bulk validation of data given an object, validationData
, which describes all entries to test.
Params:
Name | Type | Attribute | Description |
data | object | The data object to test. |
|
validationData | object<string, ValidationEntry> | Key is the accessor / value is a validation entry. |
|
dataName | string |
|
Optional name of data. |
public static validateArray(data: *, accessor: *, objectPattern2: {"type": *, "expected": *, "message": *, "required": *, "error": *}, dataName: string): boolean source
Params:
Name | Type | Attribute | Description |
data | * | ||
accessor | * | ||
objectPattern2 | {"type": *, "expected": *, "message": *, "required": *, "error": *} |
|
|
dataName | string |
|
public static validateEntry(data: *, accessor: *, objectPattern2: {"type": *, "expected": *, "message": *, "required": *, "error": *}, dataName: string): boolean source
Params:
Name | Type | Attribute | Description |
data | * | ||
accessor | * | ||
objectPattern2 | {"type": *, "expected": *, "message": *, "required": *, "error": *} |
|
|
dataName | string |
|
public static validateEntryOrArray(data: object, accessor: string, entry: ValidationEntry, dataName: string): boolean source
Dispatches validation of data entry to string or array validation depending on data entry type.
Params:
Name | Type | Attribute | Description |
data | object | The data object to test. |
|
accessor | string | A string describing the entries to access. |
|
entry | ValidationEntry |
|
A validation entry. |
dataName | string |
|
Optional name of data. |