Skip to main content

ObjectUtil

Provides common object manipulation utilities including depth traversal, obtaining accessors, safely setting values / equality tests, and validation.

Support for @ponticus/plugin-manager is enabled.

Index

Constructors

constructor

Methods

staticdepthTraverse

  • depthTraverse(data: any, func: any, modify?: boolean): any
  • 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.


    Parameters

    • data: any

      An object or array.

    • func: any

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

    • modify: boolean = false

      If true then the result of the callback function is used to modify in place the given data.

    Returns any

staticgetAccessorList

  • getAccessorList(data: any): any[]
  • Returns a list of accessor keys by traversing the given object.


    Parameters

    • data: any

      An object to traverse for accessor keys.

    Returns any[]

staticsafeAccess

  • safeAccess(data: any, accessor: string, defaultValue?: any): any
  • 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.


    Parameters

    • data: any

      An object to access entry data.

    • accessor: string

      A string describing the entries to access.

    • optionaldefaultValue: any

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

    Returns any

staticsafeBatchSet

  • safeBatchSet(data: any, accessors: string[], value: any, operation?: string, defaultAccessValue?: any, createMissing?: boolean): void
  • 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.


    Parameters

    • data: any

      An object to access entry data.

    • accessors: string[]

      A string describing the entries to access.

    • value: any

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

    • operation: string = "set"

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

    • defaultAccessValue: any = 0

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

    • createMissing: boolean = true

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

    Returns void

staticsafeEqual

  • safeEqual(source: any, target: any): boolean
  • 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.


    Parameters

    • source: any

      Source object.

    • target: any

      Target object.

    Returns boolean

staticsafeSet

  • safeSet(data: any, accessor: string, value: any, 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. To access deeper entries into the object format the accessor string with . between entries to walk.


    Parameters

    • data: any

      An object to access entry data.

    • accessor: string

      A string describing the entries to access.

    • value: any

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

    • operation: string = "set"

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

    • createMissing: boolean = true

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

    Returns boolean

staticsafeSetAll

  • safeSetAll(data: any, accessorValues: {}, operation?: string, createMissing?: boolean): void
  • Performs bulk setting of values to the given data object.


    Parameters

    • data: any

      The data object to set data.

    • accessorValues: {}

      Object of accessor keys to values to set.

    • operation: string = "set"

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

    • createMissing: boolean = true

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

    Returns void

staticvalidate

  • validate(data: any, validationData?: any, dataName?: string): boolean
  • Performs bulk validation of data given an object, validationData, which describes all entries to test.


    Parameters

    • data: any

      The data object to test.

    • validationData: any = {}

      Key is the accessor / value is a validation entry. (object<string, ValidationEntry>)

    • dataName: string = "data"

      Optional name of data.

    Returns boolean

staticvalidateArray

  • validateArray(data: any, accessor: string, options?: ValidateArrayOptions, dataName?: string): boolean
  • Validates all array entries against potential type and expected tests.


    Parameters

    • data: any

      The data object to test.

    • accessor: string

      A string describing the entries to access.

    • options: ValidateArrayOptions = {}

      ?

    • dataName: string = "data"

      Optional name of data. (“data”)

    Returns boolean

staticvalidateEntry

  • validateEntry(data: any, accessor: string, options: ValidateArrayOptions, dataName?: string): boolean
  • Validates data entry with a typeof check and potentially tests against the values in any given expected set.


    Parameters

    • data: any

      The object data to validate.

    • accessor: string

      A string describing the entries to access.

    • options: ValidateArrayOptions

      ?

    • dataName: string = "data"

      Optional name of data.

    Returns boolean

staticvalidateEntryOrArray

  • validateEntryOrArray(data: any, accessor: string, entry: ValidateArrayOptions, dataName?: string): boolean
  • Dispatches validation of data entry to string or array validation depending on data entry type.


    Parameters

    • data: any

      The data object to test.

    • accessor: string

      A string describing the entries to access.

    • entry: ValidateArrayOptions

      A validation entry.

    • dataName: string = "data"

      Optional name of data.

    Returns boolean