Skip to content

Latest commit

 

History

History
108 lines (79 loc) · 4.32 KB

README.md

File metadata and controls

108 lines (79 loc) · 4.32 KB

Create vanilla JavaScript enums

NPM version Bugs Code Smells Duplicated Lines (%) Maintainability Rating Reliability Rating Security Rating Technical Debt Vulnerabilities Quality Gate Status Coverage

Installation

npm install @trojs/enum or yarn add @trojs/enum

Test the package

npm run test or yarn test

Usage

import { Enum } from '@trojs/enum'

class Example extends Enum {
  static test = 'TEXT'
  static another = 42
}

Example.test // 'TEXT'
Example.options // { test: 'TEXT', another: 42 }
Example.options.test // 'TEXT'

const example = Example.fromKey('test')
example.key // 'test'
example.value // 'TEXT'
example.values // [ 'TEXT', 42 ]
example.keys // [ 'test', 'another ]
example.test // 'TEXT'
example.another // 42
example.length // 2
example.name // 'Example'

example.is(Example.test) // true
example.is('TEXT') // true
example.is(42) // false
example.in([Example.test]) // true
example.in(['TEXT']) // true
example.in([42]) // false

example.valueOf() // 42
example.toString() // 'test'
example.toJSON() // 'test'
JSON.stringify(example) // '"test"'

const example = Example.create('test')
example.key // 'test'
example.value // 'TEXT'

const example = Example.fromValue('TEXT')
example.key // test
example.value // TEXT

Example.hasKey('test') // teue
Example.hasKey('TEXT') // false
Example.hasValue('test') // teue
Example.hasValue('TEXT') // false

Example.toJSON() // { test: 'TEXT', another: 42 }
JSON.stringify(Example) // '{"test":"TEXT","another":42}'

const example = Example.create('test', { output: 'value' })
example.valueOf() // 42
example.toString() // '42'
example.toJSON() // 42
JSON.stringify(example) // '42'