Namespace: Object

XtraUtils.Object

Properties:
Name Type Description
aidsIn function

The Object class

Properties
Name Type Description
prototype Object

the Object prototype

Source:

Methods

(static) clone(obj) → {*}

Shallow clones the object.

Parameters:
Name Type Description
obj *

The item to clone.

Source:
Returns:

The resulting item.

Type
*

(static) cloneDeep(obj) → {Object}

Clones the object on the deep level.

Parameters:
Name Type Description
obj *

The item to clone.

Source:
Returns:

The resulting object

Type
Object
Example
// returns { a: 1, b: 2, c: { d: 4 } }, but the object is not the same
let obj = {
  a: 1,
  b: 2,
  c: {
		 d: 4
  }
}
let clone = obj.deepClone();
clone === obj;		 // false
clone.c === obj.c; // false
clone.a === obj.a; // true

(static) evolve(target, transformations, originalFlagopt) → {Object}

Modifies the object based upon transformation.

Parameters:
Name Type Attributes Default Description
target Object

The target object.

transformations ObjectDiff

The transformations to apply.

originalFlag boolean <optional>
false

Whether to modify the original object.

Source:
Returns:

the resulting object.

Type
Object
Example
let obj = {
  x: 1,
  y: 2
};
let transform = {
  x: 1,
  y: String
};
Object.evolve(obj, transform); // { x: 2, y: "3" }

(static) fromPairs(pairs) → {Object}

Creates an object from an array of key-value pairs.

Parameters:
Name Type Description
pairs Array.<Array>

The key-value pairs to create the object from.

Source:
Returns:

The resulting new object.

Type
Object
Example
// returns { a: 1, b: 2 }
Object.fromPairs([[a,1],[b,2])

(static) functions(obj) → {Array.<string>}

Gets the keys to all of the functions on an object

Parameters:
Name Type Description
obj Object

The object to get the functions from

Source:
Returns:

An array of the paths to the functions.

Type
Array.<string>
Example
Object.functions({a: 1, b: function(){return 2}}); // ["b"]

(static) functionsDeep(obj) → {Array.<string>}

Gets the keys to all of the functions on an object at the deep level.

Parameters:
Name Type Description
obj Object

The object to get the functions from.

Source:
Returns:

An array of paths to functions.

Type
Array.<string>
Example
// returns ['b']
Object.functions({a: 1, b: function(){return 2}});

(static) merge(target, …sources) → {Object}

Merges 1 or more objects.

Parameters:
Name Type Attributes Description
target Object

The object to merge onto. Use an empty object to shallow clone and merge.

sources Object <repeatable>

The objects to merge.

Source:
Returns:

The merged objects.

Type
Object
Example
// returns { a: 1, b: 2 }
Object.merge({a:1},{b:2})

(static) mergeDeep(target, …sources) → {Object}

Merges 1 or more objects on the deep scale.

Parameters:
Name Type Attributes Description
target Object

The object to merge onto. Use an empty object to deep clone and merge.

sources Object <repeatable>

The objects to merge.

Source:
Returns:

The resulting merged objects.

Type
Object
Example
// returns { a: 1, b: 2 }
Object.merge({a:1},{b:2})

(static) size(obj) → {number}

Finds the number of own keys the object has.

Parameters:
Name Type Description
obj Object

The object to get the size of.

Source:
Returns:

The size of the object.

Type
number
Example
// returns 2
Object.size({a: 1, b: {c: 3}});

(static) sizeDeep(obj) → {number}

Finds the number of own keys the object has, and the number of keys it's children has, recursively.

Parameters:
Name Type Description
obj Object

The object to get the size of.

Source:
Returns:

The size of the object.

Type
number
Example
// returns 3
Object.size({a: 1, b: {c: 3}});

(static) truthMap(obj, callBackopt, originalFlagopt, …argsopt) → {Object}

Assigns true to a value based on it's truthiness.

Parameters:
Name Type Attributes Default Description
obj Object

The object to test the truthiness of.

callBack Callback <optional>
(..._)=>{return false}

The function to test the truthiness of a property.

originalFlag boolean <optional>
false

Whether to modify the original object or return a new object.

args * <optional>
<repeatable>

The arguments to hand to the function.

Source:
Returns:

The object, with properties reduced to truthiness.

Type
Object
Example
// returns { a: true, b: true, c: false }
let obj = {
  a: 1,
  b: 2,
  c: 0
};
Object.truthMap(obj, function(key, value) {
  return Boolean(value);
});

conforms(fn, originalFlagopt) → {boolean}

Tells if an object matches the criteria of a function provided. In this method we leave most to the user in the function passed.

Parameters:
Name Type Attributes Default Description
fn function

The function to test on the object

originalFlag boolean <optional>
false

Whether to give the function the original object

Source:
Returns:

Whether the object matches criteria

Type
boolean
Examples
// returns false
let obj = {
  a: 1
};
obj.conforms(function(obj){return obj.a == 2});
// returns true
let obj = {
  b: 2
};
obj.conforms(function(obj) { return obj.b == 2});

deepEqual(obj1, obj2) → {boolean}

Tests if the properties of an object are equal, recursively.

Parameters:
Name Type Description
obj1 *

The first object to compare

obj2 *

The second object to compate

Source:
Returns:

Whether the objects are equal

Type
boolean

deepGet(obj, path) → {*}

Gets a property far down the property chain.

Parameters:
Name Type Description
obj Object

The object to get the deep property from.

path Array.<string> | string

The path to the property.

Source:
Returns:

The property

Type
*
Examples
// returns 2
let obj = {
  a: 1,
  others: {
		 b: 2
  }
};
obj.deepGet(['others','b']);
// returns 2
let obj = {
  a: 1,
  others: {
		 b: 2
  }
};
obj.deepGet('.others.b');

getKeysFor(property) → {Array.<string>}

Gets the possible keys for a value.

Parameters:
Name Type Description
property *

The value.

Source:
Returns:

An array of possible keys.

Type
Array.<string>
Example
// returns [ "a" ]
let obj = {
  a: 1,
  b: 2
}
obj.getKeysFor(1);

invert(originalFlagopt) → {Object}

Inverts the object's keys and values.

Parameters:
Name Type Attributes Default Description
originalFlag boolean <optional>
false

Whether to invert the original object or return a new object

Source:
Returns:

The resulting object

Type
Object
Example
// returns { 1: 'a', 2: 'b' }
let obj = {
  a: 1,
  b: 2
};
obj.invert();

isEmpty() → {boolean}

Tells if an object is empty.

Source:
Returns:

Whether the object is empty or not

Type
boolean
Examples
// returns false
let obj = {
  a: 1
};
obj.isEmpty();
// returns true
let obj = {};
obj.isEmpty();

iterate(callBackopt, originalFlagopt, …argsopt) → {undefined}

Iterates over each property in an object.

Parameters:
Name Type Attributes Default Description
callBack CallBack <optional>
(..._)=>{return false}

The function to call for each property. Although this is optional, this is solely for the purpose of consistency. The default callBack will not function.

originalFlag boolean <optional>
false

Whether to give it the original object;

args * <optional>
<repeatable>

The arguments to hand the function.

Source:
Returns:
Type
undefined

ommit(…omittedKeys) → {Object}

Removes properties from the visible object.

Parameters:
Name Type Attributes Description
omittedKeys string <repeatable>

The keys to omit

Source:
Returns:

The resulting object

Type
Object
Example
// returns { b: 2, __omitted__: Object }
let obj = {
  a: 1,
  b: 2
};
obj.omit("a");

pick(…pickedKeys) → {Object}

Selects properties from an object.

Parameters:
Name Type Attributes Description
pickedKeys string <repeatable>

The keys to pick

Source:
Returns:

The resulting object

Type
Object
Example
// returns { a: 1, __omitted__: Object }
let obj = {
  a: 1,
  b: 2
};
obj.pick("a");

show(…shownKeys) → {Object}

Which is the same as: let obj = { a: 1 omitted: { b: 2 } }; obj.show("b");

Parameters:
Name Type Attributes Description
shownKeys string <repeatable>

The keys to omit, or, if empty, shows all keys.

Source:
Returns:

The resulting object

Type
Object
Example
// returns { a:1, b: 2 }
let obj = {
  a: 1
  __omitted__: {
		b: 2
 }
};
obj.show();

some(callBackopt, originalFlagopt, …argsopt) → {boolean}

Tests to see if some properties pass a test.

Parameters:
Name Type Attributes Default Description
callBack Callback <optional>
(..._)=>{return false}

The function to test the object with.

originalFlag boolean <optional>
false

Whether to hand the function the original object or a duplicate.

args * <optional>
<repeatable>

The arguments to hand to the function.

Source:
Returns:

Whether some of the properties pass the function.

Type
boolean

Type Definitions

CallBack(keyopt, valueopt, objopt, …argsopt) → {*}

The base callback class. All callbacks have a default for consistency. In the case of Object.prototype.reduce, the function is handed the merger as the fourth argument, and remainder of arguments are post-pended to the end if that.

Parameters:
Name Type Attributes Description
key string <optional>

An object key.

value * <optional>

The value of the item.

obj Object <optional>

The object being called from

args * <optional>
<repeatable>

Static arguments determined at call

Default Value:
  • (..._)=>{return false}
Source:
Returns:
Type
*

EvolveCallBack(valueopt) → {*}

The callback class for Object.evolve.

Parameters:
Name Type Attributes Description
value * <optional>

The old value.

Source:
Returns:

The new value.

Type
*

ObjectDiff

Type:
Source: