Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tpkn/propexists
Quick check if a nested property exists
https://github.com/tpkn/propexists
Last synced: about 2 months ago
JSON representation
Quick check if a nested property exists
- Host: GitHub
- URL: https://github.com/tpkn/propexists
- Owner: tpkn
- License: mit
- Created: 2019-12-12T19:25:43.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-07-31T15:04:14.000Z (over 4 years ago)
- Last Synced: 2024-10-12T23:26:16.974Z (3 months ago)
- Language: JavaScript
- Size: 13.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# propexists [![npm Package](https://img.shields.io/npm/v/propexists.svg)](https://www.npmjs.org/package/propexists)
Quick check if a nested property exists## API
```javascript
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
```javascript
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
```