lodash-mixins-collection
v1.1.0
Published
A little collection of additional mix-ins for great lodash library.
Downloads
2
Readme
lodash-mixins
A little collection of additional mix-ins for great lodash
library (https://github.com/lodash/lodash). Here you can find quick docs of each mixin - they are organized the same way as in the original lodash
docs - alphabetically and by mixin category (string
, collection
, array
... ).
Installation
Install via bower using bower install lodash-mixins --save
or simply download and include lodash-mixins.min.js
. Be sure to initialize original lodash before registering these mix-ins. The only dependency to this package is lodash
itself. ES6 is not required.
NPM support added, you can download it via: npm i lodash-mixins-collection
Array
_.in(value, array)
Alias for existing _.includes
method. Added just to make code more readable in certain cases (notice that order of parameters is switched).
_.in(5, [2,3,5,6]);
// produces true
_.contains(array, value)
Alias for existing _.includes
method.
_.contains([2,3,5,6], 6);
// produces true
Collection
_.renameKeys(collection, fromKey, toKey)
Loops over collection of objects and renames a key.
_.renameKeys([{a: 2, b:4}, {x: 3, b:3}], 'x', 'a')
// produces [{"a":2,"b":4},{"b":3,"a":3}]
Lang
_.isDefined(variable)
This is the opposite of _.isUndefined
.
var a = {b: 1};
_.isDefined(a.c);
// produces false
_.isNumeric(variable)
Checks if given variable represents a numeric value.
_.isNumeric('12');
// produces true
_.isNumeric('12.5');
// produces true
_.isNumeric('12,5');
// produces false
_.isNumeric('dsa');
// produces false
String
_.connect(...)
Connects all arguments with given string. If one of the argument is an array, it will be considered as an array of values that need to be connected. You can pass any number of arguments, the last argument is the string that will connect everything.
If last argument is a string, then that will be used for connecting each part. Also, instead of a string, an object can be passed where you can set three parameters, these are the defaults:
{"main": "", "last": "", "strict": "false"}
If strict is set to false
(the default setting), then values like empty strings and null
will simply be skipped in the final output (examples below).
_.connect('Today', 'is', 'a cool', 'day', ' ');
// produces 'Today is a cool day.'
_.connect('Today', 'is', ['a cool', 'day'], '::');
// produces 'Today::is::a::cool::day.'
_.connect('One', ['two','three','four'], null, 'five', 'six', {main: ', ', last:' and ', strict: false})
// produces 'One, two, three, four, five and six'
_.connect('One', ['two','three','four'], null, 'five', 'six', {main: ', ', last:' and ', strict: true})
// produces 'One, two, three, four, null, five and six'
Additional _.connect helpers are also included:
_.spaced(...); // Connects arguments with a blank space " "
_.dashed(...); // Connects arguments with a dash "-"
_.dotted(...); // Connects arguments with a dot "."
_.slashed(...); // Connects arguments with a forward slash "/"
_.listed(...); // Connects all arguments with a comma, except last one for which 'and' word is used - useful when outputting lists in user interface
_.toSlug(string)
Transforms given string to slug.
_.toSlug('Today is a nice day');
// produces 'today-is-a-nice-day'
_.toUpperCase(string)
Just a wrapper of the original toString method.
_.toUpperCase('very simple');
// produces 'VERY SIMPLE'
_.words(sentence, count, ending)
Returns first N words from given sentence. Optionally, you can append an additional ending to the output ('...' by default).
_.words('testing this thing right now', 3);
// produces 'testing this thing...'