Socket
Socket
Sign inDemoInstall

ts-enum-util

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-enum-util - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

94

dist/commonjs/index.js

@@ -10,4 +10,4 @@ "use strict";

* instance:
* - {@link Enum.get}
* - {@link Enum.create}
* - {@link EnumWrapper.getInstance}
* - {@link EnumWrapper.createInstance}
*

@@ -17,3 +17,3 @@ * @template V - Type of the enum value.

*/
var Enum = /** @class */ (function () {
var EnumWrapper = /** @class */ (function () {
/**

@@ -24,8 +24,8 @@ * Create a new Enum instance.

* instance:
* - {@link Enum.get}
* - {@link Enum.create}
* - {@link EnumWrapper.getInstance}
* - {@link EnumWrapper.createInstance}
*
* @param enumObj - An enum-like object. See the {@link EnumLike} type for more explanation.
*/
function Enum(enumObj) {
function EnumWrapper(enumObj) {
var _this = this;

@@ -55,3 +55,3 @@ this.enumObj = enumObj;

* Creates a new Enum for an enum-like object.
* You probably want to use {@link Enum.get} for any typical enums, because it will
* You probably want to use {@link EnumWrapper.getInstance} for any typical enums, because it will
* cache the result.

@@ -64,18 +64,18 @@ * This method may be useful if you want an Enum for an enum-like object that is dynamically

*/
Enum.create = function (enumObj) {
return new Enum(enumObj);
EnumWrapper.createInstance = function (enumObj) {
return new EnumWrapper(enumObj);
};
/**
* Creates a new Enum, or returns a cached Enum if one has already been created for the same
* object via {@link Enum.get}.
* object via {@link EnumWrapper.getInstance}.
* This is most useful for typical enums that are statically defined, because the cached Enum instance
* will be quickly retrieved/reused on every sebsequent call to get() for the same enum object.
* Use {@link Enum.create} if you don't want the Enum to be cached.
* will be quickly retrieved/reused on every subsequent call to get() for the same enum object.
* Use {@link EnumWrapper.createInstance} if you don't want the Enum to be cached.
* @param enumObj - An enum-like object.
* @return An instance of Enum for the provided enumObj.
*/
Enum.get = function (enumObj) {
EnumWrapper.getInstance = function (enumObj) {
var result = this.instancesCache.get(enumObj);
if (!result) {
result = this.create(enumObj);
result = this.createInstance(enumObj);
this.instancesCache.set(enumObj, result);

@@ -85,3 +85,3 @@ }

};
Object.defineProperty(Enum.prototype, "size", {
Object.defineProperty(EnumWrapper.prototype, "size", {
/**

@@ -100,3 +100,3 @@ * The number of entries in this enum.

*/
Enum.prototype.getKeys = function () {
EnumWrapper.prototype.getKeys = function () {
return Array.from(this.keySet.values());

@@ -107,6 +107,6 @@ };

* NOTE: If this enum has any duplicate values, only unique values will be returned, and the
* length of the list will be less than {@link Enum#size}.
* length of the list will be less than {@link EnumWrapper#size}.
* @return A list of this enum's values.
*/
Enum.prototype.getValues = function () {
EnumWrapper.prototype.getValues = function () {
return Array.from(this.valueSet.values());

@@ -118,3 +118,3 @@ };

*/
Enum.prototype.getEntries = function () {
EnumWrapper.prototype.getEntries = function () {
return Array.from(this);

@@ -126,3 +126,3 @@ };

*/
Enum.prototype.keys = function () {
EnumWrapper.prototype.keys = function () {
return this.keySet.values();

@@ -133,6 +133,6 @@ };

* NOTE: If this enum has any duplicate values, only unique values will be iterated, and the
* number of values iterated will be less than {@link Enum#size}.
* number of values iterated will be less than {@link EnumWrapper#size}.
* @return An iterator that iterates over this enum's values.
*/
Enum.prototype.values = function () {
EnumWrapper.prototype.values = function () {
return this.valueSet.values();

@@ -144,3 +144,3 @@ };

*/
Enum.prototype.entries = function () {
EnumWrapper.prototype.entries = function () {
var _this = this;

@@ -169,3 +169,3 @@ var keyIterator = this.keys();

*/
Enum.prototype[Symbol.iterator] = function () {
EnumWrapper.prototype[Symbol.iterator] = function () {
return this.entries();

@@ -175,3 +175,3 @@ };

* Calls the provided iteratee on each item in this enum.
* See {@link Enum.Iteratee} for the signature of the iteratee.
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee.
* The return value of the iteratee is ignored.

@@ -181,3 +181,3 @@ * @param iteratee - The iteratee.

*/
Enum.prototype.forEach = function (iteratee, context) {
EnumWrapper.prototype.forEach = function (iteratee, context) {
var _this = this;

@@ -191,3 +191,3 @@ this.keySet.forEach(function (key) {

* Builds a new array containing the results of calling the provided iteratee on each item in this enum.
* See {@link Enum.Iteratee} for the signature of the iteratee.
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee.
* @param iteratee - The iteratee.

@@ -197,3 +197,3 @@ * @param context - If provided, then the iteratee will be called with the context as its "this" value.

*/
Enum.prototype.map = function (iteratee, context) {
EnumWrapper.prototype.map = function (iteratee, context) {
var _this = this;

@@ -206,6 +206,6 @@ var result = [];

};
Enum.prototype.isKey = function (key) {
EnumWrapper.prototype.isKey = function (key) {
return key !== undefined && this.keySet.has(key);
};
Enum.prototype.asKey = function (key) {
EnumWrapper.prototype.asKey = function (key) {
if (this.isKey(key)) {

@@ -218,3 +218,3 @@ return key;

};
Enum.prototype.asKeyOrDefault = function (key, defaultKey) {
EnumWrapper.prototype.asKeyOrDefault = function (key, defaultKey) {
if (this.isKey(key)) {

@@ -227,6 +227,6 @@ return key;

};
Enum.prototype.isValue = function (value) {
EnumWrapper.prototype.isValue = function (value) {
return value !== undefined && this.valueSet.has(value);
};
Enum.prototype.asValue = function (value) {
EnumWrapper.prototype.asValue = function (value) {
if (this.isValue(value)) {

@@ -239,3 +239,3 @@ return value;

};
Enum.prototype.asValueOrDefault = function (value, defaultValue) {
EnumWrapper.prototype.asValueOrDefault = function (value, defaultValue) {
if (this.isValue(value)) {

@@ -248,6 +248,6 @@ return value;

};
Enum.prototype.getKey = function (value) {
EnumWrapper.prototype.getKey = function (value) {
return this.keysByValueMap.get(this.asValue(value));
};
Enum.prototype.getKeyOrDefault = function (value, defaultKey) {
EnumWrapper.prototype.getKeyOrDefault = function (value, defaultKey) {
if (this.isValue(value)) {

@@ -260,6 +260,6 @@ return this.keysByValueMap.get(value);

};
Enum.prototype.getValue = function (key) {
EnumWrapper.prototype.getValue = function (key) {
return this.enumObj[this.asKey(key)];
};
Enum.prototype.getValueOrDefault = function (key, defaultValue) {
EnumWrapper.prototype.getValueOrDefault = function (key, defaultValue) {
if (this.isKey(key)) {

@@ -276,8 +276,18 @@ // type cast to "keyof T" is necessary until this bug is fixed:

* Map of enum object -> Enum instance.
* Used as a cache for {@link Enum.get}.
* Used as a cache for {@link EnumWrapper.getInstance}.
*/
Enum.instancesCache = new Map();
return Enum;
EnumWrapper.instancesCache = new Map();
return EnumWrapper;
}());
exports.Enum = Enum;
exports.EnumWrapper = EnumWrapper;
function $enum(enumObj, useCache) {
if (useCache === void 0) { useCache = true; }
if (useCache) {
return EnumWrapper.getInstance(enumObj);
}
else {
return EnumWrapper.createInstance(enumObj);
}
}
exports.$enum = $enum;
//# sourceMappingURL=index.js.map

@@ -8,4 +8,4 @@ /**

* instance:
* - {@link Enum.get}
* - {@link Enum.create}
* - {@link EnumWrapper.getInstance}
* - {@link EnumWrapper.createInstance}
*

@@ -15,3 +15,3 @@ * @template V - Type of the enum value.

*/
var Enum = /** @class */ (function () {
var EnumWrapper = /** @class */ (function () {
/**

@@ -22,8 +22,8 @@ * Create a new Enum instance.

* instance:
* - {@link Enum.get}
* - {@link Enum.create}
* - {@link EnumWrapper.getInstance}
* - {@link EnumWrapper.createInstance}
*
* @param enumObj - An enum-like object. See the {@link EnumLike} type for more explanation.
*/
function Enum(enumObj) {
function EnumWrapper(enumObj) {
var _this = this;

@@ -53,3 +53,3 @@ this.enumObj = enumObj;

* Creates a new Enum for an enum-like object.
* You probably want to use {@link Enum.get} for any typical enums, because it will
* You probably want to use {@link EnumWrapper.getInstance} for any typical enums, because it will
* cache the result.

@@ -62,18 +62,18 @@ * This method may be useful if you want an Enum for an enum-like object that is dynamically

*/
Enum.create = function (enumObj) {
return new Enum(enumObj);
EnumWrapper.createInstance = function (enumObj) {
return new EnumWrapper(enumObj);
};
/**
* Creates a new Enum, or returns a cached Enum if one has already been created for the same
* object via {@link Enum.get}.
* object via {@link EnumWrapper.getInstance}.
* This is most useful for typical enums that are statically defined, because the cached Enum instance
* will be quickly retrieved/reused on every sebsequent call to get() for the same enum object.
* Use {@link Enum.create} if you don't want the Enum to be cached.
* will be quickly retrieved/reused on every subsequent call to get() for the same enum object.
* Use {@link EnumWrapper.createInstance} if you don't want the Enum to be cached.
* @param enumObj - An enum-like object.
* @return An instance of Enum for the provided enumObj.
*/
Enum.get = function (enumObj) {
EnumWrapper.getInstance = function (enumObj) {
var result = this.instancesCache.get(enumObj);
if (!result) {
result = this.create(enumObj);
result = this.createInstance(enumObj);
this.instancesCache.set(enumObj, result);

@@ -83,3 +83,3 @@ }

};
Object.defineProperty(Enum.prototype, "size", {
Object.defineProperty(EnumWrapper.prototype, "size", {
/**

@@ -98,3 +98,3 @@ * The number of entries in this enum.

*/
Enum.prototype.getKeys = function () {
EnumWrapper.prototype.getKeys = function () {
return Array.from(this.keySet.values());

@@ -105,6 +105,6 @@ };

* NOTE: If this enum has any duplicate values, only unique values will be returned, and the
* length of the list will be less than {@link Enum#size}.
* length of the list will be less than {@link EnumWrapper#size}.
* @return A list of this enum's values.
*/
Enum.prototype.getValues = function () {
EnumWrapper.prototype.getValues = function () {
return Array.from(this.valueSet.values());

@@ -116,3 +116,3 @@ };

*/
Enum.prototype.getEntries = function () {
EnumWrapper.prototype.getEntries = function () {
return Array.from(this);

@@ -124,3 +124,3 @@ };

*/
Enum.prototype.keys = function () {
EnumWrapper.prototype.keys = function () {
return this.keySet.values();

@@ -131,6 +131,6 @@ };

* NOTE: If this enum has any duplicate values, only unique values will be iterated, and the
* number of values iterated will be less than {@link Enum#size}.
* number of values iterated will be less than {@link EnumWrapper#size}.
* @return An iterator that iterates over this enum's values.
*/
Enum.prototype.values = function () {
EnumWrapper.prototype.values = function () {
return this.valueSet.values();

@@ -142,3 +142,3 @@ };

*/
Enum.prototype.entries = function () {
EnumWrapper.prototype.entries = function () {
var _this = this;

@@ -167,3 +167,3 @@ var keyIterator = this.keys();

*/
Enum.prototype[Symbol.iterator] = function () {
EnumWrapper.prototype[Symbol.iterator] = function () {
return this.entries();

@@ -173,3 +173,3 @@ };

* Calls the provided iteratee on each item in this enum.
* See {@link Enum.Iteratee} for the signature of the iteratee.
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee.
* The return value of the iteratee is ignored.

@@ -179,3 +179,3 @@ * @param iteratee - The iteratee.

*/
Enum.prototype.forEach = function (iteratee, context) {
EnumWrapper.prototype.forEach = function (iteratee, context) {
var _this = this;

@@ -189,3 +189,3 @@ this.keySet.forEach(function (key) {

* Builds a new array containing the results of calling the provided iteratee on each item in this enum.
* See {@link Enum.Iteratee} for the signature of the iteratee.
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee.
* @param iteratee - The iteratee.

@@ -195,3 +195,3 @@ * @param context - If provided, then the iteratee will be called with the context as its "this" value.

*/
Enum.prototype.map = function (iteratee, context) {
EnumWrapper.prototype.map = function (iteratee, context) {
var _this = this;

@@ -204,6 +204,6 @@ var result = [];

};
Enum.prototype.isKey = function (key) {
EnumWrapper.prototype.isKey = function (key) {
return key !== undefined && this.keySet.has(key);
};
Enum.prototype.asKey = function (key) {
EnumWrapper.prototype.asKey = function (key) {
if (this.isKey(key)) {

@@ -216,3 +216,3 @@ return key;

};
Enum.prototype.asKeyOrDefault = function (key, defaultKey) {
EnumWrapper.prototype.asKeyOrDefault = function (key, defaultKey) {
if (this.isKey(key)) {

@@ -225,6 +225,6 @@ return key;

};
Enum.prototype.isValue = function (value) {
EnumWrapper.prototype.isValue = function (value) {
return value !== undefined && this.valueSet.has(value);
};
Enum.prototype.asValue = function (value) {
EnumWrapper.prototype.asValue = function (value) {
if (this.isValue(value)) {

@@ -237,3 +237,3 @@ return value;

};
Enum.prototype.asValueOrDefault = function (value, defaultValue) {
EnumWrapper.prototype.asValueOrDefault = function (value, defaultValue) {
if (this.isValue(value)) {

@@ -246,6 +246,6 @@ return value;

};
Enum.prototype.getKey = function (value) {
EnumWrapper.prototype.getKey = function (value) {
return this.keysByValueMap.get(this.asValue(value));
};
Enum.prototype.getKeyOrDefault = function (value, defaultKey) {
EnumWrapper.prototype.getKeyOrDefault = function (value, defaultKey) {
if (this.isValue(value)) {

@@ -258,6 +258,6 @@ return this.keysByValueMap.get(value);

};
Enum.prototype.getValue = function (key) {
EnumWrapper.prototype.getValue = function (key) {
return this.enumObj[this.asKey(key)];
};
Enum.prototype.getValueOrDefault = function (key, defaultValue) {
EnumWrapper.prototype.getValueOrDefault = function (key, defaultValue) {
if (this.isKey(key)) {

@@ -274,8 +274,17 @@ // type cast to "keyof T" is necessary until this bug is fixed:

* Map of enum object -> Enum instance.
* Used as a cache for {@link Enum.get}.
* Used as a cache for {@link EnumWrapper.getInstance}.
*/
Enum.instancesCache = new Map();
return Enum;
EnumWrapper.instancesCache = new Map();
return EnumWrapper;
}());
export { Enum };
export { EnumWrapper };
export function $enum(enumObj, useCache) {
if (useCache === void 0) { useCache = true; }
if (useCache) {
return EnumWrapper.getInstance(enumObj);
}
else {
return EnumWrapper.createInstance(enumObj);
}
}
//# sourceMappingURL=index.js.map

@@ -17,4 +17,4 @@ /**

* instance:
* - {@link Enum.get}
* - {@link Enum.create}
* - {@link EnumWrapper.getInstance}
* - {@link EnumWrapper.createInstance}
*

@@ -24,7 +24,7 @@ * @template V - Type of the enum value.

*/
export declare class Enum<V extends number | string = number | string, T extends EnumLike<V, keyof T> = any> implements Iterable<Enum.Entry<T>> {
export declare class EnumWrapper<V extends number | string = number | string, T extends EnumLike<V, keyof T> = any> implements Iterable<EnumWrapper.Entry<T>> {
private readonly enumObj;
/**
* Map of enum object -> Enum instance.
* Used as a cache for {@link Enum.get}.
* Used as a cache for {@link EnumWrapper.getInstance}.
*/

@@ -47,3 +47,3 @@ private static readonly instancesCache;

* Creates a new Enum for an enum-like object.
* You probably want to use {@link Enum.get} for any typical enums, because it will
* You probably want to use {@link EnumWrapper.getInstance} for any typical enums, because it will
* cache the result.

@@ -56,6 +56,6 @@ * This method may be useful if you want an Enum for an enum-like object that is dynamically

*/
static create<T extends EnumLike<number, keyof T>>(enumObj: T): Enum<number, T>;
static createInstance<T extends EnumLike<number, keyof T>>(enumObj: T): EnumWrapper<number, T>;
/**
* Creates a new Enum for an enum-like object.
* You probably want to use {@link Enum.get} for any typical enums, because it will
* You probably want to use {@link EnumWrapper.getInstance} for any typical enums, because it will
* cache the result.

@@ -68,6 +68,6 @@ * This method may be useful if you want an Enum for an enum-like object that is dynamically

*/
static create<T extends EnumLike<string, keyof T>>(enumObj: T): Enum<string, T>;
static createInstance<T extends EnumLike<string, keyof T>>(enumObj: T): EnumWrapper<string, T>;
/**
* Creates a new Enum for an enum-like object.
* You probably want to use {@link Enum.get} for any typical enums, because it will
* You probably want to use {@link EnumWrapper.getInstance} for any typical enums, because it will
* cache the result.

@@ -80,33 +80,33 @@ * This method may be useful if you want an Enum for an enum-like object that is dynamically

*/
static create<T extends EnumLike<number | string, keyof T>>(enumObj: T): Enum<number | string, T>;
static createInstance<T extends EnumLike<number | string, keyof T>>(enumObj: T): EnumWrapper<number | string, T>;
/**
* Creates a new Enum, or returns a cached Enum if one has already been created for the same
* object via {@link Enum.get}.
* object via {@link EnumWrapper.getInstance}.
* This is most useful for typical enums that are statically defined, because the cached Enum instance
* will be quickly retrieved/reused on every sebsequent call to get() for the same enum object.
* Use {@link Enum.create} if you don't want the Enum to be cached.
* will be quickly retrieved/reused on every subsequent call to get() for the same enum object.
* Use {@link EnumWrapper.createInstance} if you don't want the Enum to be cached.
* @param enumObj - An enum-like object.
* @return An instance of Enum for the provided enumObj.
*/
static get<T extends EnumLike<number, keyof T>>(enumObj: T): Enum<number, T>;
static getInstance<T extends EnumLike<number, keyof T>>(enumObj: T): EnumWrapper<number, T>;
/**
* Creates a new Enum, or returns a cached Enum if one has already been created for the same
* object via {@link Enum.get}.
* object via {@link EnumWrapper.getInstance}.
* This is most useful for typical enums that are statically defined, because the cached Enum instance
* will be quickly retrieved/reused on every sebsequent call to get() for the same enum object.
* Use {@link Enum.create} if you don't want the Enum to be cached.
* will be quickly retrieved/reused on every subsequent call to get() for the same enum object.
* Use {@link EnumWrapper.createInstance} if you don't want the Enum to be cached.
* @param enumObj - An enum-like object.
* @return An instance of Enum for the provided enumObj.
*/
static get<T extends EnumLike<string, keyof T>>(enumObj: T): Enum<string, T>;
static getInstance<T extends EnumLike<string, keyof T>>(enumObj: T): EnumWrapper<string, T>;
/**
* Creates a new Enum, or returns a cached Enum if one has already been created for the same
* object via {@link Enum.get}.
* object via {@link EnumWrapper.getInstance}.
* This is most useful for typical enums that are statically defined, because the cached Enum instance
* will be quickly retrieved/reused on every sebsequent call to get() for the same enum object.
* Use {@link Enum.create} if you don't want the Enum to be cached.
* will be quickly retrieved/reused on every subsequent call to get() for the same enum object.
* Use {@link EnumWrapper.createInstance} if you don't want the Enum to be cached.
* @param enumObj - An enum-like object.
* @return An instance of Enum for the provided enumObj.
*/
static get<T extends EnumLike<number | string, keyof T>>(enumObj: T): Enum<number | string, T>;
static getInstance<T extends EnumLike<number | string, keyof T>>(enumObj: T): EnumWrapper<number | string, T>;
/**

@@ -117,4 +117,4 @@ * Create a new Enum instance.

* instance:
* - {@link Enum.get}
* - {@link Enum.create}
* - {@link EnumWrapper.getInstance}
* - {@link EnumWrapper.createInstance}
*

@@ -136,3 +136,3 @@ * @param enumObj - An enum-like object. See the {@link EnumLike} type for more explanation.

* NOTE: If this enum has any duplicate values, only unique values will be returned, and the
* length of the list will be less than {@link Enum#size}.
* length of the list will be less than {@link EnumWrapper#size}.
* @return A list of this enum's values.

@@ -145,3 +145,3 @@ */

*/
getEntries(): Enum.Entry<T>[];
getEntries(): EnumWrapper.Entry<T>[];
/**

@@ -155,3 +155,3 @@ * Get an iterator for this enum's keys.

* NOTE: If this enum has any duplicate values, only unique values will be iterated, and the
* number of values iterated will be less than {@link Enum#size}.
* number of values iterated will be less than {@link EnumWrapper#size}.
* @return An iterator that iterates over this enum's values.

@@ -164,3 +164,3 @@ */

*/
entries(): IterableIterator<Enum.Entry<T>>;
entries(): IterableIterator<EnumWrapper.Entry<T>>;
/**

@@ -170,6 +170,6 @@ * Get an iterator for this enum's entries as [key, value] tuples.

*/
[Symbol.iterator](): IterableIterator<Enum.Entry<T>>;
[Symbol.iterator](): IterableIterator<EnumWrapper.Entry<T>>;
/**
* Calls the provided iteratee on each item in this enum.
* See {@link Enum.Iteratee} for the signature of the iteratee.
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee.
* The return value of the iteratee is ignored.

@@ -179,7 +179,7 @@ * @param iteratee - The iteratee.

*/
forEach(iteratee: Enum.Iteratee<V, T, void>, context?: any): void;
forEach(iteratee: EnumWrapper.Iteratee<V, T, void>, context?: any): void;
/**
* Maps this enum's entries to a new list of values.
* Builds a new array containing the results of calling the provided iteratee on each item in this enum.
* See {@link Enum.Iteratee} for the signature of the iteratee.
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee.
* @param iteratee - The iteratee.

@@ -189,3 +189,3 @@ * @param context - If provided, then the iteratee will be called with the context as its "this" value.

*/
map<R>(iteratee: Enum.Iteratee<V, T, R>, context?: any): R[];
map<R>(iteratee: EnumWrapper.Iteratee<V, T, R>, context?: any): R[];
isKey(key: string | undefined): key is keyof T;

@@ -214,5 +214,8 @@ asKey(key: string | undefined): keyof T;

}
export declare namespace Enum {
export declare namespace EnumWrapper {
type Entry<T> = [keyof T, T[keyof T]];
type Iteratee<V extends number | string, T extends EnumLike<V, keyof T>, R> = (this: any, value: V, key: keyof T, enumObj: T) => R;
}
export declare function $enum<T extends EnumLike<number, keyof T>>(enumObj: T, useCache?: boolean): EnumWrapper<number, T>;
export declare function $enum<T extends EnumLike<string, keyof T>>(enumObj: T, useCache?: boolean): EnumWrapper<string, T>;
export declare function $enum<T extends EnumLike<number | string, keyof T>>(enumObj: T, useCache?: boolean): EnumWrapper<number | string, T>;
{
"name": "ts-enum-util",
"version": "0.0.3",
"version": "0.0.4",
"description": "TypeScript Enum Utilities",

@@ -5,0 +5,0 @@ "repository": {

@@ -0,0 +0,0 @@ [![npm version](https://img.shields.io/npm/v/ts-enum-util.svg)](https://www.npmjs.com/package/ts-enum-util)

@@ -18,4 +18,4 @@ /**

* instance:
* - {@link Enum.get}
* - {@link Enum.create}
* - {@link EnumWrapper.getInstance}
* - {@link EnumWrapper.createInstance}
*

@@ -25,11 +25,11 @@ * @template V - Type of the enum value.

*/
export class Enum<
export class EnumWrapper<
V extends number | string = number | string,
T extends EnumLike<V, keyof T> = any
> implements Iterable<Enum.Entry<T>> {
> implements Iterable<EnumWrapper.Entry<T>> {
/**
* Map of enum object -> Enum instance.
* Used as a cache for {@link Enum.get}.
* Used as a cache for {@link EnumWrapper.getInstance}.
*/
private static readonly instancesCache = new Map<object, Enum>();
private static readonly instancesCache = new Map<object, EnumWrapper>();

@@ -54,3 +54,3 @@ /**

* Creates a new Enum for an enum-like object.
* You probably want to use {@link Enum.get} for any typical enums, because it will
* You probably want to use {@link EnumWrapper.getInstance} for any typical enums, because it will
* cache the result.

@@ -63,6 +63,8 @@ * This method may be useful if you want an Enum for an enum-like object that is dynamically

*/
public static create<T extends EnumLike<number, keyof T>>(enumObj: T): Enum<number, T>;
public static createInstance<T extends EnumLike<number, keyof T>>(
enumObj: T
): EnumWrapper<number, T>;
/**
* Creates a new Enum for an enum-like object.
* You probably want to use {@link Enum.get} for any typical enums, because it will
* You probably want to use {@link EnumWrapper.getInstance} for any typical enums, because it will
* cache the result.

@@ -75,6 +77,8 @@ * This method may be useful if you want an Enum for an enum-like object that is dynamically

*/
public static create<T extends EnumLike<string, keyof T>>(enumObj: T): Enum<string, T>;
public static createInstance<T extends EnumLike<string, keyof T>>(
enumObj: T
): EnumWrapper<string, T>;
/**
* Creates a new Enum for an enum-like object.
* You probably want to use {@link Enum.get} for any typical enums, because it will
* You probably want to use {@link EnumWrapper.getInstance} for any typical enums, because it will
* cache the result.

@@ -87,6 +91,8 @@ * This method may be useful if you want an Enum for an enum-like object that is dynamically

*/
public static create<T extends EnumLike<number | string, keyof T>>(enumObj: T): Enum<number | string, T>;
public static createInstance<T extends EnumLike<number | string, keyof T>>(
enumObj: T
): EnumWrapper<number | string, T>;
/**
* Creates a new Enum for an enum-like object.
* You probably want to use {@link Enum.get} for any typical enums, because it will
* You probably want to use {@link EnumWrapper.getInstance} for any typical enums, because it will
* cache the result.

@@ -99,4 +105,4 @@ * This method may be useful if you want an Enum for an enum-like object that is dynamically

*/
public static create(enumObj: any): Enum {
return new Enum(enumObj);
public static createInstance(enumObj: any): EnumWrapper {
return new EnumWrapper(enumObj);
}

@@ -106,44 +112,50 @@

* Creates a new Enum, or returns a cached Enum if one has already been created for the same
* object via {@link Enum.get}.
* object via {@link EnumWrapper.getInstance}.
* This is most useful for typical enums that are statically defined, because the cached Enum instance
* will be quickly retrieved/reused on every sebsequent call to get() for the same enum object.
* Use {@link Enum.create} if you don't want the Enum to be cached.
* will be quickly retrieved/reused on every subsequent call to get() for the same enum object.
* Use {@link EnumWrapper.createInstance} if you don't want the Enum to be cached.
* @param enumObj - An enum-like object.
* @return An instance of Enum for the provided enumObj.
*/
public static get<T extends EnumLike<number, keyof T>>(enumObj: T): Enum<number, T>;
public static getInstance<T extends EnumLike<number, keyof T>>(
enumObj: T
): EnumWrapper<number, T>;
/**
* Creates a new Enum, or returns a cached Enum if one has already been created for the same
* object via {@link Enum.get}.
* object via {@link EnumWrapper.getInstance}.
* This is most useful for typical enums that are statically defined, because the cached Enum instance
* will be quickly retrieved/reused on every sebsequent call to get() for the same enum object.
* Use {@link Enum.create} if you don't want the Enum to be cached.
* will be quickly retrieved/reused on every subsequent call to get() for the same enum object.
* Use {@link EnumWrapper.createInstance} if you don't want the Enum to be cached.
* @param enumObj - An enum-like object.
* @return An instance of Enum for the provided enumObj.
*/
public static get<T extends EnumLike<string, keyof T>>(enumObj: T): Enum<string, T>;
public static getInstance<T extends EnumLike<string, keyof T>>(
enumObj: T
): EnumWrapper<string, T>;
/**
* Creates a new Enum, or returns a cached Enum if one has already been created for the same
* object via {@link Enum.get}.
* object via {@link EnumWrapper.getInstance}.
* This is most useful for typical enums that are statically defined, because the cached Enum instance
* will be quickly retrieved/reused on every sebsequent call to get() for the same enum object.
* Use {@link Enum.create} if you don't want the Enum to be cached.
* will be quickly retrieved/reused on every subsequent call to get() for the same enum object.
* Use {@link EnumWrapper.createInstance} if you don't want the Enum to be cached.
* @param enumObj - An enum-like object.
* @return An instance of Enum for the provided enumObj.
*/
public static get<T extends EnumLike<number | string, keyof T>>(enumObj: T): Enum<number | string, T>;
public static getInstance<T extends EnumLike<number | string, keyof T>>(
enumObj: T
): EnumWrapper<number | string, T>;
/**
* Creates a new Enum, or returns a cached Enum if one has already been created for the same
* object via {@link Enum.get}.
* object via {@link EnumWrapper.getInstance}.
* This is most useful for typical enums that are statically defined, because the cached Enum instance
* will be quickly retrieved/reused on every sebsequent call to get() for the same enum object.
* Use {@link Enum.create} if you don't want the Enum to be cached.
* will be quickly retrieved/reused on every subsequent call to get() for the same enum object.
* Use {@link EnumWrapper.createInstance} if you don't want the Enum to be cached.
* @param enumObj - An enum-like object.
* @return An instance of Enum for the provided enumObj.
*/
public static get(enumObj: any): Enum {
public static getInstance(enumObj: any): EnumWrapper {
let result = this.instancesCache.get(enumObj);
if (!result) {
result = this.create(enumObj);
result = this.createInstance(enumObj);
this.instancesCache.set(enumObj, result);

@@ -160,4 +172,4 @@ }

* instance:
* - {@link Enum.get}
* - {@link Enum.create}
* - {@link EnumWrapper.getInstance}
* - {@link EnumWrapper.createInstance}
*

@@ -201,3 +213,3 @@ * @param enumObj - An enum-like object. See the {@link EnumLike} type for more explanation.

* NOTE: If this enum has any duplicate values, only unique values will be returned, and the
* length of the list will be less than {@link Enum#size}.
* length of the list will be less than {@link EnumWrapper#size}.
* @return A list of this enum's values.

@@ -213,3 +225,3 @@ */

*/
public getEntries(): Enum.Entry<T>[] {
public getEntries(): EnumWrapper.Entry<T>[] {
return Array.from(this);

@@ -229,3 +241,3 @@ }

* NOTE: If this enum has any duplicate values, only unique values will be iterated, and the
* number of values iterated will be less than {@link Enum#size}.
* number of values iterated will be less than {@link EnumWrapper#size}.
* @return An iterator that iterates over this enum's values.

@@ -241,7 +253,7 @@ */

*/
public entries(): IterableIterator<Enum.Entry<T>> {
public entries(): IterableIterator<EnumWrapper.Entry<T>> {
const keyIterator = this.keys();
return {
next: (): IteratorResult<Enum.Entry<T>> => {
next: (): IteratorResult<EnumWrapper.Entry<T>> => {
const nextKey = keyIterator.next();

@@ -257,3 +269,3 @@

[Symbol.iterator]: function(): IterableIterator<Enum.Entry<T>> {
[Symbol.iterator]: function(): IterableIterator<EnumWrapper.Entry<T>> {
return this;

@@ -268,3 +280,3 @@ }

*/
public [Symbol.iterator](): IterableIterator<Enum.Entry<T>> {
public [Symbol.iterator](): IterableIterator<EnumWrapper.Entry<T>> {
return this.entries();

@@ -275,3 +287,3 @@ }

* Calls the provided iteratee on each item in this enum.
* See {@link Enum.Iteratee} for the signature of the iteratee.
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee.
* The return value of the iteratee is ignored.

@@ -281,3 +293,3 @@ * @param iteratee - The iteratee.

*/
public forEach(iteratee: Enum.Iteratee<V, T, void>, context?: any): void {
public forEach(iteratee: EnumWrapper.Iteratee<V, T, void>, context?: any): void {
this.keySet.forEach((key) => {

@@ -291,3 +303,3 @@ iteratee.call(context, this.enumObj[key], key, this.enumObj);

* Builds a new array containing the results of calling the provided iteratee on each item in this enum.
* See {@link Enum.Iteratee} for the signature of the iteratee.
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee.
* @param iteratee - The iteratee.

@@ -297,3 +309,3 @@ * @param context - If provided, then the iteratee will be called with the context as its "this" value.

*/
public map<R>(iteratee: Enum.Iteratee<V, T, R>, context?: any): R[] {
public map<R>(iteratee: EnumWrapper.Iteratee<V, T, R>, context?: any): R[] {
const result: R[] = [];

@@ -392,3 +404,3 @@

export namespace Enum {
export namespace EnumWrapper {
export type Entry<T> = [

@@ -405,1 +417,18 @@ keyof T,

}
export function $enum<T extends EnumLike<number, keyof T>>(
enumObj: T, useCache?: boolean
): EnumWrapper<number, T>;
export function $enum<T extends EnumLike<string, keyof T>>(
enumObj: T, useCache?: boolean
): EnumWrapper<string, T>;
export function $enum<T extends EnumLike<number | string, keyof T>>(
enumObj: T, useCache?: boolean
): EnumWrapper<number | string, T>;
export function $enum(enumObj: any, useCache: boolean = true): EnumWrapper {
if (useCache) {
return EnumWrapper.getInstance(enumObj);
} else {
return EnumWrapper.createInstance(enumObj);
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc