Methods
(inner) unset(obj, path) → {Object}
- Description:
Removes a path from an object.
- Source:
Example
unset(obj, 'foo.bar')
// Returns the passed in obj, with the value of bar set to undefined
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object | Object to have the attribute removed |
path |
String | Array | Path of attribute to be removed, separated by string |
Returns:
- The passed in object, with the attribute found at the path removed
- Type
- Object
(inner) shallowEqual(col1, col2, path) → {Boolean}
- Description:
Compares a collection's keys / values with another collections keys / values
- Source:
Examples
shallowEqual({ foo: 'bar' }, { foo: 'bar' })
// Returns true
shallowEqual({ foo: 'bar', baz: {} }, { foo: 'bar', baz: {} })
// Returns false, because the baz values are different objects
// Works with array too
shallowEqual([ 1, 2 ], [ 1, 2 ])
// Returns true
shallowEqual([{ foo: 'bar' }], [{ foo: 'bar' }])
// Returns false, because the objects in index 0 are different
// Pass a path to compare instead of the root
shallowEqual({ foo: { bar: { baz: 'biz' }}}, { foo: { bar: { baz: 'biz' }}}, 'foo.bar')
// Returns true, because the bar object is compared
Parameters:
Name | Type | Description |
---|---|---|
col1 |
Object | Array | Collection to compare |
col2 |
Object | Array | Collection to compare |
path |
Array | string | Path of object to compare. Uses the get method to find the path |
Returns:
- true or false if the objects keys values are equal
- Type
- Boolean
(inner) set(obj, path, finalValue) → {Object}
- Description:
Adds a path to an object.
If the path already exists, but not in the correct format it will be replaced.
The path is built from a.
separated string.
I.E. path = 'data.foo.bar' => obj.data.foo.bar will be created on the object.
- Source:
Examples
set(obj, [ 'foo', 'bar' ], 'baz')
// Returns the passed in obj, with the value of bar set to baz
set(obj, 'foo.bar', 'baz')
// Returns the passed in obj, with the value of bar set to baz
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object | Object to have the path added to it |
path |
String | Array | Path that should be created on the object, separated by |
finalValue |
* | When ever the final value of the path should be |
Returns:
- The obj with the passed in value set to the passed in path
- Type
- Object
(inner) repeat(element, times, cloneDeep) → {Array}
- Description:
Returns an array composed of element repeated "times" times. If element is a function, it will be called.
Note: if you simply want to run a function some number of times, without returning an array of its results, @see Method.doIt
- Source:
Examples
repeat(1, 3) // returns [1, 1, 1]
repeat(() => 2 * 2, 3) // returns [4, 4, 4]
Parameters:
Name | Type | Description |
---|---|---|
element |
* | A value or a function. If it is a function, repeat will call it each repeated time |
times |
Number | Number of times that element should be included/called for the resulting array. Anything less than or equal to 0, or not a number, will return an empty array. |
cloneDeep |
Boolean | If true, it will deeply clone the element for every instance in the resulting array |
Returns:
- An array of repeated elements or results from the function call
- Type
- Array
(inner) reduceColl(obj, cb, reduceopt) → {Object}
- Description:
Loops over collection and calls reduce.
- Source:
Example
reduceColl([1, 2, 3], (key, val, coll) => { console.log(key) }, {})
// Returns what ever is returned from the last iteration of the reduce loop
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
obj |
Object | Array | Object to loop over its keys |
|
cb |
function | Predicate function to call for each key of the collection |
|
reduce |
* |
<optional> |
Starting data passed to reduce method |
Returns:
- Last returned data from the loop
- Type
- Object
(inner) mapFind(coll, mapper, testFuncnullable) → {*}
- Description:
Finds the first element in coll whose mapped value passes the testFunc function, then returns the mapped value. It will not map the entire array or object; only the subset needed to find the first passing element.
- Source:
Examples
// Find the first file path that can be required from disk
const filePaths = [...]
const loadedFile = mapFind(filePaths, tryRequireSync)
// Find the first file path whose required value is an object
const filePaths = [...]
const loadedFile = mapFind(filePaths, tryRequireSync, isObj)
// Find the first file path whose required value is an object
const filePaths = { document: "foo/bar/doc.txt", image: "foo/bar/pic.img"}
const loadedFile = mapFind(filePaths, (value, key) => tryRequireSync(value), isObj)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
coll |
Array | Object | Elements to map and find |
|
mapper |
function | Mapping function of form: (value, key, idx) -> *. "key" is the index when coll is an array. "idx" is the index of the array value or object entry. |
|
testFunc |
function |
<nullable> |
Predicate function of form: (mappedValue, key, idx) -> true/false. Defaults to checking if the mapped value is defined. "key" is the index when coll is an array. |
Returns:
- The first passing mapped value
- Type
- *
(inner) mapColl(coll) → {Array|Object}
- Description:
Loops over a collection and calls a passed in function for each one.
- Source:
Example
mapColl([1, 2, 3], (key, val, coll) => { console.log(key) })
// Will log all keys of the collection
Parameters:
Name | Type | Description |
---|---|---|
coll |
Array | Object | Collection to loop over |
Returns:
returns the same type of collection passed in
- Type
- Array | Object
(inner) isEmptyColl(obj) → {Boolean}
- Description:
Checks if passed in obj || array is empty.
- Source:
Examples
isEmptyColl({})
// Returns true
isEmptyColl({ foo: 'bar' })
// Returns false
isEmptyColl([])
// Returns true
Parameters:
Name | Type | Description |
---|---|---|
obj |
* | Object to check if empty |
Returns:
- True if the passed in collection is empty
- Type
- Boolean
(inner) isColl(val) → {Boolean}
- Description:
Checks if the value is a collection ( object || array ).
- Source:
Examples
isColl([1,2,3])
// Returns true
isColl({ foo: 'bar' })
// Returns true
isColl(null)
// Returns false
Parameters:
Name | Type | Description |
---|---|---|
val |
* | Value to check |
Returns:
True if the value is a collection (Object || Array)
- Type
- Boolean
(inner) get(obj, path, fallbackopt) → {*}
- Description:
Searches an object based on the path param
I.E. path = 'data.foo.bar' => will return obj.data.foo.bar.
If bar does not exist, then will return obj.data.foo
- Source:
Examples
get(obj, 'data.foo.bar')
// Returns the value of bar
get(obj, ['data', 'foo', 'bar'])
// Returns the value of bar
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
obj |
Object | Will search the object based on the path |
|
path |
String | Array.<string> | Dot notation string or Array of string keys of the object |
|
fallback |
* |
<optional> |
Separated string to search the object |
Returns:
- The final value found from the path
- Type
- *
(inner) deepEqual(a, b)
- Description:
Recursively checks if two collections are equal
Faster the JSON.stringify checks
See https://jsperf.com/fast-deep-equal-vs-json-stringify
- Source:
Examples
const test = { foo: [ { bar: 'baz' } ] }
const test2 = { foo: [ { bar: 'baz' } ] }
console.log(test === test2)) // prints false
deepEqual(test, test2) // returns true
// Works with arrays too
deepClone([ [ [ 0 ] ] ], [ [ [ 0 ] ] ]) // returns true
Parameters:
Name | Type | Description |
---|---|---|
a |
Object | Array | Object to check |
b |
Object | Array | Object to check against |
(inner) deepClone(obj) → {Object}
- Description:
Recursively clones an object or array.
- Source:
Examples
const test = { foo: [ { bar: 'baz' } ] }
const clone = deepClone(test)
console.log(test === clone)) // prints false
console.log(test.foo === clone.foo) // prints false
// Works with array too
deepClone([ [ [ 0 ] ] ])
// Returns copy of the passed in collection item
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object | Object to clone |
Returns:
- Cloned Object
- Type
- Object
(inner) cleanColl(coll, recursiveopt) → {Object|Array}
- Description:
Cleans a collection by creating a new collection With the null and undefined values removed
- Source:
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
coll |
Object | Array | Collection to remove empty values from |
||
recursive |
Boolean |
<optional> |
true
|
Should recursively clean child values |
Returns:
- Cleaned collection
- Type
- Object | Array