Home Reference Source Test Repository
public class | source

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

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

safeEqual(source: object, target: object): boolean

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

validate(data: object, validationData: object<string, ValidationEntry>, dataName: string): boolean

Performs bulk validation of data given an object, validationData, which describes all entries to test.

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.

Params:

NameTypeAttributeDescription
data object | Array

An object or array.

func function

A callback function to process leaf values in children arrays or object members.

modify boolean

If true then 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:

NameTypeAttributeDescription
data object

An object to traverse for accessor keys.

Return:

Array

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.

Params:

NameTypeAttributeDescription
data object

An object to access entry data.

accessor string

A string describing the entries to access.

defaultValue *

(Optional) A default value to return if an entry for accessor is not found.

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:

NameTypeAttributeDescription
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
  • optional
  • default: 'set'

Operation to perform including: 'add', 'div', 'mult', 'set', 'set-undefined', 'sub'.

defaultAccessValue object | *
  • optional
  • default: 0

A new value to set if an entry for accessor is found.

createMissing boolean
  • optional
  • default: true

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.

Params:

NameTypeAttributeDescription
source object

Source object.

target object

Target object.

Return:

boolean

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:

NameTypeAttributeDescription
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
  • optional
  • default: 'set'

Operation to perform including: 'add', 'div', 'mult', 'set', 'set-undefined', 'sub'.

createMissing boolean
  • optional
  • default: true

If true missing accessor entries will be created as objects automatically.

Return:

boolean

True if successful.

public static safeSetAll(data: object, accessorValues: object<string, *>, operation: string, createMissing: boolean) source

Performs bulk setting of values to the given data object.

Params:

NameTypeAttributeDescription
data object

The data object to set data.

accessorValues object<string, *>

Object of accessor keys to values to set.

operation string
  • optional
  • default: 'set'

Operation to perform including: 'add', 'div', 'mult', 'set', 'sub'; default (set).

createMissing boolean
  • optional
  • default: true

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:

NameTypeAttributeDescription
data object

The data object to test.

validationData object<string, ValidationEntry>

Key is the accessor / value is a validation entry.

dataName string
  • optional
  • default: 'data'

Optional name of data.

Return:

boolean

True if validation passes otherwise an exception is thrown.

public static validateArray(data: *, accessor: *, objectPattern2: {"type": *, "expected": *, "message": *, "required": *, "error": *}, dataName: string): boolean source

Params:

NameTypeAttributeDescription
data *
accessor *
objectPattern2 {"type": *, "expected": *, "message": *, "required": *, "error": *}
  • optional
  • default: {}
dataName string
  • optional
  • default: data

Return:

boolean

public static validateEntry(data: *, accessor: *, objectPattern2: {"type": *, "expected": *, "message": *, "required": *, "error": *}, dataName: string): boolean source

Params:

NameTypeAttributeDescription
data *
accessor *
objectPattern2 {"type": *, "expected": *, "message": *, "required": *, "error": *}
  • optional
  • default: {}
dataName string
  • optional
  • default: data

Return:

boolean

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:

NameTypeAttributeDescription
data object

The data object to test.

accessor string

A string describing the entries to access.

entry ValidationEntry
  • optional

A validation entry.

dataName string
  • optional
  • default: 'data'

Optional name of data.

Return:

boolean

True if validation passes otherwise an exception is thrown.