Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 97055a934cf100b0988cda8543db80cddc4c0638 0 parents
@guybrush authored
Showing with 125 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +54 −0 index.js
  3. +9 −0 package.json
  4. +58 −0 test.js
4 .gitignore
@@ -0,0 +1,4 @@
+node_modules
+*.~
+*.swp
+*.marks
54 index.js
@@ -0,0 +1,54 @@
+;(function(){
+
+if (module && module.exports && module.exports) module.exports = objpath
+else (function () { return this })().objpath = objpath
+
+/**
+ * create/access objects with a key-string
+ *
+ * with eval(): with objpath():
+ * ----------------------------- ----------------------------------
+ * var keyString = 'json.data' var keyString = 'json.data'
+ * , value = 'a value'; , value = 'a value';
+ * eval(keyString+' = '+value); objpath(this, keyString, value);
+ * alert(eval(keyString)); alert(objPath(this, keyString));
+ *
+ * @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
+ }
+}
+
+})()
9 package.json
@@ -0,0 +1,9 @@
+{ "name" : "objpath"
+, "version" : "0.0.0"
+, "scripts" : { "test" : "./node_modules/.bin/mocha ./test.js -u exports -R spec" }
+, "devDependencies" : { "mocha" : "0.11.x" }
+, "repository" :
+ { "type" : "git"
+ , "url" : ""
+ }
+}
58 test.js
@@ -0,0 +1,58 @@
+var assert = require('assert')
+ , op = require('./')
+
+module.exports =
+{ api:
+ { 'op({},"a","a")': function() {
+ var result = op({},'a','a')
+ assert.deepEqual(result,{a:'a'})
+ }
+ , 'op(obj,"a.ab","ab")': function() {
+ var obj = {a:{aa:'aa'}}
+ var result = op(obj,'a.ab','ab')
+ assert.deepEqual(result,{a:{aa:'aa',ab:'ab'}})
+ assert.deepEqual(obj,result)
+ }
+ , 'falsy values': function() {
+ 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.deepEqual(obj,result)
+ }
+ }
+, bench:
+ { 'flat':
+ { 'eval': function(done) {
+ var i = 10000
+ var x = {}
+ while(i--) eval('x['+i+'] = '+i)
+ assert.equal(x['0'],0)
+ done()
+ }
+ , 'op': function(done) {
+ var i = 10000
+ var x = {}
+ while(i--) op(x,i,i)
+ assert.equal(x['0'],0)
+ done()
+ }
+ }
+ , 'deep':
+ { 'eval': function(done) {
+ var i = 10000
+ var x = {a:{aa:{aaa:{aaaa:1}}},b:{ba:{baa:0}}}
+ while(i--) eval('x.a.aa.aaa.aaaa = '+i)
+ assert.equal(x.a.aa.aaa.aaaa,0)
+ done()
+ }
+ , 'op': function(done) {
+ var i = 10000
+ var x = {a:{aa:{aaa:{aaaa:1}}},b:{ba:{baa:0}}}
+ while(i--) op(x,'a.aa.aaa.aaaa',i)
+ assert.equal(x.a.aa.aaa.aaaa,0)
+ done()
+ }
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.