lodash

lodash

Source:
all the mixins added to _

Methods

deepFindBy(collection, propName, propValue, childrenPropNameopt) → {*}

Source:
deeply searches in a recursive tree structure with (same structure) childrenPropName or 'children' property
looking for an item with the propName === propValue

Example

eg. usage

var tree = [{
  id: '1', status: 'enabled', items: [{
    id: '1.1', status: 'enabled', items: [{
      id: '1.1.1', status: 'enabled'
    }, {
      id: '1.1.2', status: 'disabled'
    }]
  }, {
    id: '1.2', status: 'disabled'
  }]
 }, {
  id: '2', status: 'disabled', items: [{
    id: '2.1', status: 'enabled'
  }, {
    id: '2.2', status: 'enabled'
  }]
 }, {
  id: '3', status: 'enabled', items: [{
    id: '3.1', status: 'disabled'
  }, {
    id: '3.2', status: 'enabled'
  }, {
    id: '3.3', status: 'enabled'
  }]
}];

console.log(_.deepFindBy(tree, 'id', '1.1.1', 'items');
// logs {
  id: '1.1.1', status: 'enabled'
}

console.log(_.deepFindBy(tree, function(item) {
  return item.id === '3.2'
}, null, 'items');
// logs {
  id: '3.2', status: 'enabled'
}
Parameters:
Name Type Attributes Default Description
collection Array | object the collection
propName string | function the property name or the predicate function to invoke (item will be passed as parameter to the predicate)
propValue * the property value
childrenPropName string <optional>
'children' the children prop name
Returns:
Type
*

deepMap(collection, childrenPropNameopt, mapCallback)

Source:
deeply maps a recursive tree structure with (same structure) childrenPropName or 'children' property

Example

eg. usage

var tree = [{
  id: '1', status: 'enabled', items: [{
    id: '1.1', status: 'enabled', items: [{
      id: '1.1.1', status: 'enabled'
    }, {
      id: '1.1.2', status: 'disabled'
    }]
  }, {
    id: '1.2', status: 'disabled'
  }]
}];

console.log(_.deepMap(tree, 'items', function(treeItem) {
  return {
    id: treeItem.id,
    status: treeItem.status,
    combo: treeItem.id + '-' + treeItem.status
  };
});

// logs [{
 id: '1', status: 'enabled', combo: '1-enabled' items: [{
   id: '1.1', status: 'enabled', combo: '1.1-enabled', items: [{
     id: '1.1.1', status: 'enabled', combo: '1.1.1-enabled'
   }, {
     id: '1.1.2', status: 'disabled', combo: '1.1.2-disabled'
   }]
 }, {
   id: '1.2', status: 'disabled', combo: '1.2-disabled'
 }]
}]
Parameters:
Name Type Attributes Default Description
collection Array | object the collection to use for the deep mapping
childrenPropName string <optional>
'children' the property name to use for children collection
mapCallback function the item mapping callback

deepOrderBy(collection, propNames, propDirections, childrenPropNameopt) → {Array|object}

Source:
deeply sorts a recursive tree structure with (same structure) childrenPropName or 'children' property

Example

eg. usage

var tree = [{
  id: '1', status: 'enabled', items: [{
    id: '1.1', status: 'enabled', items: [{
      id: '1.1.1', status: 'enabled'
    }, {
      id: '1.1.2', status: 'disabled'
    }]
  }, {
    id: '1.2', status: 'disabled'
  }]
 }, {
  id: '2', status: 'disabled', items: [{
    id: '2.1', status: 'enabled'
  }, {
    id: '2.2', status: 'enabled'
  }]
 }, {
  id: '3', status: 'enabled', items: [{
    id: '3.1', status: 'disabled'
  }, {
    id: '3.2', status: 'enabled'
  }, {
    id: '3.3', status: 'enabled'
  }]
}];

console.log(_.deepOrderBy(tree, ['id'], ['desc'], 'items');
// logs [{
  id: '3', status: 'enabled', items: [{
    id: '3.3', status: 'enabled'
  }, {
    id: '3.2', status: 'disabled'
  }, {
    id: '3.1', status: 'enabled'
  }]
 }, {
  id: '2', status: 'disabled', items: [{
    id: '2.2', status: 'enabled'
  }, {
    id: '2.1', status: 'enabled'
  }]
 }, {
  id: '1', status: 'enabled', items: [{
    id: '1.2', status: 'disabled'
  }, {
    id: '1.1', status: 'enabled', items: [{
      id: '1.1.2', status: 'enabled'
    }, {
      id: '1.1.1', status: 'disabled'
    }]
  }]
}]
Parameters:
Name Type Attributes Default Description
collection Array | object the collection
propNames Array | string the list of property names to sort
propDirections Array | string the list of order by direction to use with propNames
childrenPropName string <optional>
'children' the children prop name
Returns:
Type
Array | object

filterByValues(collection, key, values) → {Array}

Source:
filters a collection with a list of values specified for one property

Example

eg. usage

var collection = [{
  id: 1, status: 'active'
}, {
  id: 2, status: 'disabled'
}, {
  id: 3, status: 'unactive'
}];

var allowedValues = ['active', 'unactive'];

console.log(_.filterByValues(collection, 'status', allowedValues);
// logs [{id: 1, status: 'active'}, {id: 3, status: 'unactive'}]
Parameters:
Name Type Description
collection Array | object the collection to filter
key string the key to be used as property name
values Array the list of values to check
Returns:
Type
Array

isPercentage(s) → {boolean}

Source:
checks if a string is a percentage value

Example

eg. usage

var s = '23.97%';

console.log(_.isPercentage(s)); // true

console.log(_.isPercentage('50%')); // true

console.log(_.isPercentage(10)); // false
Parameters:
Name Type Description
s string the string
Returns:
Type
boolean

parsePercentage(s) → {null|number}

Source:
parses float value in a percentage string

Example

eg. usage

var p = '50.5%';

console.log(_.parsePercentage(p)); // 50.5

console.log(_.parsePercentage('100%')); // 100

console.log(_.parsePercentage(25.3)); // null
Parameters:
Name Type Description
s string the percentage string
Returns:
Type
null | number

pullAllByComparator(collection, values, comparator, iteratee) → {array}

Source:
To Do:
  • document method
Parameters:
Name Type Description
collection collection
values array
comparator function
iteratee function
Returns:
Type
array

timesRange(start, end, iteratee, reverse)

Source:
an implementation of _.times by lodash, where you can specify start & end numbers

Examples

eg. usage

_.timesRange(5, 10, function(i) {
  console.log(i);
});

// logs
5
6
7
8
9
10

or

_.timesRange(5, 10, function(i) {
  console.log(i);
}, true);

// logs
10
9
8
7
6
5
Parameters:
Name Type Description
start number start num of times to invoke iteratee
end number end num of times to invoke iteratee
iteratee function the iteratee function to invoke
the iteratee will be invoked passing che cycle indicator as i
so the iteratee has to be something like this
function(i) {}
reverse boolean specify if you want reverse cycle

timesReverse(times, iteratee)

Source:
a reverse implementation of _.times by lodash

Example

eg. usage

_.timesReverse(5, function(i) {
  console.log(i);
});

// logs
5
4
3
2
1
Parameters:
Name Type Description
times number num of times to invoke iteratee
iteratee function the iteratee function to invoke
the iteratee will be invoked passing che cycle indicator as i
so the iteratee has to be something like this
function(i) {}