Utility functions that help to inspect, enumerate, and create JS objects

Members

(inner, constant) setPrototypeOf

Convenience method for adjusting the prototype of an object.

Methods

(inner) addScript(object, funcOrString, optNameopt, optMappingopt)

Adds a method to a given object.

Parameters:
NameTypeAttributesDescription
objectobject

The object to extend.

funcOrStringstring | function

The function object or source string for the method.

optNamestring<optional>

The name of the method.

optMappingobject<optional>

The variable mapping for the method, when provided as string.

(inner) adoptObject(object, newClass)

Adopts a given object to a new class.

Parameters:
NameTypeDescription
objectobject

The object to change the class for.

newClassfunction

The new class we want to configure for the object.

(inner) asObject(obj) → {object}

Return the object representation if given a primitive value or just the object itself.

Parameters:
NameTypeDescription
obj*

The value to convert to object representation if needed.

Returns:
Type: 
object

(inner) clone(object) → {object}

Shallow copy.

Parameters:
NameTypeDescription
objectobject

The object to shallow copy.

Returns:

The copied object.

Type: 
object

(inner) deepCopy(object) → {object}

Recursively traverses object and its properties to create a copy.

Parameters:
NameTypeDescription
objectobject

The object to copy.

Returns:

The deeply copied object.

Type: 
object

(inner) deepMerge(objA, objB)

Performs a deep merge of two objects that recursively merges the properties in case they are objects.

Parameters:
NameTypeDescription
objAobject

The first object to merge.

objBobject

The second object to merge.

(inner) dissoc(object, keys) → {object}

Returns a new object that excludes all of the properties defined in keys.

Parameters:
NameTypeDescription
objectobject

The object to reduce.

keysArray.<string>

The list of properties to exclude.

Returns:
Type: 
object

(inner) equals(a, b, m) → {boolean}

Is object a structurally equivalent to object b?. Performs a deep comparison. Functions are completely ignored, with regards to both their implementation and existence/name!

Parameters:
NameTypeDescription
aobject

The first object to compare.

bobject

The second object to compare.

marray

Used for memorizing already compared parts of the objects. Does not need to be set explicitly. Enables comparisons of circular objects.

Returns:
Type: 
boolean

(inner) extend(destination, source)

Add all properties of source to destination.

Parameters:
NameTypeDescription
destinationobject

The source object.

sourceobject

The destination object.

Example
var dest = {x: 22}, src = {x: 23, y: 24}
obj.extend(dest, src);
dest // => {x: 23,y: 24}

(inner) extract(object, properties, mapFuncopt) → {object}

Takes a list of properties and returns a new object with those properties shallow-copied from object. Similar to select but supports an additional mapFunc.

Parameters:
NameTypeAttributesDescription
objectobject

The object to extract the properties from.

propertiesArray.<string>

The list of properties to extract.

mapFuncfunction<optional>

Function to map the ectracted properties to custom values.

Returns:

A new object with the extracted properties.

Type: 
object

(inner) indent(str, indentString, depth)

Shifts the string a number of times to the right by the contents of indentString.

Parameters:
NameTypeDescription
strstring

The string whose contents to shift.

indentStringstring

The string to insert on the left.

depthnumber

The number of times to indent str by.

(inner) inherit(obj) → {object}

Wrapper for Object.create. Essentially creates a new object that is derived from obj;

Parameters:
NameTypeDescription
objobject

The object to derive.

Returns:

The derived object.

Type: 
object

(inner) inspect(object, options)

Prints a human-readable representation of obj. The printed representation will be syntactically correct JavaScript but will not necessarily evaluate to a structurally identical object. inspect is meant to be used while interactivively exploring JavaScript programs and state.

Parameters:
NameTypeDescription
objectObject

The JavaScript Object to be inspected.

optionsInspectOptions
Properties
NameTypeDescription
printFunctionSourceBoolean

Wether or not to show closures' source code.

escapeKeysBoolean

Wether or not to escape special characters.

maxDepthNumber

The maximum depth upon which to inspect the object.

customPrinterfunction

Custom print function that returns an alternative string representation of values.

maxNumberOfKeysNumber

Limit the number of keys to be printed of an object.

keySorterfunction

Custom sorting function to define the order in which object key/value pairs are printed.

(inner) isMutableType(obj) → {boolean}

Returns wether obj is a value or mutable type.

Parameters:
NameTypeDescription
obj*

The object to check for.

Returns:
Type: 
boolean

(inner) merge(objs)

Given a list of objects, return a new object, containing all properties of all objects. If the same property exist in multiple objects, the right-most property takes precedence. Like extend but will not mutate objects in objs. if objs are arrays just concat them if objs are real objs then merge properties

Parameters:
NameTypeDescription
objsArray.<object>

The list of objects to merge.

(inner) mergePropertyInHierarchy(obj, propName)

like merge but automatically gets all definitions of the value in the prototype chain and merges those.

Parameters:
NameTypeDescription
objobject

The object to whose property definitions to merge.

propNamestring

The name of the property whose definition to merge.

Example
var o1 = {x: {foo: 23}}, o2 = {x: {foo: 24, bar: 15}}, o3 = {x: {baz: "zork"}};
o2.__proto__ = o1; o3.__proto__ = o2;
obj.mergePropertyInHierarchy(o3, "x");
// => {bar: 15, baz: "zork",foo: 24}

(inner) newKeyIn(obj, baseopt) → {string}

Returns a name for a key in an object that is not yet occupied.

Parameters:
NameTypeAttributesDefaultDescription
objobject

The object within wich to look for a new unoccupied property name.

basestring<optional>
'_'

The base name of the property that allows us to generate well formed property names.

Returns:

An unoccpuied property name.

Type: 
string

(inner) objectEquals(a, b, m) → {bool}

Helper Method of equals below. Idea and partial implementation taken from Chai.JS (MIT, Copyright (c) 2017 Chai.js Assertion Library)

Parameters:
NameTypeDescription
aobject
bobject
marray

Used for memorizing already compared parts of the objects. Does not need to be set explicitly. Enables comparisons of circular objects.

Returns:

Whether a and b are equal.

Type: 
bool

(inner) print(object) → {string}

Returns a stringified representation of an object.

Parameters:
NameTypeDescription
objectobject

The object to generate a stringified representation for.

Returns:

The stringified, formatted representation of the object.

Type: 
string

(inner) safeToString(obj) → {string}

Like toString but catches errors.

Parameters:
NameTypeDescription
objobject

The object the should be converted to string.

Returns:
Type: 
string

(inner) select(obj, keys) → {object}

Returns a new object that copies all properties with keys from obj.

Parameters:
NameTypeDescription
objobject

The object to collect the properties from.

keysArray.<string>

The names of the properties to collect.

Returns:
Type: 
object

(inner) shortPrintStringOf(obj) → {string}

Returns a short stringified representation of obj.

Parameters:
NameTypeDescription
objobject
Returns:
Type: 
string

(inner) sortKeysWithBeforeAndAfterConstraints(properties, throwErrorOnMissingopt)

Expects properties to be a map of keys to objects having optional before/after attributes that, if present, should be lists of other property keys. sortProperties will return an ordered list of property keys so that the before / after requirements are fullfilled. If a cyclic dependency is encountered an error will be thrown. Example:

sortProperties({foo: {}, bar: {after: ["foo"], before: ["baz"]}, "baz": {after: ["foo"]}})
// => ["foo","bar","baz"]

ignore-in-doc

  1. convert "before" requirement into "after" and check if all properties mentioned in after/before are actually there
Parameters:
NameTypeAttributesDefaultDescription
propertiesMap.<string, {after: string, before: string}>

The map of properties to check for.

throwErrorOnMissingboolean<optional>
false

Wether or not to throw an error on detection of missing properties.

(inner) typeStringOf(obj) → {string}

Returns the constructor's name of a obj.

Parameters:
NameTypeDescription
objobject
Returns:
Type: 
string

(inner) values(object) → {Array.<any>}

Returns the values held by the object properties.

Parameters:
NameTypeDescription
objectobject

The object to retrive the values from.

Returns:
Type: 
Array.<any>
Example
var obj1 = {x: 22}, obj2 = {x: 23, y: {z: 3}};
obj2.__proto__ = obj1;
obj.values(obj1) // => [22]
obj.values(obj2) // => [23,{z: 3}]

(inner) valuesInPropertyHierarchy(obj, name)

Lookup all properties named name in the proto hierarchy of obj.

Parameters:
NameTypeDescription
objobject

The object to lookup the property values for.

namestring

The name of the property to gather the values for.

Example
var a = {foo: 3}, b = Object.create(a), c = Object.create(b);
c.foo = 4;
obj.valuesInPropertyHierarchy(c, "foo") // => [3,4]