1.2.1 • Published 4 years ago

propexists v1.2.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

propexists npm Package

Quick check if a nested property exists

API

propexists(obj, property, options)

obj

Type: Object
Target object

property

Type: String | Array
Property path string 'a.b.c.d' or an array ['a', 'b', 'c', 'd']

options.value

Type: Boolean
If true, the property's value will be returned

options.fallback

Type: Any
Return this value if the property is not found

@return

Type: Any | undefined

Usage

const propexists = require('propexists');

var obj = {
   a: 1,
   b: {
      f: null
      d: {
         e: 100
      },
   }
};

propexists(obj, 'x')     // => undefined
propexists(obj, 'a')     // => true
propexists(obj, 'b.f')   // => undefined
propexists(obj, 'b.d.e') // => true

// Or using array of properties
propexists(obj, [ 'b', 'd', 'e' ]) // => true

// Return the value if prop exists
propexists(obj, 'b.d.e', { value: true }) // => 100

// Fallback value
propexists(obj, 'b.d.s.m', { value: true, fallback: 'no' }) // => no
1.2.0

4 years ago

1.2.1

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.3

4 years ago