Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
| var assert = require('assert') | |
| var FIELD_REGEX = /\.|\:/ | |
| function get (topObj, fields) { | |
| if (typeof topObj === 'string' && arguments.length === 1) { | |
| fields = topObj | |
| topObj = this // hopefully using bind | |
| } | |
| assert(typeof fields === 'string', 'field.get(): must pass in a field string.') | |
| fields = fields.split(FIELD_REGEX) | |
| function moveUp (obj, field) { | |
| if (typeof obj[field] !== 'undefined') { // we care about falsey values | |
| if (fields.length === 0) return obj[field] | |
| else return moveUp(obj[field], fields.shift()) | |
| } else { | |
| return undefined | |
| } | |
| } | |
| return moveUp(topObj, fields.shift()) | |
| } | |
| function set (topObj, fields, value) { | |
| if (typeof topObj === 'string' && arguments.length === 2) { | |
| value = fields | |
| fields = topObj | |
| topObj = this // hopefully using bind | |
| } | |
| assert(typeof fields === 'string', 'field.get(): must pass in a field string.') | |
| fields = fields.split(FIELD_REGEX) | |
| function moveUp (obj, field, value) { | |
| if (typeof obj[field] !== 'undefined') { // we care about falsey values | |
| if (fields.length === 0) { | |
| var oldVal = obj[field] | |
| obj[field] = value | |
| return oldVal | |
| } else { | |
| if (typeof obj[field] !== 'object') { // we have more fields to go, so we need to replace the current non-object | |
| obj[field] = {} | |
| } | |
| return moveUp(obj[field], fields.shift(), value) | |
| } | |
| } else { | |
| // keep going if necessary | |
| if (fields.length === 0) { | |
| obj[field] = value | |
| return undefined | |
| } else { | |
| // var newField = fields.shift() | |
| obj[field] = {}// {newField: value} | |
| return moveUp(obj[field], fields.shift(), value) | |
| } | |
| } | |
| } | |
| return moveUp(topObj, fields.shift(), value) | |
| } | |
| module.exports = { | |
| get: get, | |
| set: set | |
| } |