npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

maybe-simple

v1.0.0

Published

Simple Maybe monade with invoke and nested object properties support

Downloads

1

Readme

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.