Why another JS util library?
1) 101 will be maintained to minimize overlap with vanilla JS.
- 101 utils are made to work well with vanilla JS methods.
- 101 will only duplicate vanilla JS to provide Functional Programming paradigms, or if the method is not available in a widely supported JS version (currently ES5).
- Other libraries often duplicate a lot of ES5: forEach, map, reduce, filter, sort, and more.
2) No need for custom builds.
- With 101, import naturally, and what you use will be bundled.
- Each util method is a module that can be required
require('101/<util>')
. - Currently node/browserify is supported, I will add other module system support on request.
- Other libraries can be large, and require manually creating custom builds when optimizing for size.
Why not release each as individual modules?
I ussually agree with this philosophy; however, while in practice, adherence to the module-pattern
can become quite annoying for micro-modules (like those in 101):
- Micro-modules existance throughout a project can change very frequently, because of this one may find themselves constantly updating their package.json (repeatedly adding and removing the same micro-modules).
- Unbundling micro-modules can lead to projects with 100's of dependencies which can be tedious to maintain.
Installation
npm install 101
Usage
assign (aka extend)
Just like ES6's Object.assign
. Extend an object with any number of objects (returns original).
; var target = foo: 1 ;var source1 = bar: 1 ;var source2 = baz: 1 ; // { foo: 1, bar: 1, baz: 1 } target extended with source objects // { foo: 1, bar: 1, baz: 1 } target extended with source objects
and
Functional version of &&
. Works great with array.reduce
.
; ; // false; // true
apply
Functional version of function.apply
.
Supports partial functionality (great with array functions).
;sum; // [6] = [sum(1,2,3)] = [1+2+3] { /* sums all arguments */ } { return thisprop; }; // 'val'
clone
It's clone (Only exporting this bc it is used internal to 101)
;var obj = foo: 1 bar: 2; ; // { foo: 1, bar: 2 }
compose
Functional composition method. Works great with array.reduce
.
; 'nope'; // isNaN(parseInt('nope')) // true
envIs
Functional version of str === process.env.NODE_ENV
.
Or's multiple environments.
;// process.env.NODE_ENV = development; // true; // false; // false; // true
equals
Functional version of ===
.
Supports partial functionality (great with array functions).
; ; // true123; // true; // false
exists
Simple exists function.
; ; // true; // false; // false
find
Just like ES6's array.find
.
Finds the first value in the list that passes the given function (predicate) and returns it. If list is not provided find will return a partial-function which accepts a list as the first argument.
;;var arr = a: 1 b: 1 b: 1 c: 1 ; var item = ;// returns { a: 1, b: 1 }// returns null if not found
findIndex
Just like ES6's array.findIndex
.
Finds the first value in the list that passes the given function (predicate) and returns it's index. If list is not provided findIndex will return a partial-function which accepts a list as the first argument.
;var arr = 1 2 3; var index = ;// returns 1// returns -1 if not found
hasKeypaths
Determines whether the keypaths exist and have the specified values. Supports partial functionality (great with array functions, and 101/find).
;var obj = foo: bar: qux: 1 ; ; // true; // true; // false; // false; // false // optional 'deep' arg, defaults to truevar barObj = bar: 1 ;; // true; // true; // false; // true; // true, uses [hasOwnProperty vs in](http://stackoverflow.com/questions/13632999/if-key-in-object-or-ifobject-hasownpropertykey)// use it with find, findIndex, or filter!var arr = obj b: 1 c: 1 ;; // { foo: { bar: { qux: 1 } } }; // { foo: { bar: { qux: 1 } } }
hasProperties
Determines whether the keys exist and, if specified, has the values. Supports partial functionality (great with array functions, and 101/find).
;var obj = foo: bar: 1 qux: 1; ; // true // true // optional 'deep' arg, defaults to truevar barObj = bar: 1 ;; // true; // true; // false; // true, uses [hasOwnProperty vs in](http://stackoverflow.com/questions/13632999/if-key-in-object-or-ifobject-hasownpropertykey)// use it with find, findIndex, or filter!var arr = a: 1 b: 1 b: 1 c: 1 ;; // { a: 1, b: 1 }; // { a: 1, b: 1 }
instanceOf
Functional version of JavaScript's instanceof. Supports partial functionality (great with array functions).
; 'foo' 'bar' 1; // [true, true, false]
isBoolean
Functional version of typeof val === 'boolean'
.
Supports partial functionality (great with array functions).
; true false 1; // [true, true, false]
isEmpty
Functional version of val empty object, array or object
; ; // true; // true; // true; // false
isFunction
Functional version of typeof val === 'function'
; parseInt {} 'foo'; // [true, true, false]
isNumber
Functional version of val typeof 'number'
; 'foo' 'bar' 1; // [false, false, true]
isObject
Functional strict version of val typeof 'object' (and not array or regexp)
; {} foo: 1 100; // [true, true, false]
isString
Functional version of val typeof 'string'
; 'foo' 'bar' 1; // [true, true, false]
last
Returns the last value of a list
; ; // 3; // 'o'
noop
No-op function
; // function () {}
not
Functional version of !
.
; 'hey'; // false100; // true
omit
Returns a new object without the specified keys. Supports partial functionality (great with array functions, like map).
;var obj = foo: 1 bar: 2; ; // { bar: 1 }; // { bar: 1 }; // { } // use it with array.mapobj obj obj; // [{ bar: 1 }, { bar: 1 }, { bar: 1 }];
or
Functional version of ||
.
Works great with array.reduce
.
; ; // true; // true; // false
passAll
Muxes arguments across many functions and &&
's the results.
Supports partial functionality (great with array functions, like map).
; '' 'foo' 'bar' 100; // [false, true, true, false]
passAny
Muxes arguments across many functions and ||
's the results.
Supports partial functionality (great with array functions, like map).
; '' 'foo' 'bar' 100; // [true, true, true, true]
pick
Returns a new object with the specified keys (with key values from obj). Supports partial functionality (great with array functions, like map).
;var obj = foo: 1 bar: 2; ; // { foo: 1 }; // { foo: 1 }; // { foo: 1, bar: 2 } // use it with array.mapobj obj obj; // [{ foo: 1 }, { foo: 1 }, { foo: 1 }];
pluck
Functional version of obj[key], returns the value of the key from obj. Supports partial functionality (great with array functions, like map).
;var obj = foo: 1 bar: 2; ; // 1 // use it with array.mapobj obj obj; // [1, 1, 1] // supports keypaths by defaultvar obj = foo: bar: 1 'foo.bar': 2; ; // 1, supports keypaths by default; // 2, pass false to not use keypaths
set
Functional version of obj[key] = val, returns a new obj with the key and value set. Supports partial functionality (great with array functions, like map).
;var obj = foo: 1 bar: 2; ; // 1 // use it with array.mapobj obj obj; // [{ foo: 100, bar: 2 }, {same}, {same}] // supports keypaths by defaultvar obj = foo: 1 bar: 2; ; // { foo: 100, bar:2 }
License
MIT