Members
(constant) REMOVE :Object
- Source:
Value that denotes that property should be removed
Type:
- Object
Methods
get(source, property, defaultValueopt) → {*}
Returns the value at given path of given object. If path is not found then default value is returned. No exceptions
are thrown when undefined/null value gets in the way.
Examples
get(object, "deep.property") // equals to safe `object.deep.property`
get(object, ["deep", "property"]) // same as above
get(object, "deep[0].property")
// equals to:
object["deep[0]"].property
// not:
object.deep[0].property
get(object, "very.deep.property", 5)
// if object.very.deep has also `property` property then it value will be returned, even if undefined
// else `5` will be returned
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
source |
Object | source object to search in | |
property |
string | Array.<string> | path to the expected value written as dot-separated property names or array with property names. Use Array when your keys includes dots. Keys are treated literally, no parsing is done on keys. | |
defaultValue |
* |
<optional> |
value to return if path is not found. If path is found, but the value is undefined - default value will NOT be used, use `get(...) || default` instead |
Returns:
- found value or default value
- Type
- *
getMultiple(source, defaultValue, …paths) → {*}
- Source:
- See:
-
- get - for base usage example with single path only
Returns first found value at given list of paths of given object. Will return and stop at undefined if found. If
nothing is found then default value (required to pass) will be returned.
Example
getMultiple(obj, 5, "details.error.message", ["error", "message"], "errorMessage")
// will look for obj.details.error.message - if path does not exist
// will look for obj.error.message - if not defined
// will look for obj.errorMessage - if not defined
// will return 5
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
source |
Object | source object to search in | |
defaultValue |
* | default value to return if nothing is found | |
paths |
string | Array.<string> |
<repeatable> |
paths defined as dot-separated properties names or array of properties name |
Returns:
- found value or default value
- Type
- *
insertSeparator(source, separator) → {Array}
- Source:
Inserts given separator between all array elements
Parameters:
Name | Type | Description |
---|---|---|
source |
Array | source array to put new elements between |
separator |
* | separator to inset |
Returns:
- new array with separator items added or same array if there isn't enough items to put separator
- Type
- Array
isEmpty(obj) → {boolean}
- Source:
Returns true if passed argument seems to be empty.
Primitives (excluding string) are always empty (even truthy).
Only empty strings are considered empty.
Objects are considered empty when doesn't have any enumerable & own property.
Arrays and array-like objects are considered empty when length value is 0.
Map, Set and -like objects are considered empty when size value is 0.
Examples
isEmpty({}) // true
isEmpty(100) // true
isEmpty([]) // true
isEmpty([1]) // false
isEmpty({ length: 5 }) // false
isEmpty({ length: 0 }) // true
isEmpty({ size: 0 }) // true
Parameters:
Name | Type | Description |
---|---|---|
obj |
* | source value |
Returns:
- is value considered empty
- Type
- boolean
last(array) → {*}
Gets last element of an array. Will crash on non-array like values.
Examples
last([1, 2]) // 2
last([1]) // 1
last([]) // undefined
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | source array |
Returns:
- last element of an array or undefined
- Type
- *
mapValues(source, fn) → {Object|Array}
- Source:
Iterates through object properties returning object with same properties but modified values. Optionally some
properties may be filtered out on returned object.
Examples
mapValues({ a: 1, b: 2 }, x => x * 2) // will return { a: 1, b: 4 }
mapValues({ a: 1, b: 2 }, () => REMOVE) // will return {}
Parameters:
Name | Type | Description |
---|---|---|
source |
Object | Array | source object |
fn |
mapValuesFn | map function callback that will return new value of a property |
Returns:
- Type
- Object | Array
set(source, path, value) → {Object}
Updates the value at given path of given object. It mutates the object. If path is not found then objects are created
"on the way". If non-objects are found, they are replaced with new plain objects. If primitives are used as source
they are ignored and returned value is empty object with updated value at given path.
Examples
set(object, "deep.property", value)
set(object, ["deep", "property"], value)
set({}, "deep[0].property", value)
// will create this structure:
{ "deep[0]": { "property": value }}
set({}, "items.0", value)
// will create object, not array
{ "items": { "0": value }}
Parameters:
Name | Type | Description |
---|---|---|
source |
Object | source object to mutate |
path |
string | Array.<string> | path where value should be stored, written as dot-separated property names or array with property names. Use Array when your keys includes dots. |
value |
* | value to be set |
Returns:
- given object or new object if source was primitive
- Type
- Object
set(source, path, value) → {Object}
- Source:
Updates the value at given path of given object. It does not mutate the object but returns a new one. If path is not
found then objects are created "on the way". If non-objects are found, they are replaced with new plain objects. If
primitives are used as source they are ignored and returned value is empty object with updated value at given path.
Examples
set(object, "deep.property", value)
set(object, ["deep", "property"], value)
set({}, "deep[0].property", value)
// will create this structure:
{ "deep[0]": { "property": value }}
set({}, "items.0", value)
// will create object, not array
{ "items": { "0": value }}
Parameters:
Name | Type | Description |
---|---|---|
source |
Object | source object to mutate |
path |
string | number | Array.<(string|number)> | path where value should be stored, written as dot-separated property names or array with property names. Use Array when your keys includes dots. |
value |
* | value to be set |
Returns:
- given object or new object if source was primitive
- Type
- Object
Type Definitions
mapValuesFn(value, key) → {*}
- Source:
Should return new value basing on property name and value. Optionally may return exported REMOVE object to filter out
property from the object.
Examples
function fn(value, key) { return value * 5; } // all properties will be multiplied
function fn(value, key) {
if (key === "name") {
return REMOVE;
}
return value.toUpperCase();
}
// will upper case all properties and filter out `name` property
Parameters:
Name | Type | Description |
---|---|---|
value |
* | property value |
key |
string | property name |
Returns:
- Type
- *