object-path
Advanced tools
Comparing version 0.1.3 to 0.2.0
211
index.js
@@ -0,107 +1,154 @@ | ||
(function (root, factory){ | ||
'use strict'; | ||
function isEmpty(value) { | ||
if(!value) { | ||
return true; | ||
/*istanbul ignore next:cant test*/ | ||
if (typeof module === 'object' && typeof module.exports === 'object') { | ||
module.exports = factory(); | ||
} else if (typeof define === 'function' && define.amd) { | ||
// AMD. Register as an anonymous module. | ||
define([], factory); | ||
} else { | ||
// Browser globals | ||
root.objectPath = factory(); | ||
} | ||
if(isArray(value) && value.length === 0) { | ||
return true; | ||
} else { | ||
for(var i in value) { | ||
if(Object.prototype.hasOwnProperty.call(value, i)) { | ||
return false; | ||
})(this, function(){ | ||
'use strict'; | ||
function isEmpty(value){ | ||
if (!value) { | ||
return true; | ||
} | ||
if (isArray(value) && value.length === 0) { | ||
return true; | ||
} else { | ||
for (var i in value) { | ||
if (Object.prototype.hasOwnProperty.call(value, i)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return true; | ||
} | ||
} | ||
function isNumber(value) { | ||
return typeof value == 'number' || Object.prototype.toString.call(value) == "[object Number]"; | ||
} | ||
function isNumber(value){ | ||
return typeof value == 'number' || Object.prototype.toString.call(value) == "[object Number]"; | ||
} | ||
function isString(obj) { | ||
return typeof obj == 'string' || Object.prototype.toString.call(obj) == "[object String]"; | ||
} | ||
function isString(obj){ | ||
return typeof obj == 'string' || Object.prototype.toString.call(obj) == "[object String]"; | ||
} | ||
function isArray(obj) { | ||
return typeof obj == 'object' && typeof obj.length == 'number' && | ||
function isArray(obj){ | ||
return typeof obj == 'object' && typeof obj.length == 'number' && | ||
Object.prototype.toString.call(obj) == '[object Array]'; | ||
} | ||
} | ||
function getKey(key) { | ||
var intKey = parseInt(key); | ||
if (intKey.toString() === key) { | ||
return intKey; | ||
function getKey(key){ | ||
var intKey = parseInt(key); | ||
if (intKey.toString() === key) { | ||
return intKey; | ||
} | ||
return key; | ||
} | ||
return key; | ||
} | ||
function set(obj, path, value, doNotReplace) { | ||
if(isEmpty(path)) { | ||
return obj; | ||
} | ||
if(isString(path)) { | ||
return set(obj, path.split('.'), value, doNotReplace); | ||
} | ||
var currentPath = getKey(path[0]); | ||
if(path.length === 1) { | ||
var oldVal = obj[currentPath]; | ||
if(oldVal === void 0 || !doNotReplace) { | ||
obj[currentPath] = value; | ||
function set(obj, path, value, doNotReplace){ | ||
if (isEmpty(path)) { | ||
return obj; | ||
} | ||
return oldVal; | ||
} else if (path.length > 1) { | ||
if(obj[currentPath] === void 0) { | ||
if(isNumber(currentPath)) { | ||
obj[currentPath] = []; | ||
} else { | ||
obj[currentPath] = {}; | ||
if (isString(path)) { | ||
return set(obj, path.split('.'), value, doNotReplace); | ||
} | ||
var currentPath = getKey(path[0]); | ||
if (path.length === 1) { | ||
var oldVal = obj[currentPath]; | ||
if (oldVal === void 0 || !doNotReplace) { | ||
obj[currentPath] = value; | ||
} | ||
return oldVal; | ||
} else if (path.length > 1) { | ||
if (obj[currentPath] === void 0) { | ||
if (isNumber(currentPath)) { | ||
obj[currentPath] = []; | ||
} else { | ||
obj[currentPath] = {}; | ||
} | ||
} | ||
return set(obj[currentPath], path.slice(1), value, doNotReplace); | ||
} | ||
return set(obj[currentPath], path.slice(1), value, doNotReplace); | ||
return undefined; | ||
} | ||
return undefined; | ||
} | ||
function del(obj, path) { | ||
if (isEmpty(path)) { | ||
return obj; | ||
} | ||
if (isEmpty(obj)) { | ||
return undefined; | ||
} | ||
if(isString(path)) { | ||
return del(obj, path.split('.')); | ||
} | ||
var currentPath = getKey(path[0]); | ||
var oldVal = obj[currentPath]; | ||
if(path.length === 1) { | ||
if (oldVal !== void 0) { | ||
if (isArray(obj[currentPath])) { | ||
obj.splice(currentPath, 1); | ||
} else { | ||
delete obj[currentPath]; | ||
} | ||
} | ||
} else if (path.length > 1) { | ||
if (obj[currentPath] !== void 0) { | ||
return del(obj[currentPath], path.slice(1)); | ||
} | ||
} | ||
var objectPath = module.exports = {}; | ||
return obj; | ||
} | ||
var objectPath = {}; | ||
objectPath.ensureExists = function(obj, path, value) { | ||
return set(obj, path, value, true); | ||
}; | ||
objectPath.ensureExists = function (obj, path, value){ | ||
return set(obj, path, value, true); | ||
}; | ||
objectPath.set = function(obj, path, value, doNotReplace) { | ||
return set(obj, path, value, doNotReplace); | ||
}; | ||
objectPath.set = function (obj, path, value, doNotReplace){ | ||
return set(obj, path, value, doNotReplace); | ||
}; | ||
objectPath.push = function (obj, path /*, values */){ | ||
var arr = objectPath.get(obj, path); | ||
if (!isArray(arr)) { | ||
arr = []; | ||
objectPath.set(obj, path, arr); | ||
} | ||
var args = Array.prototype.slice.call(arguments, 2); | ||
arr.push.apply(arr, args); | ||
}; | ||
objectPath.push = function(obj, path /*, values */) { | ||
var arr = objectPath.get(obj, path); | ||
if(!isArray(arr)) { | ||
arr = []; | ||
objectPath.set(obj, path, arr); | ||
} | ||
var args = Array.prototype.slice.call(arguments, 2); | ||
arr.push.apply(arr, args); | ||
}; | ||
objectPath.get = function (obj, path){ | ||
if (isEmpty(path)) { | ||
return obj; | ||
} | ||
if (isEmpty(obj)) { | ||
return undefined; | ||
} | ||
if (isString(path)) { | ||
return objectPath.get(obj, path.split('.')); | ||
} | ||
var currentPath = getKey(path[0]); | ||
if (path.length === 1) { | ||
return obj[currentPath]; | ||
} | ||
return objectPath.get(obj[currentPath], path.slice(1)); | ||
}; | ||
objectPath.del = function(obj, path) { | ||
return del(obj, path); | ||
}; | ||
objectPath.get = function(obj, path) { | ||
if(isEmpty(path)) { | ||
return obj; | ||
} | ||
if(isEmpty(obj)) { | ||
return undefined; | ||
} | ||
if(isString(path)) { | ||
return objectPath.get(obj, path.split('.')); | ||
} | ||
var currentPath = getKey(path[0]); | ||
if(path.length === 1) { | ||
return obj[currentPath]; | ||
} | ||
return objectPath.get(obj[currentPath], path.slice(1)); | ||
}; | ||
return objectPath; | ||
}); |
{ | ||
"name": "object-path", | ||
"description": "Access deep properties using a path", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"author": { | ||
@@ -13,11 +13,14 @@ "name": "Mario Casciaro" | ||
}, | ||
"engines": { | ||
"node": ">=0.8.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.13.0", | ||
"chai": "~1.8.0", | ||
"mocha": "~1.18.2", | ||
"chai": "~1.9.1", | ||
"mocha-lcov-reporter": "~0.0.1", | ||
"coveralls": "~2.3.0", | ||
"istanbul": "~0.1.44" | ||
"coveralls": "~2.10.0", | ||
"istanbul": "~0.2.7" | ||
}, | ||
"scripts": { | ||
"test": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha test.js --report html -- -R spec" | ||
"test": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha test.js --report html -- -R spec" | ||
}, | ||
@@ -24,0 +27,0 @@ "testling": { |
@@ -12,2 +12,3 @@ | ||
[![Coverage Status](https://coveralls.io/repos/mariocasciaro/object-path/badge.png)](https://coveralls.io/r/mariocasciaro/object-path) | ||
[![devDependency Status](https://david-dm.org/mariocasciaro/object-path/dev-status.svg)](https://david-dm.org/mariocasciaro/object-path#info=devDependencies) | ||
@@ -49,5 +50,2 @@ [![browser support](https://ci.testling.com/mariocasciaro/object-path.png)](https://ci.testling.com/mariocasciaro/object-path) | ||
``` | ||
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/mariocasciaro/object-path/trend.png)](https://bitdeli.com/free "Bitdeli Badge") | ||
``` |
23
test.js
@@ -126,1 +126,24 @@ var expect = require('chai').expect, | ||
}); | ||
describe('del', function(){ | ||
it('should delete deep paths', function(){ | ||
var obj = getTestObj(); | ||
objectPath.set(obj, 'b.g.1.1', "test"); | ||
objectPath.set(obj, 'b.g.1.2', "test"); | ||
expect(obj).to.have.deep.property("b.g.1.1","test"); | ||
expect(obj).to.have.deep.property("b.g.1.2","test"); | ||
expect(objectPath.del(obj)).to.be.equal(obj); | ||
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.2","test"); | ||
objectPath.del(obj, ['b','g','1','2']); | ||
expect(obj).to.not.have.deep.property("b.g.1.2"); | ||
expect(obj).to.have.deep.property("b.g.1"); | ||
expect(objectPath.del(obj, ['b'])).to.not.have.deep.property("b.g"); | ||
expect(obj).to.be.deep.equal({'a':'b'}); | ||
}); | ||
}); |
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
12016
253
49