UNPKG

enumify-ts

Version:

A JavaScript library that helps with the enum pattern based on TypeScript. This fork of [rauschma/enumify](https://github.com/rauschma/enumify) permits to infer the correct type from constructed collections: for example, the type of `values()` will be `A

51 lines 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Enumify = void 0; function Enumify() { return class Enumify { static get keys() { return Object.keys(this._instances); } static get values() { return Object.values(this._instances); } get key() { return this._key; } get ordinal() { return this._ordinal; } static _closeEnum(callback) { if (this._instances == null) { if (callback) { callback(); } const instances = {}; // Traverse the enum entries Object.entries(this).forEach(([key, value], index) => { instances[key] = value; value._key = key; value._ordinal = index; }); this._instances = instances; } } static valueOf(str) { return this._instances[str]; } static [Symbol.iterator]() { return this.values[Symbol.iterator](); } static fromString(str) { return this.valueOf(str); } toJSON() { return this.key; } toString() { return this.key; } }; } exports.Enumify = Enumify; //# sourceMappingURL=index.js.map