Permalink
Browse files

readme

  • Loading branch information...
1 parent 3b426a3 commit c3c624e3643abeb20a0505ac6fdb45058e248538 @guybrush committed Jan 5, 2013
Showing with 6 additions and 43 deletions.
  1. +6 −43 README.md
View
@@ -1,48 +1,11 @@
# objpath
+create/access objects with a key-string
+
``` javascript
-/**
- * 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)
+var objpath = require('objpath')
+objpath(this, 'foo.bar', 'blub')
+assert.equal(this.foo.bar,'blub')
+assert.equal(objpath(this,'foo.bar'),'blub')
- 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
- }
-}
```

0 comments on commit c3c624e

Please sign in to comment.