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 usually 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 hundreds 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 assign = ; 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
.
var and = ; ; // false; // true; // "foo"
apply
Functional version of function.apply
.
Supports partial functionality (great with array functions).
var apply = ;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 clone = ;var obj = foo: 1 bar: 2; ; // { foo: 1, bar: 2 }
compose
Functional composition method. Works great with array.reduce
.
var compose = ; 'nope'; // isNaN(parseInt('nope')) // true
converge
Converges an array of functions into one. Works great with compose
.
var converge = ; 6 2; // mul(add(6, 2), sub(6, 2)) // (6+2) * (6-2) = 36 a: true b: false a: false b: false a: true b: true; // [{a: true, b: true}] f j; // f(g(h(j), i(j)))
curry
Returns a curried function.
var curry = ; { return a + b; } var curriedAdd = ;var add2 = ; ; // 8; // 10 { return Arrayprototypeslice; } 101; // "101"
envIs
Functional version of str === process.env.NODE_ENV
.
Or's multiple environments.
var envIs = ;// process.env.NODE_ENV = development; // true; // false; // false; // true
equals
Functional version of ===
.
Supports partial functionality (great with array functions).
var equals = ; ; // true123; // true; // false
exists
Simple exists function.
var exists = ; ; // 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 find = ;var hasProps = ;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 findIndex = ;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 hasKeypaths = ;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). NOTE: I am considering deprecating this method, bc it is so similar to has-keypaths.
var hasProps = ;var obj = qux: 1;obj'foo.bar' = 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).
var instanceOf = ; 'foo' 'bar' 1; // [true, true, false]
isBoolean
Functional version of typeof val === 'boolean'
.
Supports partial functionality (great with array functions).
var isBoolean = ; true false 1; // [true, true, false]
isEmpty
Functional version of val empty object, array or object
var isEmpty = ; ; // true; // true; // true; // false
isFunction
Functional version of typeof val === 'function'
var isFunction = ; parseInt {} 'foo'; // [true, true, false]
isNumber
Functional version of val typeof 'number'
var isNumber = ; 'foo' 'bar' 1; // [false, false, true]
isObject
Functional strict version of val typeof 'object' (and not array or regexp)
var isObject = ; {} foo: 1 100; // [true, true, false]
isRegExp
Check if a value is an instance of RegExp
var isRegExp = ; '.*' /.*/ {} 1; // [true, true, false, false]
isString
Functional version of val typeof 'string'
var isString = ; 'foo' 'bar' 1; // [true, true, false]
last
Returns the last value of a list
var last = ; ; // 3; // 'o'
noop
No-op function
; // function () {}
not
Functional version of !
.
var not = ; 'hey'; // false100; // true
omit
Returns a new object without the specified keys. Supports partial functionality (great with array functions, like map).
var omit = ;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
.
var or = ; ; // true; // true; // false; // "foo"
passAll
Muxes arguments across many functions and &&
's the results.
Supports partial functionality (great with array functions, like map).
var passAll = ; '' '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).
var passAny = ; '' 'foo' 'bar' 100; // [true, true, true, true]
pick
Returns a new object with the specified keys (with key values from obj). Supports regular expressions and partial functionality (great with array functions, like map).
var pick = ;var obj = foo: 1 bar: 2; ; // { foo: 1 }; // { 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 pluck = ;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 set = ;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 }
xor
Exclusive or
Works great with array.reduce
.
var xor = ; ; // false; // true; // true; // false
License
MIT