Comparing version 0.0.8 to 0.0.9
{ | ||
"name": "getprop", | ||
"main": "getprop.js", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"homepage": "https://github.com/miguelmota/getprop", | ||
@@ -6,0 +6,0 @@ "authors": [ |
@@ -8,1 +8,6 @@ # Change Log | ||
- Partial application support. | ||
## [0.0.9] - 2016-01-04 | ||
### Added | ||
- Array as path support. | ||
(function(root) { | ||
function getProp(o, s, d) { | ||
if (arguments.length === 1 && (typeof o === 'object' || o instanceof Object)) { | ||
return function(s, d) { | ||
return getProp(o, s, d); | ||
function getProp(obj, path, defaultValue) { | ||
if (arguments.length === 1 && (typeof obj === 'object' || obj instanceof Object)) { | ||
return function(path, defaultValue) { | ||
return getProp(obj, path, defaultValue); | ||
}; | ||
} | ||
if (!(typeof o === 'object' || o instanceof Object) || o === null) 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 len = props.length, | ||
last = props[len - 1], | ||
i = 0, | ||
head = o; | ||
if (!(typeof obj === 'object' || obj instanceof Object) || obj === null) { | ||
return defaultValue; | ||
} | ||
for (i = 0; i < len; i += 1) { | ||
var props = []; | ||
if (Array.isArray(path)) { | ||
props = path; | ||
} else { | ||
if (!(typeof path === 'string' || path instanceof String)) { | ||
return defaultValue; | ||
} | ||
props = (path.match(/(\[(.*?)\]|[0-9a-zA-Z_$]+)/gi)||props).map(function(match) { | ||
return match.replace(/[\[\]]/gi,''); | ||
}); | ||
} | ||
var size = props.length; | ||
var last = props[size - 1]; | ||
var head = obj; | ||
for (var i = 0; i < size; i += 1) { | ||
if (typeof head[props[i]] === 'undefined' || | ||
head[props[i]] === null) { | ||
return d; | ||
return defaultValue; | ||
} | ||
head = head[props[i]]; | ||
if (typeof head !== 'undefined') { | ||
if (props[i] === last && i === len - 1) { | ||
if (props[i] === last && i === size - 1) { | ||
return head; | ||
@@ -32,3 +45,4 @@ } | ||
} | ||
return d; | ||
return defaultValue; | ||
} | ||
@@ -35,0 +49,0 @@ |
{ | ||
"name": "get-prop", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Get a property from object", | ||
@@ -5,0 +5,0 @@ "main": "getprop.js", |
@@ -62,2 +62,18 @@ # getprop | ||
// array for path (recommended) | ||
getProp(obj, ['foo']) // 'bar' | ||
getProp(obj, ['deedee']) // undefined | ||
getProp(obj, ['deedee'], "I'm default value") // "I'm default value" | ||
getProp(obj, ['qux', 'zee', 'boop']) // 'yo' | ||
getProp(obj, ['qux', 'zee', 'peep', 0]) // 55 | ||
getProp(obj, ['qux', 'zee', 'peep', 1]) // 'zonk' | ||
getProp(obj, ['qux', 'key.with.dots']) // 'hello' | ||
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' | ||
getProp(obj, ['qux', 'qux']) // undefined | ||
// string for path | ||
getProp(obj, 'foo') // 'bar' | ||
@@ -64,0 +80,0 @@ getProp(obj, 'deedee') // undefined |
@@ -5,3 +5,3 @@ var test = require('tape'); | ||
test('getProp', function (t) { | ||
t.plan(28); | ||
t.plan(45); | ||
@@ -57,2 +57,19 @@ var obj = { | ||
t.equal(getProp(null, 'yo', 'wut'), 'wut'); | ||
t.equal(getProp(obj, ['deedee']), undefined); | ||
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); | ||
t.equal(getProp(obj, ['qux', 'zee', 'peep', '1']), 'zonk'); | ||
t.equal(getProp(obj, ['qux', 'key.with.dots']), 'hello'); | ||
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, ['bar', 'baz', 'bar', 'nope']), undefined); | ||
t.equal(getProp(obj, ['nah', 1, 0]), undefined); | ||
t.equal(getProp(obj, ['foo.bar']), 'noob'); | ||
t.equal(getProp(obj, ['qax', 'quz']), undefined); | ||
t.equal(getProp(null, ['yo']), undefined); | ||
t.equal(getProp(null, ['yo'], 'wut'), 'wut'); | ||
@@ -59,0 +76,0 @@ var objProp = getProp(obj); |
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
10911
158
108