Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

option.ts

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

option.ts

Option ~Monad for typescript, focus is on simplicity and typesafety

  • 0.1.5
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

option.ts

Option ~Monad for typescript

option.ts can be used with either javascript or typescript 2.0.
This Option wrapper is mostly meant to be used at call sites, where you want to create and transform optional value expressions. The advantage of only using it there, is that you keep neat, non wrapped JSON structures as your data.

API

  • Option()
  • Option.all()
  • None
  • map
  • flatMap
  • filter
  • orElse
  • [Reading the Option value](#Reading the Option value)
  • isDefined
  • getOrElse

Creating an Option

Option(x)

Creates an Option from a value. If the value is null or undefined, it will create a None, else a Some.

const some = Option(33) // some === Some(33)
const none = Option(null) // none === None

Option.all(...optionsOrValues, transform)

Creates a new Option holding the computation of the passed Options if they were all Some or plain defined instances, else returns None

const some = Option.all(
  Option(10), 20, Option(5),
  (a, b, c) => a + b + c
)
// some === Some(35)

const none = Option.all(
  Option(10), None, Option(5),
  (a, b, c) => a + b + c
)
// none === None

None

The Option constant representing no value. Note: Some can not be imported as it would result in unsafe Option creations (e.g Some containing null/undefined). Instead, use Option(myValue).

import { None } from 'option.ts'

Transforming an Option

map

Maps the value contained in this Some, else returns None. Depending on the map function return value, a Some could be tranformed into a None, as a Some will never contain a null or undefined value.

const some = Option(33).map(x => x * 2)
// some === Some(66)

flatMap

Maps the value contained in this Some to a new Option, else returns None.

const some = Option(33).flatMap(_ => Option(44))
// some === Some(44)

filter

If this Option is a Some and the predicate returns true, keep that Some. In all other cases, return None.

const some = Option(33).filter(x => x > 32)
// some === Some(33)

orElse

Returns this Option unless it's a None, in which case the provided alternative is returned.

const some = Option(null).orElse(() => Option(33))
// some === Some(33)

Misc

Accessing the underlying value of the Option

Options are simple functions, call it to access the underlying value. Some instances return their value, whereas None returns undefined

const value = Option(33)()
// value === 33

isDefined

Returns whether this Option has a defined value (i.e, it's a Some(value))

getOrElse

Returns this Option's value if it's a Some, else return the provided alternative

const value = Option(undefined).getOrElse(33)

// value === 33

Keywords

FAQs

Package last updated on 08 Feb 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc