evilsoft / evilsoft/crocks

Add getPropOrError to crocks

Open
#509 6 comments 0 reactions 0 assignees View on GitHub
enhancement good first issue needs work
Dominant language
JavaScript
Stars
1.6k
Forks
103
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**
I'm on my fourth project using Crocks and I keep reaching for the same function `getPropOrError` (and `getPathOrError`). Instead of me releasing my own library, or copy/pasting the function around I think it would be good to have this in the standard library.

**Describe the solution you'd like**
`getPropOrError :: String -> a -> Result Error b`
`getPathOrError :: [ String ] -> a -> Result Error b`

**Describe alternatives for how you do this now**
Doing a `maybeToResult` on `getProp` with an `Error` if `getProp` returns a `Nothing.`

**Code**
Do you have example code or tests in a [Repl](http://repl.it/languages/babel) for what you'd like added?

```
// getPropOrError :: String -> a -> Result Error b
const getPropOrError = curry((prop, obj) =>
maybeToResult(new Error(`'${prop}' is mandatory and missing`), getProp(prop, obj))
);
```
**Additional context**
Add any other context or screenshots about the feature request here.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.