Module: rgjs/obj

RGJS6 Obj module.

Methods


<static> asArray(obj, keyKey)

Convert object to array.
Alias of rgjs.array.fromObject().

Parameters:
Name Type Description
obj object

An object.

keyKey string

Optional. If object contains objects, obj[k]['keyKey'] will be set to 'k'.

Returns:

An array.

Type
array
Example
rgjs.obj.asArray(a: {id: 1}, b: {id: 2}}) // [{id: 1}, {id: 2}]
rgjs.obj.asArray(a: {id: 1}, b: {id: 2}}, '__key') // [{id: 1, __key: 'a'}, {id: 2, __key: 'b'}]

<static> assign(target, sources)

Polyfill for Object.assign but defaults to deep copy.
If first argument is a boolean, it sets the shallow option. Defaults to false.

Parameters:
Name Type Argument Description
target object

The target object.

sources * <repeatable>

One or more sources to be copied.

Returns:

The assigned object.

Type
object
Example
const o1 = {a: 1, b: 2, c: 3}
const o2 = {x: 24, y: 25, z: 26}
const o3 = {path: {to: {deep: 'value'}}}
rgjs.obj.assign(o1, o2, o3) // {a: 1, b: 2, c: 3, x: 24, y: 25, z: 26, path: {to: {deep: 'value'}}}

<static> compare(obj1, obj2 [, ignorePaths] [, opts] [, path])

Compare two objects.

Parameters:
Name Type Argument Default Description
obj1 object

Object #1.

obj2 object

Object #2.

ignorePaths array <optional>
[]

A list of paths (string or RegExp) that should be ignored. Wildcards can be used (ex: path.to.*.this).

opts object <optional>

Options.

Properties
Name Type Argument Description
debug boolean <optional>

If true, compare() will print info to console.

maxDepth boolean <optional>

Max depth to traverse.

path string <optional>
''

Optional. Prepended for each changed path.

Returns:

An array of the changed paths.

Type
array
Example
const o1 = {a: 1, b: 2, c: {}}
const o2 = {a: 1, b: 2, c: {x: {y: {z: 26}}}}
rgjs.obj.compare(o1, o2); // ['c.x']
rgjs.obj.compare(o1, o2, ['c']); // []
rgjs.obj.compare(o1, o2, ['c.*']); // []

<static> contains()

Check if object contains a value.
Alias of includes().

See:
  • rgjs/obj/includes

<static> copy(obj [, shallow])

Copy an object.

Parameters:
Name Type Argument Description
obj object

The object you want to copy.

shallow bollean <optional>

Optional. copy() defaults to false.

Returns:

A copy of obj.

Type
object
Example
const original = {a: 1, b: 2}
const copy = rgjs.obj.copy(original);
copy.a = 2; // copy = {a: 2, b: 2}, original = {a: 1, b: 2}

<static> count(obj)

Count how many items an object contains.

Parameters:
Name Type Description
obj object

An object.

Returns:

The number of items. Returns '0' if something is amiss.

Type
number
Example
rgjs.obj.count({a: 1, b: 2}) // 2

<static> filter(obj, cb)

Iterate an object. Mimmicks Array.filter().

Parameters:
Name Type Description
obj object

An object.

cb function

The filter callback. Params: (value, key, obj).


<static> findKey(obj, path, value)

Find the key of an object's value.
Example: findKey(obj, 'value', 1) where obj = {a: {value: 1}, b: {value: 2}} returns a.

Parameters:
Name Type Description
obj array

An object containing objects.

path string

The path of the object's value. See obj.getVal(). May be null.

value string

The value to compare obj[key].path with.

Returns:

The key or null if nothing was found.

Type
*
Example
rgjs.obj.findKey({a: 1, b: 2}, null, 1) // 'a'
rgjs.obj.findKey({a: {value: 1}, b: {value: 2}}, 'value', 1) // 'a'

<static> forEach(obj, cb)

Iterate an object. Mimmicks Array.forEach().

Parameters:
Name Type Description
obj object

An object.

cb function

The forEach callback. Params: (value, key, obj).

Example
rgjs.obj.forEach(a: {id: 1}, b: {id: 2}}, (value, key, obj) => { console.log(key, value); })

<static> getByVal(obj, path, value, fallback)

Get an inner object by its property/path value.
Works on everything that can be iterated with for (x in y).
Example: to get the obj with id == 1 in {a: {id: 1}, b: {id: 2}}, use getByVal(obj, 'id', 1);

Parameters:
Name Type Description
obj object

An object.

path string

The key/path of the sub object. Example: 'key1.key2'.

value *

The value that should match

fallback *

The fallback value. Defaults to null.

Returns:

The value at the path. Returns default on failure.

Type
*
Example
rgjs.obj.getByVal({a: {id: 1}, b: {id: 2}}, 'id', 1) // {id: 1}

<static> getFirst(obj)

Get the first value.
Note: order of values in objects is not guaranteed!

Parameters:
Name Type Description
obj object

An object.

Returns:

The value.

Type
*
Example
rgjs.obj.getFirst({a: 1, b: 2}) // 1

<static> getFirstKey(obj)

Get the first key.
Note: order of values in objects is not guaranteed!

Parameters:
Name Type Description
obj object

An object.

Returns:

The key.

Type
string
Example
rgjs.obj.getFirstKey({a: 1, b: 2}) // 'a'

<static> getLast(obj)

Get the last value.
Note: order of values in objects is not guaranteed!

Parameters:
Name Type Description
obj object

An object.

Returns:

The value.

Type
*
Example
rgjs.obj.getLast({a: 1, b: 2}) // 2

<static> getLastKey(obj)

Get the last key.
Note: order of values in objects is not guaranteed!

Parameters:
Name Type Description
obj object

An object.

Returns:

The key.

Type
string
Example
rgjs.obj.getLastKey({a: 1, b: 2}) // 'a'

<static> getObjByVal()

Get an inner object by its property/path value. Alias of getByVal.

Deprecated:
  • Yes
See:
  • rgjs/obj/getByVal

<static> getVal(obj, path [, fallback])

Get a value by it's path.
Useful to safely get a deep nested value without causing a NullPointer.

Parameters:
Name Type Argument Default Description
obj object

An object.

path string

The key/path of the value. Example: 'key1.key2'.

fallback * <optional>
null

The fallback value, returned when val.isSet() returns false. Defaults to null.

Returns:

The value at the path. Returns default on failure.

Type
*
Example
const o = {a: 1, b: 2, c: 3, x: {y: {z: 26}}}
rgjs.obj.getVal(o, 'a'); // 1
rgjs.obj.getVal(o, 'x.y.z'); // 26
rgjs.obj.getVal(o, 'z'); // null
rgjs.obj.getVal(o, 'z', false); // false

<static> hasDiffs(obj1, obj2 [, options])

Compare two objects.

Parameters:
Name Type Argument Description
obj1 object

Object #1.

obj2 object

Object #2.

options object <optional>

Options.

Properties
Name Type Argument Default Description
deep boolean <optional>
false

If true, will perform recursive diff.

ltr boolean <optional>
true

Compare obj1 to obj2.

rtl boolean <optional>
true

Compare obj2 to obj1.

Returns:

True if there are diffs.

Type
boolean

<static> hasKey(obj, key)

Check if object contains 'key'.

Parameters:
Name Type Description
obj object

The object.

key *

The key.

Returns:
Type
boolean
Example
rgjs.obj.hasKey({a: 1, b: 2}, 'a') // true

<static> includes(obj, lookup)

Check if object contains a value.

Parameters:
Name Type Description
obj object

The object.

lookup *

The value you're looking for.

Returns:
Type
boolean
Example
rgjs.obj.includes({a: 1, b: 2}, 2) // true

<static> isObject(obj, strict)

Check if a value is an object.
Note: Returns true for Array's when not in strict mode!

Parameters:
Name Type Description
obj object

An object.

strict boolean

Set strict mode.

Returns:

True or false.

Type
boolean
Example
rgjs.obj.isObject({}) // true
rgjs.obj.isObject([]) // true
rgjs.obj.isObject({}, true) // true
rgjs.obj.isObject([], true) // false

<static> isPlainObject(obj, strict)

Check if plain object.
This is like isObject() in strict mode but stricter.

Parameters:
Name Type Description
obj object

An object.

strict boolean

Set strict mode.

Returns:

True or false.

Type
boolean
Example
class SomeClass() {}
const someClassInstance = new SomeClass();
rgjs.obj.isObject(someClassInstance) // true
rgjs.obj.isObject(someClassInstance, true) // true
rgjs.obj.isPlainObject(someClassInstance) // false

<static> limitToFirst(obj [, max])

Limit the object to the first 'max' entries.
Note: order of values in objects is not guaranteed!

Parameters:
Name Type Argument Default Description
obj object

The object.

max number <optional>
1

The max number of entries.

Returns:

The object containing a max of n entries.

Type
object
Example
rgjs.obj.limitToFirst({a: 1, b: 2, c: 3}, 2) // {a: 1, b: 2}

<static> limitToLast(obj [, max])

Limit the object to the last 'max' entries.
Note: order of values in objects is not guaranteed!

Parameters:
Name Type Argument Default Description
obj object

The object.

max number <optional>
1

The max number of entries.

Returns:

The object containing a max of n entries.

Type
object
Example
rgjs.obj.limitToFirst({a: 1, b: 2, c: 3}, 2) // {b: 2, c: 3}

<static> map(obj, cb)

Iterate an object. Mimmicks Array.map().

Parameters:
Name Type Description
obj object

An object.

cb function

The forEach callback. Params: (value, key, obj).


<static> merge(obj1, obj2, newObj [, opts])

Merge two objects. Similar to rgjs.obj.assign() except it appends array values by default (see opts.onArray).

Parameters:
Name Type Argument Description
obj1 object

An object.

obj2 object

Another object.

newObj boolean

Optional. Create a new copy of obj1 (true) or update the existing obj1 (false). Default true.

opts object <optional>

Optional. Additional options.

Properties
Name Type Argument Default Description
maxDepth number <optional>
32

The max depth to iterate.

onArray string <optional>
'append'

Sets behaviour when an array is found. Values: append, prepend, replace, merge. Default: append.

onArrayDuplicates string <optional>
'add'

What to do when opts.onArray = 'append' or 'prepend' and the array already contains a value. Values: 'add', 'skip'.

Returns:

The result of obj1 + obj2

Type
object

<static> reduce(obj, cb, initial)

Iterate an object. Mimmicks Array.reduce()

Parameters:
Name Type Description
obj object

An object.

cb function

The reducer callback. Params: (accumulator, value, key, obj).

initial *

The initial value


<static> setVal(obj, path, val)

Set a value by its path.
Useful to safely set a deep nested value without causing a NullPointer.

Parameters:
Name Type Description
obj object

An object.

path string

The key/path of the value. Example: 'key1.key2'.

val string

The value.

Returns:

The updated object.

Type
*
Example
rgjs.obj.setVal(obj, 'path.to.value', true) // sets `obj.path.to.value = true`.
rgjs.obj.setVal(obj, 'path.to', 'value', true) // sets `obj.path.to.value = true`.  (LEGACY)
rgjs.obj.setVal(obj, 'path.to.value', null, true) // sets `obj.path.to.value = true`. (LEGACY)
rgjs.obj.setVal(obj, null, 'value', true) // sets `obj.value = true`. (LEGACY)

<static> some(obj, cb)

Iterate an object. Mimmicks Array.some().

Parameters:
Name Type Description
obj object

An object.

cb function

The forEach callback. Params: (value, key, obj).


<static> sortByVal(obj [, path] [, reverse] [, natcase])

Sort by object by property/path's value.

Parameters:
Name Type Argument Description
obj object

An object.

path string <optional>

The key/path of the value. Example: 'key1.key2'.

reverse boolean <optional>

Reverse the array before returning it.

natcase boolean <optional>

Case insensitive.

See:
Returns:

The object as a sorted array.

Type
array
Example
rgjs.obj.sortByVal({a: 3, b: 2, c: 1}) // [1, 2, 3]
rgjs.obj.sortByVal({a: {id: 2}, b: {id: 1}}, 'id') // [{id: 1}, {id: 2}]