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

ts-jenum

v2.2.2

Published

TypeScript Java Enum

Downloads

8,160

Readme

ts-jenum

  • TypeScript Enum like java.lang.Enum
    • EnumType
    • Enum
  • Powerful tool to comfortable work with plain json struct like enum
    • EnumTools

Installation

npm i ts-jenum

Example in TypeScript

TypeScript Enum like java.lang.Enum

import {Enum, EnumType} from "ts-jenum";

@Enum("text")
export class State extends EnumType<State>() {

    static readonly NEW = new State(1, "New");
    static readonly ACTIVE = new State(2, "Active");
    static readonly BLOCKED = new State(3, "Blocked");

    private constructor(readonly code: number, readonly text: string) {
        super();
    }
}

// Usage example
console.log("" + State.ACTIVE);        // "Active"
console.log("" + State.BLOCKED);       // "Blocked"
console.log(State.values());           // [State.NEW, State.ACTIVE, State.BLOCKED]
console.log(State.valueOf("New"));     // State.NEW
State.valueOf("Unknown")               // throw Error(...)
console.log(State.valueByName("NEW")); // State.NEW
console.log(State.ACTIVE.enumName);    // ACTIVE

const first = state => state.code === 1;
console.log(State.find("New"));        // State.NEW
console.log(State.find(first));        // State.NEW
console.log(State.find("Unknown"));    // null
const last = state => state.code === 3;
console.log(State.filter(last))        // [State.BLOCKED]
console.log(State.keys())              // ["NEW", "ACTIVE", "BLOCKED"]

// be "NEW" | "ACTIVE" | "BLOCKED"
type StateNameUnion = EnumConstNames<typeof State>;

EnumTools powerful tool to comfortable work with plain json struct like enum

import {EnumTools} from "ts-jenum";
// plain json like enum
const Colors = {
    WHITE: "#FFFFFF",
    GRAY: "#808080",
    BLACK: "#000000"
};

// to be ["WHITE", "GRAY", "BLACK"]
const keys = EnumTools.keys(Colors);

// to be ["#FFFFFF", "#808080", "#000000"]
const values = EnumTools.values(Colors);

/**
 * to be {
 *    "#FFFFFF": "WHITE",
 *    "#808080": "GRAY",
 *    "#000000": "BLACK"
 * };
 */
const rStruct = EnumTools.reverse(Colors);

/**
 * to be: [
 *  {key: "WHITE", value: "#FFFFFF"},
 *  {key: "GRAY", value: "#808080"},
 *  {key: "BLACK", value: "#000000"}
 * ]
 */
const pairs = EnumTools.pairs(Colors);

/**
 * To be class like:
 * @Enum<ColorEnum>("key")
 * class ColorEnum extends EnumType<ColorEnum>() {
 *    static readonly WHITE = new ColorEnum("WHITE", "#FFFFFF");
 *    static readonly GRAY = new ColorEnum("GRAY", "#808080");
 *    static readonly BLACK = new ColorEnum("BLACK", "#000000");
 *    private constructor(readonly key: string, readonly value: string | number) {
 *        super();
 *    }
 * }
 * ColorEnum has all IDE hint for developer, type checking and type safety
 */
const ColorEnum = EnumTools.toClass(Colors);

Details. Type safety. In example above, you can write "tExt" or "txt" instead of "text" as @Enum decorator argument and no exception happen. In example below this problem is absent. Add an expression <State> to @Enum decorator

import {Enum, EnumConstNames, EnumType} from "ts-jenum";

@Enum<State>("text")
export class State extends EnumType<State>() {

    static readonly NEW = new State(1, "New");
    static readonly ACTIVE = new State(2, "Active");
    static readonly BLOCKED = new State(3, "Blocked");

    private constructor(readonly code: number, readonly text: string) {
        super();
    }
}
// Get Enum Names
// be "NEW" | "ACTIVE" | "BLOCKED"
type StateNameUnion = EnumConstNames<typeof State>;

Powerful typing.

@Enum<Person>("id")
class Person<IdType extends 1 | 2 = 1 | 2, 
             NameType extends "Ivan" | "John" = "Ivan" | "John"
            > extends EnumType<Person>() {

    static readonly IVAN = new Person(1 as const, "Ivan" as const);
    static readonly JOHN = new Person(2 as const, "John" as const);

    private constructor(readonly id: IdType, readonly name: NameType) {
        super();
    }

    static doSomeWork(): void {
        // type to be: "Ivan". Not string!
        const name = Person.IVAN.name;
        // to be: error
        // if (name === "cat") 
        //     ^ This condition will always return 'false' since the types '"Ivan"' and '"cat"' have no overlap.

        // type to be: 1. Not number!
        const id = Person.IVAN.id;
        // to be: error
        // if (id === 3) 
        //     ^ This condition will always return 'false' since the types '1' and '3' have no overlap
    }
}