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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@eriveltondasilva/option.js

v1.0.3

Published

A lightweight, type-safe library inspired by Rust's `Option` enum for handling optional values in JavaScript and TypeScript without `null` or `undefined` pitfalls.

Downloads

430

Readme

Option.js

npm version npm size CI Checked with Biome Zero Dependencies License

A lightweight, type-safe library inspired by Rust's Option enum for handling optional values in JavaScript and TypeScript without null or undefined pitfalls.

Features

  • Type-safe by design: Eliminates unsafe null/undefined access by enforcing explicit handling.
  • Composable API: Chainable methods like map, andThen, filter, unwrapOr, and more.
  • Singleton NONE: Single shared instance for better memory usage.
  • TypeScript-first: Fully typed with excellent inference.
  • Tiny footprint: Zero dependencies and minimal bundle size.

Installation

npm install @eriveltondasilva/option.js
bun add @eriveltondasilva/option.js

Import

// Recommended
import { Option } from '@eriveltondasilva/option.js'

// Default Import
import Option from '@eriveltondasilva/option.js'

// Named Helpers - shortcuts for Option.some() and Option.none()
import { some, none } from '@eriveltondasilva/option.js'

// Types
import type { Option, AsyncOption, Some, None } from '@eriveltondasilva/option.js'

Usage

Basic Example

function getUsername(id: number): Option<string> {
  const users = { 1: 'Erivelton' }
  return Option.fromNullable(users[id])
}

const user = getUsername(1)
  .map((name) => name.toUpperCase())
  .unwrapOr('ANONYMOUS')

console.log(user)
// => log "ERIVELTON"

Chaining with and() and andThen()

const some = Option.some(10)
const other = Option.some(20)

// Returns 'other' only if 'some' is Some
const result = some.and(other)

// Chain operations that return Options
const result = some.andThen((val) => Option.some(val * 2))

Pattern Matching

const result = Option.fromNullable(someData)

const message = result.match({
  some: (val) => `Found: ${val}`,
  none: () => 'Nothing here',
})

API Overview

Factories

  • Option.some(val): Wraps a value.
  • Option.none(): The singleton instance for absent values.
  • Option.fromTry(fn): Creates an Option from a function that may throw.
  • Option.fromNullable(val): Safely converts null | undefined to None.

Guards

  • Option.isOption(val): Checks if a value is an instance of Some or None.
  • Option.isSome(val): Type guard for Some.
  • Option.isNone(val): Type guard for None.

Key Instance Methods

| Method | Description | | :----------------- | :-------------------------------------------------- | | .map(fn) | Transforms the value inside a Some. | | .and(other) | Returns other if instance is Some, else None. | | .andThen(fn) | Returns the Option resulting from fn if Some. | | .unwrapOr(alt) | Returns value or the provided fallback. | | .match(handlers) | Executes a branch based on the state. | | .inspect(fn) | Runs a side-effect without changing the Option. |

License

MIT © Erivelton Silva

Related Projects

If you find this library useful, check out my other functional utilities:

@eriveltondasilva/result.js — A type-safe way to handle errors and successes without try/catch overhead, inspired by Rust's Result type.

import { Option } from '@eriveltondasilva/option.js'
import { Result } from '@eriveltondasilva/result.js'

const user = Option.fromNullable(null) // => None

// Converting an Option to a Result (conceptually)
const userResult = user.match({
  some: (val) => Result.ok(val),
  none: () => Result.err('User not found'),
})
// => Err("User not found")