Comparing version 0.6.0 to 0.7.0
{ | ||
"name": "101", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "common javascript utils that can be required selectively that assume es5+", | ||
@@ -38,4 +38,5 @@ "main": "index.js", | ||
"deep-eql": "^0.1.3", | ||
"extend": "^1.3.0", | ||
"keypather": "^1.6.0" | ||
} | ||
} |
@@ -28,2 +28,16 @@ 101 [![Build Status](https://travis-ci.org/tjmehta/101.svg?branch=master)](https://travis-ci.org/tjmehta/101) | ||
## clone | ||
It's [clone](https://www.npmjs.org/package/clone). | ||
```js | ||
var clone = require('101/clone'); | ||
var obj = { | ||
foo: 1, | ||
bar: 2 | ||
}; | ||
clone(obj); // { foo: 1, bar: 2 } | ||
``` | ||
## envIs | ||
@@ -214,2 +228,21 @@ | ||
## omit | ||
Returns a new object without the specified keys. | ||
```js | ||
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 | ||
@@ -266,35 +299,2 @@ | ||
## clone | ||
It's [clone](https://www.npmjs.org/package/clone). | ||
```js | ||
var clone = require('101/clone'); | ||
var obj = { | ||
foo: 1, | ||
bar: 2 | ||
}; | ||
clone(obj); // { foo: 1, bar: 2 } | ||
``` | ||
## omit | ||
Returns a new object without the specified keys. | ||
```js | ||
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 }]; | ||
``` | ||
## pluck | ||
@@ -301,0 +301,0 @@ |
43
set.js
@@ -7,2 +7,5 @@ /** | ||
var keypather = require('keypather')(); | ||
var extend = require('extend'); | ||
var isString = require('./is-string'); | ||
var isObject = require('./is-object'); | ||
@@ -19,17 +22,35 @@ /** | ||
module.exports = function (obj, key, val) { | ||
if (arguments.length === 2) { | ||
val = key; | ||
key = obj; | ||
var setObj; | ||
if (arguments.length === 1) { | ||
// (setObj) | ||
setObj = obj; | ||
return function (obj) { | ||
return set(obj, key, val); | ||
return extend(obj, setObj); // extends original | ||
}; | ||
} | ||
if (arguments.length === 2) { | ||
if (isString(obj) || typeof obj === 'number') { | ||
// (key, val) | ||
val = key; | ||
key = obj; | ||
setObj = {}; | ||
setObj[key] = val; | ||
return function (obj) { | ||
return extend(obj, setObj); // extends original | ||
}; | ||
} | ||
else if (isObject(key)) { | ||
// (obj, setObj) | ||
setObj = key; | ||
return extend(obj, setObj); // extends original | ||
} | ||
else { | ||
throw new TypeError('Invalid arguments: expected str, val or val, obj'); | ||
} | ||
} | ||
else { | ||
return set(obj, key, val); | ||
setObj = {}; | ||
setObj[key] = val; | ||
return extend(obj, setObj); // extends original | ||
} | ||
}; | ||
function set (obj, key, val) { | ||
obj[key] = val; | ||
return obj; | ||
} | ||
}; |
@@ -11,12 +11,13 @@ var Lab = require('lab'); | ||
describe('apply', function () { | ||
var ctx = {}; | ||
before(function (done) { | ||
this.args = [1,2,3]; | ||
ctx.args = [1,2,3]; | ||
done(); | ||
}); | ||
after(function (done) { | ||
delete this.args; | ||
delete ctx.args; | ||
done(); | ||
}); | ||
it('should apply context and arguments to a function - working array functions', function (done) { | ||
expect([sum].map(apply(this.args))).to.eql([sum.apply(null, this.args)]); | ||
expect([sum].map(apply(null, ctx.args))).to.eql([sum.apply(null, ctx.args)]); | ||
done(); | ||
@@ -26,15 +27,15 @@ }); | ||
before(function (done) { | ||
this.ctx = { foo: 1 }; | ||
ctx.ctx = { foo: 1 }; | ||
done(); | ||
}); | ||
after(function (done) { | ||
delete this.ctx; | ||
delete ctx.ctx; | ||
done(); | ||
}); | ||
it('should apply context and arguments to a function - working array functions', function (done) { | ||
var self = this; | ||
apply(this.ctx)(checkContext); | ||
var context = {}; | ||
apply(context)(checkContext); | ||
done(); | ||
function checkContext () { | ||
return expect(this).to.equal(self.ctx); | ||
return expect(this).to.equal(context); | ||
} | ||
@@ -46,6 +47,7 @@ }); | ||
function sum (/* args */) { | ||
args.reduce(function (memo, item) { | ||
var args = Array.prototype.slice.call(arguments); | ||
return args.reduce(function (memo, item) { | ||
return memo + item; | ||
}); | ||
}, 0); | ||
} | ||
@@ -14,4 +14,5 @@ var Lab = require('lab'); | ||
describe('findIndex', function () { | ||
var ctx = {}; | ||
beforeEach(function (done) { | ||
this.arr = [ | ||
ctx.arr = [ | ||
{ | ||
@@ -33,8 +34,8 @@ bar: 1 | ||
]; | ||
this.str = 'hello'; | ||
ctx.str = 'hello'; | ||
done(); | ||
}); | ||
afterEach(function (done) { | ||
delete this.arr; | ||
delete this.str; | ||
delete ctx.arr; | ||
delete ctx.str; | ||
done(); | ||
@@ -48,3 +49,3 @@ }); | ||
it('should get the index of an item in an array/string that passes a given function', function (done) { | ||
var arr = this.arr; | ||
var arr = ctx.arr; | ||
expect(findIndex(arr, isObject)).to.equal(0); | ||
@@ -56,3 +57,3 @@ expect(findIndex(arr, isArray)).to.equal(2); | ||
it('should get the index of an item in an array/string that passes a given function when used with map', function (done) { | ||
var arr = this.arr; | ||
var arr = ctx.arr; | ||
expect([arr].map(findIndex(isObject))).to.eql([0]); | ||
@@ -77,3 +78,3 @@ expect([arr].map(findIndex(isArray))).to.eql([2]); | ||
try { | ||
findIndex(this.arr, {}); | ||
findIndex(ctx.arr, {}); | ||
} | ||
@@ -80,0 +81,0 @@ catch (err) { |
@@ -14,4 +14,5 @@ var Lab = require('lab'); | ||
describe('find', function () { | ||
var ctx = {}; | ||
beforeEach(function (done) { | ||
this.arr = [ | ||
ctx.arr = [ | ||
{ | ||
@@ -33,8 +34,8 @@ bar: 1 | ||
]; | ||
this.str = 'hello'; | ||
ctx.str = 'hello'; | ||
done(); | ||
}); | ||
afterEach(function (done) { | ||
delete this.arr; | ||
delete this.str; | ||
delete ctx.arr; | ||
delete ctx.str; | ||
done(); | ||
@@ -48,3 +49,3 @@ }); | ||
it('should get the index of an item in an array/string that passes a given function', function (done) { | ||
var arr = this.arr; | ||
var arr = ctx.arr; | ||
expect(find(arr, isObject)).to.equal(arr[0]); | ||
@@ -56,3 +57,3 @@ expect(find(arr, isArray)).to.equal(arr[2]); | ||
it('should get the index of an item in an array/string that passes a given function when used with map', function (done) { | ||
var arr = this.arr; | ||
var arr = ctx.arr; | ||
expect([arr].map(find(isObject))).to.eql([arr[0]]); | ||
@@ -77,3 +78,3 @@ expect([arr].map(find(isArray))).to.eql([arr[2]]); | ||
try { | ||
find(this.arr, {}); | ||
find(ctx.arr, {}); | ||
} | ||
@@ -80,0 +81,0 @@ catch (err) { |
@@ -14,4 +14,5 @@ var Lab = require('lab'); | ||
describe('hasKeypaths', function () { | ||
var ctx = {}; | ||
beforeEach(function (done) { | ||
this.obj = { | ||
ctx.obj = { | ||
foo: 1, | ||
@@ -24,7 +25,7 @@ bar: 2, | ||
afterEach(function (done) { | ||
delete this.obj; | ||
delete ctx.obj; | ||
done(); | ||
}); | ||
it('should return true if the object has the properties (object)', function (done) { | ||
var obj = this.obj; | ||
var obj = ctx.obj; | ||
expect(hasKeypaths(obj, { foo: 1 })).to.equal(true); | ||
@@ -37,3 +38,3 @@ expect(hasKeypaths(obj, { foo: 1, bar: 2 })).to.equal(true); | ||
it('should return false if the object doesn\'t have the properties (object)', function (done) { | ||
var obj = this.obj; | ||
var obj = ctx.obj; | ||
expect(hasKeypaths(obj, { foo: 1, bar: 2, qux: 3, nope: 4 })).to.equal(false); | ||
@@ -46,3 +47,3 @@ expect(hasKeypaths(obj, { bar: 5 })).to.equal(false); | ||
it('should return true if the object has the properties (array)', function (done) { | ||
var obj = this.obj; | ||
var obj = ctx.obj; | ||
expect(hasKeypaths(obj, ['foo'])).to.equal(true); | ||
@@ -53,3 +54,3 @@ expect(hasKeypaths(obj, ['foo', 'bar', 'qux'])).to.equal(true); | ||
it('should return false if the object doesn\'t have the properties (array)', function (done) { | ||
var obj = this.obj; | ||
var obj = ctx.obj; | ||
expect(hasKeypaths(obj, ['foo', 'bar', 'qux', 'nope'])).to.equal(false); | ||
@@ -61,3 +62,3 @@ expect(hasKeypaths(obj, ['nope'])).to.equal(false); | ||
beforeEach(function (done) { | ||
this.obj = { | ||
ctx.obj = { | ||
foo: { | ||
@@ -73,11 +74,12 @@ x: 1 | ||
}; | ||
Object.getPrototypeOf(this.obj.bar).secret = true; | ||
Object.getPrototypeOf(ctx.obj.bar).secret = true; | ||
done(); | ||
}); | ||
afterEach(function (done) { | ||
delete this.obj; | ||
delete Object.prototype.secret; | ||
delete ctx.obj; | ||
done(); | ||
}); | ||
it('should return true if the object has the properties (object)', function (done) { | ||
var obj = this.obj; | ||
var obj = ctx.obj; | ||
expect(hasKeypaths(obj, { foo: { x: 1 } })).to.equal(true); | ||
@@ -91,3 +93,3 @@ expect(hasKeypaths(obj, { 'foo.x': 1 })).to.equal(true); | ||
it('should return false if the object doesn\'t have the properties (object)', function (done) { | ||
var obj = this.obj; | ||
var obj = ctx.obj; | ||
expect(hasKeypaths(obj, { bar: { x: 5 } })).to.equal(false); | ||
@@ -100,3 +102,3 @@ expect(hasKeypaths(obj, { 'bar.x': 5 })).to.equal(false); | ||
it('should return true if the object has the properties (array)', function (done) { | ||
var obj = this.obj; | ||
var obj = ctx.obj; | ||
expect(hasKeypaths(obj, ['foo.x'])).to.equal(true); | ||
@@ -109,3 +111,3 @@ expect(hasKeypaths(obj, ['foo.x', 'bar', 'qux'])).to.equal(true); | ||
it('should return false if the object doesn\'t have the properties (array)', function (done) { | ||
var obj = this.obj; | ||
var obj = ctx.obj; | ||
expect(hasKeypaths(obj, ['foo.x', 'bar', 'qux', 'foo.nope'])).to.equal(false); | ||
@@ -119,3 +121,3 @@ expect(hasKeypaths(obj, ['foo.nope'])).to.equal(false); | ||
beforeEach(function (done) { | ||
this.arr = [ | ||
ctx.arr = [ | ||
{ | ||
@@ -144,7 +146,7 @@ bar: 1 | ||
afterEach(function (done) { | ||
delete this.arr; | ||
delete ctx.arr; | ||
done(); | ||
}); | ||
it('should return true for objects that have the properties (object)', function (done) { | ||
var arr = this.arr; | ||
var arr = ctx.arr; | ||
expect(arr.map(hasKeypaths({ bar:1 }))).to.eql([true, true, true, true]); | ||
@@ -155,3 +157,3 @@ expect(arr.map(hasKeypaths({ qux:2 }))).to.eql([false, true, false, true]); | ||
it('should return true for objects that have the properties (array)', function (done) { | ||
var arr = this.arr; | ||
var arr = ctx.arr; | ||
expect(arr.map(hasKeypaths(['bar']))).to.eql([true, true, true, true]); | ||
@@ -158,0 +160,0 @@ expect(arr.map(hasKeypaths(['bar', 'qux']))).to.eql([false, true, false, true]); |
@@ -15,4 +15,5 @@ var Lab = require('lab'); | ||
describe('hasProperties', function () { | ||
var ctx = {}; | ||
beforeEach(function (done) { | ||
this.obj = { | ||
ctx.obj = { | ||
foo: 1, | ||
@@ -25,7 +26,8 @@ bar: 2, | ||
afterEach(function (done) { | ||
delete this.obj; | ||
delete ctx.obj; | ||
delete Object.prototype.qux; | ||
done(); | ||
}); | ||
it('should return true if the object has the properties (object)', function (done) { | ||
var obj = this.obj; | ||
var obj = ctx.obj; | ||
expect(hasProperties(obj, { foo: 1 })).to.equal(true); | ||
@@ -38,3 +40,3 @@ expect(hasProperties(obj, { foo: 1, bar: 2 })).to.equal(true); | ||
it('should return false if the object doesn\'t have the properties (object)', function (done) { | ||
var obj = this.obj; | ||
var obj = ctx.obj; | ||
expect(hasProperties(obj, { foo: 1, bar: 2, qux: 3, nope: 4 })).to.equal(false); | ||
@@ -47,3 +49,3 @@ expect(hasProperties(obj, { bar: 5 })).to.equal(false); | ||
it('should return true if the object has the properties (array)', function (done) { | ||
var obj = this.obj; | ||
var obj = ctx.obj; | ||
expect(hasProperties(obj, ['foo'])).to.equal(true); | ||
@@ -54,3 +56,3 @@ expect(hasProperties(obj, ['foo', 'bar', 'qux'])).to.equal(true); | ||
it('should return false if the object doesn\'t have the properties (array)', function (done) { | ||
var obj = this.obj; | ||
var obj = ctx.obj; | ||
expect(hasProperties(obj, ['foo', 'bar', 'qux', 'nope'])).to.equal(false); | ||
@@ -62,3 +64,3 @@ expect(hasProperties(obj, ['nope'])).to.equal(false); | ||
beforeEach(function (done) { | ||
this.obj = { | ||
ctx.obj = { | ||
foo: { | ||
@@ -73,7 +75,7 @@ x: 1 | ||
afterEach(function (done) { | ||
delete this.obj; | ||
delete ctx.obj; | ||
done(); | ||
}); | ||
it('should return true if the object has the properties (object)', function (done) { | ||
var obj = this.obj; | ||
var obj = ctx.obj; | ||
expect(hasProperties(obj, { foo: { x: 1 } })).to.equal(true); | ||
@@ -84,3 +86,3 @@ expect(hasProperties(obj, { foo: { x: 1 } }, true)).to.equal(true); | ||
it('should return false if the object doesn\'t have the properties (object)', function (done) { | ||
var obj = this.obj; | ||
var obj = ctx.obj; | ||
expect(hasProperties(obj, { foo: { x: 1 } }, false)).to.equal(false); | ||
@@ -93,3 +95,3 @@ expect(hasProperties(obj, { bar: { x: 5 } })).to.equal(false); | ||
beforeEach(function (done) { | ||
this.arr = [ | ||
ctx.arr = [ | ||
{ | ||
@@ -124,7 +126,7 @@ bar: 1 | ||
afterEach(function (done) { | ||
delete this.arr; | ||
delete ctx.arr; | ||
done(); | ||
}); | ||
it('should return true for objects that have the properties (object)', function (done) { | ||
var arr = this.arr; | ||
var arr = ctx.arr; | ||
expect(arr.map(hasProperties({ bar:1 }))).to.eql([true, true, true, true, true]); | ||
@@ -139,3 +141,3 @@ expect(arr.map(hasProperties({ qux:2 }))).to.eql([false, true, false, true, false]); | ||
it('should return true for objects that have the properties (array)', function (done) { | ||
var arr = this.arr; | ||
var arr = ctx.arr; | ||
expect(arr.map(hasProperties(['bar']))).to.eql([true, true, true, true, true]); | ||
@@ -142,0 +144,0 @@ expect(arr.map(hasProperties(['bar', 'qux']))).to.eql([false, true, false, true, false]); |
@@ -13,23 +13,23 @@ var Lab = require('lab'); | ||
describe('passAny', function () { | ||
var ctx = {}; | ||
before(function (done) { | ||
this.arr = [true, 'foo', 'bar', 'qux']; | ||
this.ctx = { foo: 1 }; | ||
ctx.arr = [true, 'foo', 'bar', 'qux']; | ||
ctx.ctx = { foo: 1 }; | ||
done(); | ||
}); | ||
after(function (done) { | ||
delete this.arr; | ||
delete this.ctx; | ||
delete ctx.arr; | ||
delete ctx.ctx; | ||
done(); | ||
}); | ||
it('should work with array functions', function(done) { | ||
expect(this.arr.filter(passAny(isString, isFunction))) | ||
.eql(this.arr.filter(isStringOrFunction)); | ||
expect(ctx.arr.filter(passAny(isString, isFunction))) | ||
.eql(ctx.arr.filter(isStringOrFunction)); | ||
done(); | ||
}); | ||
it('should apply its context to the functions', function(done) { | ||
var self = this; | ||
this.arr.forEach(passAny(checkContext).bind(this.ctx)); | ||
ctx.arr.forEach(passAny(checkContext).bind(ctx.ctx)); | ||
done(); | ||
function checkContext () { | ||
return expect(this).to.equal(self.ctx); | ||
return expect(this).to.equal(ctx.ctx); | ||
} | ||
@@ -39,3 +39,3 @@ }); | ||
try { | ||
this.arr.forEach(passAny(true, false)); | ||
ctx.arr.forEach(passAny(true, false)); | ||
} | ||
@@ -42,0 +42,0 @@ catch (err) { |
@@ -9,2 +9,3 @@ var Lab = require('lab'); | ||
var pluck = require('../pluck'); | ||
var extend = require('extend'); | ||
@@ -29,3 +30,3 @@ describe('set', function () { | ||
}); | ||
it('should pluck keys from objects in an array when used with map', function(done) { | ||
it('should set a key with a val when used with array functions', function(done) { | ||
var objs = [ | ||
@@ -55,5 +56,65 @@ { | ||
expect(objs.map(set(key, val))).to.eql(expected); | ||
expect(objs.map(pluck('bar'))).to.eql([100, 100, 100]); // original obj modified | ||
expect(objs.map(pluck(key))).to.eql([100, 100, 100]); // original obj modified | ||
done(); | ||
}); | ||
it('should set a set of keys and vals when given an object', function (done) { | ||
var obj = { | ||
foo: 1, | ||
bar: 1, | ||
qux: 1 | ||
}; | ||
var key = 'bar'; | ||
var val = 100; | ||
var setObj = {}; | ||
setObj[key] = val; | ||
var expected = clone(obj); | ||
expected[key] = val; | ||
expect(set(obj, setObj)).to.eql(expected); | ||
expect(obj[key]).to.equal(100); // original obj modified | ||
done(); | ||
}); | ||
it('should set a set of keys and vals when given an object when used with array functions', function (done) { | ||
var objs = [ | ||
{ | ||
bar: 1 | ||
}, | ||
{ | ||
foo: 2, | ||
bar: 2, | ||
qux: 2 | ||
}, | ||
{ | ||
foo: 3, | ||
bar: 3, | ||
koo: 3, | ||
goo: 3 | ||
} | ||
]; | ||
var key = 'bar'; | ||
var val = 100; | ||
var setObj = {}; | ||
setObj[key] = val; | ||
var expected = objs.map(function (obj) { | ||
var copy = clone(obj); | ||
copy[key] = val; | ||
return copy; | ||
}); | ||
expect(objs.map(set(setObj))).to.eql(expected); | ||
expect(objs.map(pluck(key))).to.eql([100, 100, 100]); // original obj modified | ||
done(); | ||
}); | ||
describe('errors', function() { | ||
it('should error when given two args that artent str, val or obj, obj', function (done) { | ||
try { | ||
set(/what/, /what/); | ||
} | ||
catch (err) { | ||
expect(err).to.be.ok; | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); |
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
61115
1877
4
+ Addedextend@^1.3.0
+ Addedextend@1.3.0(transitive)