101
common utils that can be required selectively
and
Functional version of &&
var and = require('101/and');
and(true, false);
and(true, true);
apply
Functional version of function.apply
var apply = require('101/apply');
and(true, false);
and(true, true);
exists
Simple exists function
var exists = require('101/exists');
exists('foo');
exists(null);
exists(undefined);
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;
});
hasKeypaths
Determines whether the keypaths exist and have the specified values.
var hasKeypaths = require('101/has-keypaths');
var obj = {
foo: {
bar: {
qux: 1
}
}
};
hasKeypaths(obj, ['foo.bar.qux']);
hasKeypaths(obj, { 'foo.bar.qux': 1 });
hasKeypaths(obj, ['foo.qux']);
hasKeypaths(obj, { 'foo.bar': 2 });
hasKeypaths(obj, { 'foo.bar': 1, 'nope': 1 });
var barObj = { bar: 1 };
hasKeypaths(obj, { 'foo.bar': barObj });
hasKeypaths(obj, { 'foo.bar': barObj }, true);
hasKeypaths(obj, { 'foo.bar': barObj }, false);
hasKeypaths(obj, { 'foo.bar': obj.foo }, false);
hasKeypaths(obj, ['foo.bar'], false);
hasProperties
Determines whether the keys exist and, if specified, has the values.
var hasProperties = require('101/has-properties');
var obj = {
foo: {
bar: 1
},
qux: 1
};
hasProperties(obj, ['foo', 'qux']);
hasProperties(obj, { qux: 1 })
var barObj = { bar: 1 };
hasProperties(obj, { 'foo.bar': barObj });
hasProperties(obj, { 'foo.bar': barObj }, true);
hasProperties(obj, { 'foo.bar': barObj }, false);
hasProperties(obj, ['foo.bar'], false);
instanceOf
Functional version of JavaScript's instanceof, returns a
partial function which expects a value argument and returns
var instanceOf = require('101/instance-of');
['foo', 'bar', 1].map(instanceOf('string'));
isBoolean
Functional version of val typeof 'boolean'
var isBoolean = require('101/is-boolean');
[true, false, 1].map(isBoolean);
isFunction
Functional version of val typeof 'function'
var isFunction = require('101/is-function');
[parseInt, function () {}, 'foo'].map(isFunction);
isObject
Functional strict version of val typeof 'object' (and not array or regexp)
var isObject = require('101/is-object');
[{}, { foo: 1 }, 100].map(isObject);
isString
Functional version of val typeof 'string'
var isObject = require('101/is-object');
[{}, { foo: 1 }, 100].map(isObject);
last
Returns the last value of the item.
var last = require('101/last');
last([1, 2, 3]);
last('hello');
last({
foo: 1,
bar: 2
});
not
Functional version of !
var not = require('101/not');
not(isString)('hey');
not(isString)(100);
or
Functional version of ||
var or = require('101/or');
or(true, true);
or(true, false);
or(false, false);
passAll
Muxes arguments across many functions and &&'s the results
var passAll = require('101/pass-all');
['', 'foo', 'bar', 100].map(passAll(isString, isTruthy));
passAny
Muxes arguments across many functions and ||'s the results
var passAny = require('101/pass-any');
['', 'foo', 'bar', 100].map(passAny(isString, isNumber));
pick
Returns a new object with the specified keys (with key values from obj)
var pick = require('101/pick');
var obj = {
foo: 1,
bar: 2
};
pick(obj, 'foo');
pick(obj, ['foo']);
pick(obj, ['foo', 'bar']);
[obj, obj, obj].map(pick('foo'));
pluck
Functional version of obj[key], returns the value of the key from obj.
var pluck = require('101/pluck');
var obj = {
foo: 1,
bar: 2
};
pluck(obj, 'foo');
[obj, obj, obj].map(pluck('foo'));
var obj = {
foo: {
bar: 1
},
'foo.bar': 2
};
pluck(obj, 'foo.bar');
pluck(obj, 'foo.bar', false);
set
Functional version of obj[key] = val, returns a new obj with the key and value set.
var set = require('101/set');
var obj = {
foo: 1,
bar: 2
};
set(obj, 'foo');
[obj, obj, obj].map(set('foo', 100));
var obj = {
foo: 1,
bar: 2
};
set(obj, 'foo', 100);
License
MIT