📜 ⬆️ ⬇️

Underscore.js is a library that is so good that it should be outlawed

Anyone who had to write voluminous chunks of meaningful javascript code, sooner or later realized that he lacks a lot in this language or some innate constructs are just uncomfortable. To smooth the roughness, jQuery, Prototype, MooTools etc. are used. Someone already has little idea how to code without them. Today I will talk about another little little library that makes the world of the javascript programmer even more beautiful. It's about Underscore.js

Underscore.js or simply _.js is a set of utility functions that functional programming lovers are accustomed to, Ruby, Python, or Prototype.js (but, unlike Prototype, this library does not extend the basic JavaScript classes). It was written to get along well with jQuery.
Underscore.js provides more than 60 functions. Some of them are designed for fans of map-reduce, the other - special auxiliary functions for javascript. The library is able to delegate calls if some functionality is implemented by browser developers.

Here is a list of functions that it implements and examples from official documentation:

Work with collections
each, map, reduce, reduceRight, detect, select, reject, all, any, include, invoke, pluck, max, min, sortBy, sortedIndex, toArray, size
Examples
// Map & Reduce
_.map([1, 2, 3], function(num){ return num * 3 }); // => [3, 6, 9]
var sum = _.reduce([1, 2, 3], 0, function(memo, num){ return memo + num }); // => 6
// true
_.any([null, 0, 'yes', false]); //=> true
// true
_.all([true, 1, null, 'yes']); // => false
//
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}];
_.pluck(stooges, 'name'); // => ["moe", "larry"]

')
Work with arrays
first, rest, last, compact, flatten, without, uniq, intersect, zip, indexOf, lastIndexOf, range
Examples
//
_.first([5, 4, 3, 2, 1]); // => 5
//
_.last([5, 4, 3, 2, 1]); // => 1
//
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1); // => [2, 3, 4]
//
_.uniq([1, 2, 1, 3, 1, 4]); // => [1, 2, 3, 4]


Work with functions
bind, bindAll, memoize, delay, defer, wrap, compose
Examples
//
var fibonacci = function(n) {
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
};
var fastFibonacci = _.memoize(fibonacci);


Work with objects
keys, values, functions, extend, clone, tap, isEqual, isEmpty, isElement, isArray, isArguments, isFunction, isString, isNumber, isBoolean, isDate, isRegExp isNaN, isNull, isUndefined
Examples
//
_.keys({one : 1, two : 2, three : 3}); // => ["one", "two", "three"]
//
_.values({one : 1, two : 2, three : 3}); // => [1, 2, 3]
//
_.extend({name : 'moe'}, {age : 50}); // => {name : 'moe', age : 50}


Utilities
noConflict, identity, times, breakLoop, mixin, uniqueId, template
Examples
//
_(5).times(function(){ console.log('Odelay!"); });


For consecutive calls
chain, value

The names speak for themselves. Documentation can be found here , there are very good and clear examples. I think that even ignorance of English will not stop anyone. But if there is a great need, then you can translate into Russian.
It is worth adding that the compressed library weighs 2.9kb, and the source code is on GitHub .

Upd. Added a few examples.

Source: https://habr.com/ru/post/99161/


All Articles