cheprop

1.0.0 • Public • Published

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
hakarapet@gmail.com

License

MIT License
https://opensource.org/licenses/MIT

Package Sidebar

Install

npm i cheprop

Weekly Downloads

0

Version

1.0.0

License

MIT

Last publish

Collaborators

  • hakarapet