/**
* create/access objects with a key-string
*
* objpath(this, 'foo.bar', 'blub')
* assert.equal(this.foo.bar,'blub')
* assert.equal(objpath(this,'foo.bar'),'blub')
*
* @param {Object} scope
* @param {String} key-string (e.g. 'a.b.c.d')
* @param {Object} value to store in key (optional)
* @return {Object} if value-param is given it will return
* the object, otherwise it will return the value
* of the selected key
*/
function objpath(obj, keyString, value) {
if (arguments.length<2) return false
keyString = keyString+''
var keys = keyString.split('.') || [keyString]
if (obj[keys[0]] === undefined) obj[keys[0]] = {}
var temp = obj[keys[0]]
, keys = keys.slice(1)
if (value === undefined) { // get data
var value = temp
for (var i=0, len=keys.length; i<len; i++) {
if (value === undefined) return false
value = value[keys[i]]
}
return value
} else { // set data
if (keys.length==0) {
obj[keyString] = value
return obj
}
for (var i=0, len=keys.length; i<len; i++) {
if (i==(len-1)) {
temp[keys[i]] = value
} else {
temp = temp[keys[i]]
}
}
return obj
}
}
Created
February 6, 2012 03:50
-
-
Save guybrush/1749439 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| node_modules | |
| *.~ | |
| *.swp | |
| *.marks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;(function(){ | |
| if (module && module.exports && module.exports) module.exports = objpath | |
| else (function () { return this })().objpath = objpath | |
| /** | |
| * create/access objects with a key-string | |
| * | |
| * objpath(this, 'foo.bar', 'blub') | |
| * assert.equal(this.foo.bar,'blub') | |
| * assert.equal(objpath(this,'foo.bar'),'blub') | |
| * | |
| * @param {Object} scope | |
| * @param {String} key-string (e.g. 'a.b.c.d') | |
| * @param {Object} value to store in key (optional) | |
| * @return {Object} if value-param is given it will return | |
| * the object, otherwise it will return the value | |
| * of the selected key | |
| */ | |
| function objpath(obj, keyString, value) { | |
| if (arguments.length<2) return false | |
| keyString = keyString+'' | |
| var keys = keyString.split('.') || [keyString] | |
| if (obj[keys[0]] === undefined) obj[keys[0]] = {} | |
| var temp = obj[keys[0]] | |
| , keys = keys.slice(1) | |
| if (value === undefined) { // get data | |
| var value = temp | |
| for (var i=0, len=keys.length; i<len; i++) { | |
| if (value === undefined) return false | |
| value = value[keys[i]] | |
| } | |
| return value | |
| } else { // set data | |
| if (keys.length==0) { | |
| obj[keyString] = value | |
| return obj | |
| } | |
| for (var i=0, len=keys.length; i<len; i++) { | |
| if (i==(len-1)) { | |
| temp[keys[i]] = value | |
| } else { | |
| temp = temp[keys[i]] | |
| } | |
| } | |
| return obj | |
| } | |
| } | |
| })() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "objpath", | |
| "version": "0.0.4", | |
| "main": "./objpath.js", | |
| "scripts": { | |
| "test": "node ./test.js" | |
| }, | |
| "repository": { | |
| "type": "git", | |
| "url": "git://github.com/guybrush/objpath.git" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var assert = require('assert') | |
| var op = require('./') | |
| var result = op(this, 'foo.bar', 'blub') | |
| assert.equal(this.foo.bar,'blub') | |
| assert.equal(op(this,'foo.bar'),'blub') | |
| var result = op({},'a','a') | |
| assert.deepEqual(result,{a:'a'}) | |
| var obj = {a:{aa:'aa'}} | |
| var result = op(obj,'a.ab','ab') | |
| assert.deepEqual(result,{a:{aa:'aa',ab:'ab'}}) | |
| assert.deepEqual(obj,result) | |
| var obj = {a:{aa:undefined}} | |
| var result = op(obj,'a.ab',0) | |
| var result = op(obj,'a.ac',false) | |
| assert.deepEqual(result,{a:{aa:undefined,ab:0,ac:false}}) | |
| assert.equal(op(obj,'a.aa'),undefined) | |
| assert.equal(op(obj,'a.ac'),false) | |
| assert.deepEqual(obj,result) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment