|
| 1 | +// deno-lint-ignore-file no-slow-types |
| 2 | +// @ts-self-types="../type/enum.d.ts" |
| 3 | + |
| 4 | +/** |
| 5 | + * Abstract base class for creating enumeration types in JavaScript |
| 6 | + * @abstract |
| 7 | + * @example |
| 8 | + * ```js |
| 9 | + * class Color extends Enum { |
| 10 | + * static RED = new Color('RED', '#FF0000'); |
| 11 | + * static GREEN = new Color('GREEN', '#00FF00'); |
| 12 | + * static BLUE = new Color('BLUE', '#0000FF'); |
| 13 | + * |
| 14 | + * static { |
| 15 | + * this.freeze(); |
| 16 | + * } |
| 17 | + * } |
| 18 | + * ``` |
| 19 | + */ |
| 20 | +export class Enum { |
| 21 | + /** @private @static @type {Map<Function, Map<string, Enum>>} */ |
| 22 | + static #instances = new Map(); |
| 23 | + |
| 24 | + /** @private @static @type {Set<Function>} */ |
| 25 | + static #frozen = new Set(); |
| 26 | + |
| 27 | + /** @private @type {string} */ |
| 28 | + #name; |
| 29 | + |
| 30 | + /** @private @type {*} */ |
| 31 | + #value; |
| 32 | + |
| 33 | + /** @private @type {number} */ |
| 34 | + #ordinal; |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a new enum constant |
| 38 | + * @param {string} name - The name of the enum constant |
| 39 | + * @param {*} value - The value associated with the enum constant |
| 40 | + * @throws {TypeError} If attempting to instantiate Enum directly |
| 41 | + * @throws {Error} If enum is frozen or if duplicate name/value exists |
| 42 | + */ |
| 43 | + constructor(name, value) { |
| 44 | + if (this.constructor === Enum) { |
| 45 | + throw new TypeError("Cannot instantiate abstract Enum class directly"); |
| 46 | + } |
| 47 | + |
| 48 | + this.#name = name; |
| 49 | + this.#value = value; |
| 50 | + this.#ordinal = this.constructor.size; |
| 51 | + |
| 52 | + // Register instance |
| 53 | + if (!Enum.#instances.has(this.constructor)) { |
| 54 | + Enum.#instances.set(this.constructor, new Map()); |
| 55 | + } |
| 56 | + |
| 57 | + const instanceMap = Enum.#instances.get(this.constructor); |
| 58 | + |
| 59 | + if (Enum.#frozen.has(this.constructor)) { |
| 60 | + throw new Error(`Cannot add new enum constant ${name} to frozen enum ${this.constructor.name}`); |
| 61 | + } |
| 62 | + |
| 63 | + if (instanceMap.has(name)) { |
| 64 | + throw new Error(`Duplicate enum constant name: ${name}`); |
| 65 | + } |
| 66 | + |
| 67 | + for (const [, instance] of instanceMap) { |
| 68 | + if (instance.value === value) { |
| 69 | + throw new Error(`Duplicate enum value: ${value} for constant ${name}`); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + instanceMap.set(name, this); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Gets the name of the enum constant |
| 78 | + * @returns {string} The enum constant's name |
| 79 | + */ |
| 80 | + get name() { |
| 81 | + return this.#name; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Gets the value of the enum constant |
| 86 | + * @returns {*} The enum constant's value |
| 87 | + */ |
| 88 | + get value() { |
| 89 | + return this.#value; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Gets the ordinal (position) of the enum constant |
| 94 | + * @returns {number} The enum constant's ordinal |
| 95 | + */ |
| 96 | + get ordinal() { |
| 97 | + return this.#ordinal; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Gets the number of constants in the enum |
| 102 | + * @returns {number} The number of enum constants |
| 103 | + */ |
| 104 | + static get size() { |
| 105 | + return this.values().length; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Gets all constants of the enum |
| 110 | + * @returns {Enum[]} Array of all enum constants |
| 111 | + */ |
| 112 | + static values() { |
| 113 | + const instanceMap = Enum.#instances.get(this) || new Map(); |
| 114 | + return Array.from(instanceMap.values()); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Gets all constant names of the enum |
| 119 | + * @returns {string[]} Array of all enum constant names |
| 120 | + */ |
| 121 | + static names() { |
| 122 | + const instanceMap = Enum.#instances.get(this) || new Map(); |
| 123 | + return Array.from(instanceMap.keys()); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Gets an enum constant by name |
| 128 | + * @param {string} name - The name of the enum constant |
| 129 | + * @returns {Enum} The enum constant |
| 130 | + * @throws {Error} If no constant exists with the given name |
| 131 | + */ |
| 132 | + static valueOf(name) { |
| 133 | + const instanceMap = Enum.#instances.get(this); |
| 134 | + if (!instanceMap || !instanceMap.has(name)) { |
| 135 | + throw new Error(`No enum constant ${this.name}.${name}`); |
| 136 | + } |
| 137 | + return instanceMap.get(name); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Gets an enum constant by value |
| 142 | + * @param {*} value - The value to look up |
| 143 | + * @returns {Enum} The enum constant |
| 144 | + * @throws {Error} If no constant exists with the given value |
| 145 | + */ |
| 146 | + static fromValue(value) { |
| 147 | + const instance = this.values().find(inst => inst.value === value); |
| 148 | + if (!instance) { |
| 149 | + throw new Error(`No enum constant with value ${value} in ${this.name}`); |
| 150 | + } |
| 151 | + return instance; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Freezes the enum, preventing further constant additions |
| 156 | + * @returns {typeof Enum} The enum class |
| 157 | + */ |
| 158 | + static freeze() { |
| 159 | + Enum.#frozen.add(this); |
| 160 | + return this; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Checks if the enum is frozen |
| 165 | + * @returns {boolean} True if the enum is frozen |
| 166 | + */ |
| 167 | + static isFrozen() { |
| 168 | + return Enum.#frozen.has(this); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Gets string representation of the enum constant |
| 173 | + * @returns {string} String representation |
| 174 | + */ |
| 175 | + toString() { |
| 176 | + return `${this.constructor.name}.${this.name}`; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Gets JSON representation of the enum constant |
| 181 | + * @returns {{name: string, value: *, ordinal: number}} JSON representation |
| 182 | + */ |
| 183 | + toJSON() { |
| 184 | + return { |
| 185 | + name: this.name, |
| 186 | + value: this.value, |
| 187 | + ordinal: this.ordinal |
| 188 | + }; |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Converts enum constant to primitive type |
| 193 | + * @param {string} hint - The type hint ('string', 'number', or 'default') |
| 194 | + * @returns {string|number|*} The primitive value |
| 195 | + */ |
| 196 | + [Symbol.toPrimitive](hint) { |
| 197 | + switch (hint) { |
| 198 | + case 'number': |
| 199 | + return this.value; |
| 200 | + case 'string': |
| 201 | + return this.toString(); |
| 202 | + default: |
| 203 | + return this.value; |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Checks if this enum constant equals another |
| 209 | + * @param {Enum} other - The enum constant to compare with |
| 210 | + * @returns {boolean} True if the constants are equal |
| 211 | + */ |
| 212 | + equals(other) { |
| 213 | + if (!(other instanceof this.constructor)) { |
| 214 | + return false; |
| 215 | + } |
| 216 | + return this.name === other.name && this.value === other.value; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Compares this enum constant with another by ordinal |
| 221 | + * @param {Enum} other - The enum constant to compare with |
| 222 | + * @returns {number} Negative if this comes before other, positive if after |
| 223 | + * @throws {TypeError} If comparing with different enum type |
| 224 | + */ |
| 225 | + compareTo(other) { |
| 226 | + if (!(other instanceof this.constructor)) { |
| 227 | + throw new TypeError(`Cannot compare ${this.constructor.name} with ${other?.constructor?.name}`); |
| 228 | + } |
| 229 | + return this.ordinal - other.ordinal; |
| 230 | + } |
| 231 | +} |
| 232 | + |
| 233 | + |
| 234 | + |
| 235 | +// npx -p typescript tsc ./src/enum.js --declaration --allowJs --emitDeclarationOnly --lib ESNext --outDir ./dist |
0 commit comments