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

@octantis/option

v1.3.3

Published

![License](https://img.shields.io/github/license/sigmasoldi3r/ts-option.svg)

Downloads

8

Readme

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}`)
}