UNPKG

@northscaler/better-enum

Version:

Better enumeration support for TypeScript than its `enum` keyword. This class is modeled after [Java's enumeration pattern](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html), where enums are instances of classes. This library provides a base

92 lines (91 loc) 5.33 kB
/** * Base enumeration class for the pattern where enumerated values are represented by instances of classes rather than by scalar string or numeric values alone. * * Every subclass `T` has the following requirements. * * * `T` must not be extended. * * * `T` must define a `private` constructor whose first two arguments are `name: string, ordinal: number`, * * where `name` is a valid JavaScript identifier that is the same as the instance's static identifier on `T`, and * * where `ordinal` is an integral value that is unique amongst all instances. * * * `T` must define a `static` method `of(it: T | string | number): T` that returns an instance of `T` whose `name` or `ordinal` is equal to the given value, or that is identical to the instance of `T` given, and must throw {@link UnidentifiableEnumerationValueError} otherwise. * * > NOTE: {@link Enumeration} provides a convenient function that subclasses can delegate to in order to implement the required `of` method above. * > Use `import { _of } from '@northscaler/better-enum' and delegate the subclass's `of()` method to it. * * * `T` must define a `static` method `values(): T[]` that returns all instances of `T`. * No ordering is prescribed. * However, you can use one of the convenient sorting functions provided by this class, {@link sortByName} or {@link sortByOrdinal}, to sort the instances, or write your own. * * > NOTE: {@link Enumeration} provides a convenient function that subclasses can delegate to in order to implement the required `values` method above. * > Use `import { _values } from '@northscaler/better-enum' and delegate the subclass's `values()` method to it. */ export declare class Enumeration<T extends Enumeration<T>> { protected _name: string; protected _ordinal: number; protected _class: Function; /** * Constructs an enumerated value. * @param _name The symbolic name of this instance. * It must be a valid JavaScript symbol and must be unique amongst all instances of this class's type. * @param _ordinal The numeric ordinal of this instance. * It must be an integer value and must be unique amongst instances of this class's type. * @param _class The class function of the subclass calling this constructor. * For example, given `class Bool extends Enumeration<Bool> { ... }`, the reference to the class's function is `Bool`. * @throws InvalidEnumerationNameError If the `_name` parameter is not a valid JavaScript identifier. * @throws InvalidEnumerationOrdinalError If the `_ordinal` parameter is not an integer. * @throws DuplicateEnumerationDeclarationError If there is already a value with the given `_name` or `_ordinal` amongst all instances of this class's type. * @protected */ protected constructor(_name: string, _ordinal: number, _class: Function); /** * The symbolic name of this enumerated value. */ name(): string; /** * The integer ordinal of this enumerated value. */ ordinal(): number; /** * Returns whether the given object has the same name, ordinal & constructor name as this object. */ equals(that: T): boolean; /** * Returns the symbolic name of this enumerated value. * @param fullyQualified Whether to return the return value of {@link toFullyQualifiedString} or just the symbolic name; defaults to `false`. */ toString(fullyQualified?: boolean): string; /** * Returns a colon-separated string of this class's name, its symbolic name, and its ordinal (like `Bool:TRUE:1`). */ toFullyQualifiedString(): string; /** * Default `toJSON()` protocol method. * Returns the symbolic name of this enumerated value. * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior * * To implement a JSON [`reviver`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#using_the_reviver_parameter), use the static `of` method defined on your class. */ toJSON(): string; } /** * A function that attempts to locate an enumerated value given its name or ordinal. * If given an argument that is `typeof object`, it must be an enumerated value, which is simply returned. * Any value that is not among the symbolic names, ordinals, or instances of the enumeration class will cause this method to throw an `UnidentifiableEnumerationValueError`. * * This function is only intended to be used by subclasses of {@link Enumeration}. * * @param it The value being used to identify the enumerated value. * @param clazz A reference to the enumeration class (if `class Foo ... {}`, then the reference `Foo` should be given). */ export declare function _of<T extends Enumeration<T>>(it: T | string | number, clazz: Function): T; export declare function _values<T extends Enumeration<T>>(clazz: Function): T[]; /** * Returns a convenient function to sort enumerations by their names. */ export declare const sortByName: <T extends Enumeration<T>>(a: Enumeration<T>, b: Enumeration<T>) => number; /** * Returns a convenient function to sort enumerations by their ordinals. */ export declare const sortByOrdinal: <T extends Enumeration<T>>(a: Enumeration<T>, b: Enumeration<T>) => number;