Skip to content

TeneoMedia/lodash-addons

 
 

Repository files navigation

Lodash Addons

Build Status Dependencies

A collection of utility mixins for lodash. Supports both CommonJS and AMD module formats (meaning, works well in node/webpack or RequireJS-based projects).

Installation

Node: npm install -save lodash-addons

Bower bower install --save lodash-addons

Array

Collection

Lang

Object

Preconditions

String

Util

Methods

“Array” Methods

_.changes(first, second)

#

Gets indices for which elements differ between two arrays.

Arguments

  1. first (array): First array
  2. second (array): Second array

Example

_.changes([false, true], [false, false]);
// => [1]

_.exceptKeys(array, indices, iteratee)

#

Iterate array skipping given indices.

Arguments

  1. array (array): Source array
  2. indices (array): Indices to skip
  3. iteratee (function): Iteratee

Example

_.exceptKeys(['a', 'b', 'c', 'd'], [1, 3], function(val) {
  // skips "b" and "d"
});

_.indexesOf(array, predicate)

#

Gets indices for elements which predicate returns truthy for.

Arguments

  1. array (array): Target array
  2. predicate (function): Predicate value or function.

Example

_.indexesOf([3, false, 3], _.isNumber);
// => [0, 2]

_.indexesOf([3, false, 3], 3);
// => [0, 2]

“Collection” Methods

_.mapFiltered(array, predicate, iteratee)

#

Map a function to filtered array elements.

Arguments

  1. array (array): Array
  2. predicate (function): Validation method for each element.
  3. iteratee (function): Function to call on each valid element.

_.recurse(collection, iteratee)

#

Invoke a function recursively on every element in a collection.

Arguments

  1. collection (object|array): Collection
  2. iteratee (function): Function to invoke

“Lang” Methods

_.getArray(value, replacement)

#

Returns value if an array, otherwise a default.

Arguments

  1. value (mixed): Source value
  2. replacement (number): Custom default if value is invalid type.

Example

_.getArray(null)
// => []

_.getArray(null, ['test'])
// => ['test']

_.getBoolean(value, replacement)

#

Returns value if a boolean, otherwise a default boolean.

Arguments

  1. value (mixed): Source value
  2. replacement (number): Custom default if value is invalid type.

Example

_.getBoolean(null)
// => false

_.getBoolean(null, true)
// => true

_.getFunction(value, replacement)

#

Returns value if a function, otherwise a default function.

Arguments

  1. value (mixed): Source value
  2. replacement (number): Custom default if value is invalid type.

Example

_.getFunction(null)
// => function () {}

_.getNumber(value, replacement)

#

Returns value if a number, otherwise a default number.

Arguments

  1. value (mixed): Source value
  2. replacement (number): Custom default if value is invalid type.

Example

_.getNumber('')
// => 0

_.getNumber('', 100)
// => 100

_.getObject(value, replacement)

#

Returns value if a object, otherwise a default object.

Arguments

  1. value (mixed): Source value
  2. replacement (number): Custom default if value is invalid type.

Example

_.getObject('')
// => {}

_.getString(value, replacement)

#

Returns value if a string, otherwise a default string.

Arguments

  1. value (mixed): Source value
  2. replacement (number): Custom default if value is invalid type.

Example

_.getString(false)
// => ''

_.toBool(value)

#

Converts a value to a boolean.

Arguments

  1. value (*): Source value

Example

_.toBool(1)
// => true

“Object” Methods

_.fromQueryString(string)

#

Parses query string into key/value object.

Arguments

  1. string (string): Query string.

Example

_.fromQueryString('key=value');
// => { key: 'value' }

_.hasOfType(value, property, validator)

#

If _.has returns true, run a validator on value.

Arguments

  1. value (mixed): Collection for _.has
  2. property (string|number): Propert/key name.
  3. validator (function): Function to validate value.

Example

_.hasOfType({ test: '' }, 'test', _.isString)
// => true

_.hasPrototypeOfType(value, property, predicate)

#

Returns whether an object has a prototype property of the given type.

Arguments

  1. value (*): Source value.
  2. property (string): Prototype property.
  3. predicate (function): Validation function.

_.immutable(object, key, value)

#

Creates an immutable property on an object.

Arguments

  1. object (object): Target object
  2. key (string): Property.
  3. value (mixed): Value.

_.mixInto(target, source)

#

Merge prototype properties from source to target.

Arguments

  1. target (object|function): Target object.
  2. source (object|function): Object/function to mixin.

_.objectWith(object, path, value)

#

Shorthand object creation when sole property is a variable, or a path.

Arguments

  1. object ([object]): Existing object (optional)
  2. path (string|number): Property
  3. value (mixed): Value

Example

// 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 }

_.omitDeep(object, keys)

#

Run _.omit recursively down a collection.

Arguments

  1. object (array|object): Collection
  2. keys (array): Array of property/key names to omit

_.requestSetter(object, property)

#

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.

Arguments

  1. object (object): Target object.
  2. property (string): Property name.

_.toObject(object)

#

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.

Arguments

  1. object (object): Original object.

_.toQueryString(object)

#

Converts an object's key/values to a query string.

Arguments

  1. object (object): Source key/value collection

Example

_.toQueryString({ a: 1, b: 2 })
// => a=1&b=2

_.validatedAssign(models, source, strict)

#

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.

Arguments

  1. models (object): Object of default properties and/or a schema for validation.
  2. source (object): Object to be validated and merged.
  3. strict (boolean): Only properties defined in the model allowed through

Example

_.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 }

“Preconditions” Methods

_.check(value)

#

Throw a TypeError if value doesn't match one of any provided validation methods.

Arguments

  1. value (mixed): Value

_.checkArray(array)

#

Throw a TypeError if value isn't an array.

Arguments

  1. array (mixed): Value

_.checkBoolean(boolean)

#

Throw a TypeError if value isn't a boolean.

Arguments

  1. boolean (mixed): Value

_.checkCollection(collection)

#

Throw a TypeError if value isn't an array or object.

Arguments

  1. collection (mixed): Value

_.checkFunction(func)

#

Throw a TypeError if value isn't a function.

Arguments

  1. func (mixed): Value

_.checkKey(value)

#

Throw a TypeError if value isn't a number/string.

Arguments

  1. value (mixed): Value

_.checkNonEmpty(value)

#

Throw a TypeError if value _.isEmpty

Arguments

  1. value (mixed): Value

_.checkNumber(number)

#

Throw a TypeError if value isn't a number.

Arguments

  1. number (mixed): Value

_.checkObject(object)

#

Throw a TypeError if value isn't an object.

Arguments

  1. object (mixed): Value

_.checkPlainObject(object)

#

Throw a TypeError if value isn't a plain object.

Arguments

  1. object (mixed): Value

_.checkString(string)

#

Throw a TypeError if value isn't a string.

Arguments

  1. string (mixed): Value

“String” Methods

_.generateKey(n)

#

Generates a random alphanumeric string with length n.

Arguments

  1. n (int): Desired length.

Example

_.generateKey(5)
// => 'L7IpD'

_.isNonEmptyString(string)

#

Checks if value is a non-empty string.

Arguments

  1. string (object): String

Example

_.isNonEmptyString('')
// => false

_.slugify(string)

#

Generates a url-safe "slug" form of a string.

Arguments

  1. string (string): String value.

Example

_.slugify('A Test')
// => a-test

“Util” Methods

_.getPrototype(value)

#

Gets the prototype for the given value.

Arguments

  1. value (*): Source value

Example

_.getPrototype(5)
// => { toFixed: func(), ... }

_.hasPrototype(value, property)

#

Checks if a prototype exists, or property exists on the prototype.

Arguments

  1. value (*): Source value
  2. property (string): Prototype property.

Example

_.hasPrototype(null)
// => false

_.hasPrototype(5)
// => true

Methods

isCollection(collection)

#

Checks if a value is either an array or an object.

Arguments

  1. collection (mixed): Value to check

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 99.4%
  • Other 0.6%