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

@afitzek/unenum

v1.0.0

Published

0kb, Rust-like Enums for TypeScript. Forked just for publishing.

Downloads

4

Readme

unenum

A 0kb, Rust-like Enum/ADT mechanism for TypeScript with zero runtime requirements.

OverviewInstallationEnumPatternsResultFuturematchsafely

Overview

TypeScript should have a more versitile and ergonomic Enum/ADT mechanism that feels like native utility, as opposed its limited, misused, and redundant built-in enum keyword which can be mostly replaced with a plain key-value mapping object using as const.

Introducing unenum; a Rust-inspired, discriminable Enum/ADT type generic, featuring:

  • Zero dependencies; unenum is extremely lightweight.
  • Zero runtime requirements; unenum can be completely compiled away -- no runtime or bundle size cost.
  • Enum variants that can define custom per-instance data; impossible with native TypeScript enums.

unenum wants to feel like a native TypeScript utility type, like a pattern, rather than a library:

  • Enums are defined as type statements; instead of factory functions.
  • Enums are instantiated with plain object { ... } syntax; instead of constructors.
  • Enums can be consumed (and narrowed) with plain if statements; instead of imported match utilities.

Here's an example of unenum's Enum compared with Rust's enum:

Installation

npm install unenum

For Applications (Global):

import "unenum/global";

For Libraries (Imported):

import type { Enum, ... } from "unenum";

Enum<Variants>

Creates a union of mutually exclusive, discriminable variants.

import "unenum/global.enum"; // global
import type { Enum } from "unenum"; // imported

type Foo = Enum<{
	A: undefined;
	B: { b: string };
	C: { c: number };
}>;
-> | { is: "A" }
   | { is: "B"; b: string }
   | { is: "C"; c: number }

Enum.Keys<Enum>

Infers all possible variants' keys of the given Enum.

type Foo = Enum<{ A: undefined; B: { b: string }; C: { c: number } }>;

Enum.Keys<Foo>
-> "A" | "B" | "C"

Enum.Values<Enum>

Infers all possible variants' values of the given Enum.

type Foo = Enum<{ A: undefined; B: { b: string }; C: { c: number } }>;

Enum.Values<Foo>
-> | { b: string }
   | { c: number }

Enum.Props<Enum, All?>

Infers only common variants' properties' names of the given Enum. If All is true, then all variants' properties' names are inferred.

type Foo = Enum<{ A: undefined; B: { x: string }; C: { x: string; y: number } }>;

Enum.Props<Foo>
-> "x"

Enum.Props<Foo, true>
-> "x" | "y"

Enum.Pick<Enum, VariantKeys>

Narrows a given Enum by including only the given variants by key.

type Foo = Enum<{ A: undefined; B: { b: string }; C: { c: number } }>;

Enum.Pick<Foo, "A" | "C">
-> | { is: "A" }
   | { is: "C"; c: number }

Enum.Omit<Enum, VariantKeys>

Narrows a given Enum by excluding only the given variants by key.

type Foo = Enum<{ A: undefined; B: { b: string }; C: { c: number } }>;

Enum.Omit<Foo, "A" | "C">
-> | { is: "B"; b: string }

Patterns

Enums are disciminated unions that use is as a property to differentiate between variants. TypeScript supports type narrowing by analysing control flow statements like if and return to determine when certain Enum variants are accessible, allowing for safe property access.

If a function's return type is not explicitly annotated it will be inferred instead, which will lead to inaccurate Enum return types. Explicitly specifying an Enum (e.g. Foo) as a return type will ensure that a function returns a valid Enum variant and provides autocompletion to help instantiate Enum variants with all their properties (e.g. return { is: "B", b: "..." }).

With explicit return types (recommended)

type Foo = Enum<{ A: undefined; B: { b: string }; C: { c: number } }>;

function getFoo(value: string | number): Foo {
	if (!value) {
		return { is: "A" };
	}

	if (typeof value === "string") {
		return { is: "B", b: value };
	}

	return { is: "C", c: value };
}

Note

If you need to limit the range of possible Enum variants that can be returned (or used as a value/parameter/etc), use Enum.Pick or Enum.Omit.

With if statements (recommended)

type Foo = Enum<{ A: undefined; B: { b: string }; C: { c: number } }>;
const foo: Foo = { ... };

if (foo.is === "A") {
	return 123;
}

if (foo.is === "B") {
	return foo.b === "" ? "empty" : "abc";
}

return null;

Note

if statements are the most universal and native way to handle Enum variants without any dependencies.

With match function (dependency)

See match.

type Foo = Enum<{ A: undefined; B: { b: string }; C: { c: number } }>;
const foo: Foo = { ... };

import { match } from "unenum";
match(foo, {
	A: () => 123,
	B: ({ b }) => b === "" ? "empty" : "abc",
	C: () => null,
});

Note

Using the match utility will make unenum a runtime dependency with a non-0kb bundle-size cost instead of being a type-only utility. However, match is tiny and very helpful for reducing complexity of conditional variable assignments instead of needing to write one-off functions, IIFEs, or ternary expressions.

With ternary expressions

type Foo = Enum<{ A: undefined; B: { b: string }; C: { c: number } }>;
const foo: Foo = { ... };

foo.is === "A"
	? 123
: foo.is === "B"
	? (foo.b === "" ? "empty" : "abc")
: null;

Note

Ternary expressions are often criticised for poor readibility, where sufficiently complex and nested expression (such as the above example) are strong candidates for refactoring into functions that may use if statements and the early-return pattern to cleanly narrow down an Enum's variants.

With switch statements

type Foo = Enum<{ A: undefined; B: { b: string }; C: { c: number } }>;
const foo: Foo = { ... }

switch (foo.is) {
	case "A": {
		return 123;
	}
	case "B": {
		return foo.b === "" ? "empty" : "abc";
	}
	default: {
		return null;
	}
}

Note

switch statements are severely limited because they can only branch based on an Enum's is variant discriminant. if statements allow for more versitile conditional expressions that may accomodate evaluating other variables or even properties on the Enum variant itself (e.g. if (foo.is === "B" && foo.b === "hello") ...).

Included Enums

Result<Value?, Error?>

Represents either success value (Ok) or failure error (Error).

Result uses value?: never and error?: never to allow for shorthand access to .value or .error if you want to safely default to undefined if either property is not available.

import "unenum/global.result"; // global
import type { Result } from "unenum"; // imported

Result
-> | { is: "Ok"; value: unknown; error?: never }
   | { is: "Error"; error: unknown; value?: never }

Result<number>
-> | { is: "Ok"; value: number; error?: never }
   | { is: "Error"; error: unknown; value?: never }

Result<number, "FetchError">
-> | { is: "Ok"; value: number; error?: never }
   | { is: "Error"; error: "FetchError"; value?: never }
const getUser = async (name: string): Promise<Result<User, "NotFound">> => {
	return { is: "Ok", value: user };
	return { is: "Error", error: "NotFound" };
}

const $user = await getUser("foo");
if ($user.is === "Error") { return ... }
const user = $user.value;

const $user = await getUser("foo");
const userOrUndefined = $user.value;
const userOrUndefined = $user.is === "Ok" ? $user.value : undefined;

const $user = await getUser("foo");
const userOrDefault = $user.value ?? defaultUser;
const userOrDefault = $user.is === "Ok" ? $user.value : defaultUser;

Based on Rust's Result enum.

Note

You may find it useful to name variables for container-like Enums (like Results and Futures) with a $ prefix (e.g. $user) before unwrapping the desired value into non-prefixed value (e.g. const user = $user.value).

Future<ValueOrEnum?>

Represents an asynchronous value that is either loading (Pending) or resolved (Ready). If defined with an Enum type, Future will omit its Ready variant in favour of the "non-pending" Enum's variants.

Future uses value?: never to allow for shorthand access to .value if you want to safely default to undefined if it is not available. If using with an Enum type, all its common properties will be extended as ?: never properties on the Pending variant to allow for shorthand undefined access also. (See Enum.Props.)

import type { Future } from "unenum"; // imported

Future
-> | { is: "Pending"; value?: never }
   | { is: "Ready"; value: unknown }

Future<string>
-> | { is: "Pending"; value?: never }
   | { is: "Ready"; value: string }

Future<Result<number>>
-> | { is: "Pending"; value?: never; error?: never }
   | { is: "Ok"; value: number; error?: never }
   | { is: "Error"; error: unknown; value?: never }
const useRemoteUser = (name: string): Future<Result<User, "NotFound">> => {
	return { is: "Pending" };
	return { is: "Ok", value: user };
	return { is: "Error", error: "NotFound" };
};

const $user = useRemoteUser("foo");
if ($user.is === "Pending") { return <Loading />; }
if ($user.is === "Error") { return <Error />; }
const user = $user.value;
return <View user={user} />;

const $user = useRemoteUser("foo");
const userOrUndefined = $user.value;
const userOrUndefined = $user.is === "Ok" ? $user.value : undefined;

const $user = useRemoteUser("foo");
const userOrDefault = $user.value ?? defaultUser;
const userOrDefault = $user.is === "Ok" ? $user.value : defaultUser;

Based on Rust's Future trait and Poll enum.

Utils

match(value, matcher) -> ...

Uses a given Enum value to execute its corresponding variants' matcher function and return its result. Use match.orUndefined(...) or match.orDefault(...) if you want to match against only a subset of variants.

import { match } from "unenum"; // dependency

type Foo = Enum<{ A: undefined; B: { b: string }; C: { c: number } }>;
const foo: Foo = ...

// all cases
match(foo, {
	A: () => null,
	B: ({ b }) => b,
	C: ({ c }) => c,
})
-> null | string | number

// some cases or undefined
match.orUndefined(foo, {
	A: () => null,
	B: ({ b }) => b,
})
-> null | string | undefined

// some cases or default
match.orDefault(
	foo,
	{ A: () => null },
	($) => $.is === "B" ? true : false
)
-> null | string | boolean

safely(fn) -> Result

Executes a given function and returns a Result that wraps its normal return value as Ok and any thrown errors as Error. Supports async/Promise returns.

import { safely } from "unenum"; // dependency

safely(() => JSON.stringify(...))
-> Result<string>

safely(() => JSON.parse(...))
-> Result<unknown>

safely(() => fetch("/endpoint").then(res => res.json() as Data))
-> Promise<Result<Data>>