1.0.0 • Published 4 years ago
@teplovs/js-enum v1.0.0
js-enum
Javascript enum implementation
Table of contents:
Getting started
Install js-enum as dependency:
npm install @teplovs/js-enum --save
# or:
yarn add @teplovs/js-enum
Then import it in your project:
import { Enum } from "@teplovs/js-enum"
Usage
// example #1: creating enum without specifying enum item values.
// (when value is not specified `Symbol(itemName)` will be used)
class UserType extends Enum {
static admin = new UserType("admin")
static moderator = new UserType("moderator")
static member = new UserType("member")
}
Object.freeze(UserType)
console.log(UserType.admin instanceof UserType) // true
console.log(UserType.admin.value) // Symbol('admin')
console.log(UserType.admin.name) // "admin"
// example #2: creating enum with specifying enum item values.
class CSSStyleMapping extends Enum {
static foregroundColor = new CSSStyleMapping("foregroundColor", "color")
static fontSize = new CSSStyleMapping("fontSize", "fontSize")
}
Object.freeze(CSSStyleMapping)
console.log(CSSStyleMapping.foregroundColor instanceof CSSStyleMapping) // true
console.log(CSSStyleMapping.foregroundColor.value) // "color"
console.log(CSSStyleMapping.foregroundColor.name) // "foregroundColor"
API
Enum(itemName: string, value: null|undefined|*)
Constructor for enum item. If value is not specified, Symbol(itemName)
will be used.
Properties
name: string
Name of the enum item
value: Symbol|*
Value of the enum item
1.0.0
4 years ago