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

@ywroh/class-enum

v0.1.2

Published

Extendable class-enum on typescript

Downloads

1

Readme

npm:version

class-enum

The "class-enum" was developed to replace the TypeScript enum.
Since TypeScript's enum only supports formats like numbers and strings, it is lacking in terms of feature expansion.
As a result, we decided to create an enum similar to Java's style that supports constructors.
The "class-enum" can be used in the same way as class constants and offers several convenient features.

Installing

NPM

$ npm i class-enum

Example

Define 'Animal' enum

This library allows you to create enumerations by inheriting ClassEnum<?> from a class.
Please enter the constant variable name as the first argument (of string type). If possible, please follow the rule.

import { ClassEnum, Enum } from 'class-enum'

class Animal extends ClassEnum<Animal> {
    public static readonly DOG = new Animal("DOG")
    public static readonly CAT = new Animal("CAT")
    public static readonly MOUSE = "foo" // ignored in ClassEnum
}

Extending

Sometimes, we need additional attributes for a value. For example, 'DOG' can have values like 'title' and 'color'.
Simply increase the arguments in the constructor and add the values, and that's it.

class Animal extends ClassEnum<Animal> {
    public static readonly DOG = new Animal('DOG', 'My Dog', 3)
    public static readonly CAT = new Animal('CAT', 'Cute Cat', 6)

    private readonly title!: string
    private readonly age!: number

    public constructor(value: string, title: string, age: number) {
        super(value)
        this.title = title
        this.age = age
    }

    public printTitle() {
        console.log(this.title)
    }

    public getAge() {
        return this.age
    }
}

Animal.DOG.printTitle() // My Dog
console.log(`cat age: ${Animal.CAT.getAge()}`) // cat age: 6

API

values()

Retrieve all the values of the Enum as an array.

console.log(Animal.values()) // [ Animal { value: 'DOG' }, Animal { value: 'CAT' } ]
Animal.values().map((animal: Animal) => {
    console.log(animal.name())
})

// DOG -> string
// CAT -> string

valueOf(s: string, defaultEnum = null)

Retrieve the Enum using the value string. If not found, an EnumNotFound exception is raised.
However If you specify the defaultEnum, that will be returned instead.

console.log(Animal.valueOf('DOG'))
console.log(Animal.valueOf('CAT'))
console.log(Animal.valueOf('PARROT')) // occurs EnumNotFound exception
console.log(Animal.valueOf("MONKEY", Animal.ETC))

name()

Retrieve the name of the Enum.

console.log(Animal.DOG.name()) // "DOG"

equals(e: Enum)

Compare if they are the same Enum based on the value.

console.log(Animal.DOG.equals(Animal.DOG)) // true
console.log(Animal.DOG.equals(Animal.CAT)) // false
console.log(Animal.DOG.equals(Other.DOG)) // false

Vue.js

Here's an example of displaying different names and colors based on the selected animal(Enum).

<template>
  <select v-model="selectedAnimal">
    <option v-for="animal in Animal.values()" :key="animal.name()" :value="animal">{{ animal.title }}</option>
  </select>

  <p :style="{color: selectedAnimal.color}">selected:{{ selectedAnimal.name() }}</p>
</template>

<script setup lang="ts">
import { ClassEnum } from "class-enum";
import { ref } from "vue";

class Animal extends ClassEnum<Animal> {
  public static readonly DOG = new Animal("DOG", "cute dog", "red");
  public static readonly CAT = new Animal("CAT", "beautiful cat", "green");
  public static readonly MONKEY = new Animal("MONKEY", "big money", "blue");

  public readonly title!: string;
  public readonly color!: string;

  public constructor(value: string, title: string, color: string) {
    super(value);
    this.title = title;
    this.color = color;
  }
}

const selectedAnimal = ref(Animal.DOG);
</script>

Test

$ npm run clean
$ npm run test

Build/Deploy

$ npm run clean
$ npm run build
$ npm publish