object-path
Advanced tools
Comparing version 0.11.4 to 0.11.5
15
index.js
@@ -86,4 +86,11 @@ (function (root, factory){ | ||
function hasShallowProperty(obj, prop) { | ||
return (options.includeInheritedProps || (typeof prop === 'number' && Array.isArray(obj)) || hasOwnProperty(obj, prop)) | ||
var hasShallowProperty | ||
if (options.includeInheritedProps) { | ||
hasShallowProperty = function () { | ||
return true | ||
} | ||
} else { | ||
hasShallowProperty = function (obj, prop) { | ||
return (typeof prop === 'number' && Array.isArray(obj)) || hasOwnProperty(obj, prop) | ||
} | ||
} | ||
@@ -109,2 +116,6 @@ | ||
var currentValue = getShallowProperty(obj, currentPath); | ||
if (options.includeInheritedProps && (currentPath === '__proto__' || | ||
(currentPath === 'constructor' && typeof currentValue === 'function'))) { | ||
throw new Error('For security reasons, object\'s magic properties cannot be set') | ||
} | ||
if (path.length === 1) { | ||
@@ -111,0 +122,0 @@ if (currentValue === void 0 || !doNotReplace) { |
{ | ||
"name": "object-path", | ||
"description": "Access deep object properties using a path", | ||
"version": "0.11.4", | ||
"version": "0.11.5", | ||
"author": { | ||
@@ -14,14 +14,17 @@ "name": "Mario Casciaro" | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">= 10.12.0" | ||
}, | ||
"devDependencies": { | ||
"@mariocasciaro/benchpress": "^0.1.3", | ||
"chai": "^3.5.0", | ||
"coveralls": "^2.11.2", | ||
"istanbul": "^0.4.4", | ||
"mocha": "^2.2.4", | ||
"mocha-lcov-reporter": "^1.2.0" | ||
"chai": "^4.2.0", | ||
"coveralls": "^3.1.0", | ||
"nyc": "^15.1.0", | ||
"mocha": "^8.1.3", | ||
"mocha-lcov-reporter": "^1.3.0" | ||
}, | ||
"scripts": { | ||
"test": "istanbul cover ./node_modules/mocha/bin/_mocha test.js --report html -- -R spec" | ||
"test": "mocha test.js", | ||
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls", | ||
"coverage": "nyc npm test", | ||
"benchmark": "node benchmark.js" | ||
}, | ||
@@ -28,0 +31,0 @@ "keywords": [ |
@@ -14,5 +14,8 @@ | ||
## Changelog | ||
### 0.11.5 | ||
* **SECURITY FIX**. Fix a prototype pollution vulnerability in the `set()` function when using the "inherited props" mode (e.g. when a new `object-path` instance is created with the `includeInheritedProps` option set to `true` or when using the `withInheritedProps` default instance. The vulnerability does not exist in the default instance exposed by object path (e.g `objectPath.set()`). | ||
### 0.11.0 | ||
@@ -19,0 +22,0 @@ |
1302
test.js
@@ -1,7 +0,7 @@ | ||
'use strict'; | ||
'use strict' | ||
var expect = require('chai').expect, | ||
objectPath = require('./index.js'); | ||
objectPath = require('./index.js') | ||
function getTestObj() { | ||
function getTestObj () { | ||
return { | ||
@@ -12,10 +12,10 @@ a: 'b', | ||
d: ['a', 'b'], | ||
e: [{},{f: 'g'}], | ||
e: [{}, {f: 'g'}], | ||
f: 'i' | ||
} | ||
}; | ||
} | ||
} | ||
describe('get', function() { | ||
it('should return the value using unicode key', function() { | ||
describe('get', function () { | ||
it('should return the value using unicode key', function () { | ||
var obj = { | ||
@@ -25,8 +25,8 @@ '15\u00f8C': { | ||
} | ||
}; | ||
expect(objectPath.get(obj, '15\u00f8C.3\u0111')).to.be.equal(1); | ||
expect(objectPath.get(obj, ['15\u00f8C','3\u0111'])).to.be.equal(1); | ||
}); | ||
} | ||
expect(objectPath.get(obj, '15\u00f8C.3\u0111')).to.be.equal(1) | ||
expect(objectPath.get(obj, ['15\u00f8C', '3\u0111'])).to.be.equal(1) | ||
}) | ||
it('should return the value using dot in key', function() { | ||
it('should return the value using dot in key', function () { | ||
var obj = { | ||
@@ -36,85 +36,86 @@ 'a.b': { | ||
} | ||
}; | ||
expect(objectPath.get(obj, 'a.b.looks.like')).to.be.equal(void 0); | ||
expect(objectPath.get(obj, ['a.b','looks.like'])).to.be.equal(1); | ||
}); | ||
} | ||
expect(objectPath.get(obj, 'a.b.looks.like')).to.be.equal(void 0) | ||
expect(objectPath.get(obj, ['a.b', 'looks.like'])).to.be.equal(1) | ||
}) | ||
it('should return the value under shallow object', function() { | ||
var obj = getTestObj(); | ||
expect(objectPath.get(obj, 'a')).to.be.equal('b'); | ||
expect(objectPath.get(obj, ['a'])).to.be.equal('b'); | ||
}); | ||
it('should return the value under shallow object', function () { | ||
var obj = getTestObj() | ||
expect(objectPath.get(obj, 'a')).to.be.equal('b') | ||
expect(objectPath.get(obj, ['a'])).to.be.equal('b') | ||
}) | ||
it('should work with number path', function() { | ||
var obj = getTestObj(); | ||
expect(objectPath.get(obj.b.d, 0)).to.be.equal('a'); | ||
expect(objectPath.get(obj.b, 0)).to.be.equal(void 0); | ||
}); | ||
it('should work with number path', function () { | ||
var obj = getTestObj() | ||
expect(objectPath.get(obj.b.d, 0)).to.be.equal('a') | ||
expect(objectPath.get(obj.b, 0)).to.be.equal(void 0) | ||
}) | ||
it('should return the value under deep object', function() { | ||
var obj = getTestObj(); | ||
expect(objectPath.get(obj, 'b.f')).to.be.equal('i'); | ||
expect(objectPath.get(obj, ['b','f'])).to.be.equal('i'); | ||
}); | ||
it('should return the value under deep object', function () { | ||
var obj = getTestObj() | ||
expect(objectPath.get(obj, 'b.f')).to.be.equal('i') | ||
expect(objectPath.get(obj, ['b', 'f'])).to.be.equal('i') | ||
}) | ||
it('should return the value under array', function() { | ||
var obj = getTestObj(); | ||
expect(objectPath.get(obj, 'b.d.0')).to.be.equal('a'); | ||
expect(objectPath.get(obj, ['b','d',0])).to.be.equal('a'); | ||
}); | ||
it('should return the value under array', function () { | ||
var obj = getTestObj() | ||
expect(objectPath.get(obj, 'b.d.0')).to.be.equal('a') | ||
expect(objectPath.get(obj, ['b', 'd', 0])).to.be.equal('a') | ||
}) | ||
it('should return the value under array deep', function() { | ||
var obj = getTestObj(); | ||
expect(objectPath.get(obj, 'b.e.1.f')).to.be.equal('g'); | ||
expect(objectPath.get(obj, ['b','e',1,'f'])).to.be.equal('g'); | ||
}); | ||
it('should return the value under array deep', function () { | ||
var obj = getTestObj() | ||
expect(objectPath.get(obj, 'b.e.1.f')).to.be.equal('g') | ||
expect(objectPath.get(obj, ['b', 'e', 1, 'f'])).to.be.equal('g') | ||
}) | ||
it('should return undefined for missing values under object', function() { | ||
var obj = getTestObj(); | ||
expect(objectPath.get(obj, 'a.b')).to.not.exist; | ||
expect(objectPath.get(obj, ['a','b'])).to.not.exist; | ||
}); | ||
it('should return undefined for missing values under object', function () { | ||
var obj = getTestObj() | ||
expect(objectPath.get(obj, 'a.b')).to.not.exist | ||
expect(objectPath.get(obj, ['a', 'b'])).to.not.exist | ||
}) | ||
it('should return undefined for missing values under array', function() { | ||
var obj = getTestObj(); | ||
expect(objectPath.get(obj, 'b.d.5')).to.not.exist; | ||
expect(objectPath.get(obj, ['b','d','5'])).to.not.exist; | ||
}); | ||
it('should return undefined for missing values under array', function () { | ||
var obj = getTestObj() | ||
expect(objectPath.get(obj, 'b.d.5')).to.not.exist | ||
expect(objectPath.get(obj, ['b', 'd', '5'])).to.not.exist | ||
}) | ||
it('should return the value under integer-like key', function() { | ||
var obj = { '1a': 'foo' }; | ||
expect(objectPath.get(obj, '1a')).to.be.equal('foo'); | ||
expect(objectPath.get(obj, ['1a'])).to.be.equal('foo'); | ||
}); | ||
it('should return the value under integer-like key', function () { | ||
var obj = {'1a': 'foo'} | ||
expect(objectPath.get(obj, '1a')).to.be.equal('foo') | ||
expect(objectPath.get(obj, ['1a'])).to.be.equal('foo') | ||
}) | ||
it('should return the default value when the key doesnt exist', function() { | ||
var obj = { '1a': 'foo' }; | ||
expect(objectPath.get(obj, '1b', null)).to.be.equal(null); | ||
expect(objectPath.get(obj, ['1b'], null)).to.be.equal(null); | ||
}); | ||
it('should return the default value when the key doesnt exist', function () { | ||
var obj = {'1a': 'foo'} | ||
expect(objectPath.get(obj, '1b', null)).to.be.equal(null) | ||
expect(objectPath.get(obj, ['1b'], null)).to.be.equal(null) | ||
}) | ||
it('should return the default value when path is empty', function() { | ||
var obj = { '1a': 'foo' }; | ||
expect(objectPath.get(obj, '', null)).to.be.deep.equal({ '1a': 'foo' }); | ||
expect(objectPath.get(obj, [])).to.be.deep.equal({ '1a': 'foo' }); | ||
expect(objectPath.get({ }, ['1'])).to.be.equal(undefined); | ||
}); | ||
it('should return the default value when path is empty', function () { | ||
var obj = {'1a': 'foo'} | ||
expect(objectPath.get(obj, '', null)).to.be.deep.equal({'1a': 'foo'}) | ||
expect(objectPath.get(obj, [])).to.be.deep.equal({'1a': 'foo'}) | ||
expect(objectPath.get({}, ['1'])).to.be.equal(undefined) | ||
}) | ||
it('should return the default value when object is null or undefined', function() { | ||
expect(objectPath.get(null, 'test', 'a')).to.be.deep.equal('a'); | ||
expect(objectPath.get(undefined, 'test', 'a')).to.be.deep.equal('a'); | ||
}); | ||
it('should return the default value when object is null or undefined', function () { | ||
expect(objectPath.get(null, 'test', 'a')).to.be.deep.equal('a') | ||
expect(objectPath.get(undefined, 'test', 'a')).to.be.deep.equal('a') | ||
}) | ||
it( | ||
'should not fail on an object with a null prototype', | ||
function assertSuccessForObjWithNullProto(){ | ||
var foo = 'FOO'; | ||
var objWithNullProto = Object.create(null); | ||
objWithNullProto.foo = foo; | ||
expect(objectPath.get(objWithNullProto, 'foo')).to.equal(foo); | ||
function assertSuccessForObjWithNullProto () { | ||
var foo = 'FOO' | ||
var objWithNullProto = Object.create(null) | ||
objWithNullProto.foo = foo | ||
expect(objectPath.get(objWithNullProto, 'foo')).to.equal(foo) | ||
} | ||
); | ||
) | ||
it('should skip non own properties', function() { | ||
var Base = function(enabled){ }; | ||
it('should skip non own properties', function () { | ||
var Base = function (enabled) { | ||
} | ||
Base.prototype = { | ||
@@ -124,21 +125,21 @@ one: { | ||
} | ||
}; | ||
var Extended = function(){ | ||
Base.call(this, true); | ||
}; | ||
Extended.prototype = Object.create(Base.prototype); | ||
} | ||
var Extended = function () { | ||
Base.call(this, true) | ||
} | ||
Extended.prototype = Object.create(Base.prototype) | ||
var extended = new Extended(); | ||
var extended = new Extended() | ||
expect(objectPath.get(extended, ['one','two'])).to.be.equal(undefined); | ||
extended.enabled = true; | ||
expect(objectPath.get(extended, ['one', 'two'])).to.be.equal(undefined) | ||
extended.enabled = true | ||
expect(objectPath.get(extended, 'enabled')).to.be.equal(true); | ||
expect(objectPath.get(extended, 'one')).to.be.equal(undefined); | ||
}); | ||
}); | ||
expect(objectPath.get(extended, 'enabled')).to.be.equal(true) | ||
expect(objectPath.get(extended, 'one')).to.be.equal(undefined) | ||
}) | ||
}) | ||
describe('set', function() { | ||
it('should set the value using unicode key', function() { | ||
describe('set', function () { | ||
it('should set the value using unicode key', function () { | ||
var obj = { | ||
@@ -148,10 +149,10 @@ '15\u00f8C': { | ||
} | ||
}; | ||
objectPath.set(obj, '15\u00f8C.3\u0111', 2); | ||
expect(objectPath.get(obj, '15\u00f8C.3\u0111')).to.be.equal(2); | ||
objectPath.set(obj, '15\u00f8C.3\u0111', 3); | ||
expect(objectPath.get(obj, ['15\u00f8C','3\u0111'])).to.be.equal(3); | ||
}); | ||
} | ||
objectPath.set(obj, '15\u00f8C.3\u0111', 2) | ||
expect(objectPath.get(obj, '15\u00f8C.3\u0111')).to.be.equal(2) | ||
objectPath.set(obj, '15\u00f8C.3\u0111', 3) | ||
expect(objectPath.get(obj, ['15\u00f8C', '3\u0111'])).to.be.equal(3) | ||
}) | ||
it('should set the value using dot in key', function() { | ||
it('should set the value using dot in key', function () { | ||
var obj = { | ||
@@ -161,175 +162,211 @@ 'a.b': { | ||
} | ||
}; | ||
objectPath.set(obj, ['a.b','looks.like'], 2); | ||
expect(objectPath.get(obj, ['a.b','looks.like'])).to.be.equal(2); | ||
}); | ||
} | ||
objectPath.set(obj, ['a.b', 'looks.like'], 2) | ||
expect(objectPath.get(obj, ['a.b', 'looks.like'])).to.be.equal(2) | ||
}) | ||
it('should set value under shallow object', function() { | ||
var obj = getTestObj(); | ||
objectPath.set(obj, 'c', {m: 'o'}); | ||
expect(obj).to.have.deep.property('c.m', 'o'); | ||
obj = getTestObj(); | ||
objectPath.set(obj, ['c'], {m: 'o'}); | ||
expect(obj).to.have.deep.property('c.m', 'o'); | ||
}); | ||
it('should set value under shallow object', function () { | ||
var obj = getTestObj() | ||
objectPath.set(obj, 'c', {m: 'o'}) | ||
expect(obj).to.include.nested.property('c.m', 'o') | ||
obj = getTestObj() | ||
objectPath.set(obj, ['c'], {m: 'o'}) | ||
expect(obj).to.include.nested.property('c.m', 'o') | ||
}) | ||
it('should set value using number path', function() { | ||
var obj = getTestObj(); | ||
objectPath.set(obj.b.d, 0, 'o'); | ||
expect(obj).to.have.deep.property('b.d.0', 'o'); | ||
}); | ||
it('should set value using number path', function () { | ||
var obj = getTestObj() | ||
objectPath.set(obj.b.d, 0, 'o') | ||
expect(obj).to.have.nested.property('b.d.0', 'o') | ||
}) | ||
it('should set value under deep object', function() { | ||
var obj = getTestObj(); | ||
objectPath.set(obj, 'b.c', 'o'); | ||
expect(obj).to.have.deep.property('b.c', 'o'); | ||
obj = getTestObj(); | ||
objectPath.set(obj, ['b','c'], 'o'); | ||
expect(obj).to.have.deep.property('b.c', 'o'); | ||
}); | ||
it('should set value under deep object', function () { | ||
var obj = getTestObj() | ||
objectPath.set(obj, 'b.c', 'o') | ||
expect(obj).to.have.nested.property('b.c', 'o') | ||
obj = getTestObj() | ||
objectPath.set(obj, ['b', 'c'], 'o') | ||
expect(obj).to.have.nested.property('b.c', 'o') | ||
}) | ||
it('should set value under array', function() { | ||
var obj = getTestObj(); | ||
objectPath.set(obj, 'b.e.1.g', 'f'); | ||
expect(obj).to.have.deep.property('b.e.1.g', 'f'); | ||
obj = getTestObj(); | ||
objectPath.set(obj, ['b','e',1,'g'], 'f'); | ||
expect(obj).to.have.deep.property('b.e.1.g', 'f'); | ||
it('should set value under array', function () { | ||
var obj = getTestObj() | ||
objectPath.set(obj, 'b.e.1.g', 'f') | ||
expect(obj).to.have.nested.property('b.e.1.g', 'f') | ||
obj = getTestObj() | ||
objectPath.set(obj, ['b', 'e', 1, 'g'], 'f') | ||
expect(obj).to.have.nested.property('b.e.1.g', 'f') | ||
obj = {} | ||
objectPath.set(obj, 'b.0', 'a'); | ||
objectPath.set(obj, 'b.1', 'b'); | ||
expect(obj.b).to.be.deep.equal(['a', 'b']); | ||
}); | ||
objectPath.set(obj, 'b.0', 'a') | ||
objectPath.set(obj, 'b.1', 'b') | ||
expect(obj.b).to.be.deep.equal(['a', 'b']) | ||
}) | ||
it('should create intermediate objects', function() { | ||
var obj = getTestObj(); | ||
objectPath.set(obj, 'c.d.e.f', 'l'); | ||
expect(obj).to.have.deep.property('c.d.e.f', 'l'); | ||
obj = getTestObj(); | ||
objectPath.set(obj, ['c','d','e','f'], 'l'); | ||
expect(obj).to.have.deep.property('c.d.e.f', 'l'); | ||
}); | ||
it('should create intermediate objects', function () { | ||
var obj = getTestObj() | ||
objectPath.set(obj, 'c.d.e.f', 'l') | ||
expect(obj).to.have.nested.property('c.d.e.f', 'l') | ||
obj = getTestObj() | ||
objectPath.set(obj, ['c', 'd', 'e', 'f'], 'l') | ||
expect(obj).to.have.nested.property('c.d.e.f', 'l') | ||
}) | ||
it('should create intermediate arrays', function() { | ||
var obj = getTestObj(); | ||
objectPath.set(obj, 'c.0.1.m', 'l'); | ||
expect(obj.c).to.be.an('array'); | ||
expect(obj.c[0]).to.be.an('array'); | ||
expect(obj).to.have.deep.property('c.0.1.m', 'l'); | ||
obj = getTestObj(); | ||
objectPath.set(obj, ['c','0', 1,'m'], 'l'); | ||
expect(obj.c).to.be.an('object'); | ||
expect(obj.c[0]).to.be.an('array'); | ||
expect(obj).to.have.deep.property('c.0.1.m', 'l'); | ||
}); | ||
it('should create intermediate arrays', function () { | ||
var obj = getTestObj() | ||
objectPath.set(obj, 'c.0.1.m', 'l') | ||
expect(obj.c).to.be.an('array') | ||
expect(obj.c[0]).to.be.an('array') | ||
expect(obj).to.have.nested.property('c.0.1.m', 'l') | ||
obj = getTestObj() | ||
objectPath.set(obj, ['c', '0', 1, 'm'], 'l') | ||
expect(obj.c).to.be.an('object') | ||
expect(obj.c[0]).to.be.an('array') | ||
expect(obj).to.have.nested.property('c.0.1.m', 'l') | ||
}) | ||
it('should set value under integer-like key', function() { | ||
var obj = getTestObj(); | ||
objectPath.set(obj, '1a', 'foo'); | ||
expect(obj).to.have.deep.property('1a', 'foo'); | ||
obj = getTestObj(); | ||
objectPath.set(obj, ['1a'], 'foo'); | ||
expect(obj).to.have.deep.property('1a', 'foo'); | ||
}); | ||
it('should set value under integer-like key', function () { | ||
var obj = getTestObj() | ||
objectPath.set(obj, '1a', 'foo') | ||
expect(obj).to.have.nested.property('1a', 'foo') | ||
obj = getTestObj() | ||
objectPath.set(obj, ['1a'], 'foo') | ||
expect(obj).to.have.nested.property('1a', 'foo') | ||
}) | ||
it('should set value under empty array', function() { | ||
var obj = []; | ||
objectPath.set(obj, [0], 'foo'); | ||
expect(obj[0]).to.be.equal('foo'); | ||
obj = []; | ||
objectPath.set(obj, '0', 'foo'); | ||
expect(obj[0]).to.be.equal('foo'); | ||
}); | ||
}); | ||
it('should set value under empty array', function () { | ||
var obj = [] | ||
objectPath.set(obj, [0], 'foo') | ||
expect(obj[0]).to.be.equal('foo') | ||
obj = [] | ||
objectPath.set(obj, '0', 'foo') | ||
expect(obj[0]).to.be.equal('foo') | ||
}) | ||
it('[security] should not set magic properties in default mode', function () { | ||
objectPath.set({}, '__proto__.injected', 'this is bad') | ||
expect(Object.prototype.injected).to.be.undefined | ||
describe('push', function() { | ||
it('should push value to existing array using unicode key', function() { | ||
var obj = getTestObj(); | ||
objectPath.push(obj, 'b.\u1290c', 'l'); | ||
expect(obj).to.have.deep.property('b.\u1290c.0', 'l'); | ||
objectPath.push(obj, ['b','\u1290c'], 'l'); | ||
expect(obj).to.have.deep.property('b.\u1290c.1', 'l'); | ||
}); | ||
function Clazz() {} | ||
Clazz.prototype.test = 'original' | ||
it('should push value to existing array using dot key', function() { | ||
var obj = getTestObj(); | ||
objectPath.push(obj, ['b','z.d'], 'l'); | ||
expect(objectPath.get(obj, ['b','z.d', 0])).to.be.equal('l'); | ||
}); | ||
objectPath.set(new Clazz(), '__proto__.test', 'this is bad') | ||
expect(Clazz.prototype.test).to.be.equal('original') | ||
it('should push value to existing array', function() { | ||
var obj = getTestObj(); | ||
objectPath.push(obj, 'b.c', 'l'); | ||
expect(obj).to.have.deep.property('b.c.0', 'l'); | ||
obj = getTestObj(); | ||
objectPath.push(obj, ['b','c'], 'l'); | ||
expect(obj).to.have.deep.property('b.c.0', 'l'); | ||
}); | ||
objectPath.set(new Clazz(), 'constructor.prototype.test', 'this is bad') | ||
expect(Clazz.prototype.test).to.be.equal('original') | ||
}) | ||
it('should push value to new array', function() { | ||
var obj = getTestObj(); | ||
objectPath.push(obj, 'b.h', 'l'); | ||
expect(obj).to.have.deep.property('b.h.0', 'l'); | ||
obj = getTestObj(); | ||
objectPath.push(obj, ['b','h'], 'l'); | ||
expect(obj).to.have.deep.property('b.h.0', 'l'); | ||
}); | ||
it('[security] should throw an exception if trying to set magic properties in inheritedProps mode', function () { | ||
expect(function() {objectPath.withInheritedProps.set({}, '__proto__.injected', 'this is bad')}) | ||
.to.throw('For security reasons') | ||
expect(Object.prototype.injected).to.be.undefined | ||
it('should push value to existing array using number path', function() { | ||
var obj = getTestObj(); | ||
objectPath.push(obj.b.e, 0, 'l'); | ||
expect(obj).to.have.deep.property('b.e.0.0', 'l'); | ||
}); | ||
function Clazz() {} | ||
Clazz.prototype.test = 'original' | ||
}); | ||
expect(function() {objectPath.withInheritedProps.set(new Clazz(), '__proto__.test', 'this is bad')}) | ||
.to.throw('For security reasons') | ||
expect(Clazz.prototype.test).to.be.equal('original') | ||
expect(function() {objectPath.withInheritedProps.set(new Clazz(), 'constructor.prototype.test', 'this is bad')}) | ||
.to.throw('For security reasons') | ||
expect(Clazz.prototype.test).to.be.equal('original') | ||
describe('ensureExists', function() { | ||
it('should create the path if it does not exists', function() { | ||
var obj = getTestObj(); | ||
var oldVal = objectPath.ensureExists(obj, 'b.g.1.l', 'test'); | ||
expect(oldVal).to.not.exist; | ||
expect(obj).to.have.deep.property('b.g.1.l', 'test'); | ||
oldVal = objectPath.ensureExists(obj, 'b.g.1.l', 'test1'); | ||
expect(oldVal).to.be.equal('test'); | ||
expect(obj).to.have.deep.property('b.g.1.l', 'test'); | ||
oldVal = objectPath.ensureExists(obj, 'b.\u8210', 'ok'); | ||
expect(oldVal).to.not.exist; | ||
expect(obj).to.have.deep.property('b.\u8210', 'ok'); | ||
oldVal = objectPath.ensureExists(obj, ['b','dot.dot'], 'ok'); | ||
expect(oldVal).to.not.exist; | ||
expect(objectPath.get(obj, ['b','dot.dot'])).to.be.equal('ok'); | ||
}); | ||
const obj = {} | ||
expect(function() {objectPath.withInheritedProps.set(obj, 'constructor.prototype.injected', 'this is OK')}) | ||
.to.throw('For security reasons') | ||
expect(Object.prototype.injected).to.be.undefined | ||
}) | ||
}) | ||
it('should return the object if path is empty', function() { | ||
var obj = getTestObj(); | ||
expect(objectPath.ensureExists(obj, [], 'test')).to.have.property('a', 'b'); | ||
}); | ||
describe('push', function () { | ||
it('should push value to existing array using unicode key', function () { | ||
var obj = getTestObj() | ||
objectPath.push(obj, 'b.\u1290c', 'l') | ||
expect(obj).to.have.nested.property('b.\u1290c.0', 'l') | ||
objectPath.push(obj, ['b', '\u1290c'], 'l') | ||
expect(obj).to.have.nested.property('b.\u1290c.1', 'l') | ||
}) | ||
it('Issue #26', function() { | ||
var any = {}; | ||
objectPath.ensureExists(any, ['1','1'], {}); | ||
expect(any).to.be.an('object'); | ||
expect(any[1]).to.be.an('object'); | ||
expect(any[1][1]).to.be.an('object'); | ||
}); | ||
}); | ||
it('should push value to existing array using dot key', function () { | ||
var obj = getTestObj() | ||
objectPath.push(obj, ['b', 'z.d'], 'l') | ||
expect(objectPath.get(obj, ['b', 'z.d', 0])).to.be.equal('l') | ||
}) | ||
describe('coalesce', function(){ | ||
it('should return the first non-undefined value', function(){ | ||
it('should push value to existing array', function () { | ||
var obj = getTestObj() | ||
objectPath.push(obj, 'b.c', 'l') | ||
expect(obj).to.have.nested.property('b.c.0', 'l') | ||
obj = getTestObj() | ||
objectPath.push(obj, ['b', 'c'], 'l') | ||
expect(obj).to.have.nested.property('b.c.0', 'l') | ||
}) | ||
it('should push value to new array', function () { | ||
var obj = getTestObj() | ||
objectPath.push(obj, 'b.h', 'l') | ||
expect(obj).to.have.nested.property('b.h.0', 'l') | ||
obj = getTestObj() | ||
objectPath.push(obj, ['b', 'h'], 'l') | ||
expect(obj).to.have.nested.property('b.h.0', 'l') | ||
}) | ||
it('should push value to existing array using number path', function () { | ||
var obj = getTestObj() | ||
objectPath.push(obj.b.e, 0, 'l') | ||
expect(obj).to.have.nested.property('b.e.0.0', 'l') | ||
}) | ||
}) | ||
describe('ensureExists', function () { | ||
it('should create the path if it does not exists', function () { | ||
var obj = getTestObj() | ||
var oldVal = objectPath.ensureExists(obj, 'b.g.1.l', 'test') | ||
expect(oldVal).to.not.exist | ||
expect(obj).to.have.nested.property('b.g.1.l', 'test') | ||
oldVal = objectPath.ensureExists(obj, 'b.g.1.l', 'test1') | ||
expect(oldVal).to.be.equal('test') | ||
expect(obj).to.have.nested.property('b.g.1.l', 'test') | ||
oldVal = objectPath.ensureExists(obj, 'b.\u8210', 'ok') | ||
expect(oldVal).to.not.exist | ||
expect(obj).to.have.nested.property('b.\u8210', 'ok') | ||
oldVal = objectPath.ensureExists(obj, ['b', 'dot.dot'], 'ok') | ||
expect(oldVal).to.not.exist | ||
expect(objectPath.get(obj, ['b', 'dot.dot'])).to.be.equal('ok') | ||
}) | ||
it('should return the object if path is empty', function () { | ||
var obj = getTestObj() | ||
expect(objectPath.ensureExists(obj, [], 'test')).to.have.property('a', 'b') | ||
}) | ||
it('Issue #26', function () { | ||
var any = {} | ||
objectPath.ensureExists(any, ['1', '1'], {}) | ||
expect(any).to.be.an('object') | ||
expect(any[1]).to.be.an('object') | ||
expect(any[1][1]).to.be.an('object') | ||
}) | ||
}) | ||
describe('coalesce', function () { | ||
it('should return the first non-undefined value', function () { | ||
var obj = { | ||
should: {have: 'prop'} | ||
}; | ||
} | ||
expect(objectPath.coalesce(obj, [ | ||
'doesnt.exist', | ||
['might','not','exist'], | ||
['might', 'not', 'exist'], | ||
'should.have' | ||
])).to.equal('prop'); | ||
}); | ||
])).to.equal('prop') | ||
}) | ||
it('should work with falsy values (null, 0, \'\', false)', function(){ | ||
it('should work with falsy values (null, 0, \'\', false)', function () { | ||
var obj = { | ||
@@ -342,3 +379,3 @@ is: { | ||
} | ||
}; | ||
} | ||
@@ -348,3 +385,3 @@ expect(objectPath.coalesce(obj, [ | ||
'is.zero' | ||
])).to.equal(0); | ||
])).to.equal(0) | ||
@@ -354,3 +391,3 @@ expect(objectPath.coalesce(obj, [ | ||
'is.false' | ||
])).to.equal(false); | ||
])).to.equal(false) | ||
@@ -360,3 +397,3 @@ expect(objectPath.coalesce(obj, [ | ||
'is.null' | ||
])).to.equal(null); | ||
])).to.equal(null) | ||
@@ -366,48 +403,49 @@ expect(objectPath.coalesce(obj, [ | ||
'is.empty' | ||
])).to.equal(''); | ||
}); | ||
])).to.equal('') | ||
}) | ||
it('returns defaultValue if no paths found', function(){ | ||
it('returns defaultValue if no paths found', function () { | ||
var obj = { | ||
doesnt: 'matter' | ||
}; | ||
} | ||
expect(objectPath.coalesce(obj, ['some.inexistant','path',['on','object']], 'false')).to.equal('false'); | ||
}); | ||
expect(objectPath.coalesce(obj, ['some.inexistant', 'path', ['on', 'object']], 'false')).to.equal('false') | ||
}) | ||
it('works with unicode and dot keys', function(){ | ||
it('works with unicode and dot keys', function () { | ||
var obj = { | ||
'\u7591': true, | ||
'dot.dot': false | ||
}; | ||
} | ||
expect(objectPath.coalesce(obj, ['1', '\u7591', 'a.b'])).to.equal(true); | ||
expect(objectPath.coalesce(obj, ['1', ['dot.dot'], '\u7591'])).to.equal(false); | ||
}); | ||
}); | ||
expect(objectPath.coalesce(obj, ['1', '\u7591', 'a.b'])).to.equal(true) | ||
expect(objectPath.coalesce(obj, ['1', ['dot.dot'], '\u7591'])).to.equal(false) | ||
}) | ||
}) | ||
describe('empty', function(){ | ||
it('should ignore invalid arguments safely', function(){ | ||
var obj = {}; | ||
expect(objectPath.empty()).to.equal(void 0); | ||
expect(objectPath.empty(obj, 'path')).to.equal(void 0); | ||
expect(objectPath.empty(obj, '')).to.equal(void 0); | ||
describe('empty', function () { | ||
it('should ignore invalid arguments safely', function () { | ||
var obj = {} | ||
expect(objectPath.empty()).to.equal(void 0) | ||
expect(objectPath.empty(obj, 'path')).to.equal(void 0) | ||
expect(objectPath.empty(obj, '')).to.equal(void 0) | ||
obj.path = true; | ||
obj.path = true | ||
expect(objectPath.empty(obj, 'inexistant')).to.equal(void 0); | ||
expect(objectPath.empty(obj, 'inexistant')).to.equal(void 0) | ||
expect(objectPath.empty(null, 'path')).to.equal(void 0); | ||
expect(objectPath.empty(void 0, 'path')).to.equal(void 0); | ||
}); | ||
expect(objectPath.empty(null, 'path')).to.equal(void 0) | ||
expect(objectPath.empty(void 0, 'path')).to.equal(void 0) | ||
}) | ||
it('should empty each path according to their types', function(){ | ||
function Instance(){ | ||
this.notOwn = true; | ||
it('should empty each path according to their types', function () { | ||
function Instance () { | ||
this.notOwn = true | ||
} | ||
/*istanbul ignore next: not part of code */ | ||
Instance.prototype.test = function(){}; | ||
Instance.prototype.test = function () { | ||
} | ||
/*istanbul ignore next: not part of code */ | ||
Instance.prototype.arr = []; | ||
Instance.prototype.arr = [] | ||
@@ -417,7 +455,7 @@ var | ||
string: 'some string', | ||
array: ['some','array',[1,2,3]], | ||
array: ['some', 'array', [1, 2, 3]], | ||
number: 21, | ||
boolean: true, | ||
object: { | ||
some:'property', | ||
some: 'property', | ||
sub: { | ||
@@ -430,156 +468,157 @@ 'property': true | ||
instance: new Instance() | ||
}; | ||
} | ||
/*istanbul ignore next: not part of code */ | ||
obj['function'] = function(){}; | ||
obj['function'] = function () { | ||
} | ||
objectPath.empty(obj, ['array','2']); | ||
expect(obj.array[2]).to.deep.equal([]); | ||
objectPath.empty(obj, ['array', '2']) | ||
expect(obj.array[2]).to.deep.equal([]) | ||
objectPath.empty(obj, 'object.sub'); | ||
expect(obj.object.sub).to.deep.equal({}); | ||
objectPath.empty(obj, 'object.sub') | ||
expect(obj.object.sub).to.deep.equal({}) | ||
objectPath.empty(obj, 'object.nullProp'); | ||
expect(obj.object.nullProp).to.equal(null); | ||
objectPath.empty(obj, 'object.nullProp') | ||
expect(obj.object.nullProp).to.equal(null) | ||
objectPath.empty(obj, 'object.undefinedProp'); | ||
expect(obj.object.undefinedProp).to.equal(void 0); | ||
expect(obj.object).to.have.property('undefinedProp'); | ||
objectPath.empty(obj, 'object.undefinedProp') | ||
expect(obj.object.undefinedProp).to.equal(void 0) | ||
expect(obj.object).to.have.property('undefinedProp') | ||
objectPath.empty(obj, 'object.notAProp'); | ||
expect(obj.object.notAProp).to.equal(void 0); | ||
expect(obj.object).to.not.have.property('notAProp'); | ||
objectPath.empty(obj, 'object.notAProp') | ||
expect(obj.object.notAProp).to.equal(void 0) | ||
expect(obj.object).to.not.have.property('notAProp') | ||
objectPath.empty(obj, 'instance.test'); | ||
objectPath.empty(obj, 'instance.test') | ||
//instance.test is not own property, so it shouldn't be emptied | ||
expect(obj.instance.test).to.be.a('function'); | ||
expect(Instance.prototype.test).to.be.a('function'); | ||
expect(obj.instance.test).to.be.a('function') | ||
expect(Instance.prototype.test).to.be.a('function') | ||
objectPath.empty(obj, 'string'); | ||
objectPath.empty(obj, 'number'); | ||
objectPath.empty(obj, 'boolean'); | ||
objectPath.empty(obj, 'function'); | ||
objectPath.empty(obj, 'array'); | ||
objectPath.empty(obj, 'object'); | ||
objectPath.empty(obj, 'instance'); | ||
objectPath.empty(obj, 'string') | ||
objectPath.empty(obj, 'number') | ||
objectPath.empty(obj, 'boolean') | ||
objectPath.empty(obj, 'function') | ||
objectPath.empty(obj, 'array') | ||
objectPath.empty(obj, 'object') | ||
objectPath.empty(obj, 'instance') | ||
expect(obj.string).to.equal(''); | ||
expect(obj.array).to.deep.equal([]); | ||
expect(obj.number).to.equal(0); | ||
expect(obj.boolean).to.equal(false); | ||
expect(obj.object).to.deep.equal({}); | ||
expect(obj.instance.notOwn).to.be.an('undefined'); | ||
expect(obj.instance.arr).to.be.an('array'); | ||
expect(obj['function']).to.equal(null); | ||
}); | ||
}); | ||
expect(obj.string).to.equal('') | ||
expect(obj.array).to.deep.equal([]) | ||
expect(obj.number).to.equal(0) | ||
expect(obj.boolean).to.equal(false) | ||
expect(obj.object).to.deep.equal({}) | ||
expect(obj.instance.notOwn).to.be.an('undefined') | ||
expect(obj.instance.arr).to.be.an('array') | ||
expect(obj['function']).to.equal(null) | ||
}) | ||
}) | ||
describe('del', function(){ | ||
it('should work with number path', function(){ | ||
var obj = getTestObj(); | ||
objectPath.del(obj.b.d, 1); | ||
expect(obj.b.d).to.deep.equal(['a']); | ||
}); | ||
describe('del', function () { | ||
it('should work with number path', function () { | ||
var obj = getTestObj() | ||
objectPath.del(obj.b.d, 1) | ||
expect(obj.b.d).to.deep.equal(['a']) | ||
}) | ||
it('should remove null and undefined props (but not explode on nested)', function(){ | ||
var obj = { nullProp: null, undefinedProp: void 0 }; | ||
expect(obj).to.have.property('nullProp'); | ||
expect(obj).to.have.property('undefinedProp'); | ||
it('should remove null and undefined props (but not explode on nested)', function () { | ||
var obj = {nullProp: null, undefinedProp: void 0} | ||
expect(obj).to.have.property('nullProp') | ||
expect(obj).to.have.property('undefinedProp') | ||
objectPath.del(obj, 'nullProp.foo'); | ||
objectPath.del(obj, 'undefinedProp.bar'); | ||
expect(obj).to.have.property('nullProp'); | ||
expect(obj).to.have.property('undefinedProp'); | ||
expect(obj).to.deep.equal({ nullProp: null, undefinedProp: void 0 }); | ||
objectPath.del(obj, 'nullProp.foo') | ||
objectPath.del(obj, 'undefinedProp.bar') | ||
expect(obj).to.have.property('nullProp') | ||
expect(obj).to.have.property('undefinedProp') | ||
expect(obj).to.deep.equal({nullProp: null, undefinedProp: void 0}) | ||
objectPath.del(obj, 'nullProp'); | ||
objectPath.del(obj, 'undefinedProp'); | ||
expect(obj).to.not.have.property('nullProp'); | ||
expect(obj).to.not.have.property('undefinedProp'); | ||
expect(obj).to.deep.equal({}); | ||
}); | ||
objectPath.del(obj, 'nullProp') | ||
objectPath.del(obj, 'undefinedProp') | ||
expect(obj).to.not.have.property('nullProp') | ||
expect(obj).to.not.have.property('undefinedProp') | ||
expect(obj).to.deep.equal({}) | ||
}) | ||
it('should delete deep paths', function(){ | ||
var obj = getTestObj(); | ||
it('should delete deep paths', function () { | ||
var obj = getTestObj() | ||
expect(objectPath.del(obj)).to.be.equal(obj); | ||
expect(objectPath.del(obj)).to.be.equal(obj) | ||
objectPath.set(obj, 'b.g.1.0', 'test'); | ||
objectPath.set(obj, 'b.g.1.1', 'test'); | ||
objectPath.set(obj, 'b.h.az', 'test'); | ||
objectPath.set(obj, 'b.\ubeef', 'test'); | ||
objectPath.set(obj, ['b','dot.dot'], 'test'); | ||
objectPath.set(obj, 'b.g.1.0', 'test') | ||
objectPath.set(obj, 'b.g.1.1', 'test') | ||
objectPath.set(obj, 'b.h.az', 'test') | ||
objectPath.set(obj, 'b.\ubeef', 'test') | ||
objectPath.set(obj, ['b', 'dot.dot'], 'test') | ||
expect(obj).to.have.deep.property('b.g.1.0','test'); | ||
expect(obj).to.have.deep.property('b.g.1.1','test'); | ||
expect(obj).to.have.deep.property('b.h.az','test'); | ||
expect(obj).to.have.deep.property('b.\ubeef','test'); | ||
expect(obj).to.have.nested.property('b.g.1.0', 'test') | ||
expect(obj).to.have.nested.property('b.g.1.1', 'test') | ||
expect(obj).to.have.nested.property('b.h.az', 'test') | ||
expect(obj).to.have.nested.property('b.\ubeef', 'test') | ||
objectPath.del(obj, 'b.h.az'); | ||
expect(obj).to.not.have.deep.property('b.h.az'); | ||
expect(obj).to.have.deep.property('b.h'); | ||
objectPath.del(obj, 'b.h.az') | ||
expect(obj).to.not.have.nested.property('b.h.az') | ||
expect(obj).to.have.nested.property('b.h') | ||
objectPath.del(obj, 'b.g.1.1'); | ||
expect(obj).to.not.have.deep.property('b.g.1.1'); | ||
expect(obj).to.have.deep.property('b.g.1.0','test'); | ||
objectPath.del(obj, 'b.g.1.1') | ||
expect(obj).to.not.have.nested.property('b.g.1.1') | ||
expect(obj).to.have.nested.property('b.g.1.0', 'test') | ||
objectPath.del(obj, 'b.\ubeef'); | ||
expect(obj).to.not.have.deep.property('b.\ubeef'); | ||
objectPath.del(obj, 'b.\ubeef') | ||
expect(obj).to.not.have.nested.property('b.\ubeef') | ||
objectPath.del(obj, ['b','dot.dot']); | ||
expect(objectPath.get(obj, ['b','dot.dot'])).to.be.equal(void 0); | ||
objectPath.del(obj, ['b', 'dot.dot']) | ||
expect(objectPath.get(obj, ['b', 'dot.dot'])).to.be.equal(void 0) | ||
objectPath.del(obj, ['b','g','1','0']); | ||
expect(obj).to.not.have.deep.property('b.g.1.0'); | ||
expect(obj).to.have.deep.property('b.g.1'); | ||
objectPath.del(obj, ['b', 'g', '1', '0']) | ||
expect(obj).to.not.have.nested.property('b.g.1.0') | ||
expect(obj).to.have.nested.property('b.g.1') | ||
expect(objectPath.del(obj, ['b'])).to.not.have.deep.property('b.g'); | ||
expect(obj).to.be.deep.equal({'a':'b'}); | ||
}); | ||
expect(objectPath.del(obj, ['b'])).to.not.have.nested.property('b.g') | ||
expect(obj).to.be.deep.equal({'a': 'b'}) | ||
}) | ||
it('should remove items from existing array', function(){ | ||
var obj = getTestObj(); | ||
it('should remove items from existing array', function () { | ||
var obj = getTestObj() | ||
objectPath.del(obj, 'b.d.0'); | ||
expect(obj.b.d).to.have.length(1); | ||
expect(obj.b.d).to.be.deep.equal(['b']); | ||
objectPath.del(obj, 'b.d.0') | ||
expect(obj.b.d).to.have.length(1) | ||
expect(obj.b.d).to.be.deep.equal(['b']) | ||
objectPath.del(obj, 'b.d.0'); | ||
expect(obj.b.d).to.have.length(0); | ||
expect(obj.b.d).to.be.deep.equal([]); | ||
}); | ||
}); | ||
objectPath.del(obj, 'b.d.0') | ||
expect(obj.b.d).to.have.length(0) | ||
expect(obj.b.d).to.be.deep.equal([]) | ||
}) | ||
}) | ||
describe('insert', function(){ | ||
it('should insert value into existing array', function(){ | ||
var obj = getTestObj(); | ||
describe('insert', function () { | ||
it('should insert value into existing array', function () { | ||
var obj = getTestObj() | ||
objectPath.insert(obj, 'b.c', 'asdf'); | ||
expect(obj).to.have.deep.property('b.c.0', 'asdf'); | ||
expect(obj).to.not.have.deep.property('b.c.1'); | ||
}); | ||
objectPath.insert(obj, 'b.c', 'asdf') | ||
expect(obj).to.have.nested.property('b.c.0', 'asdf') | ||
expect(obj).to.not.have.nested.property('b.c.1') | ||
}) | ||
it('should create intermediary array', function(){ | ||
var obj = getTestObj(); | ||
it('should create intermediary array', function () { | ||
var obj = getTestObj() | ||
objectPath.insert(obj, 'b.c.0', 'asdf'); | ||
expect(obj).to.have.deep.property('b.c.0.0', 'asdf'); | ||
}); | ||
objectPath.insert(obj, 'b.c.0', 'asdf') | ||
expect(obj).to.have.nested.property('b.c.0.0', 'asdf') | ||
}) | ||
it('should insert in another index', function(){ | ||
var obj = getTestObj(); | ||
it('should insert in another index', function () { | ||
var obj = getTestObj() | ||
objectPath.insert(obj, 'b.d', 'asdf', 1); | ||
expect(obj).to.have.deep.property('b.d.1', 'asdf'); | ||
expect(obj).to.have.deep.property('b.d.0', 'a'); | ||
expect(obj).to.have.deep.property('b.d.2', 'b'); | ||
}); | ||
objectPath.insert(obj, 'b.d', 'asdf', 1) | ||
expect(obj).to.have.nested.property('b.d.1', 'asdf') | ||
expect(obj).to.have.nested.property('b.d.0', 'a') | ||
expect(obj).to.have.nested.property('b.d.2', 'b') | ||
}) | ||
it('should handle sparse array', function(){ | ||
var obj = getTestObj(); | ||
obj.b.d = new Array(4); | ||
obj.b.d[0] = 'a'; | ||
obj.b.d[1] = 'b'; | ||
it('should handle sparse array', function () { | ||
var obj = getTestObj() | ||
obj.b.d = new Array(4) | ||
obj.b.d[0] = 'a' | ||
obj.b.d[1] = 'b' | ||
objectPath.insert(obj, 'b.d', 'asdf', 3); | ||
objectPath.insert(obj, 'b.d', 'asdf', 3) | ||
expect(obj.b.d).to.have.members([ | ||
@@ -591,159 +630,159 @@ 'a', | ||
'asdf' | ||
]); | ||
}); | ||
}); | ||
]) | ||
}) | ||
}) | ||
describe('has', function () { | ||
it('should return false for empty object', function () { | ||
expect(objectPath.has({}, 'a')).to.be.equal(false); | ||
}); | ||
expect(objectPath.has({}, 'a')).to.be.equal(false) | ||
}) | ||
it('should handle empty paths properly', function () { | ||
var obj = getTestObj(); | ||
expect(objectPath.has(obj, '')).to.be.equal(false); | ||
expect(objectPath.has(obj, [''])).to.be.equal(false); | ||
var obj = getTestObj() | ||
expect(objectPath.has(obj, '')).to.be.equal(false) | ||
expect(objectPath.has(obj, [''])).to.be.equal(false) | ||
obj[''] = 1 | ||
expect(objectPath.has(obj, '')).to.be.equal(true); | ||
expect(objectPath.has(obj, [''])).to.be.equal(true); | ||
expect(objectPath.has(obj, '')).to.be.equal(true) | ||
expect(objectPath.has(obj, [''])).to.be.equal(true) | ||
expect(objectPath.has(obj, [])).to.be.equal(true); | ||
expect(objectPath.has(null, [])).to.be.equal(false); | ||
}); | ||
expect(objectPath.has(obj, [])).to.be.equal(true) | ||
expect(objectPath.has(null, [])).to.be.equal(false) | ||
}) | ||
it('should test under shallow object', function() { | ||
var obj = getTestObj(); | ||
expect(objectPath.has(obj, 'a')).to.be.equal(true); | ||
expect(objectPath.has(obj, ['a'])).to.be.equal(true); | ||
expect(objectPath.has(obj, 'z')).to.be.equal(false); | ||
expect(objectPath.has(obj, ['z'])).to.be.equal(false); | ||
}); | ||
it('should test under shallow object', function () { | ||
var obj = getTestObj() | ||
expect(objectPath.has(obj, 'a')).to.be.equal(true) | ||
expect(objectPath.has(obj, ['a'])).to.be.equal(true) | ||
expect(objectPath.has(obj, 'z')).to.be.equal(false) | ||
expect(objectPath.has(obj, ['z'])).to.be.equal(false) | ||
}) | ||
it('should work with number path', function() { | ||
var obj = getTestObj(); | ||
expect(objectPath.has(obj.b.d, 0)).to.be.equal(true); | ||
expect(objectPath.has(obj.b, 0)).to.be.equal(false); | ||
expect(objectPath.has(obj.b.d, 10)).to.be.equal(false); | ||
expect(objectPath.has(obj.b, 10)).to.be.equal(false); | ||
}); | ||
it('should work with number path', function () { | ||
var obj = getTestObj() | ||
expect(objectPath.has(obj.b.d, 0)).to.be.equal(true) | ||
expect(objectPath.has(obj.b, 0)).to.be.equal(false) | ||
expect(objectPath.has(obj.b.d, 10)).to.be.equal(false) | ||
expect(objectPath.has(obj.b, 10)).to.be.equal(false) | ||
}) | ||
it('should test under deep object', function() { | ||
var obj = getTestObj(); | ||
expect(objectPath.has(obj, 'b.f')).to.be.equal(true); | ||
expect(objectPath.has(obj, ['b','f'])).to.be.equal(true); | ||
expect(objectPath.has(obj, 'b.g')).to.be.equal(false); | ||
expect(objectPath.has(obj, ['b','g'])).to.be.equal(false); | ||
}); | ||
it('should test under deep object', function () { | ||
var obj = getTestObj() | ||
expect(objectPath.has(obj, 'b.f')).to.be.equal(true) | ||
expect(objectPath.has(obj, ['b', 'f'])).to.be.equal(true) | ||
expect(objectPath.has(obj, 'b.g')).to.be.equal(false) | ||
expect(objectPath.has(obj, ['b', 'g'])).to.be.equal(false) | ||
}) | ||
it('should test value under array', function() { | ||
it('should test value under array', function () { | ||
var obj = { | ||
b: ['a'] | ||
}; | ||
} | ||
obj.b[3] = {o: 'a'} | ||
expect(objectPath.has(obj, 'b.0')).to.be.equal(true); | ||
expect(objectPath.has(obj, 'b.1')).to.be.equal(true); | ||
expect(objectPath.has(obj, 'b.3.o')).to.be.equal(true); | ||
expect(objectPath.has(obj, 'b.3.qwe')).to.be.equal(false); | ||
expect(objectPath.has(obj, 'b.4')).to.be.equal(false); | ||
}); | ||
expect(objectPath.has(obj, 'b.0')).to.be.equal(true) | ||
expect(objectPath.has(obj, 'b.1')).to.be.equal(true) | ||
expect(objectPath.has(obj, 'b.3.o')).to.be.equal(true) | ||
expect(objectPath.has(obj, 'b.3.qwe')).to.be.equal(false) | ||
expect(objectPath.has(obj, 'b.4')).to.be.equal(false) | ||
}) | ||
it('should test the value under array deep', function() { | ||
var obj = getTestObj(); | ||
expect(objectPath.has(obj, 'b.e.1.f')).to.be.equal(true); | ||
expect(objectPath.has(obj, ['b','e',1,'f'])).to.be.equal(true); | ||
expect(objectPath.has(obj, 'b.e.1.f.g.h.i')).to.be.equal(false); | ||
expect(objectPath.has(obj, ['b','e',1,'f','g','h','i'])).to.be.equal(false); | ||
}); | ||
it('should test the value under array deep', function () { | ||
var obj = getTestObj() | ||
expect(objectPath.has(obj, 'b.e.1.f')).to.be.equal(true) | ||
expect(objectPath.has(obj, ['b', 'e', 1, 'f'])).to.be.equal(true) | ||
expect(objectPath.has(obj, 'b.e.1.f.g.h.i')).to.be.equal(false) | ||
expect(objectPath.has(obj, ['b', 'e', 1, 'f', 'g', 'h', 'i'])).to.be.equal(false) | ||
}) | ||
it('should test the value under integer-like key', function() { | ||
var obj = { '1a': 'foo' }; | ||
expect(objectPath.has(obj, '1a')).to.be.equal(true); | ||
expect(objectPath.has(obj, ['1a'])).to.be.equal(true); | ||
}); | ||
it('should test the value under integer-like key', function () { | ||
var obj = {'1a': 'foo'} | ||
expect(objectPath.has(obj, '1a')).to.be.equal(true) | ||
expect(objectPath.has(obj, ['1a'])).to.be.equal(true) | ||
}) | ||
it('should distinct nonexistent key and key = undefined', function() { | ||
var obj = {}; | ||
expect(objectPath.has(obj, 'key')).to.be.equal(false); | ||
it('should distinct nonexistent key and key = undefined', function () { | ||
var obj = {} | ||
expect(objectPath.has(obj, 'key')).to.be.equal(false) | ||
obj.key = undefined; | ||
expect(objectPath.has(obj, 'key')).to.be.equal(true); | ||
}); | ||
obj.key = undefined | ||
expect(objectPath.has(obj, 'key')).to.be.equal(true) | ||
}) | ||
it('should work with deep undefined/null values', function() { | ||
var obj = {}; | ||
expect(objectPath.has(obj, 'missing.test')).to.be.equal(false); | ||
it('should work with deep undefined/null values', function () { | ||
var obj = {} | ||
expect(objectPath.has(obj, 'missing.test')).to.be.equal(false) | ||
obj.missing = null; | ||
expect(objectPath.has(obj, 'missing.test')).to.be.equal(false); | ||
obj.missing = null | ||
expect(objectPath.has(obj, 'missing.test')).to.be.equal(false) | ||
obj.sparseArray = [1, undefined, 3] | ||
expect(objectPath.has(obj, 'sparseArray.1.test')).to.be.equal(false); | ||
}); | ||
}); | ||
expect(objectPath.has(obj, 'sparseArray.1.test')).to.be.equal(false) | ||
}) | ||
}) | ||
describe('bind object', function () { | ||
// just get one scenario from each feature, so whole functionality is proxied well | ||
it('should return the value under shallow object', function() { | ||
var obj = getTestObj(); | ||
var model = objectPath(obj); | ||
expect(model.get('a')).to.be.equal('b'); | ||
expect(model.get(['a'])).to.be.equal('b'); | ||
}); | ||
it('should return the value under shallow object', function () { | ||
var obj = getTestObj() | ||
var model = objectPath(obj) | ||
expect(model.get('a')).to.be.equal('b') | ||
expect(model.get(['a'])).to.be.equal('b') | ||
}) | ||
it('should set value under shallow object', function() { | ||
var obj = getTestObj(); | ||
var model = objectPath(obj); | ||
model.set('c', {m: 'o'}); | ||
expect(obj).to.have.deep.property('c.m', 'o'); | ||
obj = getTestObj(); | ||
model = objectPath(obj); | ||
model.set(['c'], {m: 'o'}); | ||
expect(obj).to.have.deep.property('c.m', 'o'); | ||
}); | ||
it('should set value under shallow object', function () { | ||
var obj = getTestObj() | ||
var model = objectPath(obj) | ||
model.set('c', {m: 'o'}) | ||
expect(obj).to.have.nested.property('c.m', 'o') | ||
obj = getTestObj() | ||
model = objectPath(obj) | ||
model.set(['c'], {m: 'o'}) | ||
expect(obj).to.have.nested.property('c.m', 'o') | ||
}) | ||
it('should push value to existing array', function() { | ||
var obj = getTestObj(); | ||
var model = objectPath(obj); | ||
model.push('b.c', 'l'); | ||
expect(obj).to.have.deep.property('b.c.0', 'l'); | ||
obj = getTestObj(); | ||
model = objectPath(obj); | ||
model.push(['b','c'], 'l'); | ||
expect(obj).to.have.deep.property('b.c.0', 'l'); | ||
}); | ||
it('should push value to existing array', function () { | ||
var obj = getTestObj() | ||
var model = objectPath(obj) | ||
model.push('b.c', 'l') | ||
expect(obj).to.have.nested.property('b.c.0', 'l') | ||
obj = getTestObj() | ||
model = objectPath(obj) | ||
model.push(['b', 'c'], 'l') | ||
expect(obj).to.have.nested.property('b.c.0', 'l') | ||
}) | ||
it('should create the path if it does not exists', function() { | ||
var obj = getTestObj(); | ||
var model = objectPath(obj); | ||
var oldVal = model.ensureExists('b.g.1.l', 'test'); | ||
expect(oldVal).to.not.exist; | ||
expect(obj).to.have.deep.property('b.g.1.l', 'test'); | ||
oldVal = model.ensureExists('b.g.1.l', 'test1'); | ||
expect(oldVal).to.be.equal('test'); | ||
expect(obj).to.have.deep.property('b.g.1.l', 'test'); | ||
}); | ||
it('should create the path if it does not exists', function () { | ||
var obj = getTestObj() | ||
var model = objectPath(obj) | ||
var oldVal = model.ensureExists('b.g.1.l', 'test') | ||
expect(oldVal).to.not.exist | ||
expect(obj).to.have.nested.property('b.g.1.l', 'test') | ||
oldVal = model.ensureExists('b.g.1.l', 'test1') | ||
expect(oldVal).to.be.equal('test') | ||
expect(obj).to.have.nested.property('b.g.1.l', 'test') | ||
}) | ||
it('should return the first non-undefined value', function(){ | ||
it('should return the first non-undefined value', function () { | ||
var obj = { | ||
should: {have: 'prop'} | ||
}; | ||
var model = objectPath(obj); | ||
} | ||
var model = objectPath(obj) | ||
expect(model.coalesce([ | ||
'doesnt.exist', | ||
['might','not','exist'], | ||
['might', 'not', 'exist'], | ||
'should.have' | ||
])).to.equal('prop'); | ||
}); | ||
])).to.equal('prop') | ||
}) | ||
it('should empty each path according to their types', function(){ | ||
function Instance(){ | ||
this.notOwn = true; | ||
it('should empty each path according to their types', function () { | ||
function Instance () { | ||
this.notOwn = true | ||
} | ||
/*istanbul ignore next: not part of code */ | ||
Instance.prototype.test = function(){}; | ||
Instance.prototype.test = function () { | ||
} | ||
/*istanbul ignore next: not part of code */ | ||
Instance.prototype.arr = []; | ||
Instance.prototype.arr = [] | ||
@@ -753,7 +792,7 @@ var | ||
string: 'some string', | ||
array: ['some','array',[1,2,3]], | ||
array: ['some', 'array', [1, 2, 3]], | ||
number: 21, | ||
boolean: true, | ||
object: { | ||
some:'property', | ||
some: 'property', | ||
sub: { | ||
@@ -764,98 +803,100 @@ 'property': true | ||
instance: new Instance() | ||
}; | ||
} | ||
/*istanbul ignore next: not part of code */ | ||
obj['function'] = function(){}; | ||
obj['function'] = function () { | ||
} | ||
var model = objectPath(obj); | ||
var model = objectPath(obj) | ||
model.empty(['array','2']); | ||
expect(obj.array[2]).to.deep.equal([]); | ||
model.empty(['array', '2']) | ||
expect(obj.array[2]).to.deep.equal([]) | ||
model.empty('object.sub'); | ||
expect(obj.object.sub).to.deep.equal({}); | ||
model.empty('object.sub') | ||
expect(obj.object.sub).to.deep.equal({}) | ||
model.empty('instance.test'); | ||
model.empty('instance.test') | ||
//instance.test is not own property so it shouldn't be emptied | ||
expect(obj.instance.test).to.be.a('function'); | ||
expect(Instance.prototype.test).to.be.a('function'); | ||
expect(obj.instance.test).to.be.a('function') | ||
expect(Instance.prototype.test).to.be.a('function') | ||
model.empty('string'); | ||
model.empty('number'); | ||
model.empty('boolean'); | ||
model.empty('function'); | ||
model.empty('array'); | ||
model.empty('object'); | ||
model.empty('instance'); | ||
model.empty('string') | ||
model.empty('number') | ||
model.empty('boolean') | ||
model.empty('function') | ||
model.empty('array') | ||
model.empty('object') | ||
model.empty('instance') | ||
expect(obj.string).to.equal(''); | ||
expect(obj.array).to.deep.equal([]); | ||
expect(obj.number).to.equal(0); | ||
expect(obj.boolean).to.equal(false); | ||
expect(obj.object).to.deep.equal({}); | ||
expect(obj.instance.notOwn).to.be.an('undefined'); | ||
expect(obj.instance.arr).to.be.an('array'); | ||
expect(obj['function']).to.equal(null); | ||
}); | ||
expect(obj.string).to.equal('') | ||
expect(obj.array).to.deep.equal([]) | ||
expect(obj.number).to.equal(0) | ||
expect(obj.boolean).to.equal(false) | ||
expect(obj.object).to.deep.equal({}) | ||
expect(obj.instance.notOwn).to.be.an('undefined') | ||
expect(obj.instance.arr).to.be.an('array') | ||
expect(obj['function']).to.equal(null) | ||
}) | ||
it('should delete deep paths', function(){ | ||
var obj = getTestObj(); | ||
var model = objectPath(obj); | ||
it('should delete deep paths', function () { | ||
var obj = getTestObj() | ||
var model = objectPath(obj) | ||
expect(model.del()).to.be.equal(obj); | ||
expect(model.del()).to.be.equal(obj) | ||
model.set('b.g.1.0', 'test'); | ||
model.set('b.g.1.1', 'test'); | ||
model.set('b.h.az', 'test'); | ||
model.set('b.g.1.0', 'test') | ||
model.set('b.g.1.1', 'test') | ||
model.set('b.h.az', 'test') | ||
expect(obj).to.have.deep.property('b.g.1.0','test'); | ||
expect(obj).to.have.deep.property('b.g.1.1','test'); | ||
expect(obj).to.have.deep.property('b.h.az','test'); | ||
expect(obj).to.have.nested.property('b.g.1.0', 'test') | ||
expect(obj).to.have.nested.property('b.g.1.1', 'test') | ||
expect(obj).to.have.nested.property('b.h.az', 'test') | ||
model.del('b.h.az'); | ||
expect(obj).to.not.have.deep.property('b.h.az'); | ||
expect(obj).to.have.deep.property('b.h'); | ||
model.del('b.h.az') | ||
expect(obj).to.not.have.nested.property('b.h.az') | ||
expect(obj).to.have.nested.property('b.h') | ||
model.del('b.g.1.1'); | ||
expect(obj).to.not.have.deep.property('b.g.1.1'); | ||
expect(obj).to.have.deep.property('b.g.1.0','test'); | ||
model.del('b.g.1.1') | ||
expect(obj).to.not.have.nested.property('b.g.1.1') | ||
expect(obj).to.have.nested.property('b.g.1.0', 'test') | ||
model.del(['b','g','1','0']); | ||
expect(obj).to.not.have.deep.property('b.g.1.0'); | ||
expect(obj).to.have.deep.property('b.g.1'); | ||
model.del(['b', 'g', '1', '0']) | ||
expect(obj).to.not.have.nested.property('b.g.1.0') | ||
expect(obj).to.have.nested.property('b.g.1') | ||
expect(model.del(['b'])).to.not.have.deep.property('b.g'); | ||
expect(obj).to.be.deep.equal({'a':'b'}); | ||
}); | ||
expect(model.del(['b'])).to.not.have.nested.property('b.g') | ||
expect(obj).to.be.deep.equal({'a': 'b'}) | ||
}) | ||
it('should insert value into existing array', function(){ | ||
var obj = getTestObj(); | ||
var model = objectPath(obj); | ||
it('should insert value into existing array', function () { | ||
var obj = getTestObj() | ||
var model = objectPath(obj) | ||
model.insert('b.c', 'asdf'); | ||
expect(obj).to.have.deep.property('b.c.0', 'asdf'); | ||
expect(obj).to.not.have.deep.property('b.c.1'); | ||
}); | ||
model.insert('b.c', 'asdf') | ||
expect(obj).to.have.nested.property('b.c.0', 'asdf') | ||
expect(obj).to.not.have.nested.property('b.c.1') | ||
}) | ||
it('should test under shallow object', function() { | ||
var obj = getTestObj(); | ||
var model = objectPath(obj); | ||
it('should test under shallow object', function () { | ||
var obj = getTestObj() | ||
var model = objectPath(obj) | ||
expect(model.has('a')).to.be.equal(true); | ||
expect(model.has(['a'])).to.be.equal(true); | ||
expect(model.has('z')).to.be.equal(false); | ||
expect(model.has(['z'])).to.be.equal(false); | ||
}); | ||
}); | ||
expect(model.has('a')).to.be.equal(true) | ||
expect(model.has(['a'])).to.be.equal(true) | ||
expect(model.has('z')).to.be.equal(false) | ||
expect(model.has(['z'])).to.be.equal(false) | ||
}) | ||
}) | ||
describe('Don\'t access not own properties [default]', function () { | ||
it('should not get a not own property', function() { | ||
var Obj = function() {}; | ||
Obj.prototype.notOwn = {a: 'a'}; | ||
var obj = new Obj(); | ||
it('should not get a not own property', function () { | ||
var Obj = function () { | ||
} | ||
Obj.prototype.notOwn = {a: 'a'} | ||
var obj = new Obj() | ||
expect(objectPath.get(obj, 'notOwn')).to.be.undefined | ||
}); | ||
}) | ||
it('should set a not own property on the instance (not the prototype)', function() { | ||
it('should set a not own property on the instance (not the prototype)', function () { | ||
var proto = { | ||
@@ -866,8 +907,8 @@ notOwn: {} | ||
objectPath.set(obj, 'notOwn.test', 'a'); | ||
expect(obj.notOwn.test).to.be.equal('a'); | ||
expect(proto.notOwn).to.be.deep.equal({}); | ||
}); | ||
objectPath.set(obj, 'notOwn.test', 'a') | ||
expect(obj.notOwn.test).to.be.equal('a') | ||
expect(proto.notOwn).to.be.deep.equal({}) | ||
}) | ||
it('has should return false on a not own property', function() { | ||
it('has should return false on a not own property', function () { | ||
var proto = { | ||
@@ -879,25 +920,25 @@ notOwn: {a: 'a'} | ||
expect(objectPath.has(obj, 'notOwn')).to.be.false; | ||
expect(objectPath.has(obj, 'notOwn.a')).to.be.false; | ||
}); | ||
expect(objectPath.has(obj, 'notOwn')).to.be.false | ||
expect(objectPath.has(obj, 'notOwn.a')).to.be.false | ||
}) | ||
it('empty should not empty on a not own property', function() { | ||
it('empty should not empty on a not own property', function () { | ||
var proto = { | ||
notOwn: {a: 'a'} | ||
} | ||
var obj = Object.create(proto); | ||
var obj = Object.create(proto) | ||
objectPath.empty(obj, 'notOwn'); | ||
expect(proto.notOwn).to.be.deep.equal({a: 'a'}); | ||
expect(obj.notOwn).to.be.deep.equal({a: 'a'}); | ||
}); | ||
objectPath.empty(obj, 'notOwn') | ||
expect(proto.notOwn).to.be.deep.equal({a: 'a'}) | ||
expect(obj.notOwn).to.be.deep.equal({a: 'a'}) | ||
}) | ||
it('del should not delete not own property', function() { | ||
it('del should not delete not own property', function () { | ||
var proto = { | ||
notOwn: {a: 'a'} | ||
} | ||
var obj = Object.create(proto); | ||
var obj = Object.create(proto) | ||
objectPath.del(obj, 'notOwn.a'); | ||
expect(proto.notOwn).to.be.deep.equal({a: 'a'}); | ||
objectPath.del(obj, 'notOwn.a') | ||
expect(proto.notOwn).to.be.deep.equal({a: 'a'}) | ||
//expect(obj.notOwn).to.be.deep.equal({a: 'a'}); | ||
@@ -907,15 +948,16 @@ //objectPath.del(obj, 'notOwn'); | ||
//expect(obj).to.be.deep.equal({notOwn: {a: 'a'}}); | ||
}); | ||
}); | ||
}) | ||
}) | ||
describe('Access own properties [optional]', function () { | ||
it('should get a not own property', function() { | ||
var Obj = function() {}; | ||
Obj.prototype.notOwn = {a: 'a'}; | ||
var obj = new Obj(); | ||
it('should get a not own property', function () { | ||
var Obj = function () { | ||
} | ||
Obj.prototype.notOwn = {a: 'a'} | ||
var obj = new Obj() | ||
expect(objectPath.withInheritedProps.get(obj, 'notOwn.a')).to.be.equal('a') | ||
}); | ||
}) | ||
it('should set a deep not own property on the prototype (if exists)', function() { | ||
it('should set a deep not own property on the prototype (if exists)', function () { | ||
var proto = { | ||
@@ -926,9 +968,9 @@ notOwn: {} | ||
objectPath.withInheritedProps.set(obj, 'notOwn.test', 'a'); | ||
expect(obj.notOwn.test).to.be.equal('a'); | ||
expect(proto.notOwn).to.be.deep.equal({test: 'a'}); | ||
}); | ||
objectPath.withInheritedProps.set(obj, 'notOwn.test', 'a') | ||
expect(obj.notOwn.test).to.be.equal('a') | ||
expect(proto.notOwn).to.be.deep.equal({test: 'a'}) | ||
}) | ||
it('has should return true on a not own property', function() { | ||
it('has should return true on a not own property', function () { | ||
var proto = { | ||
@@ -939,30 +981,30 @@ notOwn: {a: 'a'} | ||
expect(objectPath.withInheritedProps.has(obj, 'notOwn')).to.be.true; | ||
expect(objectPath.withInheritedProps.has(obj, 'notOwn.a')).to.be.true; | ||
}); | ||
expect(objectPath.withInheritedProps.has(obj, 'notOwn')).to.be.true | ||
expect(objectPath.withInheritedProps.has(obj, 'notOwn.a')).to.be.true | ||
}) | ||
it('empty should empty a not own property', function() { | ||
it('empty should empty a not own property', function () { | ||
var proto = { | ||
notOwn: {a: 'a'} | ||
} | ||
var obj = Object.create(proto); | ||
var obj = Object.create(proto) | ||
objectPath.withInheritedProps.empty(obj, 'notOwn'); | ||
expect(proto.notOwn).to.be.deep.equal({}); | ||
expect(obj.notOwn).to.be.deep.equal({}); | ||
}); | ||
objectPath.withInheritedProps.empty(obj, 'notOwn') | ||
expect(proto.notOwn).to.be.deep.equal({}) | ||
expect(obj.notOwn).to.be.deep.equal({}) | ||
}) | ||
it('del should delete a not own property', function() { | ||
it('del should delete a not own property', function () { | ||
var proto = { | ||
notOwn: {a: 'a'} | ||
} | ||
var obj = Object.create(proto); | ||
var obj = Object.create(proto) | ||
objectPath.withInheritedProps.del(obj, 'notOwn.a'); | ||
expect(proto.notOwn).to.be.deep.equal({}); | ||
objectPath.withInheritedProps.del(obj, 'notOwn.a') | ||
expect(proto.notOwn).to.be.deep.equal({}) | ||
//expect(obj.notOwn).to.be.deep.equal({}); | ||
objectPath.withInheritedProps.del(obj, 'notOwn'); | ||
objectPath.withInheritedProps.del(obj, 'notOwn') | ||
//expect(proto).to.be.deep.equal({notOwn: {}}); | ||
//expect(obj).to.be.deep.equal({notOwn: {}}); | ||
}); | ||
}); | ||
}) | ||
}) |
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
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
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
48185
1164
184
9
1