A collection of utility mixins for lodash. Supports both CommonJS and AMD module formats (meaning, works well with module bundlers or RequireJS-based projects).
- Yarn:
yarn add --dev lodash-addons - NPM:
npm install --save-dev lodash-addons
_.getArray_.getBoolean_.getFinite_.getFunction_.getMap_.getNumber_.getObject_.getPlainObject_.getSet_.getString_.getWeakMap_.getWeakSet_.isIterable_.isNonEmptyString_.toBool
Transforms a value in each element of collection if the path is not undefined.
collection(Array): Array of objectspath(string): The path of the value to transformtransformer(function): Callback which returns the transformed value
[Ⓢ](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.
first(array): First arraysecond(array): Second array
_.differenceKeys([false, true], [false, false]);
// => [1][Ⓢ](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).
collection(object): The object to iterate over.iteratee(function): The function invoked per iteration.
[Ⓢ](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().
val(string): Value to be parsed
[Ⓢ](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.
value(mixed): Source valuereplacement(number): Custom default if value is invalid type.
_.getArray(null);
// => []
_.getArray(null, ['test']);
// => ['test'][Ⓢ](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.
value(mixed): Source valuereplacement(number): Custom default if value is invalid type.
_.getBoolean(null);
// => false
_.getBoolean(null, true);
// => true[Ⓢ](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.
value(mixed): Source valuereplacement(number): Custom default if value is invalid type.
_.getFinite('');
// => 0
_.getFinite('', 100);
// => 100
_.getFinite(NaN, 25);
// => 25[Ⓢ](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.
value(mixed): Source valuereplacement(number): Custom default if value is invalid type.
_.getFunction(null);
// => function () {}[Ⓢ](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.
value(mixed): Source valuereplacement(number): Custom default if value is invalid type.
[Ⓢ](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.
value(mixed): Source valuereplacement(number): Custom default if value is invalid type.
_.getNumber('');
// => 0
_.getNumber('', 100);
// => 100[Ⓢ](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.
value(mixed): Source valuereplacement(number): Custom default if value is invalid type.
_.getObject('');
// => {}[Ⓢ](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.
value(mixed): Source valuereplacement(number): Custom default if value is invalid type.
_.getPlainObject('');
// => {}[Ⓢ](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.
value(mixed): Source valuereplacement(set): Custom default if value is invalid type.
_.getSet('');
// => Set()[Ⓢ](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.
value(mixed): Source valuereplacement(number): Custom default if value is invalid type.
_.getString(false);
// => ''[Ⓢ](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.
value(mixed): Source valuereplacement(weakmap): Custom default if value is invalid type.
_.getWeakMap(false);
// => ''[Ⓢ](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.
value(mixed): Source valuereplacement(weakset): Custom default if value is invalid type.
_.getWeakSet(false);
// => ''[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L447 "View in source") [Ⓣ][1]
Checks if value is iterable.
object(object): An object
_.isIterable([]);
// => true[Ⓢ](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.
string(object): String
_.isNonEmptyString('');
// => false[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L430 "View in source") [Ⓣ][1]
Converts a value to a boolean.
value(*): Source value
_.toBool(1);
// => true[Ⓢ](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.
value(number): A number
(number): A number representing the sign
_.sign(10);
// => 1
_.sign(-10);
// => -1[Ⓢ](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.
value(mixed): Collection for _.hasInpath(number|string): Path.validator(function): Function to validate value.
[Ⓢ](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.
value(mixed): Collection for _.haspath(string): Pathvalidator(function): Function to validate value.
_.hasOfType({ test: '' }, 'test', _.isString);
// => true[Ⓢ](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.
object(): Existing object *(optional)*path(number|string): Propertyvalue(mixed): Value
// To create a new object:
_.objectWith('key', 'value');
// => { key: 'value' }
_.objectWith('a.deep.path', 'value');
// => {
a: {
deep: {
path: 'value'
}
}
}
// Using existing:
_.objectWith({ a: 1 }, 'b', 2);
// => { a: 1, b: 2 }[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L562 "View in source") [Ⓣ][1]
Parses query string into key/value object.
string(string): Query string.
_.parseQueryString('key=value');
// => { key: 'value' }[Ⓢ](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.
object(object): Source key/value collection
_.toQueryString({ a: 1, b: 2 });
// => a=1&b=2[Ⓢ](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.
value(mixed): Value
[Ⓢ](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.
length(int): Desired length.
_.generateKey(5);
// => 'L7IpD'[Ⓢ](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.
string(string): String value.
_.slugify('A Test');
// => a-test[Ⓢ](https://github.com/helion3/lodash-addons/blob/master/dist/lodash-addons.js#L295 "View in source") [Ⓣ][1]
Gets the prototype for the given value.
value(*): Source value
_.getPrototype(5);
// => { toFixed: func(), ... }