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

@alan404/enum

v0.2.1

Published

'Algebraic' enum types for typescript

Downloads

8

Readme

@alan404/enum

TypeScript "algebraic" enums

Usage

import { Enum, match, createFactory, variant } from "@alan404/enum";

type Segment = Enum<{
    text: { content: string; bold: boolean };
    link: { label: string; link: string };
    image: { src: string };
}>;

// Segment is represented as:
// { type: "text", data: { ... } }
//   | { type: "link", data: { ... } }
//   | ...

const segmentToString = (segment: Segment) => {
    // rust-style match!
    return match(segment)({
        text: ({ content }) => content,
        link: ({ label, link }) => `[${label}](${link})`,
        _: () => "Unknown",
    });
    // types inferred!
};

segmentToString({
    type: "text",
    data: {
        content: "hi",
        bold: false
    },
});

// NEW! in 0.2
// Optional variant function
segmentToString(
    variant<Segment>("image", { src: "..." })
)

// NEW! in 0.2
// Optional factory-style
const Segment = createFactory<Segment>();

segmentToString(
    Segment.text({ content: "meow", bold: true })
)

Exports

Enum<O>

O extends Record<string, any>

O is an object where the key is the enum variant's name and the value is it's data.

Example:

type SingleOrMany<T> = Enum<{
    single: T,
    many: T[],
}>

// type SingleOrMany<T> =
//   { type: "single", data: T } | { type: "many", data: T[] }

match

Signature: match<Enum, R>(value: Enum) => (matchers: Matcher<Enum, R>) => R

Match on an enum variant. You need to either exhaustively match all variants or provide a wildcard match

Example:

type MyEnum = Enum<{
    a: number;
    b: string[];
    c: boolean;
}>

let value: MyEnum = { type: "a", data: 1 };

// Exhaustive match
match(value)({
    a: () => {},
    b: () => {},
    c: () => {},
})

// Wildcard match
match(value)({
    a: () => {},
    _: () => {},
})

// Example with return value
let stringified = match(value)({
    a: (n: number) => n.toString(),
    b: (arr: string[]) => arr.join(", "),
    c: (b: boolean) => b ? "yes" : "no",

    // example with wildcard
    _: (value: MyEnum) => JSON.stringify(value),
});

// PS. types are annotated purely for documentation,
// your IDE should be able to infer them!

EnumVariant<Enum, Type>

Enum extends Enum<O>, Type extends Enum["type"]

Extract a variant of an enum

Example:

EnumVariant<SingleOrMany<T>, "single">
// = { type: "single", data: T }

EnumData<Enum, Type>

Enum extends Enum<O>, Type extends Enum["type"]

Similar to EnumVariant, but extracts its data type instead

EnumData<SingleOrMany<T>, "many">
// = T[]

variant<Enum>(type, data) => Enum

Create an enum variant

createFactory<Enum>() => EnumInitializers<Enum>

Creates an enum factory. Use it like this:

const SingleOrMany = createFactory<SingleOrMany>();
type SingleOrMany = Enum<{
    single: string,
    many: string[],
}>

SingleOrMany.single("hello world");
// { type: "single", data: "hello world" }