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

iron-enum

v1.0.4

Published

Rust like enums for Typescript

Downloads

12

Readme

Iron Enum

Finally Rust like enums in Typescript!

  • Ergonomic AF!
  • Fully type safe!
  • Only 400 bytes gzipped!
  • Includes Option and Result types!

| Github | NPM | JSR |

Typescript enums only provide simple variants:

enum Shape {
    Square,
    Circle
}

But what if you wanted to provide data for each variant that is context specific? Well now you can!

Code Example

import { IronEnum } from "iron-enum";

const ShapeEnum = IronEnum<{
    Empty: {},
    Square: { width: number, height: number },
    Circle: { radius: number }
}>();

const exampleShape = ShapeEnum.Square({width: 22, height: 50});

// Supports matching, similar to switch case statements
exampleShape.match({
    // case Empty:
    Empty: () => {
        // runs if the shape is empty
    },
    // case Square: 
    Square: ({width, height}) => {
        // runs if the shape is square
    },
    // case Circle:
    Circle: ({radius}) => {
        // runs if the shape is circle
    }
});

// supports fallback cases
exampleShape.match({
    Square: ({width, height}) => {
        // runs if the shape is square
    },
    _: () => {
        // runs if it's anything but a square
    }
});


// Supports returns through match
const result = exampleShape.match({
    Empty: () => return 0;
    Square: ({width, height}) => width,
    _: () => false
});
// result type is inherited from match arm return types.
// typeof result == number | boolean

if (exampleShape.if.Square()) {
    // runs if the shape is a square
}

if (exampleShape.ifNot.Square()) {
    // runs if the shape is NOT a square
}

console.log(exampleShape.unwrap())
// output: ["Square", { width: 22, height: 50 }]

// this method will only allow ShapeEnum variants as an argument
const someFn = (onlyShapeEnum: typeof ShapeEnum._self.prototype) => {

}

Just like in Rust, the .match(...) keys must contain a callback for each variant OR provide a fallback method with a _ property. Failing this constraint leads to a type error.

Option & Result Examples

import { Option, Result } from "iron-enum";

const NumOption = Option<number>();

const myNum = NumOption.Some(22);

myNum.match({
    Some: (num) => {
        // only runs if myNum is "Some" variant
    },
    None: () => {
        // only runs if myNum is "None" variant
    }
})

const NumResult = Result<number, Error>();

const myNum2 = NumResult.Ok(22);

myNum2.match({
    Ok: (num) => {
        // only runs if myNum2 is "Ok" variant
    },
    Err: () => {
        // only runs if myNum2 is "Err" variant
    }
})

if (myNum2.if.Ok()) {
    // only runs if myNum2 is "Ok" variant
}