ObjectHelper

Helper for Object manipulation.

Functions

42

Checks that the supplied value is an array. Throws if it is not

ParameterTypeDescription
valueObject

Value to check type of

nameString

Name of the value, used in error message

Checks that the supplied value is a boolean. Throws if it is not

ParameterTypeDescription
valueObject

Value to check type of

nameString

Name of the value, used in error message

Checks that the supplied value is a Bryntum class. Throws if it is not

ParameterTypeDescription
valueObject

Value to check type of

nameString

Name of the value, used in error message

Checks that the supplied value is a function. Throws if it is not

ParameterTypeDescription
valueObject

Value to check type of

nameString

Name of the value, used in error message

Checks that the supplied value is an instance of a Bryntum class. Throws if it is not

ParameterTypeDescription
valueObject

Value to check type of

nameString

Name of the value, used in error message

Checks that the supplied value is a number. Throws if it is not

ParameterTypeDescription
valueObject

Value to check type of

nameString

Name of the value, used in error message

Checks that the supplied value is a plain object. Throws if it is not

ParameterTypeDescription
valueObject

Value to check type of

nameString

Name of the value, used in error message

Checks that the supplied value is a string. Throws if it is not

ParameterTypeDescription
valueObject

Value to check type of

nameString

Name of the value, used in error message

Checks that the supplied value is of the specified type.Throws if it is not

ParameterTypeDescription
valueObject

Value to check type of

typeString

Expected type

nameString

Name of the value, used in error message

allowNullBoolean

Accept null without throwing

assignstatic

Copies all enumerable properties from the supplied source objects to dest. Unlike Object.assign, this copy also includes inherited properties.

ParameterTypeDescription
destObject

The destination object.

sourcesObject

The source objects.

Returns: Object -

The dest object.

assignIfstatic

Copies all enumerable properties from the supplied source objects to dest, only including properties that does not already exist on dest. Unlike Object.assign, this copy also includes inherited properties.

ParameterTypeDescription
destObject

The destination object.

sourcesObject

The source objects.

Returns: Object -

The dest object.

Changes the passed object and removes all null and undefined properties from it

ParameterTypeDescription
objectObject

Target object

keepNullBoolean

Pass true to only remove undefined properties

Returns: Object -

Passed object

clonestatic

Creates a deep copy of the value. Simple objects (isObject, arrays and Date objects are cloned. The enumerable properties of simple objects and the elements of arrays are cloned recursively.

ParameterTypeDescription
value*

The value to clone.

handlerfunction

An optional function to call for values of types other than simple object, array or Date. This function should return the clone of the value passed to it. It is only called for truthy values whose typeof equals 'object'.

handler.value*

The value to clone.

Returns: * -

The cloned value.

Copies the named properties from the source parameter into the dest parameter.

ParameterTypeDescription
destObject

The destination into which properties are copied.

sourceObject

The source from which properties are copied.

propsString[]

The list of property names.

Returns: Object -

The dest object.

Copies all properties from the source parameter into the dest parameter except those specified in except.

ParameterTypeDescription
destObject

The destination into which properties are copied.

sourceObject

The source from which properties are copied.

exceptObject

Object whose keys specify the properties to exclude.

Returns: Object -

The dest object.

Copies the named properties from the source parameter into the dest parameter unless the property already exists in the dest.

ParameterTypeDescription
destObject

The destination into which properties are copied.

sourceObject

The source from which properties are copied.

propsString[]

The list of property names.

Returns: Object -

The dest object.

Converts a list of names (either a space separated string or an array), into an object with those properties assigned truthy values. The converse of getTruthyKeys.

ParameterTypeDescription
sourceString | String[]

The list of names to convert to object form.

filterstatic

Returns an object retaining the enumerable key/value pairs for which the provided function fn returns a truthy value.

ParameterTypeDescription
objectObject | Object[]

The object or array of objects whose properties are to be filtered. If an array is passed, the key/value pairs of each object is evaluated and copied into the result (based on fn).

fnfunction

The function that returns a truthy if the key/value pair should be in the result.

fn.value*

The value of the object property name.

fn.nameString

The name of the current property for the iteration.

fn.objectObject

The object being filtered.

forEachstatic

Calls a provided function fn for each key/value pair of a given object.

ParameterTypeDescription
objectObject

The object to iterate

fnfunction

The function to call with each property and value of the given object.

fn.value*

The value of the object property name.

fn.nameString

The name of the current property for the iteration.

Returns a non-null entry from a Map for a given key path. This enables a specified defaultValue to be added "just in time" which is returned if the key is not already present.

ParameterTypeDescription
mapMap

The Map to find the key in (and potentially add to).

pathString | Number | String[] | Number[]

Dot-separated path, e.g. 'firstChild.childObject.someKey', or the key path as an array, e.g. ['firstChild', 'childObject', 'someKey'].

defaultValueObject

Optionally the value to insert if the key is not found.

getPathstatic

Returns value for a given path in the object

ParameterTypeDescription
objectObject

Object to check path on

pathString

Dot-separated path, e.g. 'object.childObject.someKey'

Returns: * -

Value associated with passed key

Gathers the names of properties which have truthy values into an array.

This is useful when gathering CSS class names for complex element production. Instead of appending to an array or string which may already contain the name, and instead of contending with space separation and concatenation and conditional execution, just set the properties of an object:

cls = {
    [this.selectedCls] : this.isSelected(thing),
    [this.dirtyCls] : this.isDirty(thing)
};

If the source parameter is an array, it is returned. If it is a single string, it is wrapped in an array and returned. Otherwise, the value must be an object (or null) it will be processed as described above.

ParameterTypeDescription
sourceObject | String | String[]

Source of keys to gather into an array.

Returns: String[] -

The keys which had a truthy value.

Gathers the values of properties which are truthy into an array.

ParameterTypeDescription
sourceObject

Source of values to gather into an array.

Returns: String[] -

The truthy values from the passed object.

Checks if two objects are deeply equal

ParameterTypeDescription
aObject
bObject
optionsObject

Additional comparison options

options.ignoreObject

Map of property names to ignore when comparing

options.shouldEvaluatefunction

Function used to evaluate if a property should be compared or not. Return false to prevent comparison

options.evaluatefunction

Function used to evaluate equality. Return true/false as evaluation result or anything else to let isEqual handle the comparison

Returns: Boolean
isEmptystatic

Tests whether a passed object has any enumerable properties.

ParameterTypeDescription
objectObject
Returns: Boolean -

true if the passed object has no enumerable properties.

isEqualstatic

Checks if two values are equal. Basically === but special handling of dates.

ParameterTypeDescription
a*

First value

b*

Second value

useIsDeeplyBoolean

Pass true to match arrays & objects using a deep equality check.

matchNullishBoolean

Pass true to have nullish (undefined and null) evaluated as equal.

Returns: * -

true if values are equal, otherwise false

Checks if value a is smaller than value b.

ParameterTypeDescription
a*

First value

b*

Second value

Returns: Boolean -

true if a < b

Checks if value a is bigger than value b.

ParameterTypeDescription
a*

First value

b*

Second value

Returns: Boolean -

true if a > b

isObjectstatic

Returns true if the value is a simple Object.

ParameterTypeDescription
valueObject
Returns: Boolean -

true if the value is a simple Object.

isPartialstatic

Checks if value B is partially equal to value A.

ParameterTypeDescription
a*

First value

b*

Second value

Returns: Boolean -

true if values are partially equal, false otherwise

keysstatic

Returns an array containing all enumerable property names from every prototype level for the object. If object is null, this method returns an empty array.

ParameterTypeDescription
objectObject

Object from which to retrieve property names.

ignoreObject | function

Optional object of names to ignore or a function accepting the name and value which returns true to ignore the item.

mapperfunction

Optional function to call for each non-ignored item. If provided, the result of this function is stored in the returned array. It is called with the array element as the first parameter, and the index in the result array as the second argument (0 for the first, non-ignored element, 1 for the second and so on).

Returns: String[]
mergestatic

Copies all enumerable properties from the supplied source objects to dest, recursing when the properties of both the source and dest are objects.

 const o = {
     a : 1,
     b : {
         c : 2
     }
 };
 const o2 = {
     b : {
         d : 3
     }
 }

 console.log(merge(o, o2));

 > { a : 1, b : { c : 2, d : 3 } }
ParameterTypeDescription
destObject

The destination object.

sourcesObject

The source objects.

Returns: Object -

The dest object.

Recursively wraps an object and all nested objects in Proxy instances. Invokes a callback whenever any property is mutated (set or deleted) at any depth.

This is useful for observing changes in complex objects, such as configuration objects, where you want to track changes to any property, no matter how deeply nested.

It is advisable to debounce the onChange callback to avoid performance issues with frequent updates.

ParameterTypeDescription
targetObject

The root object to wrap in Proxies.

onChangefunction

A callback function called on any mutation. Receives an object with: path (array of keys), oldValue, newValue, and optional deleted flag.

onChange.pathString[]

The path to the changed property.

onChange.oldValueObject

The previous value of the property.

onChange.newValueObject

The new value of the property if any.

onChange.deletedBoolean

Indicates if the property was deleted.

Returns: Object -

A Proxy which wraps the original object.

Checks if a given path exists in an object

ParameterTypeDescription
objectObject

Object to check path on

pathString

Dot-separated path, e.g. 'object.childObject.someKey'

Returns: Boolean -

Returns true if path exists or false if it does not

Changes the passed object and removes all properties from it. Used while mutating when need to keep reference to the object but replace its properties.

ParameterTypeDescription
objectObject

Target object

Returns: Object -

Passed object

roundstatic

Round the passed number to the passed number of decimals.

ParameterTypeDescription
numberNumber

The number to round.

digitsNumber

The number of decimal places to round to.

Returns: Number -

The number rounded to the passed number of decimal places.

roundTostatic

Round the passed number to closest passed step value.

ParameterTypeDescription
numberNumber

The number to round.

stepNumber

The step value to round to.

Returns: Number -

The number rounded to the closest step.

setPathstatic

Sets value for a given path in the object

ParameterTypeDescription
objectObject

Target object

pathString

Dot-separated path, e.g. 'object.childObject.someKey'

value*

Value for a given path

Returns: Object -

Returns passed object

toFixedstatic

Number.toFixed(), with polyfill for browsers that needs it

ParameterTypeDescription
numberNumber
digitsNumber
Returns: String -

A fixed point string representation of the passed number.

Creates a new object where key is a property in array item (ref by default) or index in the array and value is array item.

From:

[
    {
         text : 'foo',
         ref : 'fooItem'
    },
    {
         text : 'bar'
    }
]

To:

{
    fooItem : {
        text : 'foo',
        ref  : 'fooItem'
    },
    1 : {
        text : 'bar'
    }
}
ParameterTypeDescription
arrayOfItemsObject[]

Array to transform.

propString

Property to read the key from. ref by default.

Returns: Object -

namedItems

Creates a new array from object values and saves key in a property (ref by default) of each item.

From:

{
    fooItem : {
        text : 'foo'
    },
    1 : {
        text : 'bar'
    },
    barItem : false // will be ignored
}

To:

[
    {
         text : 'foo',
         ref : 'fooItem'
    },
    {
         text : 'bar',
         ref : 1
    }
]
ParameterTypeDescription
namedItemsObject

Object to transform.

propString

Property to save the key to. ref by default.

Returns: Object[] -

arrayOfItems

typeOfstatic

Returns the specific type of the given value. Unlike the typeof operator, this function returns the text from the Object.prototype.toString result allowing Date, Array, RegExp, and others to be differentiated.

 console.log(typeOf(null));
 > null

 console.log(typeOf({}));
 > object

 console.log(typeOf([]));
 > array

 console.log(typeOf(new Date()));
 > date

 console.log(typeOf(NaN));
 > nan

 console.log(typeOf(/a/));
 > regexp
ParameterTypeDescription
value*
Returns: String