@andrewdonelson/byte-enum
Version:
A high-performance, memory-optimized enum implementation for TypeScript
63 lines (62 loc) • 2.53 kB
TypeScript
/**
* ByteEnum - Memory-Efficient Enum Utilities for TypeScript
*
* A high-performance, memory-optimized enum implementation with
* type safety and runtime utility methods.
*
* Features:
* - Minimal memory footprint using byte values or single characters
* - High-performance lookups using Map-based implementation
* - Type safety with TypeScript
* - Runtime utility methods for validation and display
* - Case-insensitive key handling (all keys normalized to UPPERCASE)
*
* @license MIT
* File: src/byteEnum.ts
*/
/**
* Creates a memory-efficient enum using byte values (0-255)
*
* @param keys - Array of enum keys as string literals
* @returns Enum object with utility methods
*/
export declare function createByteEnum<T extends string>(keys: readonly T[]): TinyByteEnum<Uppercase<T>, number>;
export type TinyByteEnum<K extends string, V extends number> = {
[key in K]: V;
} & {
values: () => V[];
keys: () => K[];
isValue: (value: number) => value is V;
isKey: (key: string) => key is K;
getKeyByValue: (value: V) => K | undefined;
getDisplayName: (value: V) => string;
getFormattedName: (value: V, formatter: (key: string) => string) => string;
};
/**
* Creates a memory-efficient enum using single byte characters (0-255)
*
* @param keys - Array of enum keys as string literals
* @param customCharSet - Optional custom character set to use (defaults to all 256 byte values)
* @returns Enum object with utility methods
*/
export declare function createCharEnum<T extends string>(keys: readonly T[], customCharSet?: string): TinyCharEnum<Uppercase<T>, string>;
/**
* Creates a memory-efficient enum using alphanumeric characters (0-9, a-z, A-Z)
* This is provided for compatibility with systems that require printable characters.
*
* @param keys - Array of enum keys as string literals
* @returns Enum object with utility methods
*/
export declare function createAlphaNumEnum<T extends string>(keys: readonly T[]): TinyCharEnum<Uppercase<T>, string>;
export type TinyCharEnum<K extends string, V extends string> = {
[key in K]: V;
} & {
values: () => V[];
keys: () => K[];
isValue: (value: string) => value is V;
isKey: (key: string) => key is K;
getKeyByValue: (value: V) => K | undefined;
getDisplayName: (value: V) => string;
getFormattedName: (value: V, formatter: (key: string) => string) => string;
};
export type TinyEnumValue<T> = T extends TinyByteEnum<any, infer V> ? V : T extends TinyCharEnum<any, infer V> ? V : never;