underscore-es

javaScript's functional programming helper library for ES6 and beyond.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
underscore-es
1.9.86 years ago6 years agoMinified + gzip package size for underscore-es in KB

Readme

__
                      /\ \                                                         __
     __  __    ___    \_\ \     __   _ __   ____    ___    ___   _ __    __       /\_\    ____
    /\ \/\ \ /' _ `\  /'_  \  /'__`\/\  __\/ ,__\  / ___\ / __`\/\  __\/'__`\     \/\ \  /',__\
    \ \ \_\ \/\ \/\ \/\ \ \ \/\  __/\ \ \//\__, `\/\ \__//\ \ \ \ \ \//\  __/  __  \ \ \/\__, `\
     \ \____/\ \_\ \_\ \___,_\ \____\\ \_\\/\____/\ \____\ \____/\ \_\\ \____\/\_\ _\ \ \/\____/
      \/___/  \/_/\/_/\/__,_ /\/____/ \/_/ \/___/  \/____/\/___/  \/_/ \/____/\/_//\ \_\ \/___/
                                                           \ \____/
                                                            \/___/
Underscore.js is a utility-belt library for JavaScript that provides support for the usual functional suspects (each, map, reduce, filter...) without extending any core JavaScript objects. It's available in ES6 and UMD (cjs, amd, iife) formats
ES6 & Beyond usage:
  • partial importation of features (just import what you need, with a name that suits you).

```js import template from 'underscore-es/template';
var basicTemplate =
template("Ok, i use <%= builder %> for my es6 and beyond stuff !"); var result = basicTemplate({builder: 'rollup'}); console.log( result ); // => "Ok, i use rollup for my es6 and beyond stuff !" `` *__nb__: only chain` isn't available in this case.
another way to use chaining feature here is through _compose or _use. ```js import use from 'underscore-es/use'; import initial from 'underscore-es/initial'; import filter from 'underscore-es/filter'; import union from 'underscore-es/union'; import last from 'underscore-es/last';
function note (num) {return 'result: '+ (num + 10) + " / 20"} var test =
use(1, 2, 3, 6, 9
)
.do(_union, [1, 3, 11], [2, 6])
.do([], Array.prototype.concat, [10, 14]) // or simply .do([], [].concat, [10, 14])
.do(_filter, (num) => num % 2 == 0)
.do(_initial)
.do(_last)
.do(note)
.value();
console.log( test ); // => result: 20 / 20 ``` import features depending of their category ```js import as $co from 'underscore-es/collections'; import as $ar from 'underscore-es/arrays'; import as $ob from 'underscore-es/objects'; import as $fu from 'underscore-es/functions'; import as $ut from 'underscore-es/utilities'; console.log( $ar.union(4,7, 7,0,3) ); // => 4, 7, 0, 3 ```
  • global importation of all features

```js import from 'underscore-es'; // or from 'underscore-es/namespace' .chain(4,7) .union(7,0,3) .initial() .reverse() .head() .value() // => 0 ```
  • breaking changes

Since this underscore source code has been rewritten to be more es6 friendly : the _.templateSettings property is now _.template.settings ```js import template from 'underscore-es/template'; template.settings = {
evaluate: /\{\{([\s\S]+?)\}\}/g,
interpolate: /\{\{=([\s\S]+?)\}\}/g
};
var custom = template('
    {{ for (var key in people) { }}
  • {{= peoplekey
}}{{ } }}'); var result = custom({people: {moe: 'Moe', larry: 'Larry', curly: 'Curly'}}); console.log( result ); // =>
  • Moe
  • Larry
  • Curly
`` *the .iteratee shall now (for global importation) be overwritten through .setIteratee(fn)` method ```js import iteratee, {setIteratee} from './underscore-es/iteratee'; import countBy from './underscore-es/countBy'; import isRegExp from './underscore-es/isRegExp'; import filter from './underscore-es/filter'; setIteratee( function(value) {
// RegEx values return a function that returns the number of matches
if (_isRegExp(value)) return function(obj) {
   return (obj.match(value) || []).length;
};
return value;
});
// test some methods that claim to be transformed through _iteratee var collection = 'foo', 'bar', 'bbiz'
; console.log(countBy(collection, /b/g)) // => {0: 1, 1: 1, 2: 1} console.log(filter(collection, /b/g)) // => 'bar', 'bbiz' ``` Documentation is the place to find what you need to know !
This project adheres to a code of conduct. By participating, you are expected to uphold this code.
For support and questions, please use the gitter channel or stackoverflow
Underscore is an open-sourced component of DocumentCloud: https://github.com/documentcloud
Many thanks to our contributors: https://github.com/tnga/underscore-es/contributors https://github.com/jashkenas/underscore/contributors