object-path
Advanced tools
Comparing version 0.4.0 to 0.5.0
57
index.js
@@ -17,2 +17,6 @@ (function (root, factory){ | ||
var | ||
toStr = Object.prototype.toString, | ||
_hasOwnProperty = Object.prototype.hasOwnProperty; | ||
function isEmpty(value){ | ||
@@ -26,3 +30,3 @@ if (!value) { | ||
for (var i in value) { | ||
if (Object.prototype.hasOwnProperty.call(value, i)) { | ||
if (_hasOwnProperty.call(value, i)) { | ||
return false; | ||
@@ -35,15 +39,26 @@ } | ||
function toString(type){ | ||
return toStr.call(type); | ||
} | ||
function isNumber(value){ | ||
return typeof value == 'number' || Object.prototype.toString.call(value) == "[object Number]"; | ||
return typeof value === 'number' || toString(value) === "[object Number]"; | ||
} | ||
function isString(obj){ | ||
return typeof obj == 'string' || Object.prototype.toString.call(obj) == "[object String]"; | ||
return typeof obj === 'string' || toString(obj) === "[object String]"; | ||
} | ||
function isObject(obj){ | ||
return typeof obj === 'object' && toString(obj) === "[object Object]"; | ||
} | ||
function isArray(obj){ | ||
return typeof obj == 'object' && typeof obj.length == 'number' && | ||
Object.prototype.toString.call(obj) == '[object Array]'; | ||
return typeof obj === 'object' && typeof obj.length === 'number' && toString(obj) === '[object Array]'; | ||
} | ||
function isBoolean(obj){ | ||
return typeof obj === 'boolean' || toString(obj) === '[object Boolean]'; | ||
} | ||
function getKey(key){ | ||
@@ -126,2 +141,34 @@ var intKey = parseInt(key); | ||
objectPath.empty = function(obj, path) { | ||
if (isEmpty(path)) { | ||
return obj; | ||
} | ||
if (isEmpty(obj)) { | ||
return void 0; | ||
} | ||
var value, i; | ||
if (!(value = objectPath.get(obj, path))) { | ||
return obj; | ||
} | ||
if (isString(value)) { | ||
return objectPath.set(obj, path, ''); | ||
} else if (isBoolean(value)) { | ||
return objectPath.set(obj, path, false); | ||
} else if (isNumber(value)) { | ||
return objectPath.set(obj, path, 0); | ||
} else if (isArray(value)) { | ||
value.length = 0; | ||
} else if (isObject(value)) { | ||
for (i in value) { | ||
if (_hasOwnProperty.call(value, i)) { | ||
delete value[i]; | ||
} | ||
} | ||
} else { | ||
return objectPath.set(obj, path, null); | ||
} | ||
}; | ||
objectPath.push = function (obj, path /*, values */){ | ||
@@ -128,0 +175,0 @@ var arr = objectPath.get(obj, path); |
{ | ||
"name": "object-path", | ||
"description": "Access deep properties using a path", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Mario Casciaro" |
@@ -47,4 +47,11 @@ | ||
//get the first non-undefined value | ||
objectPath.coalesce(obj, ['a.z', 'a.d', ['a','b']], 'default'); | ||
objectPath.coalesce(obj, ['a.z', 'a.d', ['a','b']], 'default'); | ||
//empty a given path (but do not delete it) depending on their type,so it retains reference to objects and arrays. | ||
//functions that are not inherited from prototype are set to null. | ||
//object instances are considered objects and just own property names are deleted | ||
objectPath.empty(obj, 'a.b'); // obj.a.b is now '' | ||
objectPath.empty(obj, 'a.c'); // obj.a.c is now [] | ||
objectPath.empty(obj, 'a'); // obj.a is now {} | ||
//works also with arrays | ||
@@ -74,2 +81,11 @@ objectPath.get(obj, "a.c.1"); //returns "f" | ||
``` | ||
``` | ||
### Credits | ||
[Mario Casciaro](https://github.com/mariocasciaro) - Author | ||
[Paulo Cesar](https://github.com/pocesar) - Major contributor | ||
69
test.js
@@ -255,2 +255,71 @@ var expect = require('chai').expect, | ||
describe('empty', function(){ | ||
it('should ignore invalid arguments safely', function(){ | ||
var obj = {}; | ||
expect(objectPath.empty()).to.equal(undefined); | ||
expect(objectPath.empty(obj, 'path')).to.equal(undefined); | ||
expect(objectPath.empty(obj, '')).to.equal(obj); | ||
obj.path = true; | ||
expect(objectPath.empty(obj, 'inexistant')).to.equal(obj); | ||
}); | ||
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(){}; | ||
/*istanbul ignore next: not part of code */ | ||
Instance.prototype.arr = []; | ||
var | ||
obj = { | ||
string: 'some string', | ||
array: ['some','array',[1,2,3]], | ||
number: 21, | ||
boolean: true, | ||
object: { | ||
some:'property', | ||
sub: { | ||
'property': true | ||
} | ||
}, | ||
instance: new Instance() | ||
}; | ||
/*istanbul ignore next: not part of code */ | ||
obj['function'] = function(){}; | ||
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, 'instance.test'); | ||
expect(obj.instance.test).to.equal(null); | ||
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'); | ||
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(){ | ||
@@ -257,0 +326,0 @@ it('should return undefined on empty object', function(){ |
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
21461
511
90