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

tsoption

v0.7.0

Published

Correct, easy to use Option type for TypeScript

Downloads

1,112

Readme

tsoption Build Status npm mit fantasy

Correct, easy to use Option type for TypeScript. Like Scala options; see the introductory blog post.

Installation

# Using Yarn:
yarn add tsoption

# Or, using NPM:
npm install tsoption --save

Usage

Note: You can use JavaScript instead of TypeScript, but it's not as fun.

let a = Option.of(3)                  // Some<number>(3)
  .flatMap(_ => Some.of(5))           // Some<number>(5)
  .map(_ => 'a')                      // Some<string>('a')
  .orElse(Option.of('b'))             // Some<string>('a')   (non-string type gives a compile error)
  .getOrElse('c')                     // 'a'

let b = Option.of('a')                // Some<string>('a')
  .flatMap(_ => new None)             // None<string>()      (flatMap can map to any type)
  .orElse(Option.of('b'))             // Some<string>('b')   (non-string type gives a compile error)
  .get()                              // 'b'

API

Note: The types of each of the expressions below are known at compile time.

// Create an option
Option.of(3)                            // Some(3)
Option.of('abc')                        // Some('abc')
Option.of(null)                         // None                (for convenience)
Option.of(undefined)                    // None                (for convenience)
Some.of(3)                              // Some(3)
Some.of(null)                           // Some(null)
new Some([1, 2, 3])                     // Some([1, 2, 3])
new None                                // None

// #flatMap
Some.of(3).flatMap(_ => Some.of(_ * 2)) // Some(6)
None.of().flatMap(() => Some.of(2))     // None

// #get
Some.of(3).get()                        // 3
None.of().get()                         // COMPILE ERROR! Can't call get() on None

// #getOrElse
Some.of(1).getOrElse(2)                 // 1
None.of().getOrElse(2)                  // 2

// #isEmpty
Some.of(2).isEmpty()                    // false (known at compile time too!)
None.of().isEmpty()                     // true (known at compile time too!)

// #map
Some.of(2).map(_ => _ * 2)              // Some(4)
None.of().map(() => 2)                  // None (known at compile time too!)

// #nonEmpty
Some.of(2).nonEmpty()                   // true (known at compile time too!)
None.of().nonEmpty()                    // false (known at compile time too!)

// #orElse
Some.of(2).orElse(Option.of(3))         // Some(2)
None.of().orElse(Option.of(3))          // Some(3)

// #toString
Some.of(2).toString()                   // "Some(2)"
None.of().toString()                    // "None"

Fantasyland

TSOption is Fantasyland-compliant. It implements:

Fantasyland-Compatible API

import * as fl from 'fantasy-land'
const {ap, chain, map, of} = fl

// Create an option
Option[of](3)                           // Some(3)
Option[of]('abc')                       // Some('abc')
Option[of](null)                        // None
Option[of](undefined)                   // None

// #chain
Option[of](3)[chain](_ => Option[of](_ * 2)) // Some(6)
Option[of](null)[chain](() => Option[of](2)) // None (known at compile time too!)

// #map
Option[of](2)[map](_ => _ * 2)          // Some(4)
Option[of](null)[map](() => 2)          // None (known at compile time too!)

// #ap
Option[of](2)[ap](Option[of]((_: number) => _ * 2)) // Some(4)
Option[of](null)[ap](Option[of](() => 2)) // None (known at compile time too!)

Tests

npm test

License

MIT