Giter Club home page Giter Club logo

ts-option's Introduction

Option Typescript

License

Optional values are meant to describe a value that is explicitly present or not.

This makes the presence of the value more clear, in contrast to making the value nullable (Which will not work if the compiler is not set to check strictly null values), or undefined.

As a side effect, makes an optional value capable of holding a null value, because you might encounter situations where you want a null value to be a valid one.

Installation

Run yarn add @octantis/option or npm i @octantis/option

API

Option is iterable, in case that the value is present yields the value.

The option value is meant to mimic Scala's option[A] type.

/**
 * True if not empty
 */
isDefined(): this is Some<A>;
/**
 * Return value, throw exception if empty
 */
get(): A;
/**
 * True if empty
 */
isEmpty(): this is None<A>;
/**
 * True if not empty
 */
nonEmpty(): this is Some<A>;
/**
 * Evaluate and return alternate optional value if empty
 */
orElse(alternative: option<A>): option<A>;
/**
 * Evaluate and return alternate value if empty
 */
getOrElse(alternative: A): A;
/**
 * Apply function on optional value, return default if empty
 */
fold<B>(fallback: B, folder: (value: A) => B): B;
/**
 * Apply a function on the optional value
 */
map<B>(map: (value: A) => B): option<B>;
/**
 * Same as map but function must return an optional value
 */
flatMap<B>(map: (value: A) => option<B>): option<B>;
/**
 * Apply a procedure on option value
 */
forEach(consumer: (value: A) => void): void;
/**
 * Apply partial pattern match on optional value
 */
collect<B>(collector: (value: A) => B | undefined): option<B>;
/**
 * An optional value satisfies predicate
 */
filter(filter: (value: A) => boolean): option<A>;
/**
 * An optional value doesn't satisfy predicate
 */
filterNot(filter: (value: A) => boolean): option<A>;
/**
 * Apply predicate on optional value, or false if empty
 */
exists(predicate: (value: A) => boolean): boolean;
/**
 * Apply predicate on optional value, or true if empty
 */
forAll(predicate: (value: A) => boolean): boolean;
/**
 * Checks if value equals optional value, or false if empty
 */
contains<B extends A>(element: B): boolean;
/**
 * Combine two optional values to make a paired optional value
 */
zip<B>(that: option<B>): option<[A, B]>;
/**
 * Unary list of optional value, otherwise the empty list
 */
toList(): A[];

Example usage

Simplest example:

// Assume a function
function maybe(): option<number>

let n = maybe()

// This acts as a guard, so now n is of type Some<number>
if (n.isDefined()) {
  console.log(`n = ${n.value}`)
}

// Or at runtime
console.log(`n = ${n.get()}`)
// This throws if the value wasn't present.

With iteration:

for (const value of maybe()) {
  console.log(`value = ${value}`)
}

ts-option's People

Contributors

sigmasoldi3r avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.