Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "getprop", | ||
"main": "getprop.js", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"homepage": "https://github.com/miguelmota/getprop", | ||
@@ -6,0 +6,0 @@ "authors": [ |
@@ -5,15 +5,16 @@ (function(root) { | ||
if (!o || !s) return; | ||
if (!(typeof o === 'object' || o instanceof Object)) return; | ||
if (!(typeof s === 'string' || s instanceof String)) return; | ||
if (!(typeof o === 'object' || o instanceof Object)) return d; | ||
if (!(typeof s === 'string' || s instanceof String)) return d; | ||
var props = s.match(/(\[(.*?)\]|[0-9a-zA-Z_$]+)/gi).map(function(m) { return m.replace(/[\[\]]/gi,''); }); | ||
var last = props[props.length - 1], | ||
var len = props.length, | ||
last = props[len - 1], | ||
i = 0, | ||
head = o; | ||
for (i = 0; i < props.length; i += 1) { | ||
if (!head[props[i]]) d; | ||
for (i = 0; i < len; i += 1) { | ||
if (typeof head[props[i]] === 'undefined') return d; | ||
head = head[props[i]]; | ||
if (typeof head !== 'undefined') { | ||
if (props[i] === last) { | ||
if (props[i] === last && i === len - 1) { | ||
return head; | ||
@@ -20,0 +21,0 @@ } |
{ | ||
"name": "get-prop", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Get a property from object", | ||
@@ -5,0 +5,0 @@ "main": "getprop.js", |
# getprop | ||
Get a property from object | ||
Get a property from nested object, the easy way. | ||
Old fashion way: | ||
```javascript | ||
var value = 'default value'; | ||
if ( | ||
obj && | ||
obj.qux && | ||
obj.qux.zee && | ||
obj.qux.zee.peep && | ||
obj.qux.zee.peep[2] && | ||
obj.qux.zee.peep[2].__data) { | ||
value = obj.qux.zee.peep[2].__data; | ||
} | ||
``` | ||
with getProp: | ||
```javascript | ||
var value = getProp(obj, 'qux.zee.peep.2.__data', 'default value'); | ||
``` | ||
# Install | ||
@@ -11,2 +34,6 @@ | ||
```bash | ||
bower install getprop | ||
``` | ||
# Usage | ||
@@ -21,3 +48,3 @@ | ||
zee: { | ||
boop: 'yo', | ||
boop: 4, | ||
peep: [55,'zonk', { | ||
@@ -33,2 +60,3 @@ __data: 'pow' | ||
}, | ||
'foo.bar': 'noob' | ||
}; | ||
@@ -45,4 +73,6 @@ | ||
getProp(obj, 'qux["key.with.quotes"].greet') // 'hi' | ||
getProp(obj, 'qux.zee.peep.2') // {__data: 'pow'} | ||
getProp(obj, 'qux.zee.peep.2.__data') // 'pow' | ||
getProp(obj, 'qux.$el') // 'element' | ||
getProp(obj, '[foo.bar]') // 'noob' | ||
``` | ||
@@ -49,0 +79,0 @@ |
@@ -5,3 +5,3 @@ var test = require('tape'); | ||
test('getProp', function (t) { | ||
t.plan(14); | ||
t.plan(21); | ||
@@ -13,2 +13,3 @@ var obj = { | ||
boop: 'yo', | ||
num: 4, | ||
peep: [55,'zonk', { | ||
@@ -24,2 +25,8 @@ __data: 'pow' | ||
}, | ||
bar: { | ||
baz: { | ||
bar: 9 | ||
} | ||
}, | ||
'foo.bar': 'noob' | ||
}; | ||
@@ -31,2 +38,3 @@ | ||
t.equal(getProp(obj, 'qux.zee.boop'), 'yo'); | ||
t.equal(getProp(obj, 'qux.zee.num'), 4); | ||
t.equal(getProp(obj, 'qux.zee.peep.0'), 55); | ||
@@ -37,7 +45,26 @@ t.equal(getProp(obj, 'qux.zee.peep.1'), 'zonk'); | ||
t.equal(getProp(obj, 'qux["key.with.quotes"].greet'), 'hi'); | ||
t.deepEqual(getProp(obj, 'qux.zee.peep.2'), {__data: 'pow'}); | ||
t.equal(getProp(obj, 'qux.zee.peep.2.__data'), 'pow'); | ||
t.equal(getProp(obj, 'qux.$el'), 'element'); | ||
t.equal(getProp(obj, 'bar.baz.bar'), 9); | ||
t.equal(getProp(obj, ''), undefined); | ||
t.equal(getProp(obj, {}), undefined); | ||
t.equal(getProp(obj, 3), undefined); | ||
t.equal(getProp(obj, 'nah.1.0'), undefined); | ||
t.equal(getProp('foo.bar', ''), undefined); | ||
t.equal(getProp(obj, '[foo.bar]'), 'noob'); | ||
// oldschool | ||
var value = 'default value'; | ||
if ( | ||
obj && | ||
obj.qux && | ||
obj.qux.zee && | ||
obj.qux.zee.peep && | ||
obj.qux.zee.peep[1]) { | ||
value = obj.qux.zee.peep[2].__data; | ||
} | ||
t.equal(value, 'pow'); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8040
115
79