101-tomekwi 中文文档教程
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?
我通常同意这种哲学; 然而,在实践中,遵守模块模式
对于微模块(如 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)
就像 ES6 的 Object.assign
。 使用任意数量的对象扩展对象(返回原始对象)。
var assign = require('101/assign');
var target = { foo: 1 };
var source1 = { bar: 1 };
var source2 = { baz: 1 };
assign(target, source1) // { foo: 1, bar: 1, baz: 1 } target extended with source objects
assign(target, source1, source2) // { foo: 1, bar: 1, baz: 1 } target extended with source objects
and
&&
的功能版本。 与 array.reduce
配合使用效果很好。
var and = require('101/and');
and(true, false); // false
and(true, true); // true
and(true, "foo"); // "foo"
apply
function.apply
的功能版本。 支持部分功能(非常适合数组函数)。
var apply = require('101/apply');
[sum].map(apply(null, [1, 2, 3])); // [6] = [sum(1,2,3)] = [1+2+3]
function sum () { /* sums all arguments */ }
apply({ prop: 'val' })(function () { return this.prop; }); // 'val'
clone
它是 clone (只导出这个 bc 它在 101 内部使用)
var clone = require('101/clone');
var obj = {
foo: 1,
bar: 2
};
clone(obj); // { foo: 1, bar: 2 }
compose
功能组合方法。 与 array.reduce
配合使用效果很好。
var compose = require('101/compose');
compose(isNaN, parseInt)('nope'); // isNaN(parseInt('nope')) // true
converge
将一组函数收敛为一个。 与 compose
配合使用效果很好。
var converge = require('101/converge');
converge(mul, [add, sub])(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}
].filter(converge(and , [pluck("a") , pluck("b")])); // [{a: true, b: true}]
[f, converge(g, [h, i]), j].reduce(compose); // f(g(h(j), i(j)))
curry
返回一个柯里化函数。
var curry = require('101/curry');
function add(a, b) { return a + b; }
var curriedAdd = curry(add);
var add2 = curriedAdd(2);
add2(6); // 8
add2(8); // 10
function join() { return Array.prototype.slice.call(arguments).join(''); }
curry(join, 3)(1)(0)(1); // "101"
envIs
str === process.env.NODE_ENV
的功能版本。 或多个环境。
var envIs = require('101/env-is');
// process.env.NODE_ENV = development
envIs('development'); // true
envIs('production'); // false
envIs('staging', 'production'); // false
envIs('development', 'production'); // true
equals
===
的功能版本。 支持部分功能(非常适合数组函数)。
var equals = require('101/equals');
equals(1, 1); // true
[1,2,3].some(equals(1)); // true
equals(1, '1'); // false
exists
简单的存在函数。
var exists = require('101/exists');
exists('foo'); // true
exists(null); // false
exists(undefined); // false
find
就像 ES6 的 array.find
一样。
在列表中查找传递给定函数(谓词)的第一个值并返回它。 如果没有提供列表,find 将返回一个部分函数,它接受一个列表作为第一个参数。
var find = require('101/find');
var hasProps = require('101/has-properties');
var arr = [{ a: 1, b: 1 }, { b: 1 }, { c: 1 }];
var item = find(arr, hasProps({ a:1 }));
// returns { a: 1, b: 1 }
// returns null if not found
findIndex
就像 ES6 的 array.findIndex
一样。
在列表中查找传递给定函数(谓词)的第一个值并返回它的索引。 如果没有提供列表,findIndex 将返回一个部分函数,它接受一个列表作为第一个参数。
var findIndex = require('101/find-index');
var arr = [1, 2, 3];
var index = findIndex(arr, function (val, i, arr) {
return val === 2;
});
// returns 1
// returns -1 if not found
hasKeypaths
确定键路径是否存在并具有指定的值。 支持部分功能(非常适合数组函数和 101/find)。
var hasKeypaths = require('101/has-keypaths');
var obj = {
foo: {
bar: {
qux: 1
}
}
};
hasKeypaths(obj, ['foo.bar.qux']); // true
hasKeypaths(obj, { 'foo.bar.qux': 1 }); // true
hasKeypaths(obj, ['foo.qux']); // false
hasKeypaths(obj, { 'foo.bar': 2 }); // false
hasKeypaths(obj, { 'foo.bar': 1, 'nope': 1 }); // false
// optional 'deep' arg, defaults to true
var barObj = { bar: 1 };
hasKeypaths(obj, { 'foo.bar': barObj }); // true
hasKeypaths(obj, { 'foo.bar': barObj }, true); // true
hasKeypaths(obj, { 'foo.bar': barObj }, false); // false
hasKeypaths(obj, { 'foo.bar': obj.foo }, false); // true
hasKeypaths(obj, ['foo.bar'], 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 = [obj, { b: 1 }, { c: 1 }];
find(arr, hasProps({ 'foo.bar.qux':1 })); // { foo: { bar: { qux: 1 } } }
find(arr, hasProps(['foo.bar.qux'])); // { foo: { bar: { qux: 1 } } }
hasProperties
确定键是否存在,如果指定,是否具有值。 支持部分功能(非常适合数组函数和 101/find)。 注意:我正在考虑弃用这种方法,因为它与 has-keypaths 非常相似。
var hasProps = require('101/has-properties');
var obj = {
qux: 1
};
obj['foo.bar'] = 1
hasProps(obj, ['foo', 'qux']); // true
hasProps(obj, { qux: 1 }) // true
// optional 'deep' arg, defaults to true
var barObj = { bar: 1 };
hasProps(obj, { 'foo.bar': barObj }); // true
hasProps(obj, { 'foo.bar': barObj }, true); // true
hasProps(obj, { 'foo.bar': barObj }, false); // false
hasProps(obj, ['foo.bar'], 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 }];
find(arr, hasProps({ a:1 })); // { a: 1, b: 1 }
find(arr, hasProps(['a'])); // { a: 1, b: 1 }
instanceOf
JavaScript 的 instanceof 的函数式版本。 支持部分功能(非常适合数组函数)。
var instanceOf = require('101/instance-of');
['foo', 'bar', 1].map(instanceOf('string')); // [true, true, false]
isBoolean
typeof val === 'boolean'
的功能版本。 支持部分功能(非常适合数组函数)。
var isBoolean = require('101/is-boolean');
[true, false, 1].map(isBoolean); // [true, true, false]
isEmpty
val 空对象、数组或对象
var isEmpty = require('101/is-empty');
isEmpty([]); // true
isEmpty({}); // true
isEmpty(""); // true
isEmpty(" "); // false
isFunction
的功能版本 typeof val === 'function'
var isFunction = require('101/is-function');
[parseInt, function () {}, 'foo'].map(isFunction); // [true, true, false]
isNumber
的功能版本 val typeof 'number' 的
var isNumber = require('101/is-number');
['foo', 'bar', 1].map(isNumber); // [false, false, true]
isObject
功能版本 strict object'(而不是数组或 regexp)
var isObject = require('101/is-object');
[{}, { foo: 1 }, 100].map(isObject); // [true, true, false]
isRegExp
检查值是否是 RegExp 的实例
var isRegExp = require('101/is-regexp');
[new RegExp('.*'), /.*/, {}, 1].map(isRegExp); // [true, true, false, false]
isString
val typeof 'string' 的功能版本
var isString = require('101/is-string');
['foo', 'bar', 1].map(isString); // [true, true, false]
last
返回列表的最后一个值
var last = require('101/last');
last([1, 2, 3]); // 3
last('hello'); // 'o'
noop
No-op 函数
require('101/noop'); // function () {}
not
!
的功能版本。
var not = require('101/not');
not(isString)('hey'); // false
not(isString)(100); // true
omit
返回一个没有指定键的新对象。 支持部分功能(非常适合数组函数,如地图)。
var omit = require('101/omit');
var obj = {
foo: 1,
bar: 2
};
omit(obj, 'foo'); // { bar: 1 }
omit(obj, ['foo']); // { bar: 1 }
omit(obj, ['foo', 'bar']); // { }
// use it with array.map
[obj, obj, obj].map(omit('foo')); // [{ bar: 1 }, { bar: 1 }, { bar: 1 }];
or
||
的功能版本。 与 array.reduce
配合使用效果很好。
var or = require('101/or');
or(true, true); // true
or(true, false); // true
or(false, false); // false
or("foo", false); // "foo"
passAll
多路复用多个函数的参数和 &&
的结果。 支持部分功能(非常适合数组函数,如地图)。
var passAll = require('101/pass-all');
['', 'foo', 'bar', 100].map(passAll(isString, isTruthy)); // [false, true, true, false]
passAny
多路复用多个函数的参数和 ||
的结果。 支持部分功能(非常适合数组函数,如地图)。
var passAny = require('101/pass-any');
['', 'foo', 'bar', 100].map(passAny(isString, isNumber)); // [true, true, true, true]
pick
返回具有指定键(键值来自 obj)的新对象。 支持正则表达式和部分功能(非常适合数组函数,如 map)。
var pick = require('101/pick');
var obj = {
foo: 1,
bar: 2
};
pick(obj, 'foo'); // { foo: 1 }
pick(obj, RegExp('oo$')); // { foo: 1 }
pick(obj, ['foo']); // { foo: 1 }
pick(obj, ['foo', 'bar']); // { foo: 1, bar: 2 }
// use it with array.map
[obj, obj, obj].map(pick('foo')); // [{ foo: 1 }, { foo: 1 }, { foo: 1 }];
pluck
obj[key] 的函数式版本,从 obj 返回键的值。 支持部分功能(非常适合数组函数,如地图)。
var pluck = require('101/pluck');
var obj = {
foo: 1,
bar: 2
};
pluck(obj, 'foo'); // 1
// use it with array.map
[obj, obj, obj].map(pluck('foo')); // [1, 1, 1]
// supports keypaths by default
var obj = {
foo: {
bar: 1
},
'foo.bar': 2
};
pluck(obj, 'foo.bar'); // 1, supports keypaths by default
pluck(obj, 'foo.bar', false); // 2, pass false to not use keypaths
set
obj[key] = val 的函数式版本,返回一个带有键和值集的新 obj。 支持部分功能(非常适合数组函数,如地图)。
var set = require('101/set');
var obj = {
foo: 1,
bar: 2
};
set(obj, 'foo'); // 1
// use it with array.map
[obj, obj, obj].map(set('foo', 100)); // [{ foo: 100, bar: 2 }, {same}, {same}]
// supports keypaths by default
var obj = {
foo: 1,
bar: 2
};
set(obj, 'foo', 100); // { foo: 100, bar:2 }
xor
异或 与 array.reduce
配合使用效果很好。
var xor = require('101/xor');
xor(true, true); // false
xor(true, false); // true
xor(false, true); // true
xor(false, false); // false
License
麻省理工学院
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 = require('101/assign');
var target = { foo: 1 };
var source1 = { bar: 1 };
var source2 = { baz: 1 };
assign(target, source1) // { foo: 1, bar: 1, baz: 1 } target extended with source objects
assign(target, source1, source2) // { foo: 1, bar: 1, baz: 1 } target extended with source objects
and
Functional version of &&
. Works great with array.reduce
.
var and = require('101/and');
and(true, false); // false
and(true, true); // true
and(true, "foo"); // "foo"
apply
Functional version of function.apply
. Supports partial functionality (great with array functions).
var apply = require('101/apply');
[sum].map(apply(null, [1, 2, 3])); // [6] = [sum(1,2,3)] = [1+2+3]
function sum () { /* sums all arguments */ }
apply({ prop: 'val' })(function () { return this.prop; }); // 'val'
clone
It's clone (Only exporting this bc it is used internal to 101)
var clone = require('101/clone');
var obj = {
foo: 1,
bar: 2
};
clone(obj); // { foo: 1, bar: 2 }
compose
Functional composition method. Works great with array.reduce
.
var compose = require('101/compose');
compose(isNaN, parseInt)('nope'); // isNaN(parseInt('nope')) // true
converge
Converges an array of functions into one. Works great with compose
.
var converge = require('101/converge');
converge(mul, [add, sub])(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}
].filter(converge(and , [pluck("a") , pluck("b")])); // [{a: true, b: true}]
[f, converge(g, [h, i]), j].reduce(compose); // f(g(h(j), i(j)))
curry
Returns a curried function.
var curry = require('101/curry');
function add(a, b) { return a + b; }
var curriedAdd = curry(add);
var add2 = curriedAdd(2);
add2(6); // 8
add2(8); // 10
function join() { return Array.prototype.slice.call(arguments).join(''); }
curry(join, 3)(1)(0)(1); // "101"
envIs
Functional version of str === process.env.NODE_ENV
. Or's multiple environments.
var envIs = require('101/env-is');
// process.env.NODE_ENV = development
envIs('development'); // true
envIs('production'); // false
envIs('staging', 'production'); // false
envIs('development', 'production'); // true
equals
Functional version of ===
. Supports partial functionality (great with array functions).
var equals = require('101/equals');
equals(1, 1); // true
[1,2,3].some(equals(1)); // true
equals(1, '1'); // false
exists
Simple exists function.
var exists = require('101/exists');
exists('foo'); // true
exists(null); // false
exists(undefined); // 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 = require('101/find');
var hasProps = require('101/has-properties');
var arr = [{ a: 1, b: 1 }, { b: 1 }, { c: 1 }];
var item = find(arr, hasProps({ a:1 }));
// 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 = require('101/find-index');
var arr = [1, 2, 3];
var index = findIndex(arr, function (val, i, arr) {
return val === 2;
});
// 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 = require('101/has-keypaths');
var obj = {
foo: {
bar: {
qux: 1
}
}
};
hasKeypaths(obj, ['foo.bar.qux']); // true
hasKeypaths(obj, { 'foo.bar.qux': 1 }); // true
hasKeypaths(obj, ['foo.qux']); // false
hasKeypaths(obj, { 'foo.bar': 2 }); // false
hasKeypaths(obj, { 'foo.bar': 1, 'nope': 1 }); // false
// optional 'deep' arg, defaults to true
var barObj = { bar: 1 };
hasKeypaths(obj, { 'foo.bar': barObj }); // true
hasKeypaths(obj, { 'foo.bar': barObj }, true); // true
hasKeypaths(obj, { 'foo.bar': barObj }, false); // false
hasKeypaths(obj, { 'foo.bar': obj.foo }, false); // true
hasKeypaths(obj, ['foo.bar'], 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 = [obj, { b: 1 }, { c: 1 }];
find(arr, hasProps({ 'foo.bar.qux':1 })); // { foo: { bar: { qux: 1 } } }
find(arr, hasProps(['foo.bar.qux'])); // { 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 = require('101/has-properties');
var obj = {
qux: 1
};
obj['foo.bar'] = 1
hasProps(obj, ['foo', 'qux']); // true
hasProps(obj, { qux: 1 }) // true
// optional 'deep' arg, defaults to true
var barObj = { bar: 1 };
hasProps(obj, { 'foo.bar': barObj }); // true
hasProps(obj, { 'foo.bar': barObj }, true); // true
hasProps(obj, { 'foo.bar': barObj }, false); // false
hasProps(obj, ['foo.bar'], 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 }];
find(arr, hasProps({ a:1 })); // { a: 1, b: 1 }
find(arr, hasProps(['a'])); // { a: 1, b: 1 }
instanceOf
Functional version of JavaScript's instanceof. Supports partial functionality (great with array functions).
var instanceOf = require('101/instance-of');
['foo', 'bar', 1].map(instanceOf('string')); // [true, true, false]
isBoolean
Functional version of typeof val === 'boolean'
. Supports partial functionality (great with array functions).
var isBoolean = require('101/is-boolean');
[true, false, 1].map(isBoolean); // [true, true, false]
isEmpty
Functional version of val empty object, array or object
var isEmpty = require('101/is-empty');
isEmpty([]); // true
isEmpty({}); // true
isEmpty(""); // true
isEmpty(" "); // false
isFunction
Functional version of typeof val === 'function'
var isFunction = require('101/is-function');
[parseInt, function () {}, 'foo'].map(isFunction); // [true, true, false]
isNumber
Functional version of val typeof 'number'
var isNumber = require('101/is-number');
['foo', 'bar', 1].map(isNumber); // [false, false, true]
isObject
Functional strict version of val typeof 'object' (and not array or regexp)
var isObject = require('101/is-object');
[{}, { foo: 1 }, 100].map(isObject); // [true, true, false]
isRegExp
Check if a value is an instance of RegExp
var isRegExp = require('101/is-regexp');
[new RegExp('.*'), /.*/, {}, 1].map(isRegExp); // [true, true, false, false]
isString
Functional version of val typeof 'string'
var isString = require('101/is-string');
['foo', 'bar', 1].map(isString); // [true, true, false]
last
Returns the last value of a list
var last = require('101/last');
last([1, 2, 3]); // 3
last('hello'); // 'o'
noop
No-op function
require('101/noop'); // function () {}
not
Functional version of !
.
var not = require('101/not');
not(isString)('hey'); // false
not(isString)(100); // true
omit
Returns a new object without the specified keys. Supports partial functionality (great with array functions, like map).
var omit = require('101/omit');
var obj = {
foo: 1,
bar: 2
};
omit(obj, 'foo'); // { bar: 1 }
omit(obj, ['foo']); // { bar: 1 }
omit(obj, ['foo', 'bar']); // { }
// use it with array.map
[obj, obj, obj].map(omit('foo')); // [{ bar: 1 }, { bar: 1 }, { bar: 1 }];
or
Functional version of ||
. Works great with array.reduce
.
var or = require('101/or');
or(true, true); // true
or(true, false); // true
or(false, false); // false
or("foo", false); // "foo"
passAll
Muxes arguments across many functions and &&
's the results. Supports partial functionality (great with array functions, like map).
var passAll = require('101/pass-all');
['', 'foo', 'bar', 100].map(passAll(isString, isTruthy)); // [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 = require('101/pass-any');
['', 'foo', 'bar', 100].map(passAny(isString, isNumber)); // [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 = require('101/pick');
var obj = {
foo: 1,
bar: 2
};
pick(obj, 'foo'); // { foo: 1 }
pick(obj, RegExp('oo$')); // { foo: 1 }
pick(obj, ['foo']); // { foo: 1 }
pick(obj, ['foo', 'bar']); // { foo: 1, bar: 2 }
// use it with array.map
[obj, obj, obj].map(pick('foo')); // [{ 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 = require('101/pluck');
var obj = {
foo: 1,
bar: 2
};
pluck(obj, 'foo'); // 1
// use it with array.map
[obj, obj, obj].map(pluck('foo')); // [1, 1, 1]
// supports keypaths by default
var obj = {
foo: {
bar: 1
},
'foo.bar': 2
};
pluck(obj, 'foo.bar'); // 1, supports keypaths by default
pluck(obj, 'foo.bar', false); // 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 = require('101/set');
var obj = {
foo: 1,
bar: 2
};
set(obj, 'foo'); // 1
// use it with array.map
[obj, obj, obj].map(set('foo', 100)); // [{ foo: 100, bar: 2 }, {same}, {same}]
// supports keypaths by default
var obj = {
foo: 1,
bar: 2
};
set(obj, 'foo', 100); // { foo: 100, bar:2 }
xor
Exclusive or Works great with array.reduce
.
var xor = require('101/xor');
xor(true, true); // false
xor(true, false); // true
xor(false, true); // true
xor(false, false); // false
License
MIT