Lodash Addons
A collection of utility mixins for lodash. Supports both CommonJS and AMD module formats (meaning, works well with module bundlers or RequireJS-based projects).
Installation
- Yarn:
yarn add --dev lodash-addons
- NPM:
npm install --save-dev lodash-addons
Array
Collection
Date
Lang
_.getArray
_.getBoolean
_.getFinite
_.getFunction
_.getMap
_.getNumber
_.getObject
_.getPlainObject
_.getSet
_.getString
_.getWeakMap
_.getWeakSet
_.isIterable
_.isNonEmptyString
_.toBool
Math
Object
Preconditions
String
Util
“Array” Methods
_.transformValueMap(collection, path, transformer)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L664 "View in source") [Ⓣ][1]
Transforms a value in each element of collection if the path is not undefined.
Arguments
collection
(Array): Array of objectspath
(string): The path of the value to transformtransformer
(function): Callback which returns the transformed value
“Collection” Methods
_.differenceKeys(first, second)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L72 "View in source") [Ⓣ][1]
Gets indices for which elements differ between two arrays.
Arguments
first
(array): First arraysecond
(array): Second array
Example
_;// => [1]
_.filterKeys(collection, iteratee)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L50 "View in source") [Ⓣ][1]
Iterates over keys of a collection, returning an array of all keys predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).
Arguments
collection
(object): The object to iterate over.iteratee
(function): The function invoked per iteration.
“Date” Methods
_.parseDate(val)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L630 "View in source") [Ⓣ][1]
Parses a value by passing it to new Date().
Arguments
val
(string): Value to be parsed
“Lang” Methods
_.getArray(value, replacement)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L165 "View in source") [Ⓣ][1]
Returns value if an array, otherwise a default.
Arguments
value
(mixed): Source valuereplacement
(number): Custom default if value is invalid type.
Example
_;// => [] _;// => ['test']
_.getBoolean(value, replacement)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L186 "View in source") [Ⓣ][1]
Returns value if a boolean, otherwise a default boolean.
Arguments
value
(mixed): Source valuereplacement
(number): Custom default if value is invalid type.
Example
_;// => false _;// => true
_.getFinite(value, replacement)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L210 "View in source") [Ⓣ][1]
Returns value if a finite number, otherwise a default number.
Arguments
value
(mixed): Source valuereplacement
(number): Custom default if value is invalid type.
Example
_;// => 0 _;// => 100 _;// => 25
_.getFunction(value, replacement)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L228 "View in source") [Ⓣ][1]
Returns value if a function, otherwise a default function.
Arguments
value
(mixed): Source valuereplacement
(number): Custom default if value is invalid type.
Example
_;// => function () {}
_.getMap(value, replacement)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L242 "View in source") [Ⓣ][1]
Returns value if a Map, otherwise a default map.
Arguments
value
(mixed): Source valuereplacement
(number): Custom default if value is invalid type.
_.getNumber(value, replacement)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L119 "View in source") [Ⓣ][1]
Returns value if a number, otherwise a default number.
Arguments
value
(mixed): Source valuereplacement
(number): Custom default if value is invalid type.
Example
_;// => 0 _;// => 100
_.getObject(value, replacement)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L260 "View in source") [Ⓣ][1]
Returns value if a object, otherwise a default object.
Arguments
value
(mixed): Source valuereplacement
(number): Custom default if value is invalid type.
Example
_;// => {}
_.getPlainObject(value, replacement)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L278 "View in source") [Ⓣ][1]
Returns value if a plain object, otherwise a default object.
Arguments
value
(mixed): Source valuereplacement
(number): Custom default if value is invalid type.
Example
_;// => {}
_.getSet(value, replacement)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L325 "View in source") [Ⓣ][1]
Returns value if a Set, otherwise a default set.
Arguments
value
(mixed): Source valuereplacement
(set): Custom default if value is invalid type.
Example
_;// => Set()
_.getString(value, replacement)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L343 "View in source") [Ⓣ][1]
Returns value if a string, otherwise a default string.
Arguments
value
(mixed): Source valuereplacement
(number): Custom default if value is invalid type.
Example
_;// => ''
_.getWeakMap(value, replacement)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L361 "View in source") [Ⓣ][1]
Returns value if a WeakMap, otherwise a default WeakMap.
Arguments
value
(mixed): Source valuereplacement
(weakmap): Custom default if value is invalid type.
Example
_;// => ''
_.getWeakSet(value, replacement)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L379 "View in source") [Ⓣ][1]
Returns value if a WeakSet, otherwise a default WeakSet.
Arguments
value
(mixed): Source valuereplacement
(weakset): Custom default if value is invalid type.
Example
_;// => ''
_.isIterable(object)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L447 "View in source") [Ⓣ][1]
Checks if value is iterable.
Arguments
object
(object): An object
Example
_;// => true
_.isNonEmptyString(string)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L464 "View in source") [Ⓣ][1]
Checks if value is a non-empty string.
Arguments
string
(object): String
Example
_;// => false
_.toBool(value)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L430 "View in source") [Ⓣ][1]
Converts a value to a boolean.
Arguments
value
(*): Source value
Example
_;// => true
“Math” Methods
_.sign(value)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L588 "View in source") [Ⓣ][1]
Returns a number representing the sign of value
.
If value
is a positive number, negative number, positive zero or negative zero,
the function will return 1
, -1
, 0
or -0
respectively. Otherwise, NaN is returned.
Arguments
value
(number): A number
Returns
(number): A number representing the sign
Example
_;// => 1 _;// => -1
“Object” Methods
_.hasInOfType(value, path, validator)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L394 "View in source") [Ⓣ][1]
If _.hasIn returns true, run a validator on value.
Arguments
value
(mixed): Collection for _.hasInpath
(number|string): Path.validator
(function): Function to validate value.
_.hasOfType(value, path, validator)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L413 "View in source") [Ⓣ][1]
If _.has returns true, run a validator on value.
Arguments
value
(mixed): Collection for _.haspath
(string): Pathvalidator
(function): Function to validate value.
Example
_;// => true
_.objectWith(object, path, value)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L536 "View in source") [Ⓣ][1]
Shorthand object creation when sole property is a variable, or a path.
Arguments
object
(): Existing object *(optional)*path
(number|string): Propertyvalue
(mixed): Value
Example
// To create a new object: _;// => { key: 'value' } _;// => { a: deep: path: 'value' } // Using existing:_;// => { a: 1, b: 2 }
_.parseQueryString(string)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L562 "View in source") [Ⓣ][1]
Parses query string into key/value object.
Arguments
string
(string): Query string.
Example
_;// => { key: 'value' }
_.toQueryString(object)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L647 "View in source") [Ⓣ][1]
Converts an object's key/values to a query string.
Arguments
object
(object): Source key/value collection
Example
_;// => a=1&b=2
“Preconditions” Methods
_.check(value)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L23 "View in source") [Ⓣ][1]
Throw a TypeError if value doesn't match one of any provided validation methods.
Arguments
value
(mixed): Value
“String” Methods
_.generateKey(length)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L136 "View in source") [Ⓣ][1]
Generates a random alphanumeric string with length n.
Arguments
length
(int): Desired length.
Example
_;// => 'L7IpD'
_.slugify(string)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L617 "View in source") [Ⓣ][1]
Generates a url-safe "slug" form of a string.
Arguments
string
(string): String value.
Example
_;// => a-test
“Util” Methods
_.getPrototype(value)
[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L295 "View in source") [Ⓣ][1]
Gets the prototype for the given value.
Arguments
value
(*): Source value
Example
_;// => { toFixed: func(), ... }