cheprop
v1.0.0
Published
Checks if the set property exists in the object. Returns boolean or the value of the property
Downloads
3
Readme
cheprop :)
Description
Checks if the object property is existing and/or returns the value.
Syntax
var cheprop = require('cheprop');
cheprop(object,'path.to.object', [proertyType]);
object : object where the property is to be found.
'path.to.object' : string of property path (e.g. 'object.a.b.c' ).
propertyType: (optional) string indicating which type should property have.
Possible variants: 'array', 'null', 'nun', 'object', 'function', 'number', 'string'.
Installation
npm install cheprop
https://npmjs.org/package/cheprop
Source code available at: https://github.com/hakarapet/cheprop
Usage
NodeJS
const obj = {
aa: {
bbb: {
cccc: 'some string',
dddd: [1, 2, 3, 4, 5, 6],
eeee: {
key: 'value'
},
ffff: function () {},
gggg: 12345,
hhhh: null,
iiii: NaN
}
}
};
cheProp(obj, 'obj.aa.bbb')
Returns the value of the bbb if it exists in obj.aa
cheProp(obj, 'obj.aa.bbb', 'object')
Returns true if the bbb exists in obj.aa and the type is object
All other possible methods of getting the property are available.
'obj.aa.bbb'
'obj["aa"].bbb'
'obj["aa"]["bbb"]'
'obj["aa"]["bbb"].cccc'
'obj.aa["bbb"].cccc'
'obj.aa["bbb"].dddd'
'obj.aa["bbb"].eeee'
'obj.aa["bbb"].ffff'
'obj.aa["bbb"].gggg'
'obj.aa["bbb"].hhhh'
'obj.aa["bbb"].iiii'
'obj.aa["bbb"].eeee.key'
'obj.aa["bbb"].eeee["key"]'
'obj.aa["bbb"]["eeee"]["key"]'
'obj.aa["bbb"]["eeee"].key'
Test
npm test
Author
Hayk Karapetyan
[email protected]
License
MIT License
https://opensource.org/licenses/MIT