https://github.com/uri/maybe-proxy
Maybe monad implemented using Proxy for maximum sweetness
https://github.com/uri/maybe-proxy
Last synced: 9 months ago
JSON representation
Maybe monad implemented using Proxy for maximum sweetness
- Host: GitHub
- URL: https://github.com/uri/maybe-proxy
- Owner: uri
- Created: 2017-11-15T14:19:17.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-15T14:19:37.000Z (over 8 years ago)
- Last Synced: 2025-06-05T06:57:06.139Z (about 1 year ago)
- Language: JavaScript
- Size: 1000 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# maybe-proxy
This maybe monad implemented with a `Proxy`. This monad let's you chain method
calls without relying on a higher order `then`/`chain`/`andThen` function
```javascript
var {Maybe} = require("./index");
function apiRequest() {
if (Math.random() < 0.5) {
return {person: {name: 'bobby', projects: [{lang: 'elixir', name: 'brute'}]}};
} else {
return {projects: []};
}
}
var lang = maybePerson.person.projects[0].lang.value
console.log(lang)
```
Use `value` to return the current value. This returns the value or `null`.
`andThen` is still useful if wanting to use a function not implemented on the underlying value:
```javascript
function someOp(value) {
console.log("Performing some op", value)
}
maybePerson.person.projects.andThen(projects => someOp(projects))
```
Chaining methods together with will always return a new `Maybe`.
```javascript
var maybeProjects = maybePerson.projects
console.log(maybePerson.value) // prints null or value
console.log(maybeProjects.value) // prints null or value
maybeProjects.andThen(value => { // only prints value if value is not null
console.log(value)
return value // value must be return if chaining is to continue
}).andThen(v => someOp(v)).andThen(v => someOtherOp(v))
```