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

enumify-ts

v2.0.2

Published

A JavaScript library that helps with the enum pattern based on TypeScript. This fork of [rauschma/enumify](https://github.com/rauschma/enumify) permits to infer the correct type from constructed collections: for example, the type of `values()` will be `A

Downloads

1,191

Readme

Enumify

A JavaScript library that helps with the enum pattern based on TypeScript. This fork of rauschma/enumify permits to infer the correct type from constructed collections: for example, the type of values() will be Array<YourClass> instead of Array<Enumify>. This can be useful if you want to perform some operation over all the keys or accessing custom properties using valueOf()

Installation:

npm install enumify-ts

Class usage

class Color extends Enumify<Color>() {
   static Red = new Color();
   static Orange = new Color();
   static Yellow = new Color();
   static Green = new Color();
   static Blue = new Color();
   static Purple = new Color();
   private static _ = Color._closeEnum();
}
class Weekday extends Enumify<Weekday>() {
   static Monday = new Weekday(true);
   static Tuesday = new Weekday(true);
   static Wednesday = new Weekday(true);
   static Thursday = new Weekday(true);
   static Friday = new Weekday(true);
   static Saturday = new Weekday(false);
   static Sunday = new Weekday(false);
   static _ = Weekday._closeEnum(() => {
      Weekday.Monday.nextDay = Weekday.Tuesday;
      Weekday.Tuesday.nextDay = Weekday.Wednesday;
      Weekday.Wednesday.nextDay = Weekday.Thursday;
      Weekday.Thursday.nextDay = Weekday.Friday;
      Weekday.Friday.nextDay = Weekday.Saturday;
      Weekday.Saturday.nextDay = Weekday.Sunday;
      Weekday.Sunday.nextDay = Weekday.Monday;
   });

   public nextDay?: Weekday;

   constructor(public isWorkDay: boolean) {
      super();
   }
}
class Mode extends Enumify<Mode>() {
   static user_r = new Mode(0b100000000);
   static user_w = new Mode(0b010000000);
   static user_x = new Mode(0b001000000);
   static group_r = new Mode(0b000100000);
   static group_w = new Mode(0b000010000);
   static group_x = new Mode(0b000001000);
   static all_r = new Mode(0b000000100);
   static all_w = new Mode(0b000000010);
   static all_x = new Mode(0b000000001);

   private static _ = Mode._closeEnum();

   constructor(public n: number) {
      super();
   }
}

_closeEnum() method

It's important to remember calling _closeEnum() in order to statically register all the instances in the inner dictionary. Further calls of the method won't be considered.

static _closeEnum(callback?: () => void)

The method accepts an optional callback that will be executed on the first and only call of the method. It can be used to instantiate specific properties of the instances. An example can be seen in Weekday, where the nextDay property requires a circular definition.

Iteration of instances

Color.keys: ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Purple']

Color.values: [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Purple]

[...Color]: [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Purple]

Instance properties

Color.Red.key: 'Red'

Color.Red.ordinal: 0

Weekday.Sunday.isWorkDay: false

[...Weekday].map(day => day.isWorkDay): [true, true, true, true, true, false, false]

toString and Serialization

`${Color.Red}`: 'Red'

Color.Blue.toJSON(): 'Blue'

Value of a key string

Color.valueOf('Yellow'): Color.Yellow

More examples

See:

  • test/index.spec.ts
  • test/state.spec.ts

Run tests with npm test

Support for public static fields

The enum pattern and Enumify are based on public static fields. Support for them currently looks as follows:

Further reading