@h1n0l/smart-enum
v1.0.2
Published
SmartEnum is a lightweight TypeScript utility that brings rich behavior to enums, using object-oriented principles. Unlike traditional enums, SmartEnum allows you to encapsulate logic, metadata, and utility methods within enum-like classes.
Readme
smart-enum
A lightweight TypeScript library for creating smart enums — enhanced enumerations with custom names, values, and utility methods like fromValue(), fromName(), and duplicate checking.
Features
- Strongly typed enum classes with arbitrary values
- Automatic prevention of duplicate names and values
- Retrieve enum instances by name or value
- Iterable enum collections
- Works with both ES Modules and CommonJS
Installation
npm install @h1n0l/smart-enumUsage
import { SmartEnum } from '@h1n0l/smart-enum';
class Status extends SmartEnum<string> {
static readonly Active = new Status('Active', 'active');
static readonly Inactive = new Status('Inactive', 'inactive');
protected constructor(name: string, value: string) {
super(name, value);
}
}
console.log(Status.values());
// [
// Status { name: 'Active', value: 'active' },
// Status { name: 'Inactive', value: 'inactive' }
// ]
console.log(Status.fromValue('active')); // Status { name: 'Active', value: 'active' }
console.log(Status.fromName('Inactive')); // Status { name: 'Inactive', value: 'inactive' }
console.log(Status.fromValue('missing')); // undefined
console.log(Status.hasValue('active')); // true
console.log(Status.hasName('Missing')); // falseAPI
SmartEnum<TValue>
Abstract base class. Extend this to define your smart enum types.
Static methods:
values()— returns an array of all enum instancesfromValue(value: TValue)— returns enum instance matching the value, orundefinedfromName(name: string)— returns enum instance matching the name, orundefinedhasValue(value: TValue)— boolean check if value existshasName(name: string)— boolean check if name exists
Instance properties:
name— enum item name (string)value— enum item value (TValue)
Any questions?
dm me on discord: h1n0l
