Utility functions that help to inspect, enumerate, and create JS objects
- Source
Members
(inner, constant) setPrototypeOf
Convenience method for adjusting the prototype of an object.
- Source
Methods
(inner) addScript(object, funcOrString, optNameopt, optMappingopt)
Adds a method to a given object
.
Name | Type | Attributes | Description |
---|---|---|---|
object | object | The object to extend. | |
funcOrString | string | | The function object or source string for the method. | |
optName | string | <optional> | The name of the method. |
optMapping | object | <optional> | The variable mapping for the method, when provided as string. |
- Source
(inner) adoptObject(object, newClass)
Adopts a given object to a new class.
Name | Type | Description |
---|---|---|
object | object | The object to change the class for. |
newClass | function | The new class we want to configure for the object. |
- Source
(inner) asObject(obj) → {object}
Return the object representation if given a primitive value or just the object itself.
Name | Type | Description |
---|---|---|
obj | * | The value to convert to object representation if needed. |
- Source
- Type:
- object
(inner) clone(object) → {object}
Shallow copy.
Name | Type | Description |
---|---|---|
object | object | The object to shallow copy. |
- Source
The copied object.
- Type:
- object
(inner) deepCopy(object) → {object}
Recursively traverses object
and its properties to create a copy.
Name | Type | Description |
---|---|---|
object | object | The object to copy. |
- Source
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.
Name | Type | Description |
---|---|---|
objA | object | The first object to merge. |
objB | object | The second object to merge. |
- Source
(inner) dissoc(object, keys) → {object}
Returns a new object that excludes all of the properties defined in keys
.
Name | Type | Description |
---|---|---|
object | object | The object to reduce. |
keys | Array.<string> | The list of properties to exclude. |
- Source
- 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!
Name | Type | Description |
---|---|---|
a | object | The first object to compare. |
b | object | The second object to compare. |
m | array | Used for memorizing already compared parts of the objects. Does not need to be set explicitly. Enables comparisons of circular objects. |
- Source
- Type:
- boolean
(inner) extend(destination, source)
Add all properties of source
to destination
.
Name | Type | Description |
---|---|---|
destination | object | The source object. |
source | object | The destination object. |
- Source
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
.
Name | Type | Attributes | Description |
---|---|---|---|
object | object | The object to extract the properties from. | |
properties | Array.<string> | The list of properties to extract. | |
mapFunc | function | <optional> | Function to map the ectracted properties to custom values. |
- Source
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
.
Name | Type | Description |
---|---|---|
str | string | The string whose contents to shift. |
indentString | string | The string to insert on the left. |
depth | number | The number of times to indent |
- Source
(inner) inherit(obj) → {object}
Wrapper for Object.create
. Essentially creates a new object that is derived from obj
;
Name | Type | Description |
---|---|---|
obj | object | The object to derive. |
- Source
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.
Name | Type | Description | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
object | Object | The JavaScript Object to be inspected. | |||||||||||||||||||||
options | InspectOptions | Properties
|
- Source
(inner) isMutableType(obj) → {boolean}
Returns wether obj
is a value or mutable type.
Name | Type | Description |
---|---|---|
obj | * | The object to check for. |
- Source
- 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
Name | Type | Description |
---|---|---|
objs | Array.<object> | The list of objects to merge. |
- Source
(inner) mergePropertyInHierarchy(obj, propName)
like merge
but automatically gets all definitions of the value in the prototype chain and merges those.
Name | Type | Description |
---|---|---|
obj | object | The object to whose property definitions to merge. |
propName | string | The name of the property whose definition to merge. |
- Source
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.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
obj | object | The object within wich to look for a new unoccupied property name. | ||
base | string | <optional> | '_' | The base name of the property that allows us to generate well formed property names. |
- Source
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)
Name | Type | Description |
---|---|---|
a | object | |
b | object | |
m | array | Used for memorizing already compared parts of the objects. Does not need to be set explicitly. Enables comparisons of circular objects. |
- Source
Whether a
and b
are equal.
- Type:
- bool
(inner) print(object) → {string}
Returns a stringified representation of an object.
Name | Type | Description |
---|---|---|
object | object | The object to generate a stringified representation for. |
- Source
The stringified, formatted representation of the object.
- Type:
- string
(inner) safeToString(obj) → {string}
Like toString
but catches errors.
Name | Type | Description |
---|---|---|
obj | object | The object the should be converted to string. |
- Source
- Type:
- string
(inner) select(obj, keys) → {object}
Returns a new object that copies all properties with keys
from obj
.
Name | Type | Description |
---|---|---|
obj | object | The object to collect the properties from. |
keys | Array.<string> | The names of the properties to collect. |
- Source
- Type:
- object
(inner) shortPrintStringOf(obj) → {string}
Returns a short stringified representation of obj
.
Name | Type | Description |
---|---|---|
obj | object |
- Source
- 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
- convert "before" requirement into "after" and check if all properties mentioned in after/before are actually there
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
properties | Map.<string, {after: string, before: string}> | The map of properties to check for. | ||
throwErrorOnMissing | boolean | <optional> | false | Wether or not to throw an error on detection of missing properties. |
- Source
(inner) typeStringOf(obj) → {string}
Returns the constructor's name of a obj
.
Name | Type | Description |
---|---|---|
obj | object |
- Source
- Type:
- string
(inner) values(object) → {Array.<any>}
Returns the values held by the object properties.
Name | Type | Description |
---|---|---|
object | object | The object to retrive the values from. |
- Source
- Type:
- Array.<any>
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.
Name | Type | Description |
---|---|---|
obj | object | The object to lookup the property values for. |
name | string | The name of the property to gather the values for. |
- Source
var a = {foo: 3}, b = Object.create(a), c = Object.create(b);
c.foo = 4;
obj.valuesInPropertyHierarchy(c, "foo") // => [3,4]