GithubHelp home page GithubHelp logo

class-enum's Introduction

npm:version

class-enum

class-enum은 typescript enum을 대체하기 위하여 만들어졌습니다.
typescript의 enum은 숫자, 문자열 정도의 형식만 지원하기 때문에 추가적인 기능 확장에 아쉬움이 있습니다. 따라서 생성자 지원이 가능한 자바 형태의 enum을 만들기로 했습니다.

설치

NPM

$ npm i class-enum

사용방법

기본 정의

ClassEnum클래스 상속을 통하여 상수를 선언합니다. 첫 번째 인자로 unique value 문자열을 넣습니다.

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
}

확장

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

기능

values()

Enum 값을 모두 가져옵니다.

console.log(Animal.values()) // [ Animal { value: 'DOG' }, Animal { value: 'CAT' } ]

valueOf(s: string)

value 문자열로부터 Enum을 찾아옵니다.

console.log(Animal.valueOf('DOG'))
console.log(Animal.valueOf('CAT'))
console.log(Animal.valueOf('PARROT')) // occurs EnumNotFound exception

name()

value 문자열을 가져옵니다.

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

equals(e: Enum)

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

쓰임새

vuejs

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

class-enum's People

Contributors

leejaycoke avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.