A powerful enum utility for TypeScript/JavaScript with full type inference.
npm install enum-progetOptions(), getLabel(), hasValue() attached directly to the enum object.
bash
npm install enum-pro
`
Usage
$3
`typescript
import { toEnum } from 'enum-pro'
const STATUS = toEnum({
PENDING: { label: 'Pending', value: 0 },
SUCCESS: { label: 'Success', value: 1 },
FAILED: { label: 'Failed', value: 2 }
})
// Access properties as usual
console.log(STATUS.PENDING.value) // 0
// Use helper methods
console.log(STATUS.getOptions())
// [
// { name: 'PENDING', label: 'Pending', value: 0 },
// { name: 'SUCCESS', label: 'Success', value: 1 },
// { name: 'FAILED', label: 'Failed', value: 2 }
// ]
console.log(STATUS.getLabel(1)) // 'Success'
console.log(STATUS.hasValue(99)) // false
`
$3
enum-pro automatically infers the types of your enum.
`typescript
type StatusEnum = typeof STATUS
// Enum<{ PENDING: { label: string; value: number; }; ... }>
`
API
$3
Converts a plain object into an enhanced Enum object.
- data: An object where keys are enum names and values are option objects (must have value property, label is recommended).
$3
Every enum created with toEnum has these methods:
- getOptions(): Returns an array of all options, with an added name property corresponding to the key.
- getLabel(value): Returns the label for a given value.
- getOption(value): Returns the full option object for a given value.
- hasValue(value): Returns true` if the value exists in the enum.