Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "hasprop", | ||
"main": "hasprop.js", | ||
"version": "0.0.1", | ||
"version": "0.0.4", | ||
"homepage": "https://github.com/miguelmota/hasprop", | ||
@@ -9,3 +9,3 @@ "authors": [ | ||
], | ||
"description": "Check if object has property", | ||
"description": "Check if nested object has property", | ||
"keywords": [ | ||
@@ -12,0 +12,0 @@ "property", |
# Change Log | ||
All notable changes to this project will be documented in this file. | ||
## [0.0.4] - 2016-01-05 | ||
### Added | ||
- Partial application support and array as path support. | ||
(function(){ | ||
function hasProp(obj, path) { | ||
if (arguments.length === 1 && (typeof obj === 'object' || obj instanceof Object)) { | ||
return function(path) { | ||
return hasProp(obj, path); | ||
}; | ||
} | ||
function hasprop(o, s) { | ||
if (!o || !s) return false; | ||
if (!(typeof o === 'object' || o instanceof Object)) return false; | ||
if (!(typeof s === 'string' || s instanceof String)) return false; | ||
var props = s.split('.'), | ||
last = props[props.length - 1], | ||
i = 0, | ||
head = o; | ||
if (!(typeof obj === 'object' || obj instanceof Object) || obj === null) { | ||
return false; | ||
} | ||
for (i = 0; i < props.length; i += 1) { | ||
if (!head[props[i]]) return false; | ||
var props = []; | ||
if (Array.isArray(path)) { | ||
props = path; | ||
} else { | ||
if (!(typeof path === 'string' || path instanceof String)) { | ||
return false; | ||
} | ||
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 false; | ||
} | ||
head = head[props[i]]; | ||
if (typeof head !== 'undefined') { | ||
if (props[i] === last) { | ||
if (props[i] === last && i === size - 1) { | ||
return true; | ||
@@ -27,9 +50,9 @@ } | ||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { | ||
module.exports = hasprop; | ||
module.exports = hasProp; | ||
} else if (typeof exports !== 'undefined') { | ||
exports.hasprop = hasprop; | ||
exports.hasProp = hasProp; | ||
} else { | ||
window.hasprop = hasprop; | ||
window.hasProp = hasProp; | ||
} | ||
})(); |
{ | ||
"name": "hasprop", | ||
"version": "0.0.3", | ||
"description": "Check if object has property", | ||
"version": "0.0.4", | ||
"description": "Check if nested object has property", | ||
"main": "hasprop.js", | ||
@@ -6,0 +6,0 @@ "directories": { |
# hasprop | ||
Check if object has property | ||
Check if nested object has property, the easy way. | ||
Antiquated way: | ||
```javascript | ||
var exists = Boolean( | ||
obj && | ||
obj.qux && | ||
obj.qux.zee && | ||
obj.qux.zee.peep && | ||
obj.qux.zee.peep[2] && | ||
obj.qux.zee.peep[2].__data); | ||
``` | ||
with hasProp: | ||
```javascript | ||
var exists = hasProp(obj, ['qux', 'zee', 'peep', 2, '__data']); | ||
``` | ||
# Install | ||
@@ -16,28 +34,66 @@ | ||
var foo = { | ||
bar: { | ||
baz: { | ||
qux: 'corge', | ||
c: [1,2] | ||
var obj = { | ||
foo: 'bar', | ||
qux: { | ||
zee: { | ||
boop: 4, | ||
peep: [55,'zonk', { | ||
__data: 'pow' | ||
}], | ||
}, | ||
garphly: [{a:'b'}] | ||
'key.with.dots': 'hello', | ||
'"key.with.quotes"': { | ||
greet: 'hi' | ||
}, | ||
$el: 'element' | ||
}, | ||
fred: 'plugh' | ||
'foo.bar': 'noob', | ||
qax: null | ||
}; | ||
hasProp(foo, 'bar') // true | ||
hasProp(foo, 'foo') // false | ||
hasProp(foo, 'bar.baz') // true | ||
hasProp(foo, 'bar.baz.grault') // false | ||
hasProp(foo, 'fred') // true | ||
hasProp(foo, 'bar.garphly.0') // true | ||
hasProp(foo, 'bar.garphly.0.a') // true | ||
hasProp(foo, 'bar.baz.garphly.1') // false | ||
hasProp(foo, 'bar.baz.corge.garphly.5') // false | ||
hasProp(foo, 'bar.baz.c.1') // true | ||
hasProp(foo, 'bar.baz.c.2') // false | ||
// array for path (recommended) | ||
hasProp(obj, ['foo']) // true | ||
hasProp(obj, ['deedee']) // false | ||
hasProp(obj, ['qux', 'zee', 'boop']) // true | ||
hasProp(obj, ['qux', 'zee', 'peep', 0]) // true | ||
hasProp(obj, ['qux', 'zee', 'peep', 1]) // true | ||
hasProp(obj, ['qux', 'key.with.dots']) // true | ||
hasProp(obj, ['qux', '"key.with.quotes"', 'greet']) // true | ||
hasProp(obj, ['qux', 'zee', 'peep', 2]) // true | ||
hasProp(obj, ['qux', 'zee', 'peep', 2, '__data']) // true | ||
hasProp(obj, ['qux', '$el']) // true | ||
hasProp(obj, ['foo.bar']) // true | ||
hasProp(obj, ['qux', 'qux']) // false | ||
// string for path | ||
hasProp(obj, 'foo') // true | ||
hasProp(obj, 'deedee') // false | ||
hasProp(obj, 'qux.zee.boop') // true | ||
hasProp(obj, 'qux.zee.peep.0') // true | ||
hasProp(obj, 'qux.zee.peep.1') // true | ||
hasProp(obj, 'qux.zee.peep[1]') // true | ||
hasProp(obj, 'qux[key.with.dots]') // true | ||
hasProp(obj, 'qux["key.with.quotes"].greet') // true | ||
hasProp(obj, 'qux.zee.peep.2') // true | ||
hasProp(obj, 'qux.zee.peep.2.__data') // true | ||
hasProp(obj, 'qux.$el') // true | ||
hasProp(obj, '[foo.bar]') // true | ||
hasProp(obj, 'qux.qux') // false | ||
``` | ||
Partially applied: | ||
``` | ||
var objHasProp = hasProp(obj); | ||
objHasProp(['foo']) // true | ||
objHasProp('[foo.bar']) // true | ||
objHasProp(['qux']) // true | ||
objHasProp(['yo']) // false | ||
``` | ||
For getting the value, check out the module [getprop](https://github.com/miguelmota/getprop). | ||
# License | ||
MIT |
var test = require('tape'); | ||
var hasProp = require('../hasprop'); | ||
test('has prop', function (t) { | ||
t.plan(15); | ||
test('hasProp', function (t) { | ||
t.plan(47); | ||
var foo = { | ||
var obj = { | ||
foo: 'bar', | ||
qux: { | ||
zee: { | ||
boop: 'yo', | ||
num: 4, | ||
peep: [55,'zonk', { | ||
__data: 'pow' | ||
}], | ||
}, | ||
'key.with.dots': 'hello', | ||
'"key.with.quotes"': { | ||
greet: 'hi' | ||
}, | ||
$el: 'element' | ||
}, | ||
bar: { | ||
baz: { | ||
qux: 'corge', | ||
c: [1,2] | ||
}, | ||
garphly: [{a:'b'}] | ||
bar: 9 | ||
} | ||
}, | ||
fred: 'plugh' | ||
'foo.bar': 'noob', | ||
qax: null | ||
}; | ||
t.equal(hasProp(), false); | ||
t.equal(hasProp(''), false); | ||
t.equal(hasProp(foo, ''), false); | ||
t.equal(hasProp('{}'), false); | ||
t.equal(hasProp(foo, 'bar'), true); | ||
t.equal(hasProp(foo, 'foo'), false); | ||
t.equal(hasProp(foo, 'bar.baz'), true); | ||
t.equal(hasProp(foo, 'bar.baz.grault'), false); | ||
t.equal(hasProp(foo, 'fred'), true); | ||
t.equal(hasProp(foo, 'bar.garphly.0'), true); | ||
t.equal(hasProp(foo, 'bar.garphly.0.a'), true); | ||
t.equal(hasProp(foo, 'bar.baz.garphly.1'), false); | ||
t.equal(hasProp(foo, 'bar.baz.corge.garphly.5'), false); | ||
t.equal(hasProp(foo, 'bar.baz.c.1'), true); | ||
t.equal(hasProp(foo, 'bar.baz.c.2'), false); | ||
t.equal(hasProp(obj, 'foo'), true); | ||
t.equal(hasProp(obj, 'deedee'), false); | ||
t.equal(hasProp(obj, 'qux.zee.boop'), true); | ||
t.equal(hasProp(obj, 'qux.zee.num'), true); | ||
t.equal(hasProp(obj, 'qux.zee.peep.0'), true); | ||
t.equal(hasProp(obj, 'qux.zee.peep.1'), true); | ||
t.equal(hasProp(obj, 'qux.zee.peep[1]'), true); | ||
t.equal(hasProp(obj, 'qux[key.with.dots]'), true); | ||
t.equal(hasProp(obj, 'qux["key.with.quotes"].greet'), true); | ||
t.deepEqual(hasProp(obj, 'qux.zee.peep.2'), true); | ||
t.equal(hasProp(obj, 'qux.zee.peep.2.__data'), true); | ||
t.equal(hasProp(obj, 'qux.$el'), true); | ||
t.equal(hasProp(obj, 'bar.baz.bar'), true); | ||
t.equal(hasProp(obj, 'bar.baz.bar.nope'), false); | ||
t.equal(hasProp(obj, ''), false); | ||
t.equal(hasProp(obj, {}), false); | ||
t.equal(hasProp(obj, 3), false); | ||
t.equal(hasProp(obj, 'nah.1.0'), false); | ||
t.equal(hasProp('foo.bar', ''), false); | ||
t.equal(hasProp(obj, '[foo.bar]'), true); | ||
t.equal(hasProp(obj, 'qax.quz'), false); | ||
t.equal(hasProp(null, 'yo'), false); | ||
t.equal(hasProp(null, 'yo'), false); | ||
t.equal(hasProp(obj, ['deedee']), false); | ||
t.equal(hasProp(obj, ['qux', 'zee', 'boop']), true); | ||
t.equal(hasProp(obj, ['qux', 'zee', 'num']), true); | ||
t.equal(hasProp(obj, ['qux', 'zee', 'peep', 0]), true); | ||
t.equal(hasProp(obj, ['qux', 'zee', 'peep', '1']), true); | ||
t.equal(hasProp(obj, ['qux', 'key.with.dots']), true); | ||
t.equal(hasProp(obj, ['qux', '"key.with.quotes"', 'greet']), true); | ||
t.deepEqual(hasProp(obj, ['qux', 'zee', 'peep', 2]), true); | ||
t.equal(hasProp(obj, ['qux', 'zee', 'peep', 2, '__data']), true); | ||
t.equal(hasProp(obj, ['qux', '$el']), true); | ||
t.equal(hasProp(obj, ['bar', 'baz', 'bar']), true); | ||
t.equal(hasProp(obj, ['bar', 'baz', 'bar', 'nope']), false); | ||
t.equal(hasProp(obj, ['nah', 1, 0]), false); | ||
t.equal(hasProp(obj, ['foo.bar']), true); | ||
t.equal(hasProp(obj, ['qax', 'quz']), false); | ||
t.equal(hasProp(null, ['yo']), false); | ||
t.equal(hasProp(null, ['yo']), false); | ||
var objHasProp = hasProp(obj); | ||
t.equal(objHasProp('foo'), true); | ||
t.equal(objHasProp('[foo.bar]'), true); | ||
t.equal(objHasProp('yo'), false); | ||
t.equal(objHasProp(['foo']), true); | ||
t.equal(objHasProp(['foo.bar']), true); | ||
t.equal(objHasProp(['yo']), false); | ||
// oldschool | ||
var exists = Boolean( | ||
obj && | ||
obj.qux && | ||
obj.qux.zee && | ||
obj.qux.zee.peep && | ||
obj.qux.zee.peep[1]); | ||
t.equal(exists, true); | ||
}); |
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
10329
152
99