Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including cheprop with all npm packages installed. Try it out:

var cheprop = require("cheprop")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

cheprop v1.0.0

Checks if the set property exists in the object. Returns boolean or the value of the property

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

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free