This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
field/lib/field.js /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
80 lines (70 sloc)
2.08 KB
This file contains 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 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 = split(fields) | |
| 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 = split(fields) | |
| 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) | |
| } | |
| // hacky way to split '../domwindow:localStorage.getItem' | |
| // i.e. we want all '.' after ':' | |
| function split (str) { | |
| if (str.indexOf(':') >= 0) { | |
| var parts = str.split(':') | |
| var m = parts.shift() | |
| parts = parts.join(':').split(FIELD_REGEX) | |
| parts.unshift(m) | |
| return parts | |
| } else { | |
| return str.split(FIELD_REGEX) | |
| } | |
| } | |
| module.exports = { | |
| get: get, | |
| set: set | |
| } |