Object Utilities Reference

Table of contents


Introduction

The object utility functions operate on classes, objects, and arrays. All object utility functions are properties of the global/window object and may be used without importing.


Functions

arrayCompact()

Syntax

arrayCompact(a)

Returns a copy of array a with all falsey elements removed.

Parameters

Return value

An array.


arrayFlatten()

Syntax

arrayFlatten(a)

Returns a copy of array a with the contents of array elements added in place of that array. The array elements are recursively processed in depth-first order as required to remove all array elements. Unlike the array flat() method, arrayFlatten(a) will flatten all nested arrays regardless of depth.

Parameters

Return value

An array.


arrayGenerate()

Syntax

arrayGenerate(length, fn)

Returns a new array of length length with the contents specified by the function fn.

Parameters

Return value

An array.


dedup()

Syntax

dedup(a)

Returns a copy of array a with duplicate elements removed.

Parameters

Return value

An array.


deepCompare()

Syntax

deepCompare(a, b)

Deeply compare values a and b. Values of different types return false. If the values are not arrays or objects, they are directly compared. However, numbers will return true if both are NaN. Arrays and the enumerable own keys of objects are recursively explored and compared.

Parameters

Return value

A Boolean. True if the two are identical.


deepCopy()

Syntax

deepCopy(value)

Return a deep copy of a value, object, or array. For arrays, all sub-arrays are recursively copied. For objects, the enumerable own properties of all nested objects are recursively copied, as well as any nested arrays. Other types are returned.

Parameters

Return value

Any type. A deep copy of the value.


mapToObj()

Syntax

mapToObj(map)

Return an object with the Map keys an value transferred to object keys and values.

Parameters

Return value

An object..


objectClone()

Syntax

objectClone(obj)

Return a copy of an object. Unlike objectCopy(), the getters and setters are preserved as getters and setters.

Parameters

Return value

An object.


objectCopy()

Syntax

objectCopy(obj)
objectCopy(obj, deep)

Return a copy of an object. Unlike objectClone(), the getters and setters are not preserved and non-enumerable and non-own properties are ignored. If deep is truthy, the returned array is a deep copy that recursively copies all sub-objects.

Parameters

Return value

An object.


objectEach()

Syntax

objectEach(obj, fn)

Apply a function, fn, to each enumerable own property in obj. Non-enumerable properties are ignored.

Parameters


objectFilter()

Syntax

objectFilter(obj, filter)

Filter the enumerable, own properties of obj and return a new object with only those properties. Getters and setter properties are not preserved. The filter can be a function or an array of allowed keys.

Parameters

Return value

An Iterator for all enumerable object properties.


objectIsValidPath()

Syntax

objectIsValidPath(base, path)

Returns true if the array of keys in path form a valid path to a nested key in base.

Parameters

Return value

A Boolean.


objectMergeAllOver()

Syntax

objectMergeAllOver(target, ...sources)

Copy the enumerable, own properties of the objects in sources to the target object. Returns the merged target object. The keys in the objects in the sources objects override any keys in the target object and earlier objects in the argument list. Ignores inherited properties and Symbol keys. Get/set methods are transferred as get/set methods. If the property value in an args object is the DELETE_KEY symbol, the corresponding key in the target object will be deleted, though the value may again be set by subsequent sources objects.

If the target object is {}, then a new object with the merge of all the sources objects is created.

Unlike objectMergeOver() getters and setters are transferred as getters and setters.

Parameters

Return value

An object. The target object.


objectMergeKeys()

Syntax

objectMergeKeys(to, from, ...keys)

Copy the properties specified by the keys arguments from the from object to the target object.

Parameters

Return value

An object. The target object.


objectMergeOver()

Syntax

objectMergeOver(to, ...sources)

Copy the enumerable, own properties of the objects in sources to the target object. Returns the merged target object. The keys in the objects in the sources objects override any keys in the target object and earlier objects in the argument list. Ignores inherited properties and Symbol keys. Get/set methods are transferred as values, not functions. If the property value in an args object is the DELETE_KEY symbol, the corresponding key in the target object will be deleted, though the value may again be set by subsequent sources objects.

If the target object is {}, then a new object with the merge of all the sources objects is created.

Unlike objectMergeAllOver() getters and setters are transferred as values, not getters and setters.

Parameters

Return value

An object. The target object.


objectMergePathsOver()

Syntax

objectMergePathsOver(paths, target, ...sources)

Copy the enumerable, own properties of the objects in sources to the target object. The nested objects listed in the paths argument will be recursively merged over as well. The paths argument is an array of strings of the form used by objectPath() (e.g., 'a.b') to specify the nested objects in both the target and sources that will be recursively merged over as well.

The keys in a sources object override any keys in the target object and any earlier sources objects in the argument list. Ignores inherited properties and Symbol keys. Get/set methods are transferred as values, not functions. If the property value in an args object is the DELETE_KEY symbol, the corresponding key in the target object will be deleted, though the value may again be set by subsequent sources objects. Returns the merged target object.

If the target object is {}, then a new object with the merge of all the sources objects is created.

Parameters

Return value

The target object.


objectMergePathsUnder()

Syntax

objectMergePathsUnder(paths, target, ...sources)

Copy the enumerable, own properties of the objects in sources to the target object. The nested objects listed in the paths argument will be recursively merged over as well. The paths argument is an array of strings of the form used by objectPath() (e.g., 'a.b') to specify the nested objects in both the target and sources that will be recursively merged over as well.

The keys in the target object or in earlier sources objects will override keys in later objects in the arguments list. Ignores inherited properties and Symbol keys. Get/set methods are transferred as values, not functions.

If the target object is {}, then a new object with the merge of all the sources objects is created.

Return value

The target object.


objectMergeUnder()

Syntax

objectMergeUnder(to, ...sources)

Copy the enumerable, own properties of the objects in sources to the target object. Returns the merged target object. The keys in the target object or in earlier sources objects will override keys in later objects in the arguments list. Ignores inherited properties and Symbol keys. Get/set methods are transferred as values, not functions. If the property value in an args object is the DELETE_KEY symbol, the corresponding key in the target object will be deleted, though the value may again be set by subsequent sources objects.

If the target object is {}, then a new object with the merge of all the sources objects is created.

Parameters

Return value

An object. The target object.


objectOverlay()

Syntax

objectOverlay(to, from, copy)

Copy the enumerable, own properties of the objects in from to the target object. Returns the merged target object. If copy is truthy, a deep copy of the target object is made before merging and the copy is returned. The merged copy recursively merges sub-objects as well.

Parameters

Return value

An object. The target object or a copy of it.


objectPath()

Syntax

objectPath(base, path)
objectPath(base, path, value)

Get or set the value of a property in an object or in a nested object. path is an array of the keys to reference in each level of nesting to the property of interest. If no value is provided, it returns the value of the property specified by path. If a value is provided, the property in the nested object is set to value and the value is returned. If the value is present but set to the DELETE_KEY symbol, then the property is deleted from the nested object.

For example, using:

const base = {
	a: 1,
	b: {ba:{baa: 2, bab:3}},
}

Parameters

Return value

The value of the property that is referenced by base and path.


objectRenameKey()

Syntax

objectRenameKey(obj, oldKey, newKey)
objectRenameKey(obj, oldKey, newKey, value)

Rename oldKey in obj to newKey. If value is provided, the renamed property will have that value; otherwise, it will have the same value as the old property. If oldKey is not present in obj, then obj is not modified.

Parameters


objectTraverse()

Syntax

objectTraverse(obj, fn)
objectTraverse(obj, fn, baseObj)
objectTraverse(obj, fn, baseObj, path)

Traverse all properties in an object and any nested objects within it, calling fn on each one. Getters, setters, inherited properties, and Symbols are ignored. The path is an array of the keys required to get to the current nested object being enumerated, starting from baseObj using objectPath(). If the call to fn() returns a truthy value, objects below the current object are traversed. In most cases, objectTraverse() should be called with only the obj and fn arguments. The baseObj and path arguments are used by internal recursion or to continue a traverse down a specific path (e.g., an Array).

Parameters

Return value

The obj object.


objToMap()

Syntax

objToMap(obj)

Return a new Map instance with all the enumerable own keys and values in obj.

Parameters

Return value

A Map instance with keys corresponding to the enumerable own properties in obj.


toArray()

Syntax

toArray(value)

toArray() ensures that value is processed as an array. If value is an array, then it is returned. If value is undefined, it returns an empty array. If value is something else, then toArray() returns an array with value as its single element.

Parameters

Return value

An array.


transpose()

Syntax

transpose(a)

Transposes a two-dimensional array. The array must have at least one level of nested arrays. Returns a new transposed array.

Parameters

Return value

An array.


useClassMethods()

Syntax

useClassMethods(ctor, obj, fn)

When a class instance that has a superclass is constructed, the superclass constructor will access the subclass' methods if it uses a method that the subclass overrides. This can lead to errors if the subclass methods rely on properties initialized in the subclass constructor. useClassMethods() allows a superclass to access its own methods while constructing. For example:

class Employee {
  constructor (name, group) {
    useClassMethods(Employee, this, function () {
      this.name = name;
      this.setGroup(group);
    })
  }
  setGroup (group) {
    this.group = group;
  }
}

class Manager extends Employee {
  constructor (name, group, subordinates) {
    super(name, group);
    this.subordinates = subordinates.map(name => new Employee(name, group));
  }
  setGroup (group) {
    super.setGroup(group);
    this.subordinates.forEach(sub => sub.setGroup(group));
  }
}
const mgr = new Manager('Fred', 'R&D', ['Wilma', 'Barney']);

Calling useClassMethods() in the Employee constructor allows it to use the Employee's setGroup() method rather than the Manager's setGroup() method when constructing a new Manager object. Calling the Manager's setGroup() method would result in an error as the subordinates property has not been initialized yet.

An alternative to solving similar problems would be to have constructors set up only the minimal information and use method chaining. For example, if the Employee and Manger constructor only set the name then, one could use:

const mgr = new Manager('Fred')
	.setGroup('R&D')
	.setSubordinates(('Wilma', 'Barney']);

However, this can sometimes be inconvenient.

Parameters


Constants

DELETE_KEY

A symbol denoting that a key should be deleted.