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

nochoices

v1.1.4

Published

Full featured implementation of options into typescript.

Downloads

739

Readme

CircleCI

With the right Options there is no choice to make

Options in rust are great. This library is a port of the rust Option type into typescript.

Why Options?

Languages derived from the JS family usually deal with tons of complexity related to null and undefined. It's common to deal with this using nullable types and logical operations. The option pattern is an elegant alternative to that. It deals with the same problem ensuring type safety.

Let's check a few examples:

// Same value expressed in 2 different ways:
const optional = Option.Some(10)
const nullable: number | null = 10

// Filter
const isEven = (n: number) => n % 2 === 0
// Transformation
const plus1 = (n: number) => n + 1

expect(
  optional.filter(isEven).map(plus1).unwrapOr(-1)
).to.eql(Option.Some(11)) // Simple logic easy to read.

expect(
  nullable && isEven(nullable) ? plus1(nullable) : -1
).to.eql(11) // Abuse of syntax, harder to read and understand.

The optional pattern also helps with border cases like this:

const maybeTen: null | number = 10;
const maybeZero: null | number = 0;

if (maybeTen) {
  // ...
} // Works as expected

if (maybeZero) {
  // ...
} // b is not null, so it's present, but the value is falsey, the if it's not executed.

Optionals make this a ton more expressive:

const maybeTen = Option.Some(10)
const maybeZero = Option.Some(0)

if (maybeTen.isSome()) {
  // ... 
} // Works as expected

if (maybeZero.isSome()) {
  // ... 
} // Works as expected

Why copy the rust implementation?

In rust optionals are everywhere. The default implementation is very versatile and rust developers use it everywhere. Rust Option interface was tested in every kind of battles, and was improved over time.

Still, this library takes a little freedom on how to translate from rust to typescript. There are certain methods that make tons of sense in rust, but no sense at all in typescript. For examples methods related to mutability or behaviors specific to rust.

There is also a few things that are very desirable in ts that were added. The names were also adapted to typescript conventions (snake case to camel case, for example).

How to use it

Install:

# yarn
yarn i nochoices
# npm
npm i nochoices
# pnpm
pnpm i nochoices

And then use:

import {Option} from 'nochoices'

const ten = Option.Some(10)
const empty = Option.None()
// ...

Api docs

Docs can found here

De datil of the behavior is tested and explained in tests of the package.

How to test

pnpm install
pnpm test

How to contribute

Issues and prs are open.

Changes from rust option lib:

There are certain rust features that are just not applicable in typescript. So methods related to those features where excluded.

There is also a few methods that are super desirable in ts that do not make sense in rust. Those were added.

Lastly, there are a few that I just like, so I added them.

Added methods

  • ifSome(fn: (t: T) => void): Option<T>: execs the provided fn only if current value is some. Returns this always.
  • ifNone(fn: (t: T) => void): Option<T>: execs the provided fn only if current value is none. Returns this always.
  • toArray(): T[]: if none returns [], if some returns an array of size 1 with the value.

Excluded methods

The following methods where excluded:

  • Every method that converts between refs and mutability

    • as_ref
    • as_mut
    • as_deref
    • as_deref_mut
    • as_pin_ref
    • as_pin_mut
    • unwrap_unchecked
  • Methods tha interact with Result type, because typescript has no analog feature to that:

    • ok_or
    • ok_or_else
    • transpose
  • Methods that transform into slice. Still, there is a kind of similar behavior with #toArray.

    • as_slice
    • as_mut_slice
  • Methods related to traits that are harder to match to typescript.

    • into_iter
    • iter
    • iter_mut
    • from_residual
    • hash
    • hash_slice
    • cmp
    • max
    • min
    • clamp
    • eq
    • ne
    • partial_cmp
    • lt
    • le
    • gt
    • ge
    • product
    • sum
    • from_output
    • branch
  • Methods that are not part of traits, but that expect parameters or generic types binded to traits that do not match easily with ts:

    • unwrap_or_default
    • get_or_insert_default
    • copied
    • cloned