A collection of utility mixins for lodash. Supports both CommonJS and AMD module formats (meaning, works well in node/webpack or RequireJS-based projects).
Node: npm install -save lodash-addons
Bower bower install --save lodash-addons
_.fromQueryString
_.hasOfType
_.hasPrototypeOfType
_.immutable
_.mixInto
_.objectWith
_.omitDeep
_.requestSetter
_.toObject
_.toQueryString
_.validatedAssign
_.check
_.checkArray
_.checkBoolean
_.checkCollection
_.checkFunction
_.checkKey
_.checkNonEmpty
_.checkNumber
_.checkObject
_.checkPlainObject
_.checkString
Gets indices for which elements differ between two arrays.
first
(array): First arraysecond
(array): Second array
_.changes([false, true], [false, false]);
// => [1]
Iterate array skipping given indices.
array
(array): Source arrayindices
(array): Indices to skipiteratee
(function): Iteratee
_.exceptKeys(['a', 'b', 'c', 'd'], [1, 3], function(val) {
// skips "b" and "d"
});
Gets indices for elements which predicate returns truthy for.
array
(array): Target arraypredicate
(function): Predicate value or function.
_.indexesOf([3, false, 3], _.isNumber);
// => [0, 2]
_.indexesOf([3, false, 3], 3);
// => [0, 2]
Map a function to filtered array elements.
array
(array): Arraypredicate
(function): Validation method for each element.iteratee
(function): Function to call on each valid element.
Invoke a function recursively on every element in a collection.
collection
(object|array): Collectioniteratee
(function): Function to invoke
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']
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
Returns value if a function, otherwise a default function.
value
(mixed): Source valuereplacement
(number): Custom default if value is invalid type.
_.getFunction(null)
// => function () {}
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
Returns value if a object, otherwise a default object.
value
(mixed): Source valuereplacement
(number): Custom default if value is invalid type.
_.getObject('')
// => {}
Returns value if a string, otherwise a default string.
value
(mixed): Source valuereplacement
(number): Custom default if value is invalid type.
_.getString(false)
// => ''
Converts a value to a boolean.
value
(*): Source value
_.toBool(1)
// => true
Parses query string into key/value object.
string
(string): Query string.
_.fromQueryString('key=value');
// => { key: 'value' }
If _.has returns true, run a validator on value.
value
(mixed): Collection for _.hasproperty
(string|number): Propert/key name.validator
(function): Function to validate value.
_.hasOfType({ test: '' }, 'test', _.isString)
// => true
Returns whether an object has a prototype property of the given type.
value
(*): Source value.property
(string): Prototype property.predicate
(function): Validation function.
Creates an immutable property on an object.
object
(object): Target objectkey
(string): Property.value
(mixed): Value.
Merge prototype properties from source to target.
target
(object|function): Target object.source
(object|function): Object/function to mixin.
Shorthand object creation when sole property is a variable, or a path.
object
([object]): Existing object (optional)path
(string|number): 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 }
Run _.omit recursively down a collection.
object
(array|object): Collectionkeys
(array): Array of property/key names to omit
Gives a setter for prop
only to the first requesting caller.
Creates request[Prop]Setter and get[Prop] methods
on the source object. The first caller to request[Prop]Setter
gets it and therefore is the only one allowed to invoke it.
Any caller may use the getter.
object
(object): Target object.property
(string): Property name.
Recursively invokes "toObject" on objects which support the method.
Many complex objects from libraries like Collections.js, Mongoose,
support to a toObject method for converting to plain objects.
object
(object): Original object.
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
This method is like _.defaultsDeep except it recursively assigns
default properties if the provided values do not exist
OR do not match a given type.
Property value types are inferred from the default value. A default
of "1" is a number, so any incoming value not of type "Number" are
rejected.
To allow a default value which is a different type then the validation,
you can define both a validator
and default
for each property.
models
(object): Object of default properties and/or a schema for validation.source
(object): Object to be validated and merged.strict
(boolean): Only properties defined in the model allowed through
_.validatedAssign({ id: 0 }, {});
// => { id: 0 }
_.validatedAssign({ id: 0 }, { id: false });
// => { id: 0 }
var model = {
name: {
validator: _.isNonEmptyString,
default: false
}
};
var incoming = {
name: ''
};
_.validatedAssign(model, incoming);
// => { name: false }
Throw a TypeError if value doesn't match one of any provided validation methods.
value
(mixed): Value
Throw a TypeError if value isn't an array.
array
(mixed): Value
Throw a TypeError if value isn't a boolean.
boolean
(mixed): Value
Throw a TypeError if value isn't an array or object.
collection
(mixed): Value
Throw a TypeError if value isn't a function.
func
(mixed): Value
Throw a TypeError if value isn't a number/string.
value
(mixed): Value
Throw a TypeError if value _.isEmpty
value
(mixed): Value
Throw a TypeError if value isn't a number.
number
(mixed): Value
Throw a TypeError if value isn't an object.
object
(mixed): Value
Throw a TypeError if value isn't a plain object.
object
(mixed): Value
Throw a TypeError if value isn't a string.
string
(mixed): Value
Generates a random alphanumeric string with length n.
n
(int): Desired length.
_.generateKey(5)
// => 'L7IpD'
Checks if value is a non-empty string.
string
(object): String
_.isNonEmptyString('')
// => false
Generates a url-safe "slug" form of a string.
string
(string): String value.
_.slugify('A Test')
// => a-test
Gets the prototype for the given value.
value
(*): Source value
_.getPrototype(5)
// => { toFixed: func(), ... }
Checks if a prototype exists, or property
exists on the prototype.
value
(*): Source valueproperty
(string): Prototype property.
_.hasPrototype(null)
// => false
_.hasPrototype(5)
// => true
Checks if a value is either an array or an object.
collection
(mixed): Value to check