Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wking-io/safe-prop
Safe property checks with Either ADT.
https://github.com/wking-io/safe-prop
Last synced: about 1 month ago
JSON representation
Safe property checks with Either ADT.
- Host: GitHub
- URL: https://github.com/wking-io/safe-prop
- Owner: wking-io
- License: mit
- Created: 2018-11-09T04:20:47.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-11-20T17:50:13.000Z (almost 6 years ago)
- Last Synced: 2024-10-10T11:20:50.152Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 192 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
![logo](./safe-prop-logo.png)
[![Build Status](https://travis-ci.org/wking-io/safe-prop.svg?branch=master)](https://travis-ci.org/wking-io/safe-prop)
[![Coverage Status](https://coveralls.io/repos/github/wking-io/safe-prop/badge.svg?branch=master)](https://coveralls.io/github/wking-io/safe-prop?branch=master)A micro-library for safe handling of object properties using the Either ADT from `data.either`.
## API
You can review the API below or take an interactive look in [CodeSandbox](https://codesandbox.io/s/x2xy3nzl9w)
### `# safeProp(prop, obj)`
```js
// safeProp : String -> Object -> Either Error Value
import { safeProp } from 'safe-prop';const obj = { value: 1, empty: null };
safeProp("fake", obj); // Either.Left(`The property "fake" does not exist on the passed in object.`)
safeProp("empty", obj); // Either.Left(`The value for the property "empty" is either null or undefined on the passed in object.`)
safeProp("value", obj); // Either.Right(1)
```Safely check if prop exists on object. Returns `Either.Left` if it doesn't exists or the value of the property is Nullable and `Either.Right` with the value of the property if it does.
### `# safePath([..props], obj)`
```js
// safeProp : Array String -> Object -> Either Error Value
import { safePath } from 'safe-prop';const obj = { one: { two: { value: 1, empty: null } } };
safeProp(["fake"], obj); // Either.Left(`The property "fake" does not exist on the passed in object.`)
safeProp(["one", "three", "value"], obj); // Either.Left(`The property "three" does not exist on the passed in object.`)
safeProp(["one", "two", "empty"], obj); // Either.Left(`The value for the property "empty" is either null or undefined on the passed in object.`)
safeProp(["one", "two", "value"], obj); // Either.Right(1)
```Safely check if nested prop exists on path for an object. Returns `Either.Left` if the path doesn't exists or the value of the property is Nullable and `Either.Right` with the value of the property if it does.