object-path
Advanced tools
Comparing version 0.1.0 to 0.1.2
33
index.js
@@ -32,14 +32,17 @@ | ||
var objectPath = module.exports = {}; | ||
objectPath.set = function(obj, path, value) { | ||
if(!path) { | ||
return; | ||
function set(obj, path, value, doNotReplace) { | ||
if(isEmpty(path)) { | ||
return obj; | ||
} | ||
if(isString(path)) { | ||
objectPath.set(obj, path.split('.'), value); | ||
return; | ||
return set(obj, path.split('.'), value, doNotReplace); | ||
} | ||
var currentPath = isNaN(parseInt(path[0])) ? path[0] : parseInt(path[0]); | ||
if(path.length === 1) { | ||
obj[currentPath] = value; | ||
var oldVal = obj[currentPath]; | ||
if(oldVal === void 0 || !doNotReplace) { | ||
obj[currentPath] = value; | ||
} | ||
return oldVal; | ||
} else if (path.length > 1) { | ||
@@ -53,6 +56,20 @@ if(obj[currentPath] === void 0) { | ||
} | ||
objectPath.set(obj[currentPath], path.slice(1), value); | ||
return set(obj[currentPath], path.slice(1), value, doNotReplace); | ||
} | ||
return undefined; | ||
} | ||
var objectPath = module.exports = {}; | ||
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.push = function(obj, path /*, values */) { | ||
@@ -59,0 +76,0 @@ var arr = objectPath.get(obj, path); |
{ | ||
"name": "object-path", | ||
"description": "Access deep properties using a path", | ||
"version": "0.1.0", | ||
"version": "0.1.2", | ||
"author": { | ||
@@ -15,7 +15,41 @@ "name": "Mario Casciaro" | ||
"mocha": "~1.13.0", | ||
"chai": "~1.8.0" | ||
"chai": "~1.8.0", | ||
"mocha-lcov-reporter": "~0.0.1", | ||
"coveralls": "~2.3.0", | ||
"istanbul": "~0.1.44" | ||
}, | ||
"scripts": { | ||
"test": "node_modules/.bin/mocha test.js --reporter spec" | ||
"test": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha test.js --report html -- -R spec" | ||
}, | ||
"testling": { | ||
"browsers": { | ||
"ie": [ | ||
8, | ||
9 | ||
], | ||
"firefox": [ | ||
10, | ||
24 | ||
], | ||
"chrome": [ | ||
30, | ||
22 | ||
], | ||
"safari": [ | ||
5, | ||
6 | ||
], | ||
"opera": [ | ||
15 | ||
], | ||
"iphone": [ | ||
6 | ||
], | ||
"android-browser": [ | ||
4.2 | ||
] | ||
}, | ||
"harness": "mocha", | ||
"files": "test.js" | ||
}, | ||
"keywords": [ | ||
@@ -22,0 +56,0 @@ "deep", |
@@ -11,3 +11,6 @@ | ||
[![Build Status](https://travis-ci.org/mariocasciaro/object-path.png)](https://travis-ci.org/mariocasciaro/object-path) | ||
[![Coverage Status](https://coveralls.io/repos/mariocasciaro/object-path/badge.png)](https://coveralls.io/r/mariocasciaro/object-path) | ||
[![browser support](https://ci.testling.com/mariocasciaro/object-path.png)](https://ci.testling.com/mariocasciaro/object-path) | ||
## Usage | ||
@@ -39,5 +42,12 @@ | ||
//push into arrays | ||
//push into arrays (and create intermediate objects/arrays) | ||
objectPath.push(obj, "a.k", "o"); | ||
``` | ||
//Ensure a path exists (if it doesn't, set the default value you provide) | ||
objectPath.ensureExists(obj, "a.k.1", "DEFAULT"); | ||
``` | ||
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/mariocasciaro/object-path/trend.png)](https://bitdeli.com/free "Bitdeli Badge") |
19
test.js
@@ -89,2 +89,21 @@ var expect = require('chai').expect, | ||
}); | ||
}); | ||
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"); | ||
}); | ||
it('should return the object if path is empty', function() { | ||
var obj = getTestObj(); | ||
expect(objectPath.ensureExists(obj, [], "test")).to.have.property('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
9051
169
51
5