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

    Everyone who had to write voluminous chunks of meaningful javascript code sooner or later realized that he lacked a lot in this language or that some innate constructions were simply inconvenient. JQuery, Prototype, MooTools etc. are used to smooth roughnesses. Someone already has little idea how to code without them. Today I will talk about another maaalenky library that makes the world of javascript-programmer even more beautiful. It's about Underscore.js.

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

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

    Working with collections
    each, map, reduce, reduceRight, detect, select, reject, all, any, include, invoke, pluck, max, min, sortBy, sortedIndex, toArray, size
    Examples Working with arrays Examples Working with functions Examples Working with objects Examples Utilities Examples For consecutive calls The names speak for themselves. The documentation can be found here.
    // 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"]



    first, rest, last, compact, flatten, without, uniq, intersect, zip, indexOf, lastIndexOf, range

    // Первый элемент массива
    _.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]



    bind, bindAll, memoize, delay, defer, wrap, compose

    // Кэшируем результаты вычислений функции
    var fibonacci = function(n) {
    return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
    };
    var fastFibonacci = _.memoize(fibonacci);



    keys, values, functions, extend, clone, tap, isEqual, isEmpty, isElement, isArray, isArguments, isFunction, isString, isNumber, isBoolean, isDate, isRegExp isNaN, isNull, isUndefined

    // Вытащить имена свойств
    _.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}



    noConflict, identity, times, breakLoop, mixin, uniqueId, template

    // Удобно и привычно
    _(5).times(function(){ console.log('Odelay!"); });



    chain, value

    , 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 in a compressed form the library weighs 2.9kb, and the sources are on GitHub .

    Upd. Added some examples.

    Also popular now: