Yet Another Maybe Monad
Designed as a simple drop in module. This monad includes support for deep object properties lookup and ability to invoke functions on objects.
But why? Most Maybe monad libraries are full featured and heavy. They try
to mimic Haskel. And many work on values and not object properties. When
working in JS I find I often want a simple optional like construct which will
let me deal with objects that violate the Law of Demeter (typically a nested
object from an API. I need a simple way to grab data deep down in a safe way
without having JS blowup with undefined problems. Rails has a try
, Swift has
optionals, CoffeeScript has a ?
guard, but JS has nothing (pun intended).
Install
NPM
$ npm install --save maybe-simple
Yarn
$ yarn add maybe-simple
Usage
var Maybe = ;
Maybe
Kind: global class
- Maybe
- new Maybe(obj, selctor, def)
- instance
- .isNothing() ⇒
boolean
- .setDefaultValue(def) ↩︎
- .value(def) ⇒
*
- .bind(fn) ⇒
Maybe
- .nothing(fn) ⇒
Maybe
- .get(selector) ⇒
Maybe
- .invoke(selector) ⇒
Maybe
- .isEqual(other) ⇒
boolean
- .toString(def) ⇒
string
- .toJSONString(def, replacer, space) ⇒
string
- .isNothing() ⇒
- static
new Maybe(obj, selctor, def)
Maybe monad can be created with or without the new keyword. This wraps an object and allows a deep selector (optional) and a default value (optional). It is safe to wrap another Maybe object.
Param | Type | Description |
---|---|---|
obj | * |
the original value. Can be anything. |
selctor | string |
(optional) used to safely pick a deep value from obj. |
def | * |
(optional) a default value if obj or any level of the selector resolves to nothing. |
Example
var example = foo: bar: baz: 'foobarbaz' ;var x = example 'foo.bar.baz'; // new is optionalvar y = ;xvalue // => 'foobarbaz'yvalue // => null
boolean
maybe.isNothing() ⇒ Check if the value resolves to nothing.
Kind: instance method of Maybe
maybe.setDefaultValue(def) ↩︎
Sets the default returned from value() when this Maybe is nothing.
Kind: instance method of Maybe
Chainable
Param | Type | Description |
---|---|---|
def | * |
a default value |
Example
value; // => 'foobar'
*
maybe.value(def) ⇒ Convert the Maybe to a resolved value. Either the value or the deafult if this Maybe is nothing.
Kind: instance method of Maybe
Returns: *
- a value, the default value, or null
Param | Type | Description |
---|---|---|
def | * |
(optional) use as default value overridding any previous defaults. |
Maybe
maybe.bind(fn) ⇒ Perform a transformation or action unless the value is nothing. This is the main way to interface with a Maybe. The functions' return value will be the new value propagated through the chain. Returning undefined (a function with no return value) does not mutate the previous value (no-op). Return null if you want to the Maybe to be nothing.
Kind: instance method of Maybe
Returns: Maybe
- a Maybe object
Param | Type | Description |
---|---|---|
fn | function |
a function to execute if this Maybe is not nothing. |
Example
// foobar value; // => 'foobarbaz' value; // => null
Maybe
maybe.nothing(fn) ⇒ Execute/mutate with the function if this Maybe is nothing.
Kind: instance method of Maybe
Returns: Maybe
- a Maybe object
Param | Type | Description |
---|---|---|
fn | function |
a function to execute if this Maybe is nothing. |
Example
value; // => 'barfoo'
Maybe
maybe.get(selector) ⇒ Helper to return a selector from a value object.
Kind: instance method of Maybe
Returns: Maybe
- a Maybe object
Param | Type | Description |
---|---|---|
selector | string |
the property selector to get from the value. |
Example
value; // 'baz'
Maybe
maybe.invoke(selector) ⇒ Invoke a method with args on the object if Maybe is not nothing.
Kind: instance method of Maybe
Returns: Maybe
- a Maybe object
Param | Type | Description |
---|---|---|
selector | string |
the property selector to get from the value. |
Example
value; // => 'foo, bar, baz'
boolean
maybe.isEqual(other) ⇒ Compare two Maybe objects. Maybes considered nothing are equal.
Kind: instance method of Maybe
Param | Type | Description |
---|---|---|
other | * |
The other value to compare. |
string
maybe.toString(def) ⇒ Coerce the value to a string.
Kind: instance method of Maybe
Returns: string
- a String
Param | Type | Description |
---|---|---|
def | * |
(optional) use as default value overridding any previous |
Example
; // => '1,2,3'
string
maybe.toJSONString(def, replacer, space) ⇒ Coerce the value to JSON.
Kind: instance method of Maybe
Returns: string
- a JSON encoded String
Param | Type | Description |
---|---|---|
def | * |
(optional) use as default value overridding any previous |
replacer | function |
(optional) See JSON.stringify() |
space | number |
(optional) See JSON.stringify() |
Example
; // => '{foo:{bar:"baz"}}' ; // => '{}'
*
Maybe.safeRead(obj, selector) ⇒ A utility function to safely recurse through an object based on a selector. if any value in the chain resolves to nothing then this will simple return null. Used internally to look up values when constructing a Maybe object.
Kind: static method of Maybe
Returns: *
- any value or null.
Param | Type | Description |
---|---|---|
obj | object |
the object to traverse. |
selector | string |
the selector to pick from the obj. |
Example
var obj = foo: bar: baz: 'foobar' ;Maybe; // => 'foobar'
Maybe
Kind: global class
- Maybe
- new Maybe(obj, selctor, def)
- instance
- .isNothing() ⇒
boolean
- .setDefaultValue(def) ↩︎
- .value(def) ⇒
*
- .bind(fn) ⇒
Maybe
- .nothing(fn) ⇒
Maybe
- .get(selector) ⇒
Maybe
- .invoke(selector) ⇒
Maybe
- .isEqual(other) ⇒
boolean
- .toString(def) ⇒
string
- .toJSONString(def, replacer, space) ⇒
string
- .isNothing() ⇒
- static
new Maybe(obj, selctor, def)
Maybe monad can be created with or without the new keyword. This wraps an object and allows a deep selector (optional) and a default value (optional). It is safe to wrap another Maybe object.
Param | Type | Description |
---|---|---|
obj | * |
the original value. Can be anything. |
selctor | string |
(optional) used to safely pick a deep value from obj. |
def | * |
(optional) a default value if obj or any level of the selector resolves to nothing. |
Example
var example = foo: bar: baz: 'foobarbaz' ;var x = example 'foo.bar.baz'; // new is optionalvar y = ;xvalue // => 'foobarbaz'yvalue // => null
boolean
maybe.isNothing() ⇒ Check if the value resolves to nothing.
Kind: instance method of Maybe
maybe.setDefaultValue(def) ↩︎
Sets the default returned from value() when this Maybe is nothing.
Kind: instance method of Maybe
Chainable
Param | Type | Description |
---|---|---|
def | * |
a default value |
Example
value; // => 'foobar'
*
maybe.value(def) ⇒ Convert the Maybe to a resolved value. Either the value or the deafult if this Maybe is nothing.
Kind: instance method of Maybe
Returns: *
- a value, the default value, or null
Param | Type | Description |
---|---|---|
def | * |
(optional) use as default value overridding any previous defaults. |
Maybe
maybe.bind(fn) ⇒ Perform a transformation or action unless the value is nothing. This is the main way to interface with a Maybe. The functions' return value will be the new value propagated through the chain. Returning undefined (a function with no return value) does not mutate the previous value (no-op). Return null if you want to the Maybe to be nothing.
Kind: instance method of Maybe
Returns: Maybe
- a Maybe object
Param | Type | Description |
---|---|---|
fn | function |
a function to execute if this Maybe is not nothing. |
Example
// foobar value; // => 'foobarbaz' value; // => null
Maybe
maybe.nothing(fn) ⇒ Execute/mutate with the function if this Maybe is nothing.
Kind: instance method of Maybe
Returns: Maybe
- a Maybe object
Param | Type | Description |
---|---|---|
fn | function |
a function to execute if this Maybe is nothing. |
Example
value; // => 'barfoo'
Maybe
maybe.get(selector) ⇒ Helper to return a selector from a value object.
Kind: instance method of Maybe
Returns: Maybe
- a Maybe object
Param | Type | Description |
---|---|---|
selector | string |
the property selector to get from the value. |
Example
value; // 'baz'
Maybe
maybe.invoke(selector) ⇒ Invoke a method with args on the object if Maybe is not nothing.
Kind: instance method of Maybe
Returns: Maybe
- a Maybe object
Param | Type | Description |
---|---|---|
selector | string |
the property selector to get from the value. |
Example
value; // => 'foo, bar, baz'
boolean
maybe.isEqual(other) ⇒ Compare two Maybe objects. Maybes considered nothing are equal.
Kind: instance method of Maybe
Param | Type | Description |
---|---|---|
other | * |
The other value to compare. |
string
maybe.toString(def) ⇒ Coerce the value to a string.
Kind: instance method of Maybe
Returns: string
- a String
Param | Type | Description |
---|---|---|
def | * |
(optional) use as default value overridding any previous |
Example
; // => '1,2,3'
string
maybe.toJSONString(def, replacer, space) ⇒ Coerce the value to JSON.
Kind: instance method of Maybe
Returns: string
- a JSON encoded String
Param | Type | Description |
---|---|---|
def | * |
(optional) use as default value overridding any previous |
replacer | function |
(optional) See JSON.stringify() |
space | number |
(optional) See JSON.stringify() |
Example
; // => '{foo:{bar:"baz"}}' ; // => '{}'
*
Maybe.safeRead(obj, selector) ⇒ A utility function to safely recurse through an object based on a selector. if any value in the chain resolves to nothing then this will simple return null. Used internally to look up values when constructing a Maybe object.
Kind: static method of Maybe
Returns: *
- any value or null.
Param | Type | Description |
---|---|---|
obj | object |
the object to traverse. |
selector | string |
the selector to pick from the obj. |
Example
var obj = foo: bar: baz: 'foobar' ;Maybe; // => 'foobar'
License
Copyright (c) 2017 Devin Weaver
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.