ObjectHelper
A static utility class for deep cloning, comparison, property copying, and other common object operations. It extends the internal Objects class and re-exports its methods for public use.
const copy = ObjectHelper.clone(original);
ObjectHelper.isEqual(a, b); // true if deeply equal
ObjectHelper.assign(dest, src); // like Object.assign but includes inherited props
Useful functions
| Function | Description |
|---|---|
| assign | Copy properties from sources to dest |
| clone | Create a deep copy of a value |
| isEqual | Test two values for deep equality |
| isEmpty | Check whether an object is empty |
See also
- StringHelper - String manipulation utilities
- ArrayHelper - Array manipulation utilities
Functions
Functions are methods available for calling on the class-
Checks that the supplied value is an array. Throws if it is not
-
Checks that the supplied value is a boolean. Throws if it is not
-
Checks that the supplied value is a Bryntum class. Throws if it is not
-
Checks that the supplied value is a function. Throws if it is not
-
Checks that the supplied value is an instance of a Bryntum class. Throws if it is not
-
Checks that the supplied value is a number. Throws if it is not
-
Checks that the supplied value is a plain object. Throws if it is not
-
Checks that the supplied value is a string. Throws if it is not
-
Checks that the supplied value is of the specified type.Throws if it is not
-
Copies all enumerable properties from the supplied source objects to
dest. UnlikeObject.assign, this copy also includes inherited properties. -
Copies all enumerable properties from the supplied source objects to
dest, only including properties that does not already exist ondest. UnlikeObject.assign, this copy also includes inherited properties. -
Changes the passed object and removes all null and undefined properties from it
-
Creates a deep copy of the
value. Simple objects (isObject, arrays andDateobjects are cloned. The enumerable properties of simple objects and the elements of arrays are cloned recursively. -
Copies the named properties from the
sourceparameter into thedestparameter. -
Copies all properties from the
sourceparameter into thedestparameter except those specified inexcept. -
Copies the named properties from the
sourceparameter into thedestparameter unless the property already exists in thedest. -
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.
-
Removes value for a given path in the object. Doesn't cleanup empty objects.
-
Returns an array containing the keys and values of all enumerable properties from every prototype level for the object. If
objectisnull, this method returns an empty array. -
Returns an object retaining the enumerable key/value pairs for which the provided function
fnreturns a truthy value. -
If
valueis iterable (for-ofloopable), this method returns the first element of the iterable sequence. Ifvalueis async-iterable (i.e., uses afor await ofloop), this method returns aPromisethat will resolve to the first element. Otherwise, this method returns the first key/value pair (a 2-element array) using afor-inloop. -
Calls a provided function
fnfor each key/value pair of a givenobject. -
Used by the Base class to make deep copies of defaultConfig blocks
-
Populates an
objectwith the providedentries. -
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.
-
Returns value for a given path in the object
-
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
sourceparameter 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. -
Gathers the values of properties which are truthy into an array.
-
Intercepts access to a
propertyof a givenobject.ObjectHelper.hookProperty(object, 'prop', class { get value() { return super.value; } set value(v) { super.value = v; } });The use of
superallows the hook's getter and setter to invoke the object's existing get/set. -
Checks if two objects are deeply equal
-
Tests whether a passed object has any enumerable properties.
-
Checks if two values are equal. Basically === but special handling of dates.
-
Checks if value a is smaller than value b.
-
Checks if value a is bigger than value b.
-
Returns
trueif thevalueis a simpleObject. -
Checks if value B is partially equal to value A.
-
Returns an array containing all enumerable property names from every prototype level for the object. If
objectisnull, this method returns an empty array. -
Copies all enumerable properties from the supplied source objects to
dest, recursing when the properties of both the source anddestare 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 } } -
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
onChangecallback to avoid performance issues with frequent updates. -
Checks if a given path exists in an object
-
Creates a simple single level key-value object from complex deep object.
-
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.
-
Round the passed number to the passed number of decimals.
-
Round the passed number to closest passed step value.
-
Sets value for a given path in the object
-
Number.toFixed(), with polyfill for browsers that needs it
-
Creates a new object where key is a property in array item (
refby 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' } } -
Creates a new array from object values and saves key in a property (
refby 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 } ] -
Returns the specific type of the given
value. Unlike thetypeofoperator, this function returns the text from theObject.prototype.toStringresult allowingDate,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 -
Returns an array containing the values of all enumerable properties from every prototype level for the object. If
objectisnull, this method returns an empty array.