Comparing version 0.2.0 to 0.3.0
@@ -1,3 +0,14 @@ | ||
module.exports = function (v) { | ||
return v !== undefined && v !== null; | ||
/** | ||
* @module {function} 101/exists | ||
* @type {function} | ||
*/ | ||
/** | ||
* Returns false for null and undefined, true for everything else. | ||
* @function module:101/exists | ||
* @param val {*} - value to be existance checked | ||
* @return {boolean} whether the value exists or not | ||
*/ | ||
module.exports = function exists (val) { | ||
return val !== undefined && val !== null; | ||
}; |
@@ -0,4 +1,16 @@ | ||
/** | ||
* @module 101/find-index | ||
*/ | ||
var isFunction = require('./is-function'); | ||
var isArray = Array.isArray; | ||
/** | ||
* 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. | ||
* @function module:101/find-index | ||
* @param {array|string} [list] - list to be searched | ||
* @param {array|string} predicate - executed on each item in the list and returns true when the item is found | ||
* @return {number|function} Index of item which passes predicate or Partial isFunction (which accepts list) | ||
*/ | ||
module.exports = function (list, predicate) { | ||
@@ -15,3 +27,3 @@ if (list && list.length && !isFunction(list)) { | ||
else { | ||
throw new TypeError('first argument must be list (have length) or function'); | ||
throw new TypeError('first argument must be a list (have length) or function'); | ||
} | ||
@@ -31,3 +43,3 @@ }; | ||
list.some(function (val, i) { | ||
if (predicate(val)) { | ||
if (predicate(val, i, list)) { | ||
index = i; | ||
@@ -34,0 +46,0 @@ return true; |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @module 101/has-keypaths | ||
*/ | ||
var eql = require('deep-eql'); | ||
@@ -7,26 +11,36 @@ var keypather = require('keypather')(); | ||
module.exports = function (obj, props, deep) { | ||
/** | ||
* Determines whether the keypaths exist and have the specified values. | ||
* If obj is not provided findIndex will return a partial-function which accepts a obj as the first argument. | ||
* @function module:101/has-keypaths | ||
* @param {object} [obj] - the object whose properties being checked | ||
* @param {object|array} keypaths - keypaths and values (object) or keypath strings expected to exist on the object (array) | ||
* @param {boolean} [deep] - deep equals when values are specified (objects are recursed), | ||
* deep keypath existance (prototype) when only keys are specified | ||
* @return {boolean|function} Has keypaths or Partial-function function hasKeypaths (which accepts obj) | ||
*/ | ||
module.exports = function (obj, keypaths, deep) { | ||
if (arguments.length === 1) { | ||
props = obj; | ||
keypaths = obj; | ||
return function (obj) { | ||
return hasKeypaths(obj, props, deep); | ||
return hasKeypaths(obj, keypaths, deep); | ||
}; | ||
} | ||
else { | ||
return hasKeypaths(obj, props, deep); | ||
return hasKeypaths(obj, keypaths, deep); | ||
} | ||
}; | ||
function hasKeypaths (obj, props, deep) { | ||
function hasKeypaths (obj, keypaths, deep) { | ||
var has = false; | ||
deep = !isBoolean(deep) ? true : deep; | ||
if (isObject(props)) { | ||
has = Object.keys(props).every(function (keypath) { | ||
if (isObject(keypaths)) { | ||
has = Object.keys(keypaths).every(function (keypath) { | ||
return deep ? | ||
eql(keypather.get(obj, keypath), props[keypath]) : | ||
keypather.get(obj, keypath) === props[keypath]; | ||
eql(keypather.get(obj, keypath), keypaths[keypath]) : | ||
keypather.get(obj, keypath) === keypaths[keypath]; | ||
}); | ||
} | ||
else if (isArray(props)) { | ||
has = props.every(function (keypath) { | ||
else if (isArray(keypaths)) { | ||
has = keypaths.every(function (keypath) { | ||
return deep ? | ||
@@ -33,0 +47,0 @@ keypather.in(obj, keypath) : |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @module 101/has-properties | ||
*/ | ||
var eql = require('deep-eql'); | ||
@@ -6,2 +10,12 @@ var isObject = require('./is-object'); | ||
/** | ||
* Determines whether the keys exist and, if specified, has the values. | ||
* If obj is not provided findIndex will return a partial function which accepts a obj as the first argument. | ||
* @function module:101/has-properties | ||
* @param {object} [obj] - the object whose properties being checked | ||
* @param {object|array} properties - keys and values (object) or keys expected to exist on the object (array) | ||
* @param {boolean} [deep] - deep equals when values are specified (objects are recursed), | ||
* deep key existance (prototype) when only keys are specified | ||
* @return {boolean|function} Has keypaths or Partial-function hasProperties (which accepts obj) | ||
*/ | ||
module.exports = function (obj, props, deep) { // deep defaults to true | ||
@@ -8,0 +22,0 @@ if (isBoolean(props)) { |
@@ -0,6 +1,19 @@ | ||
/** | ||
* @module 101/instance-of | ||
*/ | ||
/** | ||
* Functional version of JavaScript's instanceof, returns a | ||
* partial function which expects a value argument and returns | ||
* true if the value is an instance of the Class (false if not). | ||
* @function module:101/instance-of | ||
* @param {function} Class - Class of which the value should be | ||
* @return {function} Partial-function instanceOfClass (which accepts any value and returns if the value is an instance of the specified class) | ||
*/ | ||
// (function)(val) | ||
module.exports = function (Class) { | ||
return function (v) { | ||
return v instanceof Class; | ||
return function (val) { | ||
return val instanceof Class; | ||
}; | ||
}; |
@@ -1,3 +0,13 @@ | ||
module.exports = function (v) { | ||
return typeof v === 'boolean'; | ||
/** | ||
* @module 101/is-boolean | ||
*/ | ||
/** | ||
* Functional version of val typeof 'boolean' | ||
* @function module:101/is-boolean | ||
* @param {*} val - value checked to be a boolean | ||
* @return {boolean} Whether the value is a boolean or not | ||
*/ | ||
module.exports = function (val) { | ||
return typeof val === 'boolean'; | ||
}; |
@@ -0,3 +1,13 @@ | ||
/** | ||
* @module 101/is-function | ||
*/ | ||
/** | ||
* Functional version of val typeof 'function' | ||
* @function module:101/is-function | ||
* @param {*} val - value checked to be a function | ||
* @return {boolean} Whether the value is a function or not | ||
*/ | ||
module.exports = function (v) { | ||
return typeof v === 'function'; | ||
}; |
@@ -0,5 +1,15 @@ | ||
/** | ||
* Functional version of a strict object check (Arrays and RegExps are not objects) | ||
* @module 101/is-object | ||
*/ | ||
/** | ||
* @function module:101/is-object | ||
* @param {*} val - value checked to be an object | ||
* @return {boolean} Whether the value is an object or not | ||
*/ | ||
var exists = require('./exists'); | ||
module.exports = function (o) { | ||
return typeof o === 'object' && exists(o) && !Array.isArray(o) && !(o instanceof RegExp); | ||
module.exports = function (val) { | ||
return typeof val === 'object' && exists(val) && !Array.isArray(val) && !(val instanceof RegExp); | ||
}; |
@@ -1,3 +0,14 @@ | ||
module.exports = function (v) { | ||
return typeof v === 'string'; | ||
/** | ||
* @module 101/is-string | ||
* | ||
*/ | ||
/** | ||
* Functional version of val typeof 'string' | ||
* @function module:101/is-string | ||
* @param {*} val - value checked to be a string | ||
* @return {boolean} Whether the value is an string or not | ||
*/ | ||
module.exports = function (val) { | ||
return typeof val === 'string'; | ||
}; |
10
last.js
@@ -0,1 +1,5 @@ | ||
/** | ||
* @module 101/last | ||
*/ | ||
var isObject = require('./is-object'); | ||
@@ -5,2 +9,8 @@ var exists = require('./exists'); | ||
/** | ||
* Returns the last value of the item. | ||
* @function module:101/last | ||
* @param {array|string|object} item - item whose last value is returned | ||
* @return {*} Last value of an array. Last char of a string. Last value of an object. Last char of item.toString() for everything else. | ||
*/ | ||
module.exports = last; | ||
@@ -7,0 +17,0 @@ |
{ | ||
"name": "101", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "common javascript utils that can be required selectively that assume es5+", | ||
@@ -37,4 +37,5 @@ "main": "index.js", | ||
"deep-eql": "^0.1.3", | ||
"keypather": "^1.5.1" | ||
"keypather": "^1.6.0", | ||
"lab": "~3.1.1" | ||
} | ||
} |
18
pick.js
@@ -0,9 +1,15 @@ | ||
/** | ||
* @module 101/pick | ||
*/ | ||
var isObject = require('./is-object'); | ||
// (obj, keys) | ||
// (obj, keys..) | ||
// (obj, keys.., keys.., keys.., ...) | ||
// (keys)(obj) | ||
// (keys..)(obj) | ||
// (keys.., keys.., keys.., ...)(obj) | ||
/** | ||
* Returns a new object with the specified keys (with key values from obj). | ||
* When only keys are specified pick returns a partial-function which accepts obj. | ||
* @function module:101/pick | ||
* @param {object} [obj] - object whose keys are picked | ||
* @param {string|array} keys... - keys which will be taken from obj (can be specifieds as args (strings and/or arrays) | ||
* @return {object|function} Object with only the keys specified from the original obj or Partial-function pick (which accepts obj) and returns an object | ||
*/ | ||
module.exports = function () { | ||
@@ -10,0 +16,0 @@ var args = Array.prototype.slice.call(arguments); |
14
pluck.js
@@ -0,5 +1,15 @@ | ||
/** | ||
* @module 101/pluck | ||
*/ | ||
var isObject = require('./is-object'); | ||
// (obj, key) | ||
// (key)(obj) | ||
/** | ||
* Functional version of obj[key], returns the value of the key from obj. | ||
* When only a key is specified pluck returns a partial-function which accepts obj. | ||
* @function module:101/pluck | ||
* @param {object} [obj] - object from which the value is plucked | ||
* @param {string|array} key - key of the value from obj which is returned | ||
* @return {*|function} The value of the key from obj or Partial-function pluck (which accepts obj) and returns the value of the key from obj | ||
*/ | ||
module.exports = function (obj, key) { | ||
@@ -6,0 +16,0 @@ if (!isObject(obj)) { |
256
README.md
101 | ||
=== | ||
common utils / helpers that can be required selectively | ||
common utils that can be required selectively | ||
## and | ||
Functional version of && | ||
```js | ||
var and = require('101/and'); | ||
and(true, false); // false | ||
and(true, true); // true | ||
``` | ||
## apply | ||
Functional version of function.apply | ||
```js | ||
var apply = require('101/apply'); | ||
and(true, false); // false | ||
and(true, true); // true | ||
``` | ||
## exists | ||
Simple exists function | ||
```js | ||
var exists = require('101/exists'); | ||
exists('foo'); // true | ||
exists(null); // false | ||
exists(undefined); // false | ||
``` | ||
## 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. | ||
```js | ||
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. | ||
```js | ||
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) | ||
``` | ||
## hasProperties | ||
Determines whether the keys exist and, if specified, has the values. | ||
```js | ||
var hasProperties = require('101/has-properties'); | ||
var obj = { | ||
foo: { | ||
bar: 1 | ||
}, | ||
qux: 1 | ||
}; | ||
hasProperties(obj, ['foo', 'qux']); // true | ||
hasProperties(obj, { qux: 1 }) // true | ||
// optional 'deep' arg, defaults to true | ||
var barObj = { bar: 1 }; | ||
hasProperties(obj, { 'foo.bar': barObj }); // true | ||
hasProperties(obj, { 'foo.bar': barObj }, true); // true | ||
hasProperties(obj, { 'foo.bar': barObj }, false); // false | ||
hasProperties(obj, ['foo.bar'], false); // true, uses [hasOwnProperty vs in](http://stackoverflow.com/questions/13632999/if-key-in-object-or-ifobject-hasownpropertykey) | ||
``` | ||
## instanceOf | ||
Functional version of JavaScript's instanceof, returns a | ||
partial function which expects a value argument and returns | ||
```js | ||
var instanceOf = require('101/instance-of'); | ||
['foo', 'bar', 1].map(instanceOf('string')); // [true, true, false] | ||
``` | ||
## isBoolean | ||
Functional version of val typeof 'boolean' | ||
```js | ||
var isBoolean = require('101/is-boolean'); | ||
[true, false, 1].map(isBoolean); // [true, true, false] | ||
``` | ||
## isFunction | ||
Functional version of val typeof 'function' | ||
```js | ||
var isFunction = require('101/is-function'); | ||
[parseInt, function () {}, 'foo'].map(isFunction); // [true, true, false] | ||
``` | ||
## isObject | ||
Functional *strict* version of val typeof 'object' (and not array or regexp) | ||
```js | ||
var isObject = require('101/is-object'); | ||
[{}, { foo: 1 }, 100].map(isObject); // [true, true, false] | ||
``` | ||
## isString | ||
Functional version of val typeof 'string' | ||
```js | ||
var isObject = require('101/is-object'); | ||
[{}, { foo: 1 }, 100].map(isObject); // [true, true, false] | ||
``` | ||
## last | ||
Returns the last value of the item. | ||
```js | ||
var last = require('101/last'); | ||
last([1, 2, 3]); // 3 | ||
last('hello'); // 'o' | ||
last({ | ||
foo: 1, | ||
bar: 2 | ||
}); // 2 | ||
``` | ||
## not | ||
Functional version of ! | ||
```js | ||
var not = require('101/not'); | ||
not(isString)('hey'); // false | ||
not(isString)(100); // true | ||
``` | ||
## or | ||
Functional version of || | ||
```js | ||
var or = require('101/or'); | ||
or(true, true); // true | ||
or(true, false); // true | ||
or(false, false); // false | ||
``` | ||
## passAll | ||
Muxes arguments across many functions and &&'s the results | ||
```js | ||
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 | ||
```js | ||
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) | ||
```js | ||
var pick = require('101/pick'); | ||
var obj = { | ||
foo: 1, | ||
bar: 2 | ||
}; | ||
pick(obj, 'foo'); // { 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. | ||
```js | ||
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] | ||
``` | ||
## License | ||
MIT |
@@ -1,20 +0,24 @@ | ||
// var Lab = require('lab'); | ||
// var describe = Lab.experiment; | ||
// var it = Lab.test; | ||
// var expect = Lab.expect; | ||
var Lab = require('lab'); | ||
var describe = Lab.experiment; | ||
var it = Lab.test; | ||
var expect = Lab.expect; | ||
// var last = require('../last'); | ||
var exists = require('../exists'); | ||
// describe('last', function () { | ||
// it('should return the last a val from an array, string, or object', function(done) { | ||
// expect(last([1, 2, 3])).to.equal(3); | ||
// expect(last('123')).to.equal('3'); | ||
// expect(last({ | ||
// foo: 1, | ||
// bar: 2, | ||
// qux: 3 | ||
// })).to.equal(3); | ||
// expect(last(function () {})).to.equal('}'); | ||
// done(); | ||
// }); | ||
// }); | ||
describe('exists', function () { | ||
it('should return true for existant vars', function(done) { | ||
expect(exists(['foo'])).to.be.true; | ||
expect(exists('foo')).to.be.true; | ||
expect(exists(101)).to.be.true; | ||
expect(exists({})).to.be.true; | ||
expect(exists(/re/)).to.be.true; | ||
expect(exists(true)).to.be.true; | ||
expect(exists(function () {})).to.be.true; | ||
done(); | ||
}); | ||
it('should return false for non-existant vars', function(done) { | ||
expect(exists(null)).to.be.false; | ||
expect(exists(undefined)).to.be.false; | ||
done(); | ||
}); | ||
}); |
@@ -59,3 +59,3 @@ var Lab = require('lab'); | ||
catch (err) { | ||
expect(err.message).to.equal('first argument must be list (have length) or function'); | ||
expect(err.message).to.equal('first argument must be a list (have length) or function'); | ||
} | ||
@@ -66,3 +66,3 @@ try { | ||
catch (err) { | ||
expect(err.message).to.equal('first argument must be list (have length) or function'); | ||
expect(err.message).to.equal('first argument must be a list (have length) or function'); | ||
} | ||
@@ -69,0 +69,0 @@ try { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
43934
40
1281
259
0
3
+ Addedlab@~3.1.1
+ Addedamdefine@1.0.1(transitive)
+ Addedassertion-error@1.0.0(transitive)
+ Addedasync@0.2.100.8.0(transitive)
+ Addedchai@1.10.0(transitive)
+ Addeddiff@1.4.0(transitive)
+ Addedesprima@1.2.5(transitive)
+ Addedhandlebars@1.3.0(transitive)
+ Addedlab@3.1.4(transitive)
+ Addedminimist@0.0.10(transitive)
+ Addedoptimist@0.3.70.6.1(transitive)
+ Addedsource-map@0.1.43(transitive)
+ Addeduglify-js@2.3.6(transitive)
+ Addedwordwrap@0.0.3(transitive)
Updatedkeypather@^1.6.0