JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 51
  • Score
    100M100P100Q62474F
  • License MIT

Option/Maybe type to be used with flow.js

Package Exports

  • @ekz/option

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@ekz/option) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Description

Simply get rid of all nulls in your code.

This is a clone of Scala's Option class to be used with flow.js.

Examples

// @flow

let valueA = Option.of(null).map(x => x + 2).getOrElse(() => 0);
// valueA = 0

let valueB = Some('any object').flatMap(x => Some(`${x}!!!`)).getOrUndefined();
// valueB = 'any object!!!'

let valueC = Some(null).flatMap(() => Option.of(2)).map(x => x * 3).getOrElse(() => 0);
// valueC = 6

let valueD = Some(1).mapNullable(() => null).getOrReturn(-1);
// valueD = -1

Some(1).equals(Some(1))
// true

None.equals(None)
// true

Some('abc').equals(None)
// false

None.isDefined; // false
Some(1).isDefined; // true
Some(1).isEmpty; // false
Some(null).isEmpty; // false! Option<null> is valid!

Some('foo').get(); // 'foo'
None.get(); // throws an error, use getOrElse or getOrUndefined instead

Some(3).filter(x => x % 2 === 0); // None

Some('bar').forEach(x => {
    console.log(`my value: ${x}`);
});

API

Table of Contents

Option

src/index.js:9-121

Represents optional values. Instances of Option are either an instance of Some or the object None.

Parameters

  • $privateToken any

get

src/index.js:13-13

Returns the option's value.

Type: function (): A

isEmpty

src/index.js:18-18

Returns true if the option is None, false otherwise.

Type: boolean

isDefined

src/index.js:29-31

Returns true if the option is an instance of Some, false otherwise.

Returns boolean

map

src/index.js:36-38

Returns a Some containing the result of applying f to this Option's value if this Option is nonempty.

Parameters

  • f function (A): B

Returns Option<B>

mapNullable

src/index.js:43-45

Like map, but if resulting value is null, then returns None.

Parameters

  • f function (A): B?

Returns Option<B>

flatMap

src/index.js:50-52

Returns the result of applying f to this Option's value if this Option is nonempty. Returns None if this Option is empty. Slightly different from map in that f is expected to return an Option (which could be None).

Parameters

Returns Option<B>

forEach

src/index.js:57-61

Apply the given procedure f to the option's value, if it is nonempty.

Parameters

  • f function (A): any

Returns void

filter

src/index.js:66-68

Returns this Option if it is nonempty and applying the predicate to this Option's value returns true.

Parameters

Returns Option<A>

getOrElse

src/index.js:73-75

Returns the option's value if the option is nonempty, otherwise return the result of evaluating other.

Parameters

  • other function (): B

Returns (A | B)

getOrReturn

src/index.js:80-82

Returns the option's value if the option is nonempty, otherwise return other.

Parameters

  • other B

Returns (A | B)

getOrUndefined

src/index.js:87-89

Returns the option's value if the option is nonempty, otherwise returns undefined.

Returns (A | void)

equals

src/index.js:94-96

Compares the option's value with other option's value and returns true when they match. None always matches other None.

Parameters

Returns boolean

toJSON

src/index.js:101-103

Shallowly converts to value or null.

Returns (A | null)

of

src/index.js:108-110

An Option factory which creates Some(x) if the argument is not null, and None if it is null.

Parameters

  • value V?

Returns Option<V>

None

src/index.js:115-115

The empty None object

Type: Option<any>

Some

src/index.js:120-120

Creates Some(x). Note that Some(null) is valid.

Type: function (value: A): Option<A>

None

src/index.js:136-136

The empty None object.

Type: Option<any>

Some

src/index.js:160-162

Creates Some(x). Note that Some(null) is valid.

Parameters

  • value A

Returns Option<A>