ts-enum-util
Advanced tools
Comparing version 3.1.0 to 4.0.0-alpha.20181015
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* A generic wrapper for any enum-like object (see {@link EnumLike}). | ||
* Provides utilities for runtime processing of an enum's values and keys, with strict compile-time | ||
* type safety. | ||
* | ||
* EnumWrapper cannot be directly instantiated. Use one of the following to get/create an EnumWrapper | ||
* instance: | ||
* - {@link $enum} | ||
* - {@link EnumWrapper.getCachedInstance} | ||
* | ||
* @template V - Type of the enum value. | ||
* @template T - Type of the enum-like object that is being wrapped. | ||
*/ | ||
var EnumWrapper = /** @class */ (function () { | ||
/** | ||
* Create a new EnumWrapper instance. | ||
* This is for internal use only. | ||
* Use one of the following to publicly get/create an EnumWrapper | ||
* instance: | ||
* - {@link $enum} | ||
* - {@link EnumWrapper.getCachedInstance} | ||
* | ||
* @param enumObj - An enum-like object. See the {@link EnumLike} type for more explanation. | ||
*/ | ||
function EnumWrapper(enumObj) { | ||
this.enumObj = enumObj; | ||
this.keysList = Object.freeze(Object.keys(enumObj) | ||
// Include only keys that are not index keys. | ||
// This is necessary to ignore the reverse-lookup entries that are automatically added | ||
// by TypeScript to numeric enums. | ||
.filter(isNonIndexKey) | ||
// Order of Object.keys() is implementation-dependent, so sort the keys to guarantee | ||
// a consistent order for iteration. | ||
.sort()); | ||
var length = this.keysList.length; | ||
var valuesList = new Array(length); | ||
var keysByValueMap = new Map(); | ||
// According to multiple tests found on jsperf.com, a plain for loop is faster than using | ||
// Array.prototype.forEach | ||
for (var index = 0; index < length; ++index) { | ||
var key = this.keysList[index]; | ||
var value = enumObj[key]; | ||
valuesList[index] = value; | ||
keysByValueMap.set(value, key); | ||
// Type casting of "this" necessary to bypass readonly index signature for initialization. | ||
this[index] = Object.freeze([key, value]); | ||
} | ||
this.valuesList = Object.freeze(valuesList); | ||
this.keysByValueMap = keysByValueMap; | ||
this.size = this.length = length; | ||
// Make the EnumWrapper instance immutable | ||
Object.freeze(this); | ||
} | ||
/** | ||
* Gets a cached EnumWrapper for an enum-like object. | ||
* Creates and caches a new EnumWrapper if one is not already cached. | ||
* @param enumObj - An enum-like object. | ||
* @return An instance of EnumWrapper for the provided enumObj. | ||
*/ | ||
EnumWrapper.getCachedInstance = function (enumObj) { | ||
var result = EnumWrapper.instancesCache.get(enumObj); | ||
if (!result) { | ||
result = new EnumWrapper(enumObj); | ||
EnumWrapper.instancesCache.set(enumObj, result); | ||
} | ||
return result; | ||
}; | ||
Object.defineProperty(EnumWrapper.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return "EnumWrapper"; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
/** | ||
* @return "[object EnumWrapper]" | ||
*/ | ||
EnumWrapper.prototype.toString = function () { | ||
// NOTE: overriding toString in addition to Symbol.toStringTag | ||
// for maximum compatibility with older runtime environments | ||
// that do not implement Object.prototype.toString in terms | ||
// of Symbol.toStringTag | ||
return "[object EnumWrapper]"; | ||
}; | ||
/** | ||
* Get an iterator for this enum's keys. | ||
* Iteration order is based on sorted order of keys. | ||
* Part of the Map-like interface. | ||
* @return An iterator that iterates over this enum's keys. | ||
*/ | ||
EnumWrapper.prototype.keys = function () { | ||
var _a; | ||
var _this = this; | ||
var index = 0; | ||
return _a = { | ||
next: function () { | ||
var isDone = index >= _this.length; | ||
var result = { | ||
done: isDone, | ||
value: _this.keysList[index] | ||
}; | ||
++index; | ||
return result; | ||
} | ||
}, | ||
_a[Symbol.iterator] = function () { | ||
return this; | ||
}, | ||
_a; | ||
}; | ||
/** | ||
* Get an iterator for this enum's values. | ||
* Iteration order is based on sorted order of keys. | ||
* Part of the Map-like interface. | ||
* NOTE: If there are duplicate values in the enum, then there will also be duplicate values | ||
* in the result. | ||
* @return An iterator that iterates over this enum's values. | ||
*/ | ||
EnumWrapper.prototype.values = function () { | ||
var _a; | ||
var _this = this; | ||
var index = 0; | ||
return _a = { | ||
next: function () { | ||
var isDone = index >= _this.length; | ||
var result = { | ||
done: isDone, | ||
value: _this.valuesList[index] | ||
}; | ||
++index; | ||
return result; | ||
} | ||
}, | ||
_a[Symbol.iterator] = function () { | ||
return this; | ||
}, | ||
_a; | ||
}; | ||
/** | ||
* Get an iterator for this enum's entries as [key, value] tuples. | ||
* Iteration order is based on sorted order of keys. | ||
* @return An iterator that iterates over this enum's entries as [key, value] tuples. | ||
*/ | ||
EnumWrapper.prototype.entries = function () { | ||
var _a; | ||
var _this = this; | ||
var index = 0; | ||
return _a = { | ||
next: function () { | ||
var isDone = index >= _this.length; | ||
var result = { | ||
done: isDone, | ||
// NOTE: defensive copy not necessary because entries are "frozen" | ||
value: _this[index] | ||
}; | ||
++index; | ||
return result; | ||
} | ||
}, | ||
_a[Symbol.iterator] = function () { | ||
return this; | ||
}, | ||
_a; | ||
}; | ||
/** | ||
* Get an iterator for this enum's entries as [key, value] tuples. | ||
* Iteration order is based on sorted order of keys. | ||
* @return An iterator that iterates over this enum's entries as [key, value] tuples. | ||
*/ | ||
EnumWrapper.prototype[Symbol.iterator] = function () { | ||
return this.entries(); | ||
}; | ||
/** | ||
* Calls the provided iteratee on each item in this enum. | ||
* Iteration order is based on sorted order of keys. | ||
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee. | ||
* The return value of the iteratee is ignored. | ||
* @param iteratee - The iteratee. | ||
* @param context - If provided, then the iteratee will be called with the context as its "this" value. | ||
*/ | ||
EnumWrapper.prototype.forEach = function (iteratee, context) { | ||
var length = this.length; | ||
// According to multiple tests found on jsperf.com, a plain for loop is faster than using | ||
// Array.prototype.forEach | ||
for (var index = 0; index < length; ++index) { | ||
var entry = this[index]; | ||
iteratee.call(context, entry[1], entry[0], this, index); | ||
} | ||
}; | ||
/** | ||
* Maps this enum's entries to a new list of values. | ||
* Iteration order is based on sorted order of keys. | ||
* Builds a new array containing the results of calling the provided iteratee on each item in this enum. | ||
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee. | ||
* @param iteratee - The iteratee. | ||
* @param context - If provided, then the iteratee will be called with the context as its "this" value. | ||
* @return A new array containg the results of the iteratee. | ||
* | ||
* @template R - The of the mapped result for each entry. | ||
*/ | ||
EnumWrapper.prototype.map = function (iteratee, context) { | ||
var length = this.length; | ||
var result = new Array(length); | ||
// According to multiple tests found on jsperf.com, a plain for loop is faster than using Array.prototype.map | ||
for (var index = 0; index < length; ++index) { | ||
var entry = this[index]; | ||
result[index] = iteratee.call(context, entry[1], entry[0], this, index); | ||
} | ||
return result; | ||
}; | ||
/** | ||
* Get a list of this enum's keys. | ||
* Order of items in the list is based on sorted order of keys. | ||
* @return A list of this enum's keys. | ||
*/ | ||
EnumWrapper.prototype.getKeys = function () { | ||
// need to return a copy of this.keysList so it can be returned as Array instead of ReadonlyArray. | ||
return this.keysList.slice(); | ||
}; | ||
/** | ||
* Get a list of this enum's values. | ||
* Order of items in the list is based on sorted order of keys. | ||
* NOTE: If there are duplicate values in the enum, then there will also be duplicate values | ||
* in the result. | ||
* @return A list of this enum's values. | ||
*/ | ||
EnumWrapper.prototype.getValues = function () { | ||
// need to return a copy of this.valuesList so it can be returned as Array instead of ReadonlyArray. | ||
return this.valuesList.slice(); | ||
}; | ||
/** | ||
* Get a list of this enum's entries as [key, value] tuples. | ||
* Order of items in the list is based on sorted order of keys. | ||
* @return A list of this enum's entries as [key, value] tuples. | ||
*/ | ||
EnumWrapper.prototype.getEntries = function () { | ||
// Create an array from the indexed entries of "this". | ||
// NOTE: no need for defensive copy of each entry because all entries are "frozen". | ||
return Array.prototype.slice.call(this); | ||
}; | ||
/** | ||
* Tests if the provided string is actually a valid key for this enum | ||
* Acts as a type guard to confirm that the provided value is actually the enum key type. | ||
* @param key - A potential key value for this enum. | ||
* @return True if the provided key is a valid key for this enum. | ||
*/ | ||
EnumWrapper.prototype.isKey = function (key) { | ||
return (key != null && | ||
isNonIndexKey(key) && | ||
this.enumObj.hasOwnProperty(key)); | ||
}; | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Throws an error if the key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* @throws {Error} if the provided string is not a valid key for this enum. | ||
*/ | ||
EnumWrapper.prototype.asKeyOrThrow = function (key) { | ||
if (this.isKey(key)) { | ||
return key; | ||
} | ||
else { | ||
throw new Error("Unexpected key: " + key + ". Expected one of: " + this.getValues()); | ||
} | ||
}; | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Returns a default key if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultKey - The key to be returned if the provided key is invalid. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* Returns `defaultKey` if the provided key is invalid. | ||
*/ | ||
EnumWrapper.prototype.asKeyOrDefault = function (key, defaultKey) { | ||
if (this.isKey(key)) { | ||
// type cast required to work around TypeScript bug: | ||
// https://github.com/Microsoft/TypeScript/issues/21950 | ||
return key; | ||
} | ||
else { | ||
return defaultKey; | ||
} | ||
}; | ||
/** | ||
* Tests if the provided value is a valid value for this enum. | ||
* Acts as a type guard to confirm that the provided value is actually the enum value type. | ||
* @param value - A potential value for this enum. | ||
* @return True if the provided value is valid for this enum. | ||
*/ | ||
EnumWrapper.prototype.isValue = function (value) { | ||
return value != null && this.keysByValueMap.has(value); | ||
}; | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Throws an error if the value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* @throws {Error} if the provided value is not a valid value for this enum. | ||
*/ | ||
EnumWrapper.prototype.asValueOrThrow = function (value) { | ||
if (this.isValue(value)) { | ||
return value; | ||
} | ||
else { | ||
throw new Error("Unexpected value: " + value + ". Expected one of: " + this.getValues()); | ||
} | ||
}; | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Returns a default value if the provided value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @param defaultValue - The value to be returned if the provided value is invalid. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* Returns `defaultValue` if the provided value is invalid. | ||
*/ | ||
EnumWrapper.prototype.asValueOrDefault = function (value, defaultValue) { | ||
if (this.isValue(value)) { | ||
return value; | ||
} | ||
else { | ||
return defaultValue; | ||
} | ||
}; | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Throws an error if the value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @return The key for the provided value. | ||
* @throws {Error} if the provided value is not a valid value for this enum. | ||
*/ | ||
EnumWrapper.prototype.getKeyOrThrow = function (value) { | ||
// NOTE: Intentionally not using isValue() or asValueOrThrow() to avoid making two key lookups into the map | ||
// for successful lookups. | ||
var result = value != null ? this.keysByValueMap.get(value) : undefined; | ||
if (result != null) { | ||
return result; | ||
} | ||
else { | ||
throw new Error("Unexpected value: " + value + ". Expected one of: " + this.getValues()); | ||
} | ||
}; | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Returns a default key if the provided value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @param defaultKey - The key to be returned if the provided value is invalid. | ||
* @return The key for the provided value. | ||
* Returns `defaultKey` if the provided value is invalid. | ||
*/ | ||
EnumWrapper.prototype.getKeyOrDefault = function (value, defaultKey) { | ||
// NOTE: Intentionally not using isValue() to avoid making two key lookups into the map for successful lookups. | ||
var result = value != null ? this.keysByValueMap.get(value) : undefined; | ||
if (result != null) { | ||
return result; | ||
} | ||
else { | ||
return defaultKey; | ||
} | ||
}; | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Throws an error if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @return The enum value for the provided key. | ||
* @throws {Error} if the provided string is not a valid key for this enum. | ||
*/ | ||
EnumWrapper.prototype.getValueOrThrow = function (key) { | ||
// NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive | ||
// lookups for keys that match properties on Object.prototype, or keys that match the index keys of | ||
// reverse lookups on numeric enums. | ||
return this.enumObj[this.asKeyOrThrow(key)]; | ||
}; | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Returns a default value if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultValue - The value to be returned if the provided key is invalid. | ||
* @return The enum value for the provided key. | ||
* Returns `defaultValue` if the provided key is invalid. | ||
*/ | ||
EnumWrapper.prototype.getValueOrDefault = function (key, defaultValue) { | ||
// NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive | ||
// lookups for keys that match properties on Object.prototype, or keys that match the index keys of | ||
// reverse lookups on numeric enums. | ||
if (this.isKey(key)) { | ||
// type cast required to work around TypeScript bug: | ||
// https://github.com/Microsoft/TypeScript/issues/21950 | ||
return this.enumObj[key]; | ||
} | ||
else { | ||
return defaultValue; | ||
} | ||
}; | ||
/** | ||
* Map of enum object -> EnumWrapper instance. | ||
* Used as a cache for {@link EnumWrapper.getCachedInstance} (and {@link $enum}). | ||
* NOTE: WeakMap has very fast lookups and avoids memory leaks if used on a temporary | ||
* enum-like object. Even if a WeakMap implementation is very naiive (like a Map polyfill), | ||
* lookups are plenty fast for this use case of a relatively small number of enums within | ||
* a project. Just don't perform cached lookups inside tight loops when | ||
* you could cache the result in a local variable, and you'll be fine :) | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap} | ||
* {@link https://www.measurethat.net/Benchmarks/Show/2513/5/map-keyed-by-object} | ||
*/ | ||
EnumWrapper.instancesCache = new WeakMap(); | ||
return EnumWrapper; | ||
}()); | ||
exports.EnumWrapper = EnumWrapper; | ||
/** | ||
* Alias of {@link EnumWrapper.getCachedInstance} for convenience. | ||
* Gets a cached EnumWrapper for an enum-like object. | ||
* Creates and caches a new EnumWrapper if one is not already cached. | ||
* @param enumObj - An enum-like object. | ||
* @return An instance of EnumWrapper for the provided enumObj. | ||
*/ | ||
exports.$enum = EnumWrapper.getCachedInstance; | ||
/** | ||
* Return true if the specified object key value is NOT an integer index key. | ||
* @param key - An object key. | ||
* @return true if the specified object key value is NOT an integer index key. | ||
*/ | ||
function isNonIndexKey(key) { | ||
// If after converting the key to an integer, then back to a string, the result is different | ||
// than the original key, then the key is NOT an integer index. | ||
// See ECMAScript spec section 15.4: http://www.ecma-international.org/ecma-262/5.1/#sec-15.4 | ||
return key !== String(parseInt(key, 10)); | ||
} | ||
__export(require("./EnumWrapper")); | ||
__export(require("./EnumValueVisitee")); | ||
__export(require("./EnumValueMappee")); | ||
__export(require("./$enum")); | ||
//# sourceMappingURL=index.js.map |
@@ -1,433 +0,5 @@ | ||
/** | ||
* A generic wrapper for any enum-like object (see {@link EnumLike}). | ||
* Provides utilities for runtime processing of an enum's values and keys, with strict compile-time | ||
* type safety. | ||
* | ||
* EnumWrapper cannot be directly instantiated. Use one of the following to get/create an EnumWrapper | ||
* instance: | ||
* - {@link $enum} | ||
* - {@link EnumWrapper.getCachedInstance} | ||
* | ||
* @template V - Type of the enum value. | ||
* @template T - Type of the enum-like object that is being wrapped. | ||
*/ | ||
var EnumWrapper = /** @class */ (function () { | ||
/** | ||
* Create a new EnumWrapper instance. | ||
* This is for internal use only. | ||
* Use one of the following to publicly get/create an EnumWrapper | ||
* instance: | ||
* - {@link $enum} | ||
* - {@link EnumWrapper.getCachedInstance} | ||
* | ||
* @param enumObj - An enum-like object. See the {@link EnumLike} type for more explanation. | ||
*/ | ||
function EnumWrapper(enumObj) { | ||
this.enumObj = enumObj; | ||
this.keysList = Object.freeze(Object.keys(enumObj) | ||
// Include only keys that are not index keys. | ||
// This is necessary to ignore the reverse-lookup entries that are automatically added | ||
// by TypeScript to numeric enums. | ||
.filter(isNonIndexKey) | ||
// Order of Object.keys() is implementation-dependent, so sort the keys to guarantee | ||
// a consistent order for iteration. | ||
.sort()); | ||
var length = this.keysList.length; | ||
var valuesList = new Array(length); | ||
var keysByValueMap = new Map(); | ||
// According to multiple tests found on jsperf.com, a plain for loop is faster than using | ||
// Array.prototype.forEach | ||
for (var index = 0; index < length; ++index) { | ||
var key = this.keysList[index]; | ||
var value = enumObj[key]; | ||
valuesList[index] = value; | ||
keysByValueMap.set(value, key); | ||
// Type casting of "this" necessary to bypass readonly index signature for initialization. | ||
this[index] = Object.freeze([key, value]); | ||
} | ||
this.valuesList = Object.freeze(valuesList); | ||
this.keysByValueMap = keysByValueMap; | ||
this.size = this.length = length; | ||
// Make the EnumWrapper instance immutable | ||
Object.freeze(this); | ||
} | ||
/** | ||
* Gets a cached EnumWrapper for an enum-like object. | ||
* Creates and caches a new EnumWrapper if one is not already cached. | ||
* @param enumObj - An enum-like object. | ||
* @return An instance of EnumWrapper for the provided enumObj. | ||
*/ | ||
EnumWrapper.getCachedInstance = function (enumObj) { | ||
var result = EnumWrapper.instancesCache.get(enumObj); | ||
if (!result) { | ||
result = new EnumWrapper(enumObj); | ||
EnumWrapper.instancesCache.set(enumObj, result); | ||
} | ||
return result; | ||
}; | ||
Object.defineProperty(EnumWrapper.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return "EnumWrapper"; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
/** | ||
* @return "[object EnumWrapper]" | ||
*/ | ||
EnumWrapper.prototype.toString = function () { | ||
// NOTE: overriding toString in addition to Symbol.toStringTag | ||
// for maximum compatibility with older runtime environments | ||
// that do not implement Object.prototype.toString in terms | ||
// of Symbol.toStringTag | ||
return "[object EnumWrapper]"; | ||
}; | ||
/** | ||
* Get an iterator for this enum's keys. | ||
* Iteration order is based on sorted order of keys. | ||
* Part of the Map-like interface. | ||
* @return An iterator that iterates over this enum's keys. | ||
*/ | ||
EnumWrapper.prototype.keys = function () { | ||
var _a; | ||
var _this = this; | ||
var index = 0; | ||
return _a = { | ||
next: function () { | ||
var isDone = index >= _this.length; | ||
var result = { | ||
done: isDone, | ||
value: _this.keysList[index] | ||
}; | ||
++index; | ||
return result; | ||
} | ||
}, | ||
_a[Symbol.iterator] = function () { | ||
return this; | ||
}, | ||
_a; | ||
}; | ||
/** | ||
* Get an iterator for this enum's values. | ||
* Iteration order is based on sorted order of keys. | ||
* Part of the Map-like interface. | ||
* NOTE: If there are duplicate values in the enum, then there will also be duplicate values | ||
* in the result. | ||
* @return An iterator that iterates over this enum's values. | ||
*/ | ||
EnumWrapper.prototype.values = function () { | ||
var _a; | ||
var _this = this; | ||
var index = 0; | ||
return _a = { | ||
next: function () { | ||
var isDone = index >= _this.length; | ||
var result = { | ||
done: isDone, | ||
value: _this.valuesList[index] | ||
}; | ||
++index; | ||
return result; | ||
} | ||
}, | ||
_a[Symbol.iterator] = function () { | ||
return this; | ||
}, | ||
_a; | ||
}; | ||
/** | ||
* Get an iterator for this enum's entries as [key, value] tuples. | ||
* Iteration order is based on sorted order of keys. | ||
* @return An iterator that iterates over this enum's entries as [key, value] tuples. | ||
*/ | ||
EnumWrapper.prototype.entries = function () { | ||
var _a; | ||
var _this = this; | ||
var index = 0; | ||
return _a = { | ||
next: function () { | ||
var isDone = index >= _this.length; | ||
var result = { | ||
done: isDone, | ||
// NOTE: defensive copy not necessary because entries are "frozen" | ||
value: _this[index] | ||
}; | ||
++index; | ||
return result; | ||
} | ||
}, | ||
_a[Symbol.iterator] = function () { | ||
return this; | ||
}, | ||
_a; | ||
}; | ||
/** | ||
* Get an iterator for this enum's entries as [key, value] tuples. | ||
* Iteration order is based on sorted order of keys. | ||
* @return An iterator that iterates over this enum's entries as [key, value] tuples. | ||
*/ | ||
EnumWrapper.prototype[Symbol.iterator] = function () { | ||
return this.entries(); | ||
}; | ||
/** | ||
* Calls the provided iteratee on each item in this enum. | ||
* Iteration order is based on sorted order of keys. | ||
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee. | ||
* The return value of the iteratee is ignored. | ||
* @param iteratee - The iteratee. | ||
* @param context - If provided, then the iteratee will be called with the context as its "this" value. | ||
*/ | ||
EnumWrapper.prototype.forEach = function (iteratee, context) { | ||
var length = this.length; | ||
// According to multiple tests found on jsperf.com, a plain for loop is faster than using | ||
// Array.prototype.forEach | ||
for (var index = 0; index < length; ++index) { | ||
var entry = this[index]; | ||
iteratee.call(context, entry[1], entry[0], this, index); | ||
} | ||
}; | ||
/** | ||
* Maps this enum's entries to a new list of values. | ||
* Iteration order is based on sorted order of keys. | ||
* Builds a new array containing the results of calling the provided iteratee on each item in this enum. | ||
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee. | ||
* @param iteratee - The iteratee. | ||
* @param context - If provided, then the iteratee will be called with the context as its "this" value. | ||
* @return A new array containg the results of the iteratee. | ||
* | ||
* @template R - The of the mapped result for each entry. | ||
*/ | ||
EnumWrapper.prototype.map = function (iteratee, context) { | ||
var length = this.length; | ||
var result = new Array(length); | ||
// According to multiple tests found on jsperf.com, a plain for loop is faster than using Array.prototype.map | ||
for (var index = 0; index < length; ++index) { | ||
var entry = this[index]; | ||
result[index] = iteratee.call(context, entry[1], entry[0], this, index); | ||
} | ||
return result; | ||
}; | ||
/** | ||
* Get a list of this enum's keys. | ||
* Order of items in the list is based on sorted order of keys. | ||
* @return A list of this enum's keys. | ||
*/ | ||
EnumWrapper.prototype.getKeys = function () { | ||
// need to return a copy of this.keysList so it can be returned as Array instead of ReadonlyArray. | ||
return this.keysList.slice(); | ||
}; | ||
/** | ||
* Get a list of this enum's values. | ||
* Order of items in the list is based on sorted order of keys. | ||
* NOTE: If there are duplicate values in the enum, then there will also be duplicate values | ||
* in the result. | ||
* @return A list of this enum's values. | ||
*/ | ||
EnumWrapper.prototype.getValues = function () { | ||
// need to return a copy of this.valuesList so it can be returned as Array instead of ReadonlyArray. | ||
return this.valuesList.slice(); | ||
}; | ||
/** | ||
* Get a list of this enum's entries as [key, value] tuples. | ||
* Order of items in the list is based on sorted order of keys. | ||
* @return A list of this enum's entries as [key, value] tuples. | ||
*/ | ||
EnumWrapper.prototype.getEntries = function () { | ||
// Create an array from the indexed entries of "this". | ||
// NOTE: no need for defensive copy of each entry because all entries are "frozen". | ||
return Array.prototype.slice.call(this); | ||
}; | ||
/** | ||
* Tests if the provided string is actually a valid key for this enum | ||
* Acts as a type guard to confirm that the provided value is actually the enum key type. | ||
* @param key - A potential key value for this enum. | ||
* @return True if the provided key is a valid key for this enum. | ||
*/ | ||
EnumWrapper.prototype.isKey = function (key) { | ||
return (key != null && | ||
isNonIndexKey(key) && | ||
this.enumObj.hasOwnProperty(key)); | ||
}; | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Throws an error if the key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* @throws {Error} if the provided string is not a valid key for this enum. | ||
*/ | ||
EnumWrapper.prototype.asKeyOrThrow = function (key) { | ||
if (this.isKey(key)) { | ||
return key; | ||
} | ||
else { | ||
throw new Error("Unexpected key: " + key + ". Expected one of: " + this.getValues()); | ||
} | ||
}; | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Returns a default key if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultKey - The key to be returned if the provided key is invalid. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* Returns `defaultKey` if the provided key is invalid. | ||
*/ | ||
EnumWrapper.prototype.asKeyOrDefault = function (key, defaultKey) { | ||
if (this.isKey(key)) { | ||
// type cast required to work around TypeScript bug: | ||
// https://github.com/Microsoft/TypeScript/issues/21950 | ||
return key; | ||
} | ||
else { | ||
return defaultKey; | ||
} | ||
}; | ||
/** | ||
* Tests if the provided value is a valid value for this enum. | ||
* Acts as a type guard to confirm that the provided value is actually the enum value type. | ||
* @param value - A potential value for this enum. | ||
* @return True if the provided value is valid for this enum. | ||
*/ | ||
EnumWrapper.prototype.isValue = function (value) { | ||
return value != null && this.keysByValueMap.has(value); | ||
}; | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Throws an error if the value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* @throws {Error} if the provided value is not a valid value for this enum. | ||
*/ | ||
EnumWrapper.prototype.asValueOrThrow = function (value) { | ||
if (this.isValue(value)) { | ||
return value; | ||
} | ||
else { | ||
throw new Error("Unexpected value: " + value + ". Expected one of: " + this.getValues()); | ||
} | ||
}; | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Returns a default value if the provided value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @param defaultValue - The value to be returned if the provided value is invalid. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* Returns `defaultValue` if the provided value is invalid. | ||
*/ | ||
EnumWrapper.prototype.asValueOrDefault = function (value, defaultValue) { | ||
if (this.isValue(value)) { | ||
return value; | ||
} | ||
else { | ||
return defaultValue; | ||
} | ||
}; | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Throws an error if the value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @return The key for the provided value. | ||
* @throws {Error} if the provided value is not a valid value for this enum. | ||
*/ | ||
EnumWrapper.prototype.getKeyOrThrow = function (value) { | ||
// NOTE: Intentionally not using isValue() or asValueOrThrow() to avoid making two key lookups into the map | ||
// for successful lookups. | ||
var result = value != null ? this.keysByValueMap.get(value) : undefined; | ||
if (result != null) { | ||
return result; | ||
} | ||
else { | ||
throw new Error("Unexpected value: " + value + ". Expected one of: " + this.getValues()); | ||
} | ||
}; | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Returns a default key if the provided value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @param defaultKey - The key to be returned if the provided value is invalid. | ||
* @return The key for the provided value. | ||
* Returns `defaultKey` if the provided value is invalid. | ||
*/ | ||
EnumWrapper.prototype.getKeyOrDefault = function (value, defaultKey) { | ||
// NOTE: Intentionally not using isValue() to avoid making two key lookups into the map for successful lookups. | ||
var result = value != null ? this.keysByValueMap.get(value) : undefined; | ||
if (result != null) { | ||
return result; | ||
} | ||
else { | ||
return defaultKey; | ||
} | ||
}; | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Throws an error if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @return The enum value for the provided key. | ||
* @throws {Error} if the provided string is not a valid key for this enum. | ||
*/ | ||
EnumWrapper.prototype.getValueOrThrow = function (key) { | ||
// NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive | ||
// lookups for keys that match properties on Object.prototype, or keys that match the index keys of | ||
// reverse lookups on numeric enums. | ||
return this.enumObj[this.asKeyOrThrow(key)]; | ||
}; | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Returns a default value if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultValue - The value to be returned if the provided key is invalid. | ||
* @return The enum value for the provided key. | ||
* Returns `defaultValue` if the provided key is invalid. | ||
*/ | ||
EnumWrapper.prototype.getValueOrDefault = function (key, defaultValue) { | ||
// NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive | ||
// lookups for keys that match properties on Object.prototype, or keys that match the index keys of | ||
// reverse lookups on numeric enums. | ||
if (this.isKey(key)) { | ||
// type cast required to work around TypeScript bug: | ||
// https://github.com/Microsoft/TypeScript/issues/21950 | ||
return this.enumObj[key]; | ||
} | ||
else { | ||
return defaultValue; | ||
} | ||
}; | ||
/** | ||
* Map of enum object -> EnumWrapper instance. | ||
* Used as a cache for {@link EnumWrapper.getCachedInstance} (and {@link $enum}). | ||
* NOTE: WeakMap has very fast lookups and avoids memory leaks if used on a temporary | ||
* enum-like object. Even if a WeakMap implementation is very naiive (like a Map polyfill), | ||
* lookups are plenty fast for this use case of a relatively small number of enums within | ||
* a project. Just don't perform cached lookups inside tight loops when | ||
* you could cache the result in a local variable, and you'll be fine :) | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap} | ||
* {@link https://www.measurethat.net/Benchmarks/Show/2513/5/map-keyed-by-object} | ||
*/ | ||
EnumWrapper.instancesCache = new WeakMap(); | ||
return EnumWrapper; | ||
}()); | ||
export { EnumWrapper }; | ||
/** | ||
* Alias of {@link EnumWrapper.getCachedInstance} for convenience. | ||
* Gets a cached EnumWrapper for an enum-like object. | ||
* Creates and caches a new EnumWrapper if one is not already cached. | ||
* @param enumObj - An enum-like object. | ||
* @return An instance of EnumWrapper for the provided enumObj. | ||
*/ | ||
export var $enum = EnumWrapper.getCachedInstance; | ||
/** | ||
* Return true if the specified object key value is NOT an integer index key. | ||
* @param key - An object key. | ||
* @return true if the specified object key value is NOT an integer index key. | ||
*/ | ||
function isNonIndexKey(key) { | ||
// If after converting the key to an integer, then back to a string, the result is different | ||
// than the original key, then the key is NOT an integer index. | ||
// See ECMAScript spec section 15.4: http://www.ecma-international.org/ecma-262/5.1/#sec-15.4 | ||
return key !== String(parseInt(key, 10)); | ||
} | ||
export * from "./EnumWrapper"; | ||
export * from "./EnumValueVisitee"; | ||
export * from "./EnumValueMappee"; | ||
export * from "./$enum"; | ||
//# sourceMappingURL=index.js.map |
@@ -1,483 +0,7 @@ | ||
/** | ||
* Used internally to verify that some type is enum-like. | ||
* A type is enum-like if all its properties are of type number or string. | ||
* @template V - Type of the enum value. | ||
* @template K - String literal union of all keys of the enum-like type. | ||
*/ | ||
export declare type EnumLike<V extends number | string, K extends string> = { | ||
[P in K]: V; | ||
}; | ||
/** | ||
* Extracts only keys of type T that are assignable to type `string`. | ||
* This is necessary starting with TypeScript 2.9 because keyof T can now | ||
* include `number` and `symbol` types. | ||
*/ | ||
type StringKeyOf<T> = Extract<keyof T, string>; | ||
/** | ||
* A generic wrapper for any enum-like object (see {@link EnumLike}). | ||
* Provides utilities for runtime processing of an enum's values and keys, with strict compile-time | ||
* type safety. | ||
* | ||
* EnumWrapper cannot be directly instantiated. Use one of the following to get/create an EnumWrapper | ||
* instance: | ||
* - {@link $enum} | ||
* - {@link EnumWrapper.getCachedInstance} | ||
* | ||
* @template V - Type of the enum value. | ||
* @template T - Type of the enum-like object that is being wrapped. | ||
*/ | ||
export declare class EnumWrapper<V extends number | string = number | string, T extends EnumLike<V, StringKeyOf<T>> = any> implements Iterable<EnumWrapper.Entry<V, T>>, ArrayLike<EnumWrapper.Entry<V, T>> { | ||
private readonly enumObj; | ||
/** | ||
* Map of enum object -> EnumWrapper instance. | ||
* Used as a cache for {@link EnumWrapper.getCachedInstance} (and {@link $enum}). | ||
* NOTE: WeakMap has very fast lookups and avoids memory leaks if used on a temporary | ||
* enum-like object. Even if a WeakMap implementation is very naiive (like a Map polyfill), | ||
* lookups are plenty fast for this use case of a relatively small number of enums within | ||
* a project. Just don't perform cached lookups inside tight loops when | ||
* you could cache the result in a local variable, and you'll be fine :) | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap} | ||
* {@link https://www.measurethat.net/Benchmarks/Show/2513/5/map-keyed-by-object} | ||
*/ | ||
private static readonly instancesCache; | ||
/** | ||
* List of all keys for this enum, in sorted order. | ||
*/ | ||
private readonly keysList; | ||
/** | ||
* List of all values for this enum, in sorted key order. | ||
*/ | ||
private readonly valuesList; | ||
/** | ||
* Map of enum value -> enum key. | ||
* Used for reverse key lookups. | ||
* NOTE: Performance tests show that using a Map (even if it's a slow polyfill) is faster than building a lookup | ||
* string key for values and using a plain Object: | ||
* {@link https://www.measurethat.net/Benchmarks/Show/2514/1/map-keyed-by-string-or-number} | ||
*/ | ||
private readonly keysByValueMap; | ||
/** | ||
* The number of entries in this enum. | ||
* Part of the Map-like interface. | ||
*/ | ||
readonly size: number; | ||
/** | ||
* The number of entries in this enum. | ||
* Part of the ArrayLike interface. | ||
*/ | ||
readonly length: number; | ||
/** | ||
* Index signature. | ||
* Part of the ArrayLike interface. | ||
*/ | ||
readonly [key: number]: EnumWrapper.Entry<V, T>; | ||
/** | ||
* Gets a cached EnumWrapper for an enum-like object with number values. | ||
* Creates and caches a new EnumWrapper if one is not already cached. | ||
* @param enumObj - An enum-like object with number values. | ||
* @return An instance of EnumWrapper for the provided enumObj. | ||
* | ||
* @template T - Type of the enum-like object that is being wrapped. | ||
*/ | ||
static getCachedInstance<T extends EnumLike<number, StringKeyOf<T>>>(enumObj: T): EnumWrapper<number, T>; | ||
/** | ||
* Gets a cached EnumWrapper for an enum-like object with string values. | ||
* Creates and caches a new EnumWrapper if one is not already cached. | ||
* @param enumObj - An enum-like object with string values. | ||
* @return An instance of EnumWrapper for the provided enumObj. | ||
* | ||
* @template T - Type of the enum-like object that is being wrapped. | ||
*/ | ||
static getCachedInstance<T extends EnumLike<string, StringKeyOf<T>>>(enumObj: T): EnumWrapper<string, T>; | ||
/** | ||
* Gets a cached EnumWrapper for an enum-like object with a mixture of number and string values. | ||
* Creates and caches a new EnumWrapper if one is not already cached. | ||
* @param enumObj - An enum-like object with a mixture of number and string values. | ||
* @return An instance of EnumWrapper for the provided enumObj. | ||
* | ||
* @template T - Type of the enum-like object that is being wrapped. | ||
*/ | ||
static getCachedInstance<T extends EnumLike<number | string, StringKeyOf<T>>>(enumObj: T): EnumWrapper<number | string, T>; | ||
/** | ||
* Create a new EnumWrapper instance. | ||
* This is for internal use only. | ||
* Use one of the following to publicly get/create an EnumWrapper | ||
* instance: | ||
* - {@link $enum} | ||
* - {@link EnumWrapper.getCachedInstance} | ||
* | ||
* @param enumObj - An enum-like object. See the {@link EnumLike} type for more explanation. | ||
*/ | ||
private constructor(); | ||
readonly [Symbol.toStringTag]: string; | ||
/** | ||
* @return "[object EnumWrapper]" | ||
*/ | ||
toString(): string; | ||
/** | ||
* Get an iterator for this enum's keys. | ||
* Iteration order is based on sorted order of keys. | ||
* Part of the Map-like interface. | ||
* @return An iterator that iterates over this enum's keys. | ||
*/ | ||
keys(): IterableIterator<StringKeyOf<T>>; | ||
/** | ||
* Get an iterator for this enum's values. | ||
* Iteration order is based on sorted order of keys. | ||
* Part of the Map-like interface. | ||
* NOTE: If there are duplicate values in the enum, then there will also be duplicate values | ||
* in the result. | ||
* @return An iterator that iterates over this enum's values. | ||
*/ | ||
values(): IterableIterator<T[StringKeyOf<T>]>; | ||
/** | ||
* Get an iterator for this enum's entries as [key, value] tuples. | ||
* Iteration order is based on sorted order of keys. | ||
* @return An iterator that iterates over this enum's entries as [key, value] tuples. | ||
*/ | ||
entries(): IterableIterator<EnumWrapper.Entry<V, T>>; | ||
/** | ||
* Get an iterator for this enum's entries as [key, value] tuples. | ||
* Iteration order is based on sorted order of keys. | ||
* @return An iterator that iterates over this enum's entries as [key, value] tuples. | ||
*/ | ||
[Symbol.iterator](): IterableIterator<EnumWrapper.Entry<V, T>>; | ||
/** | ||
* Calls the provided iteratee on each item in this enum. | ||
* Iteration order is based on sorted order of keys. | ||
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee. | ||
* The return value of the iteratee is ignored. | ||
* @param iteratee - The iteratee. | ||
* @param context - If provided, then the iteratee will be called with the context as its "this" value. | ||
*/ | ||
forEach(iteratee: EnumWrapper.Iteratee<void, V, T>, context?: any): void; | ||
/** | ||
* Maps this enum's entries to a new list of values. | ||
* Iteration order is based on sorted order of keys. | ||
* Builds a new array containing the results of calling the provided iteratee on each item in this enum. | ||
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee. | ||
* @param iteratee - The iteratee. | ||
* @param context - If provided, then the iteratee will be called with the context as its "this" value. | ||
* @return A new array containg the results of the iteratee. | ||
* | ||
* @template R - The of the mapped result for each entry. | ||
*/ | ||
map<R>(iteratee: EnumWrapper.Iteratee<R, V, T>, context?: any): R[]; | ||
/** | ||
* Get a list of this enum's keys. | ||
* Order of items in the list is based on sorted order of keys. | ||
* @return A list of this enum's keys. | ||
*/ | ||
getKeys(): (StringKeyOf<T>)[]; | ||
/** | ||
* Get a list of this enum's values. | ||
* Order of items in the list is based on sorted order of keys. | ||
* NOTE: If there are duplicate values in the enum, then there will also be duplicate values | ||
* in the result. | ||
* @return A list of this enum's values. | ||
*/ | ||
getValues(): T[StringKeyOf<T>][]; | ||
/** | ||
* Get a list of this enum's entries as [key, value] tuples. | ||
* Order of items in the list is based on sorted order of keys. | ||
* @return A list of this enum's entries as [key, value] tuples. | ||
*/ | ||
getEntries(): EnumWrapper.Entry<V, T>[]; | ||
/** | ||
* Tests if the provided string is actually a valid key for this enum | ||
* Acts as a type guard to confirm that the provided value is actually the enum key type. | ||
* @param key - A potential key value for this enum. | ||
* @return True if the provided key is a valid key for this enum. | ||
*/ | ||
isKey(key: string | null | undefined): key is StringKeyOf<T>; | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Throws an error if the key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* @throws {Error} if the provided string is not a valid key for this enum. | ||
*/ | ||
asKeyOrThrow(key: string | null | undefined): StringKeyOf<T>; | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Returns a default key if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultKey - The key to be returned if the provided key is invalid. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* Returns `defaultKey` if the provided key is invalid. | ||
*/ | ||
asKeyOrDefault(key: string | null | undefined, defaultKey: StringKeyOf<T>): StringKeyOf<T>; | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Returns a default key if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultKey - The key to be returned if the provided key is invalid. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* Returns `defaultKey` if the provided key is invalid. | ||
*/ | ||
asKeyOrDefault(key: string | null | undefined, defaultKey?: StringKeyOf<T>): StringKeyOf<T> | undefined; | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Returns a default key if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultKey - The key to be returned if the provided key is invalid. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* Returns `defaultKey` if the provided key is invalid. | ||
*/ | ||
asKeyOrDefault(key: string | null | undefined, defaultKey: string): string; | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Returns a default key if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultKey - The key to be returned if the provided key is invalid. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* Returns `defaultKey` if the provided key is invalid. | ||
*/ | ||
asKeyOrDefault(key: string | null | undefined, defaultKey: string | undefined): string | undefined; | ||
/** | ||
* Tests if the provided value is a valid value for this enum. | ||
* Acts as a type guard to confirm that the provided value is actually the enum value type. | ||
* @param value - A potential value for this enum. | ||
* @return True if the provided value is valid for this enum. | ||
*/ | ||
isValue(value: V | null | undefined): value is T[StringKeyOf<T>]; | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Throws an error if the value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* @throws {Error} if the provided value is not a valid value for this enum. | ||
*/ | ||
asValueOrThrow(value: V | null | undefined): T[StringKeyOf<T>]; | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Returns a default value if the provided value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @param defaultValue - The value to be returned if the provided value is invalid. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* Returns `defaultValue` if the provided value is invalid. | ||
*/ | ||
asValueOrDefault(value: V | null | undefined, defaultValue: T[StringKeyOf<T>]): T[StringKeyOf<T>]; | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Returns a default value if the provided value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @param defaultValue - The value to be returned if the provided value is invalid. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* Returns `defaultValue` if the provided value is invalid. | ||
*/ | ||
asValueOrDefault(value: V | null | undefined, defaultValue?: T[StringKeyOf<T>]): T[StringKeyOf<T>] | undefined; | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Returns a default value if the provided value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @param defaultValue - The value to be returned if the provided value is invalid. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* Returns `defaultValue` if the provided value is invalid. | ||
*/ | ||
asValueOrDefault(value: V | null | undefined, defaultValue: V): V; | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Returns a default value if the provided value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @param defaultValue - The value to be returned if the provided value is invalid. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* Returns `defaultValue` if the provided value is invalid. | ||
*/ | ||
asValueOrDefault(value: V | null | undefined, defaultValue: V | undefined): V | undefined; | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Throws an error if the value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @return The key for the provided value. | ||
* @throws {Error} if the provided value is not a valid value for this enum. | ||
*/ | ||
getKeyOrThrow(value: V | null | undefined): StringKeyOf<T>; | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Returns a default key if the provided value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @param defaultKey - The key to be returned if the provided value is invalid. | ||
* @return The key for the provided value. | ||
* Returns `defaultKey` if the provided value is invalid. | ||
*/ | ||
getKeyOrDefault(value: V | null | undefined, defaultKey: StringKeyOf<T>): StringKeyOf<T>; | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Returns a default key if the provided value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @param defaultKey - The key to be returned if the provided value is invalid. | ||
* @return The key for the provided value. | ||
* Returns `defaultKey` if the provided value is invalid. | ||
*/ | ||
getKeyOrDefault(value: V | null | undefined, defaultKey?: StringKeyOf<T>): StringKeyOf<T> | undefined; | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Returns a default key if the provided value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @param defaultKey - The key to be returned if the provided value is invalid. | ||
* @return The key for the provided value. | ||
* Returns `defaultKey` if the provided value is invalid. | ||
*/ | ||
getKeyOrDefault(value: V | null | undefined, defaultKey: string): string; | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Returns a default key if the provided value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @param defaultKey - The key to be returned if the provided value is invalid. | ||
* @return The key for the provided value. | ||
* Returns `defaultKey` if the provided value is invalid. | ||
*/ | ||
getKeyOrDefault(value: V | null | undefined, defaultKey: string | undefined): string | undefined; | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Throws an error if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @return The enum value for the provided key. | ||
* @throws {Error} if the provided string is not a valid key for this enum. | ||
*/ | ||
getValueOrThrow(key: string | null | undefined): T[StringKeyOf<T>]; | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Returns a default value if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultValue - The value to be returned if the provided key is invalid. | ||
* @return The enum value for the provided key. | ||
* Returns `defaultValue` if the provided key is invalid. | ||
*/ | ||
getValueOrDefault(key: string | null | undefined, defaultValue: T[StringKeyOf<T>]): T[StringKeyOf<T>]; | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Returns a default value if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultValue - The value to be returned if the provided key is invalid. | ||
* @return The enum value for the provided key. | ||
* Returns `defaultValue` if the provided key is invalid. | ||
*/ | ||
getValueOrDefault(key: string | null | undefined, defaultValue?: T[StringKeyOf<T>]): T[StringKeyOf<T>] | undefined; | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Returns a default value if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultValue - The value to be returned if the provided key is invalid. | ||
* @return The enum value for the provided key. | ||
* Returns `defaultValue` if the provided key is invalid. | ||
*/ | ||
getValueOrDefault(key: string | null | undefined, defaultValue: V): V; | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Returns a default value if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultValue - The value to be returned if the provided key is invalid. | ||
* @return The enum value for the provided key. | ||
* Returns `defaultValue` if the provided key is invalid. | ||
*/ | ||
getValueOrDefault(key: string | null | undefined, defaultValue: V | undefined): V | undefined; | ||
} | ||
export declare namespace EnumWrapper { | ||
/** | ||
* A tuple containing the key and value of a single entry in an enum. | ||
* @template V - Type of the enum value. | ||
* @template T - Type of an enum-like object. | ||
*/ | ||
type Entry<V extends number | string = number | string, T extends EnumLike<V, StringKeyOf<T>> = any> = Readonly<[StringKeyOf<T>, T[StringKeyOf<T>]]>; | ||
/** | ||
* A function used in iterating all key/value entries in an enum. | ||
* @param value - An enum value. | ||
* @param key - An enum key. | ||
* @param enumWrapper - The EnumWrapper instance being iterated.. | ||
* @param index - The index of the enum entry, based on sorted order of keys. | ||
* @return A result. The significance of the result depends on the type of iteration being performed. | ||
* | ||
* @template R - The type of the result. | ||
* @template V - Type of the enum value. | ||
* @template T - Type of an enum-like object. | ||
*/ | ||
type Iteratee<R = any, V extends number | string = number | string, T extends EnumLike<V, StringKeyOf<T>> = any> = (this: any, value: T[StringKeyOf<T>], key: StringKeyOf<T>, enumWrapper: EnumWrapper<V, T>, index: number) => R; | ||
} | ||
/** | ||
* Type alias for an {@link EnumWrapper} for any type of enum-like object that contains only number values. | ||
* | ||
* @template T - Type of an enum-like object that contains only number values. | ||
*/ | ||
export declare type NumberEnumWrapper<T extends EnumLike<number, StringKeyOf<T>> = any> = EnumWrapper<number, T>; | ||
export declare namespace NumberEnumWrapper { | ||
/** | ||
* Type alias for an {@link EnumWrapper.Entry} for any type of enum-like object that contains only number values. | ||
* | ||
* @template T - Type of an enum-like object that contains only number values. | ||
*/ | ||
type Entry<T extends EnumLike<number, StringKeyOf<T>> = any> = EnumWrapper.Entry<number, T>; | ||
/** | ||
* Type alias for an {@link EnumWrapper.Iteratee} for any type of enum-like object that contains only number values. | ||
* | ||
* @template R - The type of the result. | ||
* @template T - Type of an enum-like object that contains only number values. | ||
*/ | ||
type Iteratee<R = any, T extends EnumLike<number, StringKeyOf<T>> = any> = EnumWrapper.Iteratee<R, number, T>; | ||
} | ||
/** | ||
* Type alias for an {@link EnumWrapper} for any type of enum-like object that contains only string values. | ||
* | ||
* @template T - Type of an enum-like object that contains only string values. | ||
*/ | ||
export declare type StringEnumWrapper<T extends EnumLike<string, StringKeyOf<T>> = any> = EnumWrapper<string, T>; | ||
export declare namespace StringEnumWrapper { | ||
/** | ||
* Type alias for an {@link EnumWrapper.Entry} for any type of enum-like object that contains only string values. | ||
* | ||
* @template T - Type of an enum-like object that contains only string values. | ||
*/ | ||
type Entry<T extends EnumLike<string, StringKeyOf<T>> = any> = EnumWrapper.Entry<string, T>; | ||
/** | ||
* Type alias for an {@link EnumWrapper.Iteratee} for any type of enum-like object that contains only string values. | ||
* | ||
* @template R - The type of the result. | ||
* @template T - Type of an enum-like object that contains only string values. | ||
*/ | ||
type Iteratee<R = any, T extends EnumLike<string, StringKeyOf<T>> = any> = EnumWrapper.Iteratee<R, string, T>; | ||
} | ||
/** | ||
* Type alias for an {@link EnumWrapper} for any type of enum-like object that contains a mix of | ||
* number and string values. | ||
* | ||
* @template T - Type of an enum-like object that contains a mix of number and string values. | ||
*/ | ||
export declare type MixedEnumWrapper<T extends EnumLike<number | string, StringKeyOf<T>> = any> = EnumWrapper<number | string, T>; | ||
export declare namespace MixedEnumWrapper { | ||
/** | ||
* Type alias for an {@link EnumWrapper.Entry} for any type of enum-like object that contains a mix of | ||
* number and string values. | ||
* | ||
* @template T - Type of an enum-like object that contains a mix of number and string values. | ||
*/ | ||
type Entry<T extends EnumLike<number | string, StringKeyOf<T>> = any> = EnumWrapper.Entry<number | string, T>; | ||
/** | ||
* Type alias for an {@link EnumWrapper.Iteratee} for any type of enum-like object that contains a mix of | ||
* number and string values. | ||
* | ||
* @template R - The type of the result. | ||
* @template T - Type of an enum-like object that contains a mix of number and string values. | ||
*/ | ||
type Iteratee<R = any, T extends EnumLike<number | string, StringKeyOf<T>> = any> = EnumWrapper.Iteratee<R, number | string, T>; | ||
} | ||
/** | ||
* Alias of {@link EnumWrapper.getCachedInstance} for convenience. | ||
* Gets a cached EnumWrapper for an enum-like object. | ||
* Creates and caches a new EnumWrapper if one is not already cached. | ||
* @param enumObj - An enum-like object. | ||
* @return An instance of EnumWrapper for the provided enumObj. | ||
*/ | ||
export declare const $enum: typeof EnumWrapper.getCachedInstance; | ||
export {}; | ||
export * from "./EnumWrapper"; | ||
export * from "./EnumValueVisitor"; | ||
export * from "./EnumValueVisitee"; | ||
export * from "./EnumValueMapper"; | ||
export * from "./EnumValueMappee"; | ||
export * from "./$enum"; | ||
//# sourceMappingURL=index.d.ts.map |
{ | ||
"name": "ts-enum-util", | ||
"version": "3.1.0", | ||
"version": "4.0.0-alpha.20181015", | ||
"description": "TypeScript Enum Utilities", | ||
@@ -38,3 +38,4 @@ "repository": { | ||
"dtslint:v2_8_plus": "dtslint type_tests/v2_8_plus", | ||
"dtslint": "run-s clean:dist build:types dtslint:v2_8_plus", | ||
"dtslint:v2_9_plus": "dtslint type_tests/v2_9_plus", | ||
"dtslint": "run-s clean:dist build:types dtslint:v2_8_plus dtslint:v2_9_plus", | ||
"test": "run-s compile prettier:test lint dtslint jest", | ||
@@ -51,14 +52,14 @@ "test:coverage": "run-s compile prettier:test lint dtslint jest:coverage", | ||
"devDependencies": { | ||
"@types/jest": "23.0.0", | ||
"@types/node": "10.3.0", | ||
"coveralls": "3.0.1", | ||
"@types/jest": "23.3.3", | ||
"@types/node": "10.11.4", | ||
"coveralls": "3.0.2", | ||
"dtslint": "0.3.0", | ||
"jest": "23.1.0", | ||
"jest": "23.6.0", | ||
"npm-run-all": "4.1.3", | ||
"prettier": "1.13.4", | ||
"prettier": "1.14.3", | ||
"rimraf": "2.6.2", | ||
"ts-jest": "22.4.6", | ||
"tslint": "5.10.0", | ||
"tslint-config-prettier": "1.13.0", | ||
"typescript": "2.9.1" | ||
"ts-jest": "23.10.3", | ||
"tslint": "5.11.0", | ||
"tslint-config-prettier": "1.15.0", | ||
"typescript": "3.1.1" | ||
}, | ||
@@ -65,0 +66,0 @@ "peerDependencies": { |
919
src/index.ts
@@ -1,913 +0,6 @@ | ||
/** | ||
* Used internally to verify that some type is enum-like. | ||
* A type is enum-like if all its properties are of type number or string. | ||
* @template V - Type of the enum value. | ||
* @template K - String literal union of all keys of the enum-like type. | ||
*/ | ||
export type EnumLike<V extends number | string, K extends string> = { | ||
[P in K]: V | ||
}; | ||
/** | ||
* Extracts only keys of type T that are assignable to type `string`. | ||
* This is necessary starting with TypeScript 2.9 because keyof T can now | ||
* include `number` and `symbol` types. | ||
*/ | ||
type StringKeyOf<T> = Extract<keyof T, string>; | ||
/** | ||
* A generic wrapper for any enum-like object (see {@link EnumLike}). | ||
* Provides utilities for runtime processing of an enum's values and keys, with strict compile-time | ||
* type safety. | ||
* | ||
* EnumWrapper cannot be directly instantiated. Use one of the following to get/create an EnumWrapper | ||
* instance: | ||
* - {@link $enum} | ||
* - {@link EnumWrapper.getCachedInstance} | ||
* | ||
* @template V - Type of the enum value. | ||
* @template T - Type of the enum-like object that is being wrapped. | ||
*/ | ||
export class EnumWrapper< | ||
V extends number | string = number | string, | ||
T extends EnumLike<V, StringKeyOf<T>> = any | ||
> | ||
implements | ||
Iterable<EnumWrapper.Entry<V, T>>, | ||
ArrayLike<EnumWrapper.Entry<V, T>> { | ||
/** | ||
* Map of enum object -> EnumWrapper instance. | ||
* Used as a cache for {@link EnumWrapper.getCachedInstance} (and {@link $enum}). | ||
* NOTE: WeakMap has very fast lookups and avoids memory leaks if used on a temporary | ||
* enum-like object. Even if a WeakMap implementation is very naiive (like a Map polyfill), | ||
* lookups are plenty fast for this use case of a relatively small number of enums within | ||
* a project. Just don't perform cached lookups inside tight loops when | ||
* you could cache the result in a local variable, and you'll be fine :) | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap} | ||
* {@link https://www.measurethat.net/Benchmarks/Show/2513/5/map-keyed-by-object} | ||
*/ | ||
private static readonly instancesCache = new WeakMap<object, EnumWrapper>(); | ||
/** | ||
* List of all keys for this enum, in sorted order. | ||
*/ | ||
private readonly keysList: ReadonlyArray<StringKeyOf<T>>; | ||
/** | ||
* List of all values for this enum, in sorted key order. | ||
*/ | ||
private readonly valuesList: ReadonlyArray<T[StringKeyOf<T>]>; | ||
/** | ||
* Map of enum value -> enum key. | ||
* Used for reverse key lookups. | ||
* NOTE: Performance tests show that using a Map (even if it's a slow polyfill) is faster than building a lookup | ||
* string key for values and using a plain Object: | ||
* {@link https://www.measurethat.net/Benchmarks/Show/2514/1/map-keyed-by-string-or-number} | ||
*/ | ||
private readonly keysByValueMap: ReadonlyMap<V, StringKeyOf<T>>; | ||
/** | ||
* The number of entries in this enum. | ||
* Part of the Map-like interface. | ||
*/ | ||
public readonly size: number; | ||
/** | ||
* The number of entries in this enum. | ||
* Part of the ArrayLike interface. | ||
*/ | ||
public readonly length: number; | ||
/** | ||
* Index signature. | ||
* Part of the ArrayLike interface. | ||
*/ | ||
readonly [key: number]: EnumWrapper.Entry<V, T>; | ||
/** | ||
* Gets a cached EnumWrapper for an enum-like object with number values. | ||
* Creates and caches a new EnumWrapper if one is not already cached. | ||
* @param enumObj - An enum-like object with number values. | ||
* @return An instance of EnumWrapper for the provided enumObj. | ||
* | ||
* @template T - Type of the enum-like object that is being wrapped. | ||
*/ | ||
public static getCachedInstance<T extends EnumLike<number, StringKeyOf<T>>>( | ||
enumObj: T | ||
): EnumWrapper<number, T>; | ||
/** | ||
* Gets a cached EnumWrapper for an enum-like object with string values. | ||
* Creates and caches a new EnumWrapper if one is not already cached. | ||
* @param enumObj - An enum-like object with string values. | ||
* @return An instance of EnumWrapper for the provided enumObj. | ||
* | ||
* @template T - Type of the enum-like object that is being wrapped. | ||
*/ | ||
public static getCachedInstance<T extends EnumLike<string, StringKeyOf<T>>>( | ||
enumObj: T | ||
): EnumWrapper<string, T>; | ||
/** | ||
* Gets a cached EnumWrapper for an enum-like object with a mixture of number and string values. | ||
* Creates and caches a new EnumWrapper if one is not already cached. | ||
* @param enumObj - An enum-like object with a mixture of number and string values. | ||
* @return An instance of EnumWrapper for the provided enumObj. | ||
* | ||
* @template T - Type of the enum-like object that is being wrapped. | ||
*/ | ||
public static getCachedInstance< | ||
T extends EnumLike<number | string, StringKeyOf<T>> | ||
>(enumObj: T): EnumWrapper<number | string, T>; | ||
/** | ||
* Gets a cached EnumWrapper for an enum-like object. | ||
* Creates and caches a new EnumWrapper if one is not already cached. | ||
* @param enumObj - An enum-like object. | ||
* @return An instance of EnumWrapper for the provided enumObj. | ||
*/ | ||
public static getCachedInstance(enumObj: object): EnumWrapper { | ||
let result = EnumWrapper.instancesCache.get(enumObj); | ||
if (!result) { | ||
result = new EnumWrapper(enumObj); | ||
EnumWrapper.instancesCache.set(enumObj, result); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Create a new EnumWrapper instance. | ||
* This is for internal use only. | ||
* Use one of the following to publicly get/create an EnumWrapper | ||
* instance: | ||
* - {@link $enum} | ||
* - {@link EnumWrapper.getCachedInstance} | ||
* | ||
* @param enumObj - An enum-like object. See the {@link EnumLike} type for more explanation. | ||
*/ | ||
private constructor(private readonly enumObj: T) { | ||
this.keysList = Object.freeze(Object.keys(enumObj) | ||
// Include only keys that are not index keys. | ||
// This is necessary to ignore the reverse-lookup entries that are automatically added | ||
// by TypeScript to numeric enums. | ||
.filter(isNonIndexKey) | ||
// Order of Object.keys() is implementation-dependent, so sort the keys to guarantee | ||
// a consistent order for iteration. | ||
.sort() as StringKeyOf<T>[]); | ||
const length = this.keysList.length; | ||
const valuesList = new Array<T[StringKeyOf<T>]>(length); | ||
const keysByValueMap = new Map<V, StringKeyOf<T>>(); | ||
// According to multiple tests found on jsperf.com, a plain for loop is faster than using | ||
// Array.prototype.forEach | ||
for (let index = 0; index < length; ++index) { | ||
const key = this.keysList[index]; | ||
const value = enumObj[key]; | ||
valuesList[index] = value; | ||
keysByValueMap.set(value, key); | ||
// Type casting of "this" necessary to bypass readonly index signature for initialization. | ||
((this as any) as EnumWrapper.Entry<V, T>[])[index] = Object.freeze( | ||
[key, value] as EnumWrapper.Entry<V, T> | ||
); | ||
} | ||
this.valuesList = Object.freeze(valuesList); | ||
this.keysByValueMap = keysByValueMap; | ||
this.size = this.length = length; | ||
// Make the EnumWrapper instance immutable | ||
Object.freeze(this); | ||
} | ||
public get [Symbol.toStringTag](): string { | ||
return "EnumWrapper"; | ||
} | ||
/** | ||
* @return "[object EnumWrapper]" | ||
*/ | ||
public toString(): string { | ||
// NOTE: overriding toString in addition to Symbol.toStringTag | ||
// for maximum compatibility with older runtime environments | ||
// that do not implement Object.prototype.toString in terms | ||
// of Symbol.toStringTag | ||
return "[object EnumWrapper]"; | ||
} | ||
/** | ||
* Get an iterator for this enum's keys. | ||
* Iteration order is based on sorted order of keys. | ||
* Part of the Map-like interface. | ||
* @return An iterator that iterates over this enum's keys. | ||
*/ | ||
public keys(): IterableIterator<StringKeyOf<T>> { | ||
let index = 0; | ||
return { | ||
next: () => { | ||
const isDone = index >= this.length; | ||
const result: IteratorResult<StringKeyOf<T>> = { | ||
done: isDone, | ||
value: this.keysList[index] | ||
}; | ||
++index; | ||
return result; | ||
}, | ||
[Symbol.iterator](): IterableIterator<StringKeyOf<T>> { | ||
return this; | ||
} | ||
}; | ||
} | ||
/** | ||
* Get an iterator for this enum's values. | ||
* Iteration order is based on sorted order of keys. | ||
* Part of the Map-like interface. | ||
* NOTE: If there are duplicate values in the enum, then there will also be duplicate values | ||
* in the result. | ||
* @return An iterator that iterates over this enum's values. | ||
*/ | ||
public values(): IterableIterator<T[StringKeyOf<T>]> { | ||
let index = 0; | ||
return { | ||
next: () => { | ||
const isDone = index >= this.length; | ||
const result: IteratorResult<T[StringKeyOf<T>]> = { | ||
done: isDone, | ||
value: this.valuesList[index] | ||
}; | ||
++index; | ||
return result; | ||
}, | ||
[Symbol.iterator](): IterableIterator<T[StringKeyOf<T>]> { | ||
return this; | ||
} | ||
}; | ||
} | ||
/** | ||
* Get an iterator for this enum's entries as [key, value] tuples. | ||
* Iteration order is based on sorted order of keys. | ||
* @return An iterator that iterates over this enum's entries as [key, value] tuples. | ||
*/ | ||
public entries(): IterableIterator<EnumWrapper.Entry<V, T>> { | ||
let index = 0; | ||
return { | ||
next: () => { | ||
const isDone = index >= this.length; | ||
const result: IteratorResult<EnumWrapper.Entry<V, T>> = { | ||
done: isDone, | ||
// NOTE: defensive copy not necessary because entries are "frozen" | ||
value: this[index] | ||
}; | ||
++index; | ||
return result; | ||
}, | ||
[Symbol.iterator](): IterableIterator<EnumWrapper.Entry<V, T>> { | ||
return this; | ||
} | ||
}; | ||
} | ||
/** | ||
* Get an iterator for this enum's entries as [key, value] tuples. | ||
* Iteration order is based on sorted order of keys. | ||
* @return An iterator that iterates over this enum's entries as [key, value] tuples. | ||
*/ | ||
public [Symbol.iterator](): IterableIterator<EnumWrapper.Entry<V, T>> { | ||
return this.entries(); | ||
} | ||
/** | ||
* Calls the provided iteratee on each item in this enum. | ||
* Iteration order is based on sorted order of keys. | ||
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee. | ||
* The return value of the iteratee is ignored. | ||
* @param iteratee - The iteratee. | ||
* @param context - If provided, then the iteratee will be called with the context as its "this" value. | ||
*/ | ||
public forEach( | ||
iteratee: EnumWrapper.Iteratee<void, V, T>, | ||
context?: any | ||
): void { | ||
const length = this.length; | ||
// According to multiple tests found on jsperf.com, a plain for loop is faster than using | ||
// Array.prototype.forEach | ||
for (let index = 0; index < length; ++index) { | ||
const entry = this[index]; | ||
iteratee.call(context, entry[1], entry[0], this, index); | ||
} | ||
} | ||
/** | ||
* Maps this enum's entries to a new list of values. | ||
* Iteration order is based on sorted order of keys. | ||
* Builds a new array containing the results of calling the provided iteratee on each item in this enum. | ||
* See {@link EnumWrapper.Iteratee} for the signature of the iteratee. | ||
* @param iteratee - The iteratee. | ||
* @param context - If provided, then the iteratee will be called with the context as its "this" value. | ||
* @return A new array containg the results of the iteratee. | ||
* | ||
* @template R - The of the mapped result for each entry. | ||
*/ | ||
public map<R>(iteratee: EnumWrapper.Iteratee<R, V, T>, context?: any): R[] { | ||
const length = this.length; | ||
const result = new Array<R>(length); | ||
// According to multiple tests found on jsperf.com, a plain for loop is faster than using Array.prototype.map | ||
for (let index = 0; index < length; ++index) { | ||
const entry = this[index]; | ||
result[index] = iteratee.call( | ||
context, | ||
entry[1], | ||
entry[0], | ||
this, | ||
index | ||
); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Get a list of this enum's keys. | ||
* Order of items in the list is based on sorted order of keys. | ||
* @return A list of this enum's keys. | ||
*/ | ||
public getKeys(): (StringKeyOf<T>)[] { | ||
// need to return a copy of this.keysList so it can be returned as Array instead of ReadonlyArray. | ||
return this.keysList.slice(); | ||
} | ||
/** | ||
* Get a list of this enum's values. | ||
* Order of items in the list is based on sorted order of keys. | ||
* NOTE: If there are duplicate values in the enum, then there will also be duplicate values | ||
* in the result. | ||
* @return A list of this enum's values. | ||
*/ | ||
public getValues(): T[StringKeyOf<T>][] { | ||
// need to return a copy of this.valuesList so it can be returned as Array instead of ReadonlyArray. | ||
return this.valuesList.slice(); | ||
} | ||
/** | ||
* Get a list of this enum's entries as [key, value] tuples. | ||
* Order of items in the list is based on sorted order of keys. | ||
* @return A list of this enum's entries as [key, value] tuples. | ||
*/ | ||
public getEntries(): EnumWrapper.Entry<V, T>[] { | ||
// Create an array from the indexed entries of "this". | ||
// NOTE: no need for defensive copy of each entry because all entries are "frozen". | ||
return Array.prototype.slice.call(this); | ||
} | ||
/** | ||
* Tests if the provided string is actually a valid key for this enum | ||
* Acts as a type guard to confirm that the provided value is actually the enum key type. | ||
* @param key - A potential key value for this enum. | ||
* @return True if the provided key is a valid key for this enum. | ||
*/ | ||
public isKey(key: string | null | undefined): key is StringKeyOf<T> { | ||
return ( | ||
key != null && | ||
isNonIndexKey(key) && | ||
this.enumObj.hasOwnProperty(key) | ||
); | ||
} | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Throws an error if the key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* @throws {Error} if the provided string is not a valid key for this enum. | ||
*/ | ||
public asKeyOrThrow(key: string | null | undefined): StringKeyOf<T> { | ||
if (this.isKey(key)) { | ||
return key; | ||
} else { | ||
throw new Error( | ||
`Unexpected key: ${key}. Expected one of: ${this.getValues()}` | ||
); | ||
} | ||
} | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Returns a default key if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultKey - The key to be returned if the provided key is invalid. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* Returns `defaultKey` if the provided key is invalid. | ||
*/ | ||
public asKeyOrDefault( | ||
key: string | null | undefined, | ||
defaultKey: StringKeyOf<T> | ||
): StringKeyOf<T>; | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Returns a default key if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultKey - The key to be returned if the provided key is invalid. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* Returns `defaultKey` if the provided key is invalid. | ||
*/ | ||
public asKeyOrDefault( | ||
key: string | null | undefined, | ||
defaultKey?: StringKeyOf<T> | ||
): StringKeyOf<T> | undefined; | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Returns a default key if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultKey - The key to be returned if the provided key is invalid. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* Returns `defaultKey` if the provided key is invalid. | ||
*/ | ||
public asKeyOrDefault( | ||
key: string | null | undefined, | ||
defaultKey: string | ||
): string; | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Returns a default key if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultKey - The key to be returned if the provided key is invalid. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* Returns `defaultKey` if the provided key is invalid. | ||
*/ | ||
public asKeyOrDefault( | ||
key: string | null | undefined, | ||
defaultKey: string | undefined | ||
): string | undefined; | ||
/** | ||
* Casts a string to a properly-typed key for this enum. | ||
* Returns a default key if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultKey - The key to be returned if the provided key is invalid. | ||
* @return The provided key value, cast to the type of this enum's keys. | ||
* Returns `defaultKey` if the provided key is invalid. | ||
*/ | ||
public asKeyOrDefault( | ||
key: string | null | undefined, | ||
defaultKey?: StringKeyOf<T> | string | ||
): string | undefined { | ||
if (this.isKey(key)) { | ||
// type cast required to work around TypeScript bug: | ||
// https://github.com/Microsoft/TypeScript/issues/21950 | ||
return key as StringKeyOf<T>; | ||
} else { | ||
return defaultKey; | ||
} | ||
} | ||
/** | ||
* Tests if the provided value is a valid value for this enum. | ||
* Acts as a type guard to confirm that the provided value is actually the enum value type. | ||
* @param value - A potential value for this enum. | ||
* @return True if the provided value is valid for this enum. | ||
*/ | ||
public isValue(value: V | null | undefined): value is T[StringKeyOf<T>] { | ||
return value != null && this.keysByValueMap.has(value); | ||
} | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Throws an error if the value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* @throws {Error} if the provided value is not a valid value for this enum. | ||
*/ | ||
public asValueOrThrow(value: V | null | undefined): T[StringKeyOf<T>] { | ||
if (this.isValue(value)) { | ||
return value; | ||
} else { | ||
throw new Error( | ||
`Unexpected value: ${value}. Expected one of: ${this.getValues()}` | ||
); | ||
} | ||
} | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Returns a default value if the provided value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @param defaultValue - The value to be returned if the provided value is invalid. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* Returns `defaultValue` if the provided value is invalid. | ||
*/ | ||
public asValueOrDefault( | ||
value: V | null | undefined, | ||
defaultValue: T[StringKeyOf<T>] | ||
): T[StringKeyOf<T>]; | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Returns a default value if the provided value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @param defaultValue - The value to be returned if the provided value is invalid. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* Returns `defaultValue` if the provided value is invalid. | ||
*/ | ||
public asValueOrDefault( | ||
value: V | null | undefined, | ||
defaultValue?: T[StringKeyOf<T>] | ||
): T[StringKeyOf<T>] | undefined; | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Returns a default value if the provided value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @param defaultValue - The value to be returned if the provided value is invalid. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* Returns `defaultValue` if the provided value is invalid. | ||
*/ | ||
public asValueOrDefault(value: V | null | undefined, defaultValue: V): V; | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Returns a default value if the provided value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @param defaultValue - The value to be returned if the provided value is invalid. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* Returns `defaultValue` if the provided value is invalid. | ||
*/ | ||
public asValueOrDefault( | ||
value: V | null | undefined, | ||
defaultValue: V | undefined | ||
): V | undefined; | ||
/** | ||
* Casts a value to a properly-typed value for this enum. | ||
* Returns a default value if the provided value is invalid. | ||
* @param value - A potential value for this enum. | ||
* @param defaultValue - The value to be returned if the provided value is invalid. | ||
* @return The provided value, cast to the type of this enum's values. | ||
* Returns `defaultValue` if the provided value is invalid. | ||
*/ | ||
public asValueOrDefault( | ||
value: V | null | undefined, | ||
defaultValue?: T[StringKeyOf<T>] | V | ||
): V | undefined { | ||
if (this.isValue(value)) { | ||
return value; | ||
} else { | ||
return defaultValue; | ||
} | ||
} | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Throws an error if the value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @return The key for the provided value. | ||
* @throws {Error} if the provided value is not a valid value for this enum. | ||
*/ | ||
public getKeyOrThrow(value: V | null | undefined): StringKeyOf<T> { | ||
// NOTE: Intentionally not using isValue() or asValueOrThrow() to avoid making two key lookups into the map | ||
// for successful lookups. | ||
const result = | ||
value != null ? this.keysByValueMap.get(value) : undefined; | ||
if (result != null) { | ||
return result; | ||
} else { | ||
throw new Error( | ||
`Unexpected value: ${value}. Expected one of: ${this.getValues()}` | ||
); | ||
} | ||
} | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Returns a default key if the provided value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @param defaultKey - The key to be returned if the provided value is invalid. | ||
* @return The key for the provided value. | ||
* Returns `defaultKey` if the provided value is invalid. | ||
*/ | ||
public getKeyOrDefault( | ||
value: V | null | undefined, | ||
defaultKey: StringKeyOf<T> | ||
): StringKeyOf<T>; | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Returns a default key if the provided value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @param defaultKey - The key to be returned if the provided value is invalid. | ||
* @return The key for the provided value. | ||
* Returns `defaultKey` if the provided value is invalid. | ||
*/ | ||
public getKeyOrDefault( | ||
value: V | null | undefined, | ||
defaultKey?: StringKeyOf<T> | ||
): StringKeyOf<T> | undefined; | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Returns a default key if the provided value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @param defaultKey - The key to be returned if the provided value is invalid. | ||
* @return The key for the provided value. | ||
* Returns `defaultKey` if the provided value is invalid. | ||
*/ | ||
public getKeyOrDefault( | ||
value: V | null | undefined, | ||
defaultKey: string | ||
): string; | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Returns a default key if the provided value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @param defaultKey - The key to be returned if the provided value is invalid. | ||
* @return The key for the provided value. | ||
* Returns `defaultKey` if the provided value is invalid. | ||
*/ | ||
public getKeyOrDefault( | ||
value: V | null | undefined, | ||
defaultKey: string | undefined | ||
): string | undefined; | ||
/** | ||
* Performs a reverse lookup from enum value to corresponding enum key. | ||
* Returns a default key if the provided value is invalid. | ||
* NOTE: If this enum has any duplicate values, then one of the keys for the duplicated value is | ||
* arbitrarily returned. | ||
* @param value - A potential value for this enum. | ||
* @param defaultKey - The key to be returned if the provided value is invalid. | ||
* @return The key for the provided value. | ||
* Returns `defaultKey` if the provided value is invalid. | ||
*/ | ||
public getKeyOrDefault( | ||
value: V | null | undefined, | ||
defaultKey?: StringKeyOf<T> | string | ||
): string | undefined { | ||
// NOTE: Intentionally not using isValue() to avoid making two key lookups into the map for successful lookups. | ||
const result = | ||
value != null ? this.keysByValueMap.get(value) : undefined; | ||
if (result != null) { | ||
return result; | ||
} else { | ||
return defaultKey; | ||
} | ||
} | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Throws an error if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @return The enum value for the provided key. | ||
* @throws {Error} if the provided string is not a valid key for this enum. | ||
*/ | ||
public getValueOrThrow(key: string | null | undefined): T[StringKeyOf<T>] { | ||
// NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive | ||
// lookups for keys that match properties on Object.prototype, or keys that match the index keys of | ||
// reverse lookups on numeric enums. | ||
return this.enumObj[this.asKeyOrThrow(key)]; | ||
} | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Returns a default value if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultValue - The value to be returned if the provided key is invalid. | ||
* @return The enum value for the provided key. | ||
* Returns `defaultValue` if the provided key is invalid. | ||
*/ | ||
public getValueOrDefault( | ||
key: string | null | undefined, | ||
defaultValue: T[StringKeyOf<T>] | ||
): T[StringKeyOf<T>]; | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Returns a default value if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultValue - The value to be returned if the provided key is invalid. | ||
* @return The enum value for the provided key. | ||
* Returns `defaultValue` if the provided key is invalid. | ||
*/ | ||
public getValueOrDefault( | ||
key: string | null | undefined, | ||
defaultValue?: T[StringKeyOf<T>] | ||
): T[StringKeyOf<T>] | undefined; | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Returns a default value if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultValue - The value to be returned if the provided key is invalid. | ||
* @return The enum value for the provided key. | ||
* Returns `defaultValue` if the provided key is invalid. | ||
*/ | ||
public getValueOrDefault( | ||
key: string | null | undefined, | ||
defaultValue: V | ||
): V; | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Returns a default value if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultValue - The value to be returned if the provided key is invalid. | ||
* @return The enum value for the provided key. | ||
* Returns `defaultValue` if the provided key is invalid. | ||
*/ | ||
public getValueOrDefault( | ||
key: string | null | undefined, | ||
defaultValue: V | undefined | ||
): V | undefined; | ||
/** | ||
* Gets the enum value for the provided key. | ||
* Returns a default value if the provided key is invalid. | ||
* @param key - A potential key value for this enum. | ||
* @param defaultValue - The value to be returned if the provided key is invalid. | ||
* @return The enum value for the provided key. | ||
* Returns `defaultValue` if the provided key is invalid. | ||
*/ | ||
public getValueOrDefault( | ||
key: string | null | undefined, | ||
defaultValue?: T[StringKeyOf<T>] | V | ||
): V | undefined { | ||
// NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive | ||
// lookups for keys that match properties on Object.prototype, or keys that match the index keys of | ||
// reverse lookups on numeric enums. | ||
if (this.isKey(key)) { | ||
// type cast required to work around TypeScript bug: | ||
// https://github.com/Microsoft/TypeScript/issues/21950 | ||
return this.enumObj[key as StringKeyOf<T>]; | ||
} else { | ||
return defaultValue; | ||
} | ||
} | ||
} | ||
export namespace EnumWrapper { | ||
/** | ||
* A tuple containing the key and value of a single entry in an enum. | ||
* @template V - Type of the enum value. | ||
* @template T - Type of an enum-like object. | ||
*/ | ||
export type Entry< | ||
V extends number | string = number | string, | ||
T extends EnumLike<V, StringKeyOf<T>> = any | ||
> = Readonly<[StringKeyOf<T>, T[StringKeyOf<T>]]>; | ||
/** | ||
* A function used in iterating all key/value entries in an enum. | ||
* @param value - An enum value. | ||
* @param key - An enum key. | ||
* @param enumWrapper - The EnumWrapper instance being iterated.. | ||
* @param index - The index of the enum entry, based on sorted order of keys. | ||
* @return A result. The significance of the result depends on the type of iteration being performed. | ||
* | ||
* @template R - The type of the result. | ||
* @template V - Type of the enum value. | ||
* @template T - Type of an enum-like object. | ||
*/ | ||
export type Iteratee< | ||
R = any, | ||
V extends number | string = number | string, | ||
T extends EnumLike<V, StringKeyOf<T>> = any | ||
> = ( | ||
this: any, | ||
value: T[StringKeyOf<T>], | ||
key: StringKeyOf<T>, | ||
enumWrapper: EnumWrapper<V, T>, | ||
index: number | ||
) => R; | ||
} | ||
/** | ||
* Type alias for an {@link EnumWrapper} for any type of enum-like object that contains only number values. | ||
* | ||
* @template T - Type of an enum-like object that contains only number values. | ||
*/ | ||
export type NumberEnumWrapper< | ||
T extends EnumLike<number, StringKeyOf<T>> = any | ||
> = EnumWrapper<number, T>; | ||
export namespace NumberEnumWrapper { | ||
/** | ||
* Type alias for an {@link EnumWrapper.Entry} for any type of enum-like object that contains only number values. | ||
* | ||
* @template T - Type of an enum-like object that contains only number values. | ||
*/ | ||
export type Entry< | ||
T extends EnumLike<number, StringKeyOf<T>> = any | ||
> = EnumWrapper.Entry<number, T>; | ||
/** | ||
* Type alias for an {@link EnumWrapper.Iteratee} for any type of enum-like object that contains only number values. | ||
* | ||
* @template R - The type of the result. | ||
* @template T - Type of an enum-like object that contains only number values. | ||
*/ | ||
export type Iteratee< | ||
R = any, | ||
T extends EnumLike<number, StringKeyOf<T>> = any | ||
> = EnumWrapper.Iteratee<R, number, T>; | ||
} | ||
/** | ||
* Type alias for an {@link EnumWrapper} for any type of enum-like object that contains only string values. | ||
* | ||
* @template T - Type of an enum-like object that contains only string values. | ||
*/ | ||
export type StringEnumWrapper< | ||
T extends EnumLike<string, StringKeyOf<T>> = any | ||
> = EnumWrapper<string, T>; | ||
export namespace StringEnumWrapper { | ||
/** | ||
* Type alias for an {@link EnumWrapper.Entry} for any type of enum-like object that contains only string values. | ||
* | ||
* @template T - Type of an enum-like object that contains only string values. | ||
*/ | ||
export type Entry< | ||
T extends EnumLike<string, StringKeyOf<T>> = any | ||
> = EnumWrapper.Entry<string, T>; | ||
/** | ||
* Type alias for an {@link EnumWrapper.Iteratee} for any type of enum-like object that contains only string values. | ||
* | ||
* @template R - The type of the result. | ||
* @template T - Type of an enum-like object that contains only string values. | ||
*/ | ||
export type Iteratee< | ||
R = any, | ||
T extends EnumLike<string, StringKeyOf<T>> = any | ||
> = EnumWrapper.Iteratee<R, string, T>; | ||
} | ||
/** | ||
* Type alias for an {@link EnumWrapper} for any type of enum-like object that contains a mix of | ||
* number and string values. | ||
* | ||
* @template T - Type of an enum-like object that contains a mix of number and string values. | ||
*/ | ||
export type MixedEnumWrapper< | ||
T extends EnumLike<number | string, StringKeyOf<T>> = any | ||
> = EnumWrapper<number | string, T>; | ||
export namespace MixedEnumWrapper { | ||
/** | ||
* Type alias for an {@link EnumWrapper.Entry} for any type of enum-like object that contains a mix of | ||
* number and string values. | ||
* | ||
* @template T - Type of an enum-like object that contains a mix of number and string values. | ||
*/ | ||
export type Entry< | ||
T extends EnumLike<number | string, StringKeyOf<T>> = any | ||
> = EnumWrapper.Entry<number | string, T>; | ||
/** | ||
* Type alias for an {@link EnumWrapper.Iteratee} for any type of enum-like object that contains a mix of | ||
* number and string values. | ||
* | ||
* @template R - The type of the result. | ||
* @template T - Type of an enum-like object that contains a mix of number and string values. | ||
*/ | ||
export type Iteratee< | ||
R = any, | ||
T extends EnumLike<number | string, StringKeyOf<T>> = any | ||
> = EnumWrapper.Iteratee<R, number | string, T>; | ||
} | ||
/** | ||
* Alias of {@link EnumWrapper.getCachedInstance} for convenience. | ||
* Gets a cached EnumWrapper for an enum-like object. | ||
* Creates and caches a new EnumWrapper if one is not already cached. | ||
* @param enumObj - An enum-like object. | ||
* @return An instance of EnumWrapper for the provided enumObj. | ||
*/ | ||
export const $enum = EnumWrapper.getCachedInstance; | ||
/** | ||
* Return true if the specified object key value is NOT an integer index key. | ||
* @param key - An object key. | ||
* @return true if the specified object key value is NOT an integer index key. | ||
*/ | ||
function isNonIndexKey(key: string): boolean { | ||
// If after converting the key to an integer, then back to a string, the result is different | ||
// than the original key, then the key is NOT an integer index. | ||
// See ECMAScript spec section 15.4: http://www.ecma-international.org/ecma-262/5.1/#sec-15.4 | ||
return key !== String(parseInt(key, 10)); | ||
} | ||
export * from "./EnumWrapper"; | ||
export * from "./EnumValueVisitor"; | ||
export * from "./EnumValueVisitee"; | ||
export * from "./EnumValueMapper"; | ||
export * from "./EnumValueMappee"; | ||
export * from "./$enum"; |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
272057
80
4363
1
1