maybe-simple

1.0.0 • Public • Published

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 = require('maybe-simple');

Maybe

Kind: global class

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 = new Maybe(example, 'foo.bar.baz'); // new is optional
var y = Maybe(example, 'foo.nosuchthing.baz');
x.value() // => 'foobarbaz'
y.value() // => null

maybe.isNothing() ⇒ boolean

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

Maybe(null)
  .setDefaultValue('foobar')
  .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.bind(fn) ⇒ Maybe

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

Maybe('foo')
  .bind(function(v) { return v + 'bar'; })
  .bind(function(v) { console.log(v); }) // foobar
  .bind(function(v) { return v + 'baz'; })
  .value(); // => 'foobarbaz'
 
Maybe('foo')
  .bind(function(v) { return null; })
  .bind(function(v) { return v + 'baz'; })
  .value(); // => null

maybe.nothing(fn) ⇒ Maybe

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

Maybe(null)
  .bind(function(v) { return v + 'foo'; })
  .nothing(function() { return 'bar'; })
  .bind(function(v) { return v + 'foo'; })
  .value(); // => 'barfoo'

maybe.get(selector) ⇒ Maybe

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

Maybe({ foo: { bar: 'baz' } })
  .get('foo.bar')
  .value(); // 'baz'

maybe.invoke(selector) ⇒ Maybe

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

Maybe(['foo', 'bar', 'baz'])
  .invoke('join', '')
  .value(); // => 'foo, bar, baz'

maybe.isEqual(other) ⇒ boolean

Compare two Maybe objects. Maybes considered nothing are equal.

Kind: instance method of Maybe

Param Type Description
other * The other value to compare.

maybe.toString(def) ⇒ string

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

Maybe([1, 2, 3])
  .toString(); // => '1,2,3'

maybe.toJSONString(def, replacer, space) ⇒ string

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

Maybe({ foo: { bar: 'baz' } })
  .toJSON(); // => '{foo:{bar:"baz"}}'
Maybe(null)
  .toJSON(); // => '{}'

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.safeRead(obj, 'foo.bar.baz'); // => 'foobar'

Maybe

Kind: global class

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 = new Maybe(example, 'foo.bar.baz'); // new is optional
var y = Maybe(example, 'foo.nosuchthing.baz');
x.value() // => 'foobarbaz'
y.value() // => null

maybe.isNothing() ⇒ boolean

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

Maybe(null)
  .setDefaultValue('foobar')
  .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.bind(fn) ⇒ Maybe

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

Maybe('foo')
  .bind(function(v) { return v + 'bar'; })
  .bind(function(v) { console.log(v); }) // foobar
  .bind(function(v) { return v + 'baz'; })
  .value(); // => 'foobarbaz'
 
Maybe('foo')
  .bind(function(v) { return null; })
  .bind(function(v) { return v + 'baz'; })
  .value(); // => null

maybe.nothing(fn) ⇒ Maybe

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

Maybe(null)
  .bind(function(v) { return v + 'foo'; })
  .nothing(function() { return 'bar'; })
  .bind(function(v) { return v + 'foo'; })
  .value(); // => 'barfoo'

maybe.get(selector) ⇒ Maybe

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

Maybe({ foo: { bar: 'baz' } })
  .get('foo.bar')
  .value(); // 'baz'

maybe.invoke(selector) ⇒ Maybe

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

Maybe(['foo', 'bar', 'baz'])
  .invoke('join', '')
  .value(); // => 'foo, bar, baz'

maybe.isEqual(other) ⇒ boolean

Compare two Maybe objects. Maybes considered nothing are equal.

Kind: instance method of Maybe

Param Type Description
other * The other value to compare.

maybe.toString(def) ⇒ string

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

Maybe([1, 2, 3])
  .toString(); // => '1,2,3'

maybe.toJSONString(def, replacer, space) ⇒ string

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

Maybe({ foo: { bar: 'baz' } })
  .toJSON(); // => '{foo:{bar:"baz"}}'
Maybe(null)
  .toJSON(); // => '{}'

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.safeRead(obj, 'foo.bar.baz'); // => '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.

Readme

Keywords

none

Package Sidebar

Install

npm i maybe-simple

Weekly Downloads

16

Version

1.0.0

License

MIT

Last publish

Collaborators

  • sukima