Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andersdjohnson/null-propagation
Emulate null propagation operator.
https://github.com/andersdjohnson/null-propagation
ecmascript esnext facebook get idx javascript lodash null null-check path propagation stage-1 traversal traverse
Last synced: 16 days ago
JSON representation
Emulate null propagation operator.
- Host: GitHub
- URL: https://github.com/andersdjohnson/null-propagation
- Owner: AndersDJohnson
- License: mit
- Created: 2017-05-17T03:48:23.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-12T06:49:11.000Z (almost 2 years ago)
- Last Synced: 2024-04-14T05:16:02.983Z (7 months ago)
- Topics: ecmascript, esnext, facebook, get, idx, javascript, lodash, null, null-check, path, propagation, stage-1, traversal, traverse
- Language: JavaScript
- Size: 484 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# null-propagation
> Emulate null propagation operator.Like [Facebook's `idx`][idx] and [Lodash's `get`][get],
but even more terse than the former, and real syntax unlike the latter.The use of real syntax enables static analysis tools.
```js
import np from 'null-propagation'const obj = {
a: {
b: 'B2'
},
z: []
}
````For undefined values, returns undefined:
```js
const c = np(() => obj.a.c)
```With a default as a 2nd argument, returns that value:
```js
const c2 = np(() => obj.a.c, 'C2')
```For defined values, returns that value:
```js
const b = np(() => obj.a.b)
```Works with arrays, returns undefined:
```js
const z = np(() => obj.z[0].y)
```Throws other errors:
```js
np(() => { throw new Error('some error') })
```[idx]: https://github.com/facebookincubator/idx
[get]: https://lodash.com/docs/4.17.4#get