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-enum-extended

v1.0.0

Published

Enums for TS|JS

Downloads

4

Readme

Enums for TypeScript/JavaScript

This package offers typed enums for TypeScript/JavaScript

class User extends Enum {
    @enumMember(1)
    static Client: User;

    @enumMember(2)
    static Manager: User

    static labels() {
        return {
            Client: 'client',
            Manager: 'manager',
        };
    }

    static descriptions() {
        return {
            Client: 'Site client',
            Manager: 'Sales manager',
        };
    }
}

User.Manager instanceof User; // true

User.Client.value // 1
User.Client.label // client
User.Client.description // Site client

User.fromValue(2) // User.Manager
User.toValues() // [1, 2]
User.toNames() // ['Client', 'Manager']
User.all() // [User.Client, User.Manager]

Installation

Yarn:

yarn add ts-enum

NPM:

npm i ts-enum

Usage

You can choose one of two ways to define enumeration members.

Using decorator:

class User extends Enum {
    @enumMember(1)
    static Client: User;

    @enumMember(2)
    static Manager: User

    static labels() {
        return {
            Client: 'client',
            Manager: 'manager',
        };
    }

    static descriptions() {
        return {
            Client: 'Site client',
            Manager: 'Sales manager',
        };
    }
}

export { User }

:warning: If you decide to use decorator to declare enumeration members, you need to set up decorator support in your project

TS:

To enable experimental support for decorators, you must enable the experimentalDecorators compiler option in your tsconfig.json:

{
 "compilerOptions": {
   "target": "ES5",
   "experimentalDecorators": true
 }
}

JS:

To enable decorators, you must add @babel/plugin-proposal-decorators in your babel.config.json

{
 "plugins": ["@babel/plugin-proposal-decorators"]
}

Using declareEnum() method:

If you don't want to install additional dependencies, you can call the method declareEnum() before exporting the class.

class EnumByDeclareEnum extends Enum {
    static Admin = 1
    static User = 2

    static labels() {
        return {
            Admin: 'admin',
            User: 'user',
        };
    }

    static descriptions() {
        return {
            Admin: 'Administrator',
            User: 'Site user',
        };
    }
}

EnumByDeclareEnum.declareEnum()

export { EnumByDeclareEnum }

:warning: This method is recommended to be used ONLY in projects using Javascript. Using the method declareEnum() to declare members can cause type errors in TypeScript

Testing

yarn install
yarn run test:unit