ObjectHelper
Helper for Object manipulation.
Functions
42
Functions
42Checks that the supplied value is an array. Throws if it is not
| Parameter | Type | Description |
|---|---|---|
value | Object | Value to check type of |
name | String | Name of the value, used in error message |
Checks that the supplied value is a boolean. Throws if it is not
| Parameter | Type | Description |
|---|---|---|
value | Object | Value to check type of |
name | String | Name of the value, used in error message |
Checks that the supplied value is a Bryntum class. Throws if it is not
| Parameter | Type | Description |
|---|---|---|
value | Object | Value to check type of |
name | String | Name of the value, used in error message |
Checks that the supplied value is a function. Throws if it is not
| Parameter | Type | Description |
|---|---|---|
value | Object | Value to check type of |
name | String | 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
| Parameter | Type | Description |
|---|---|---|
value | Object | Value to check type of |
name | String | Name of the value, used in error message |
Checks that the supplied value is a number. Throws if it is not
| Parameter | Type | Description |
|---|---|---|
value | Object | Value to check type of |
name | String | Name of the value, used in error message |
Checks that the supplied value is a plain object. Throws if it is not
| Parameter | Type | Description |
|---|---|---|
value | Object | Value to check type of |
name | String | Name of the value, used in error message |
Checks that the supplied value is a string. Throws if it is not
| Parameter | Type | Description |
|---|---|---|
value | Object | Value to check type of |
name | String | Name of the value, used in error message |
Checks that the supplied value is of the specified type.Throws if it is not
| Parameter | Type | Description |
|---|---|---|
value | Object | Value to check type of |
type | String | Expected type |
name | String | Name of the value, used in error message |
allowNull | Boolean | Accept |
Copies all enumerable properties from the supplied source objects to dest. Unlike Object.assign, this copy
also includes inherited properties.
| Parameter | Type | Description |
|---|---|---|
dest | Object | The destination object. |
sources | Object | The source objects. |
The dest object.
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.
| Parameter | Type | Description |
|---|---|---|
dest | Object | The destination object. |
sources | Object | The source objects. |
The dest object.
Changes the passed object and removes all null and undefined properties from it
| Parameter | Type | Description |
|---|---|---|
object | Object | Target object |
keepNull | Boolean | Pass true to only remove undefined properties |
Passed object
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.
| Parameter | Type | Description |
|---|---|---|
value | * | The value to clone. |
handler | function | An optional function to call for values of types other than simple object, array or
|
handler.value | * | The value to clone. |
The cloned value.
Copies the named properties from the source parameter into the dest parameter.
| Parameter | Type | Description |
|---|---|---|
dest | Object | The destination into which properties are copied. |
source | Object | The source from which properties are copied. |
props | String[] | The list of property names. |
The dest object.
Copies all properties from the source parameter into the dest parameter except those specified in except.
| Parameter | Type | Description |
|---|---|---|
dest | Object | The destination into which properties are copied. |
source | Object | The source from which properties are copied. |
except | Object | Object whose keys specify the properties to exclude. |
The dest object.
Copies the named properties from the source parameter into the dest parameter
unless the property already exists in the dest.
| Parameter | Type | Description |
|---|---|---|
dest | Object | The destination into which properties are copied. |
source | Object | The source from which properties are copied. |
props | String[] | The list of property names. |
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.
| Parameter | Type | Description |
|---|---|---|
source | String | String[] | The list of names to convert to object form. |
Returns an object retaining the enumerable key/value pairs for which the provided function fn returns a truthy
value.
| Parameter | Type | Description |
|---|---|---|
object | Object | 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 | function | The function that returns a truthy if the key/value pair should be in the result. |
fn.value | * | The value of the |
fn.name | String | The name of the current property for the iteration. |
fn.object | Object | The |
Calls a provided function fn for each key/value pair of a given object.
| Parameter | Type | Description |
|---|---|---|
object | Object | The object to iterate |
fn | function | The function to call with each property and value of the given |
fn.value | * | The value of the |
fn.name | String | 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.
| Parameter | Type | Description |
|---|---|---|
map | Map | The Map to find the key in (and potentially add to). |
path | String | Number | String[] | Number[] | Dot-separated path, e.g. 'firstChild.childObject.someKey', or the key path as an array, e.g. ['firstChild', 'childObject', 'someKey']. |
defaultValue | Object | Optionally the value to insert if the key is not found. |
Returns value for a given path in the object
| Parameter | Type | Description |
|---|---|---|
object | Object | Object to check path on |
path | String | Dot-separated path, e.g. 'object.childObject.someKey' |
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.
| Parameter | Type | Description |
|---|---|---|
source | Object | String | String[] | Source of keys to gather into an array. |
The keys which had a truthy value.
Gathers the values of properties which are truthy into an array.
| Parameter | Type | Description |
|---|---|---|
source | Object | Source of values to gather into an array. |
The truthy values from the passed object.
Checks if two objects are deeply equal
| Parameter | Type | Description |
|---|---|---|
a | Object | |
b | Object | |
options | Object | Additional comparison options |
options.ignore | Object | Map of property names to ignore when comparing |
options.shouldEvaluate | function | Function used to evaluate if a property should be compared or not. Return false to prevent comparison |
options.evaluate | function | Function used to evaluate equality. Return |
Tests whether a passed object has any enumerable properties.
| Parameter | Type | Description |
|---|---|---|
object | Object |
true if the passed object has no enumerable properties.
Checks if two values are equal. Basically === but special handling of dates.
| Parameter | Type | Description |
|---|---|---|
a | * | First value |
b | * | Second value |
useIsDeeply | Boolean | Pass |
matchNullish | Boolean | Pass |
true if values are equal, otherwise false
Checks if value a is smaller than value b.
| Parameter | Type | Description |
|---|---|---|
a | * | First value |
b | * | Second value |
true if a < b
Checks if value a is bigger than value b.
| Parameter | Type | Description |
|---|---|---|
a | * | First value |
b | * | Second value |
true if a > b
Returns true if the value is a simple Object.
| Parameter | Type | Description |
|---|---|---|
value | Object |
true if the value is a simple Object.
Checks if value B is partially equal to value A.
| Parameter | Type | Description |
|---|---|---|
a | * | First value |
b | * | Second value |
true if values are partially equal, false otherwise
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.
| Parameter | Type | Description |
|---|---|---|
object | Object | Object from which to retrieve property names. |
ignore | Object | function | Optional object of names to ignore or a function accepting the name and value
which returns |
mapper | function | 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). |
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 } }
| Parameter | Type | Description |
|---|---|---|
dest | Object | The destination object. |
sources | Object | The source objects. |
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.
| Parameter | Type | Description |
|---|---|---|
target | Object | The root object to wrap in Proxies. |
onChange | function | A callback function called on any mutation. Receives an object with: path (array of keys), oldValue, newValue, and optional deleted flag. |
onChange.path | String[] | The path to the changed property. |
onChange.oldValue | Object | The previous value of the property. |
onChange.newValue | Object | The new value of the property if any. |
onChange.deleted | Boolean | Indicates if the property was deleted. |
A Proxy which wraps the original object.
Checks if a given path exists in an object
| Parameter | Type | Description |
|---|---|---|
object | Object | Object to check path on |
path | String | Dot-separated path, e.g. 'object.childObject.someKey' |
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.
| Parameter | Type | Description |
|---|---|---|
object | Object | Target object |
Passed object
Round the passed number to the passed number of decimals.
| Parameter | Type | Description |
|---|---|---|
number | Number | The number to round. |
digits | Number | The number of decimal places to round to. |
The number rounded to the passed number of decimal places.
Round the passed number to closest passed step value.
| Parameter | Type | Description |
|---|---|---|
number | Number | The number to round. |
step | Number | The step value to round to. |
The number rounded to the closest step.
Sets value for a given path in the object
| Parameter | Type | Description |
|---|---|---|
object | Object | Target object |
path | String | Dot-separated path, e.g. 'object.childObject.someKey' |
value | * | Value for a given path |
Returns passed object
Number.toFixed(), with polyfill for browsers that needs it
| Parameter | Type | Description |
|---|---|---|
number | Number | |
digits | Number |
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'
}
}
| Parameter | Type | Description |
|---|---|---|
arrayOfItems | Object[] | Array to transform. |
prop | String | Property to read the key from. |
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
}
]
| Parameter | Type | Description |
|---|---|---|
namedItems | Object | Object to transform. |
prop | String | Property to save the key to. |
arrayOfItems
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
| Parameter | Type | Description |
|---|---|---|
value | * |