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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@manoruchan/enum

v3.0.0

Published

A simple and flexible Enum class for JavaScript / TypeScript.

Readme

Enum

A simple and flexible Enum class for JavaScript / TypeScript.

@manoruchan/enum is an Enum class implementation

that maintains your values as constant variable after its instantiation

and supports extension with your own methods.

Why use this instead of the native enum?

Because @manoruchan/enum offers custom method definition and supports dynamic enums,

just like Java's Enum class but simpler and more flexible for JavaScript / TypeScript.

Furthermore, @manoruchan/enum now supports full type inference and autocomplete via the new Enum.create() static factory method.

Installation

npm install @manoruchan/enum

Usage

Basic Usage

import { Enum } from "@manoruchan/enum";

const Color = Enum.create([
    "RED",
    "GREEN",
    "BLUE"
] as const);

console.log(Color.RED);            // 0
console.log(Color.valueOf("RED")); // 0
console.log(Color.get(0));         // 'RED'
console.log(Color.values());       // [ 0, 1, 2 ]
console.log(Color.names());        // [ 'RED', 'GREEN', 'BLUE' ]
console.log(Color.toArray());      // [ [ 'RED', 0 ], [ 'GREEN', 1 ], [ 'BLUE', 2 ] ]

With Custom Values

import { Enum } from "@manoruchan/enum";

const Fruits = Enum.create({
    APPLE: "red",
    ORANGE: "orange",
    BANANA: "yellow"
});

console.log(Fruits.APPLE);             // red
console.log(Fruits.valueOf("ORANGE")); // 'orange'
console.log(Fruits.get("yellow"));     // 'BANANA'
console.log(Fruits.values());          // [ 'red', 'orange', 'yellow' ]

Enum With Operations

import { Enum, EnumValue } from "@manoruchan/enum";

class Week extends Enum {
    public isWeekend(day: EnumValue): boolean {
        return day === this.SAT || day === this.SUN;
    }
}

const days = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"] as const;
const week = Week.create(days);

console.log(week.isWeekend(week.SUN)); // true
console.log(week.isWeekend(week.TUE)); // false
class HttpCode extends Enum<Record<string, number>> {
    public isError(value: number): boolean {
        return value >= 400;
    }

    public isClientError(value: number): boolean {
        return this.BAD_REQUEST <= value && value < this.INTERNAL_SERVER_ERROR;
    }
}

const codes = {
    OK: 200,
    BAD_REQUEST: 400,
    NOT_FOUND: 404,
    INTERNAL_SERVER_ERROR: 500,
} as const;

const http = HttpCode.create(codes);

console.log(http.isClientError(http.NOT_FOUND)); // true
console.log(http.isError(http.OK)); // false

Important Note on Autocomplete

To ensure proper type inference for array input to constructor, the argument must be annotated with as const.

Example (Array Input with Autocomplete)

// `as const` is required here
const Status= Enum.create(["ACTIVE", "INACTIVE"] as const);

console.log(Status.ACTIVE); // 0
// without `as const`, autocomplete is not available for Status.ACTIVE

Example (Object Input with Autocomplete)

// as const is not required here, but recommended.
const Fruits = Enum.create({
    APPLE: "red",
    ORANGE: "orange",
    BANANA: "yellow"
});
// } as const);

console.log(Fruits.APPLE); // red

EnumValue

type EnumValue = string | number | boolean;

EnumValue accepts primitive types.

API

static create(v: readonly string[] | Record<string, EnumValue>): Enum Creates a type-safe Enum instance. The return type provides full autocomplete based on the input v.

get(value: EnumValue): string | undefined Obtains the name associated with the enum value.

valueOf(name: string): EnumValue | undefined Obtains the enum value from its name.

values(): EnumValue[] Returns the array of enum values.

names(): string[] Returns the array of the name of enum values.

toArray(): [string, EnumValue][] Returns the entries of names and values.

LICENSE

MIT