项目作者: sukima

项目描述 :
Simple Maybe monad with invoke and nested object properties support
高级语言: JavaScript
项目地址: git://github.com/sukima/maybe-simple.git
创建时间: 2017-06-05T14:45:12Z
项目社区:https://github.com/sukima/maybe-simple

开源协议:MIT License

下载


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).

If this is too much you can accomplish much of this with 20 lines of code.
Check out the compact version.

Install

NPM

  1. $ npm install --save maybe-simple

Yarn

  1. $ yarn add maybe-simple

Usage

  1. 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

  1. var example = { foo: { bar: { baz: 'foobarbaz' } } };
  2. var x = new Maybe(example, 'foo.bar.baz'); // new is optional
  3. var y = Maybe(example, 'foo.nosuchthing.baz');
  4. x.value() // => 'foobarbaz'
  5. 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

  1. Maybe(null)
  2. .setDefaultValue('foobar')
  3. .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

  1. Maybe('foo')
  2. .bind(function(v) { return v + 'bar'; })
  3. .bind(function(v) { console.log(v); }) // foobar
  4. .bind(function(v) { return v + 'baz'; })
  5. .value(); // => 'foobarbaz'
  6. Maybe('foo')
  7. .bind(function(v) { return null; })
  8. .bind(function(v) { return v + 'baz'; })
  9. .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

  1. Maybe(null)
  2. .bind(function(v) { return v + 'foo'; })
  3. .nothing(function() { return 'bar'; })
  4. .bind(function(v) { return v + 'foo'; })
  5. .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

  1. Maybe({ foo: { bar: 'baz' } })
  2. .get('foo.bar')
  3. .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

  1. Maybe(['foo', 'bar', 'baz'])
  2. .invoke('join', ', ')
  3. .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

  1. Maybe([1, 2, 3])
  2. .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

  1. Maybe({ foo: { bar: 'baz' } })
  2. .toJSON(); // => '{foo:{bar:"baz"}}'
  3. Maybe(null)
  4. .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

  1. var obj = { foo: { bar: { baz: 'foobar' } } };
  2. 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

  1. var example = { foo: { bar: { baz: 'foobarbaz' } } };
  2. var x = new Maybe(example, 'foo.bar.baz'); // new is optional
  3. var y = Maybe(example, 'foo.nosuchthing.baz');
  4. x.value() // => 'foobarbaz'
  5. 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

  1. Maybe(null)
  2. .setDefaultValue('foobar')
  3. .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

  1. Maybe('foo')
  2. .bind(function(v) { return v + 'bar'; })
  3. .bind(function(v) { console.log(v); }) // foobar
  4. .bind(function(v) { return v + 'baz'; })
  5. .value(); // => 'foobarbaz'
  6. Maybe('foo')
  7. .bind(function(v) { return null; })
  8. .bind(function(v) { return v + 'baz'; })
  9. .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

  1. Maybe(null)
  2. .bind(function(v) { return v + 'foo'; })
  3. .nothing(function() { return 'bar'; })
  4. .bind(function(v) { return v + 'foo'; })
  5. .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

  1. Maybe({ foo: { bar: 'baz' } })
  2. .get('foo.bar')
  3. .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

  1. Maybe(['foo', 'bar', 'baz'])
  2. .invoke('join', ', ')
  3. .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

  1. Maybe([1, 2, 3])
  2. .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

  1. Maybe({ foo: { bar: 'baz' } })
  2. .toJSON(); // => '{foo:{bar:"baz"}}'
  3. Maybe(null)
  4. .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

  1. var obj = { foo: { bar: { baz: 'foobar' } } };
  2. Maybe.safeRead(obj, 'foo.bar.baz'); // => 'foobar'

License

  1. Copyright (c) 2017 Devin Weaver
  2. Permission is hereby granted, free of charge, to any person obtaining a copy
  3. of this software and associated documentation files (the "Software"), to deal
  4. in the Software without restriction, including without limitation the rights
  5. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. copies of the Software, and to permit persons to whom the Software is
  7. furnished to do so, subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in all
  9. copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  15. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  16. SOFTWARE.