Socket
Socket
Sign inDemoInstall

ts-enum-util

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-enum-util - npm Package Compare versions

Comparing version 4.0.0-alpha.20181015 to 4.0.0-alpha.20190310

dist/commonjs/objectKeysUtil.js

46

dist/commonjs/$enum.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var EnumWrapper_1 = require("./EnumWrapper");
var Symbols_1 = require("./Symbols");
var symbols = require("./symbols");
var visitEnumValue_1 = require("./visitEnumValue");
var mapEnumValue_1 = require("./mapEnumValue");
/**
* Map of enum object -> EnumWrapper instance.
* Used as a cache for {@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}
*/
var enumWrapperInstancesCache = new WeakMap();

@@ -29,28 +17,8 @@ function $enum(enumObj) {

exports.$enum = $enum;
(function ($enum) {
/**
* Convenient alias for {@link Symbols.unhandled}.
*/
$enum.unhandled = Symbols_1.Symbols.unhandledEntry;
/**
* Convenient alias for {@link Symbols.handleNull}.
*/
$enum.handleNull = Symbols_1.Symbols.handleNull;
/**
* Convenient alias for {@link Symbols.handleUndefined}.
*/
$enum.handleUndefined = Symbols_1.Symbols.handleUndefined;
/**
* Convenient alias for {@link Symbols.handleUnexpected}.
*/
$enum.handleUnexpected = Symbols_1.Symbols.handleUnexpected;
/**
* Convenient alias for {@link visitEnumValue}
*/
$enum.visitValue = visitEnumValue_1.visitEnumValue;
/**
* Convenient alias for {@link mapEnumValue}
*/
$enum.mapValue = mapEnumValue_1.mapEnumValue;
})($enum = exports.$enum || (exports.$enum = {}));
$enum.handleNull = symbols.handleNull;
$enum.handleUndefined = symbols.handleUndefined;
$enum.handleUnexpected = symbols.handleUnexpected;
$enum.unhandledEntry = symbols.unhandledEntry;
$enum.visitValue = visitEnumValue_1.visitEnumValue;
$enum.mapValue = mapEnumValue_1.mapEnumValue;
//# sourceMappingURL=$enum.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Creates an Error with a message explaining that an unhandled
* value was encountered.
* @param unhandledValue - The unhandled value.
* @return an Error with a message explaining that an unhandled
* value was encountered.
*/
function createUnhandledEntryError(unhandledValue) {

@@ -11,0 +4,0 @@ return new Error("Unhandled value: " + unhandledValue);

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var createUnhandledEntryError_1 = require("./createUnhandledEntryError");
var Symbols_1 = require("./Symbols");
/**
* A wrapper around an enum or string/number literal value to be mapped.
* Do not use this class directly. Use the {@link $enum.mapValue} function to
* get an instance of this class.
*
* @template E - An enum or string/number literal type.
*/
var EnumValueMappee = /** @class */ (function () {
/**
* Do not use this constructor directly. Use the {@link $enum.mapValue}
* function to get an instance of this class.
* @param value - The value to be wrapped by this "mappee".
*/
var symbols_1 = require("./symbols");
var EnumValueMappee = (function () {
function EnumValueMappee(value) {
this.value = value;
}
/**
* Maps the wrapped value using the supplied mapper.
* Returns the value of the mapper's property whose name matches the wrapped
* value.
*
* @template T - The data type that the enum or string/number literal value
* will be mapped to.
*
* @param mapper - A mapper implementation for type E that returns type T.
* @returns The mapped value from the mapper.
*/
EnumValueMappee.prototype.with = function (mapper) {

@@ -36,4 +13,4 @@ if (mapper.hasOwnProperty(this.value)) {

}
else if (mapper.hasOwnProperty(Symbols_1.Symbols.handleUnexpected)) {
return processEntry(mapper[Symbols_1.Symbols.handleUnexpected], this.value);
else if (mapper.hasOwnProperty(symbols_1.handleUnexpected)) {
return processEntry(mapper[symbols_1.handleUnexpected], this.value);
}

@@ -47,40 +24,11 @@ else {

exports.EnumValueMappee = EnumValueMappee;
/**
* A wrapper around an enum or string/number literal value to be mapped.
* For values that may be null.
* Do not use this class directly. Use the {@link $enum.mapValue} function to
* get an instance of this class.
*
* NOTE: At run time, this class is used by {@link $enum.mapValue} ONLY for
* handling null values.
* {@link EnumValueMappee} contains the core run time implementation that is
* applicable to all "EnumValueMappee" classes.
*
* @template E - An enum or string/number literal type.
*/
var EnumValueMappeeWithNull = /** @class */ (function () {
var EnumValueMappeeWithNull = (function () {
function EnumValueMappeeWithNull() {
}
/**
* Maps the wrapped value using the supplied mapper.
* If the wrapped value is null, returns the mapper's
* {@link Symbols.handleNull} value.
* Otherwise, returns the value of the mapper's property whose name matches
* the wrapped value.
*
* @template T - The data type that the enum or string/number literal value
* will be mapped to.
*
* @param mapper - A mapper implementation for type E that returns type T.
* @returns The mapped value from the mapper.
*/
EnumValueMappeeWithNull.prototype.with = function (mapper) {
// This class is used at run time for mapping null values regardless of
// the compile time type being visited, so we actually have to check if
// handleNull exists.
if (mapper.hasOwnProperty(Symbols_1.Symbols.handleNull)) {
return processEntry(mapper[Symbols_1.Symbols.handleNull], null);
if (mapper.hasOwnProperty(symbols_1.handleNull)) {
return processEntry(mapper[symbols_1.handleNull], null);
}
else if (mapper.hasOwnProperty(Symbols_1.Symbols.handleUnexpected)) {
return processEntry(mapper[Symbols_1.Symbols.handleUnexpected], null);
else if (mapper.hasOwnProperty(symbols_1.handleUnexpected)) {
return processEntry(mapper[symbols_1.handleUnexpected], null);
}

@@ -94,40 +42,11 @@ else {

exports.EnumValueMappeeWithNull = EnumValueMappeeWithNull;
/**
* A wrapper around an enum or string/number literal value to be mapped.
* For values that may be undefined.
* Do not use this class directly. Use the {@link $enum.mapValue} function to
* get an instance of this class.
*
* NOTE: At run time, this class is used by {@link $enum.mapValue} ONLY for
* handling undefined values.
* {@link EnumValueMappee} contains the core run time implementation that is
* applicable to all "EnumValueMappee" classes.
*
* @template E - An enum or string/number literal type.
*/
var EnumValueMappeeWithUndefined = /** @class */ (function () {
var EnumValueMappeeWithUndefined = (function () {
function EnumValueMappeeWithUndefined() {
}
/**
* Maps the wrapped value using the supplied mapper.
* If the wrapped value is undefined, returns the mapper's
* {@link Symbols.handleUndefined} value.
* Otherwise, returns the value of the mapper's property whose name matches
* the wrapped value.
*
* @template T - The data type that the enum or string/number literal value
* will be mapped to.
*
* @param mapper - A mapper implementation for type E that returns type T.
* @returns The mapped value from the mapper.
*/
EnumValueMappeeWithUndefined.prototype.with = function (mapper) {
// This class is used at run time for mapping undefined values
// regardless of the compile time type being visited, so we actually
// have to check if handleUndefined exists.
if (mapper.hasOwnProperty(Symbols_1.Symbols.handleUndefined)) {
return processEntry(mapper[Symbols_1.Symbols.handleUndefined], undefined);
if (mapper.hasOwnProperty(symbols_1.handleUndefined)) {
return processEntry(mapper[symbols_1.handleUndefined], undefined);
}
else if (mapper.hasOwnProperty(Symbols_1.Symbols.handleUnexpected)) {
return processEntry(mapper[Symbols_1.Symbols.handleUnexpected], undefined);
else if (mapper.hasOwnProperty(symbols_1.handleUnexpected)) {
return processEntry(mapper[symbols_1.handleUnexpected], undefined);
}

@@ -141,11 +60,4 @@ else {

exports.EnumValueMappeeWithUndefined = EnumValueMappeeWithUndefined;
/**
* Common implementation for processing an entry of an enum value mapper.
* @param entry - Either the mapped value entry, or {@link Symbols.unhandledEntry}.
* @param value - The value being mapped.
* @return The provided entry, if it is not an unhandledEntry.
* @throws {Error} If the provided entry is an unhandledEntry.
*/
function processEntry(entry, value) {
if (entry === Symbols_1.Symbols.unhandledEntry) {
if (entry === symbols_1.unhandledEntry) {
throw createUnhandledEntryError_1.createUnhandledEntryError(value);

@@ -152,0 +64,0 @@ }

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Symbols_1 = require("./Symbols");
var symbols_1 = require("./symbols");
//# sourceMappingURL=EnumValueMapper.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Symbols_1 = require("./Symbols");
var symbols_1 = require("./symbols");
var createUnhandledEntryError_1 = require("./createUnhandledEntryError");
/**
* A wrapper around a string literal or string enum value to be visited.
* Do not use this class directly. Use the {@link visitString} function to get an instance of this class.
*
* @template E - A string literal type or string enum type.
*/
var EnumValueVisitee = /** @class */ (function () {
/**
* Do not use this constructor directly. Use the {@link visitString} function to get an instance of this class.
* @param value - The value to be wrapped by this "visitee".
*/
var EnumValueVisitee = (function () {
function EnumValueVisitee(value) {
this.value = value;
}
/**
* Visits the wrapped value using the supplied visitor.
* Calls the visitor method whose name matches the wrapped value.
*
* @template R - The return type of the visitor methods.
*
* @param visitor - A visitor implementation for type E that returns type R.
* @returns The return value of the visitor method that is called.
*/
EnumValueVisitee.prototype.with = function (visitor) {

@@ -33,4 +14,4 @@ if (visitor.hasOwnProperty(this.value)) {

}
else if (visitor[Symbols_1.Symbols.handleUnexpected]) {
return processEntry(visitor[Symbols_1.Symbols.handleUnexpected], this
else if (visitor[symbols_1.handleUnexpected]) {
return processEntry(visitor[symbols_1.handleUnexpected], this
.value);

@@ -45,34 +26,11 @@ }

exports.EnumValueVisitee = EnumValueVisitee;
/**
* A wrapper around a string literal or string enum value to be visited.
* For values that may be null.
* Do not use this class directly. Use the {@link visitString} function to get an instance of this class.
*
* NOTE: At run time, this class is used by {@link visitString} ONLY for handling null values.
* {@link EnumValueVisitee} contains the core run time implementation that is applicable to all
* "EnumValueVisitee" classes.
*
* @template E - A string literal type or string enum type.
*/
var EnumValueVisiteeWithNull = /** @class */ (function () {
var EnumValueVisiteeWithNull = (function () {
function EnumValueVisiteeWithNull() {
}
/**
* Visits the wrapped value using the supplied visitor.
* If the wrapped value is null, calls the visitor's {@link StringNullVisitor#handleNull} method.
* Otherwise, calls the visitor method whose name matches the wrapped value.
*
* @template R - The return type of the visitor methods.
*
* @param visitor - A visitor implementation for type E that returns type R.
* @returns The return value of the visitor method that is called.
*/
EnumValueVisiteeWithNull.prototype.with = function (visitor) {
// This class is used at run time for visiting null values regardless of the compile time
// type being visited, so we actually have to check if handleNull exists.
if (visitor[Symbols_1.Symbols.handleNull]) {
return processEntry(visitor[Symbols_1.Symbols.handleNull], null);
if (visitor[symbols_1.handleNull]) {
return processEntry(visitor[symbols_1.handleNull], null);
}
else if (visitor[Symbols_1.Symbols.handleUnexpected]) {
return processEntry(visitor[Symbols_1.Symbols.handleUnexpected], null);
else if (visitor[symbols_1.handleUnexpected]) {
return processEntry(visitor[symbols_1.handleUnexpected], null);
}

@@ -86,34 +44,11 @@ else {

exports.EnumValueVisiteeWithNull = EnumValueVisiteeWithNull;
/**
* A wrapper around a string literal or string enum value to be visited.
* For values that may be undefined.
* Do not use this class directly. Use the {@link visitString} function to get an instance of this class.
*
* NOTE: At run time, this class is used by {@link visitString} ONLY for handling undefined values.
* {@link EnumValueVisitee} contains the core run time implementation that is applicable to all
* "EnumValueVisitee" classes.
*
* @template E - A string literal type or string enum type.
*/
var EnumValueVisiteeWithUndefined = /** @class */ (function () {
var EnumValueVisiteeWithUndefined = (function () {
function EnumValueVisiteeWithUndefined() {
}
/**
* Visits the wrapped value using the supplied visitor.
* If the wrapped value is undefined, calls the visitor's {@link StringNullVisitor#handleUndefined} method.
* Otherwise, calls the visitor method whose name matches the wrapped value.
*
* @template R - The return type of the visitor methods.
*
* @param visitor - A visitor implementation for type E that returns type R.
* @returns The return value of the visitor method that is called.
*/
EnumValueVisiteeWithUndefined.prototype.with = function (visitor) {
// This class is used at run time for visiting undefined values regardless of the compile time
// type being visited, so we actually have to check if handleUndefined exists.
if (visitor[Symbols_1.Symbols.handleUndefined]) {
return processEntry(visitor[Symbols_1.Symbols.handleUndefined], undefined);
if (visitor[symbols_1.handleUndefined]) {
return processEntry(visitor[symbols_1.handleUndefined], undefined);
}
else if (visitor[Symbols_1.Symbols.handleUnexpected]) {
return processEntry(visitor[Symbols_1.Symbols.handleUnexpected], undefined);
else if (visitor[symbols_1.handleUnexpected]) {
return processEntry(visitor[symbols_1.handleUnexpected], undefined);
}

@@ -127,11 +62,4 @@ else {

exports.EnumValueVisiteeWithUndefined = EnumValueVisiteeWithUndefined;
/**
* Common implementation for processing an entry of an enum value visitor.
* @param entry - Either the visitor handler implementation for an entry, or an UnhandledEntry.
* @param value - The value being mapped.
* @return The result of executing the provided entry, if it is not an UnhandledEntry.
* @throws {Error} If the provided entry is an UnhandledEntry.
*/
function processEntry(entry, value) {
if (entry === Symbols_1.Symbols.unhandledEntry) {
if (entry === symbols_1.unhandledEntry) {
throw createUnhandledEntryError_1.createUnhandledEntryError(value);

@@ -138,0 +66,0 @@ }

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Symbols_1 = require("./Symbols");
var symbols_1 = require("./symbols");
//# sourceMappingURL=EnumValueVisitor.js.map
"use strict";
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 {@link $enum} to publicly get/create an EnumWrapper
*
* @param enumObj - An enum-like object. See the {@link EnumLike} type for more explanation.
*/
var objectKeysUtil_1 = require("./objectKeysUtil");
var EnumWrapper = (function () {
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());
this.keysList = Object.freeze(objectKeysUtil_1.getOwnEnumerableNonArrayIndexKeys(enumObj));
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) {

@@ -44,3 +16,2 @@ var key = this.keysList[index];

keysByValueMap.set(value, key);
// Type casting of "this" necessary to bypass readonly index signature for initialization.
this[index] = Object.freeze([key, value]);

@@ -51,3 +22,2 @@ }

this.size = this.length = length;
// Make the EnumWrapper instance immutable
Object.freeze(this);

@@ -62,18 +32,5 @@ }

});
/**
* @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 () {

@@ -99,10 +56,2 @@ var _this = 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.
*/
EnumWrapper.prototype.values = function () {

@@ -128,7 +77,2 @@ var _this = 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.
*/
EnumWrapper.prototype.entries = function () {

@@ -143,3 +87,2 @@ var _this = this;

done: isDone,
// NOTE: defensive copy not necessary because entries are "frozen"
value: _this[index]

@@ -156,22 +99,7 @@ };

};
/**
* 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) {

@@ -182,17 +110,5 @@ var entry = 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) {

@@ -204,50 +120,16 @@ var entry = this[index];

};
/**
* 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) &&
objectKeysUtil_1.isNonArrayIndexKey(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) {

@@ -261,14 +143,4 @@ if (this.isKey(key)) {

};
/**
* 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;

@@ -280,18 +152,5 @@ }

};
/**
* 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) {

@@ -305,10 +164,2 @@ if (this.isValue(value)) {

};
/**
* 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) {

@@ -322,14 +173,3 @@ if (this.isValue(value)) {

};
/**
* 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;

@@ -343,14 +183,3 @@ if (result != null) {

};
/**
* 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;

@@ -364,30 +193,7 @@ if (result != null) {

};
/**
* 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];

@@ -402,13 +208,2 @@ }

exports.EnumWrapper = EnumWrapper;
/**
* 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));
}
//# sourceMappingURL=EnumWrapper.js.map

@@ -10,2 +10,5 @@ "use strict";

__export(require("./$enum"));
__export(require("./mapEnumValue"));
__export(require("./visitEnumValue"));
__export(require("./symbols"));
//# sourceMappingURL=index.js.map

@@ -5,5 +5,2 @@ "use strict";

function mapEnumValue(value) {
// NOTE: The run time type of EnumValueMappee created does not necessarily match
// the compile-time type. This results in unusual EnumValueMappee.with()
// implementations.
if (value === null) {

@@ -10,0 +7,0 @@ return new EnumValueMappee_1.EnumValueMappeeWithNull();

@@ -5,5 +5,2 @@ "use strict";

function visitEnumValue(value) {
// NOTE: The run time type of EnumValueVisitee created does not necessarily match
// the compile-time type. This results in unusual EnumValueVisitee.with()
// implementations.
if (value === null) {

@@ -10,0 +7,0 @@ return new EnumValueVisitee_1.EnumValueVisiteeWithNull();

import { EnumWrapper } from "./EnumWrapper";
import { Symbols } from "./Symbols";
import * as symbols from "./symbols";
import { visitEnumValue } from "./visitEnumValue";
import { mapEnumValue } from "./mapEnumValue";
/**
* Map of enum object -> EnumWrapper instance.
* Used as a cache for {@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}
*/
var enumWrapperInstancesCache = new WeakMap();

@@ -26,28 +14,8 @@ export function $enum(enumObj) {

}
(function ($enum) {
/**
* Convenient alias for {@link Symbols.unhandled}.
*/
$enum.unhandled = Symbols.unhandledEntry;
/**
* Convenient alias for {@link Symbols.handleNull}.
*/
$enum.handleNull = Symbols.handleNull;
/**
* Convenient alias for {@link Symbols.handleUndefined}.
*/
$enum.handleUndefined = Symbols.handleUndefined;
/**
* Convenient alias for {@link Symbols.handleUnexpected}.
*/
$enum.handleUnexpected = Symbols.handleUnexpected;
/**
* Convenient alias for {@link visitEnumValue}
*/
$enum.visitValue = visitEnumValue;
/**
* Convenient alias for {@link mapEnumValue}
*/
$enum.mapValue = mapEnumValue;
})($enum || ($enum = {}));
$enum.handleNull = symbols.handleNull;
$enum.handleUndefined = symbols.handleUndefined;
$enum.handleUnexpected = symbols.handleUnexpected;
$enum.unhandledEntry = symbols.unhandledEntry;
$enum.visitValue = visitEnumValue;
$enum.mapValue = mapEnumValue;
//# sourceMappingURL=$enum.js.map

@@ -1,8 +0,1 @@

/**
* Creates an Error with a message explaining that an unhandled
* value was encountered.
* @param unhandledValue - The unhandled value.
* @return an Error with a message explaining that an unhandled
* value was encountered.
*/
export function createUnhandledEntryError(unhandledValue) {

@@ -9,0 +2,0 @@ return new Error("Unhandled value: " + unhandledValue);

import { createUnhandledEntryError } from "./createUnhandledEntryError";
import { Symbols } from "./Symbols";
/**
* A wrapper around an enum or string/number literal value to be mapped.
* Do not use this class directly. Use the {@link $enum.mapValue} function to
* get an instance of this class.
*
* @template E - An enum or string/number literal type.
*/
var EnumValueMappee = /** @class */ (function () {
/**
* Do not use this constructor directly. Use the {@link $enum.mapValue}
* function to get an instance of this class.
* @param value - The value to be wrapped by this "mappee".
*/
import { handleUnexpected, handleNull, handleUndefined, unhandledEntry } from "./symbols";
var EnumValueMappee = (function () {
function EnumValueMappee(value) {
this.value = value;
}
/**
* Maps the wrapped value using the supplied mapper.
* Returns the value of the mapper's property whose name matches the wrapped
* value.
*
* @template T - The data type that the enum or string/number literal value
* will be mapped to.
*
* @param mapper - A mapper implementation for type E that returns type T.
* @returns The mapped value from the mapper.
*/
EnumValueMappee.prototype.with = function (mapper) {

@@ -34,4 +11,4 @@ if (mapper.hasOwnProperty(this.value)) {

}
else if (mapper.hasOwnProperty(Symbols.handleUnexpected)) {
return processEntry(mapper[Symbols.handleUnexpected], this.value);
else if (mapper.hasOwnProperty(handleUnexpected)) {
return processEntry(mapper[handleUnexpected], this.value);
}

@@ -45,40 +22,11 @@ else {

export { EnumValueMappee };
/**
* A wrapper around an enum or string/number literal value to be mapped.
* For values that may be null.
* Do not use this class directly. Use the {@link $enum.mapValue} function to
* get an instance of this class.
*
* NOTE: At run time, this class is used by {@link $enum.mapValue} ONLY for
* handling null values.
* {@link EnumValueMappee} contains the core run time implementation that is
* applicable to all "EnumValueMappee" classes.
*
* @template E - An enum or string/number literal type.
*/
var EnumValueMappeeWithNull = /** @class */ (function () {
var EnumValueMappeeWithNull = (function () {
function EnumValueMappeeWithNull() {
}
/**
* Maps the wrapped value using the supplied mapper.
* If the wrapped value is null, returns the mapper's
* {@link Symbols.handleNull} value.
* Otherwise, returns the value of the mapper's property whose name matches
* the wrapped value.
*
* @template T - The data type that the enum or string/number literal value
* will be mapped to.
*
* @param mapper - A mapper implementation for type E that returns type T.
* @returns The mapped value from the mapper.
*/
EnumValueMappeeWithNull.prototype.with = function (mapper) {
// This class is used at run time for mapping null values regardless of
// the compile time type being visited, so we actually have to check if
// handleNull exists.
if (mapper.hasOwnProperty(Symbols.handleNull)) {
return processEntry(mapper[Symbols.handleNull], null);
if (mapper.hasOwnProperty(handleNull)) {
return processEntry(mapper[handleNull], null);
}
else if (mapper.hasOwnProperty(Symbols.handleUnexpected)) {
return processEntry(mapper[Symbols.handleUnexpected], null);
else if (mapper.hasOwnProperty(handleUnexpected)) {
return processEntry(mapper[handleUnexpected], null);
}

@@ -92,40 +40,11 @@ else {

export { EnumValueMappeeWithNull };
/**
* A wrapper around an enum or string/number literal value to be mapped.
* For values that may be undefined.
* Do not use this class directly. Use the {@link $enum.mapValue} function to
* get an instance of this class.
*
* NOTE: At run time, this class is used by {@link $enum.mapValue} ONLY for
* handling undefined values.
* {@link EnumValueMappee} contains the core run time implementation that is
* applicable to all "EnumValueMappee" classes.
*
* @template E - An enum or string/number literal type.
*/
var EnumValueMappeeWithUndefined = /** @class */ (function () {
var EnumValueMappeeWithUndefined = (function () {
function EnumValueMappeeWithUndefined() {
}
/**
* Maps the wrapped value using the supplied mapper.
* If the wrapped value is undefined, returns the mapper's
* {@link Symbols.handleUndefined} value.
* Otherwise, returns the value of the mapper's property whose name matches
* the wrapped value.
*
* @template T - The data type that the enum or string/number literal value
* will be mapped to.
*
* @param mapper - A mapper implementation for type E that returns type T.
* @returns The mapped value from the mapper.
*/
EnumValueMappeeWithUndefined.prototype.with = function (mapper) {
// This class is used at run time for mapping undefined values
// regardless of the compile time type being visited, so we actually
// have to check if handleUndefined exists.
if (mapper.hasOwnProperty(Symbols.handleUndefined)) {
return processEntry(mapper[Symbols.handleUndefined], undefined);
if (mapper.hasOwnProperty(handleUndefined)) {
return processEntry(mapper[handleUndefined], undefined);
}
else if (mapper.hasOwnProperty(Symbols.handleUnexpected)) {
return processEntry(mapper[Symbols.handleUnexpected], undefined);
else if (mapper.hasOwnProperty(handleUnexpected)) {
return processEntry(mapper[handleUnexpected], undefined);
}

@@ -139,11 +58,4 @@ else {

export { EnumValueMappeeWithUndefined };
/**
* Common implementation for processing an entry of an enum value mapper.
* @param entry - Either the mapped value entry, or {@link Symbols.unhandledEntry}.
* @param value - The value being mapped.
* @return The provided entry, if it is not an unhandledEntry.
* @throws {Error} If the provided entry is an unhandledEntry.
*/
function processEntry(entry, value) {
if (entry === Symbols.unhandledEntry) {
if (entry === unhandledEntry) {
throw createUnhandledEntryError(value);

@@ -150,0 +62,0 @@ }

@@ -1,2 +0,2 @@

import { Symbols } from "./Symbols";
import { handleUnexpected, handleNull, handleUndefined } from "./symbols";
//# sourceMappingURL=EnumValueMapper.js.map

@@ -1,26 +0,7 @@

import { Symbols } from "./Symbols";
import { handleUnexpected, handleNull, handleUndefined, unhandledEntry } from "./symbols";
import { createUnhandledEntryError } from "./createUnhandledEntryError";
/**
* A wrapper around a string literal or string enum value to be visited.
* Do not use this class directly. Use the {@link visitString} function to get an instance of this class.
*
* @template E - A string literal type or string enum type.
*/
var EnumValueVisitee = /** @class */ (function () {
/**
* Do not use this constructor directly. Use the {@link visitString} function to get an instance of this class.
* @param value - The value to be wrapped by this "visitee".
*/
var EnumValueVisitee = (function () {
function EnumValueVisitee(value) {
this.value = value;
}
/**
* Visits the wrapped value using the supplied visitor.
* Calls the visitor method whose name matches the wrapped value.
*
* @template R - The return type of the visitor methods.
*
* @param visitor - A visitor implementation for type E that returns type R.
* @returns The return value of the visitor method that is called.
*/
EnumValueVisitee.prototype.with = function (visitor) {

@@ -31,4 +12,4 @@ if (visitor.hasOwnProperty(this.value)) {

}
else if (visitor[Symbols.handleUnexpected]) {
return processEntry(visitor[Symbols.handleUnexpected], this
else if (visitor[handleUnexpected]) {
return processEntry(visitor[handleUnexpected], this
.value);

@@ -43,34 +24,11 @@ }

export { EnumValueVisitee };
/**
* A wrapper around a string literal or string enum value to be visited.
* For values that may be null.
* Do not use this class directly. Use the {@link visitString} function to get an instance of this class.
*
* NOTE: At run time, this class is used by {@link visitString} ONLY for handling null values.
* {@link EnumValueVisitee} contains the core run time implementation that is applicable to all
* "EnumValueVisitee" classes.
*
* @template E - A string literal type or string enum type.
*/
var EnumValueVisiteeWithNull = /** @class */ (function () {
var EnumValueVisiteeWithNull = (function () {
function EnumValueVisiteeWithNull() {
}
/**
* Visits the wrapped value using the supplied visitor.
* If the wrapped value is null, calls the visitor's {@link StringNullVisitor#handleNull} method.
* Otherwise, calls the visitor method whose name matches the wrapped value.
*
* @template R - The return type of the visitor methods.
*
* @param visitor - A visitor implementation for type E that returns type R.
* @returns The return value of the visitor method that is called.
*/
EnumValueVisiteeWithNull.prototype.with = function (visitor) {
// This class is used at run time for visiting null values regardless of the compile time
// type being visited, so we actually have to check if handleNull exists.
if (visitor[Symbols.handleNull]) {
return processEntry(visitor[Symbols.handleNull], null);
if (visitor[handleNull]) {
return processEntry(visitor[handleNull], null);
}
else if (visitor[Symbols.handleUnexpected]) {
return processEntry(visitor[Symbols.handleUnexpected], null);
else if (visitor[handleUnexpected]) {
return processEntry(visitor[handleUnexpected], null);
}

@@ -84,34 +42,11 @@ else {

export { EnumValueVisiteeWithNull };
/**
* A wrapper around a string literal or string enum value to be visited.
* For values that may be undefined.
* Do not use this class directly. Use the {@link visitString} function to get an instance of this class.
*
* NOTE: At run time, this class is used by {@link visitString} ONLY for handling undefined values.
* {@link EnumValueVisitee} contains the core run time implementation that is applicable to all
* "EnumValueVisitee" classes.
*
* @template E - A string literal type or string enum type.
*/
var EnumValueVisiteeWithUndefined = /** @class */ (function () {
var EnumValueVisiteeWithUndefined = (function () {
function EnumValueVisiteeWithUndefined() {
}
/**
* Visits the wrapped value using the supplied visitor.
* If the wrapped value is undefined, calls the visitor's {@link StringNullVisitor#handleUndefined} method.
* Otherwise, calls the visitor method whose name matches the wrapped value.
*
* @template R - The return type of the visitor methods.
*
* @param visitor - A visitor implementation for type E that returns type R.
* @returns The return value of the visitor method that is called.
*/
EnumValueVisiteeWithUndefined.prototype.with = function (visitor) {
// This class is used at run time for visiting undefined values regardless of the compile time
// type being visited, so we actually have to check if handleUndefined exists.
if (visitor[Symbols.handleUndefined]) {
return processEntry(visitor[Symbols.handleUndefined], undefined);
if (visitor[handleUndefined]) {
return processEntry(visitor[handleUndefined], undefined);
}
else if (visitor[Symbols.handleUnexpected]) {
return processEntry(visitor[Symbols.handleUnexpected], undefined);
else if (visitor[handleUnexpected]) {
return processEntry(visitor[handleUnexpected], undefined);
}

@@ -125,11 +60,4 @@ else {

export { EnumValueVisiteeWithUndefined };
/**
* Common implementation for processing an entry of an enum value visitor.
* @param entry - Either the visitor handler implementation for an entry, or an UnhandledEntry.
* @param value - The value being mapped.
* @return The result of executing the provided entry, if it is not an UnhandledEntry.
* @throws {Error} If the provided entry is an UnhandledEntry.
*/
function processEntry(entry, value) {
if (entry === Symbols.unhandledEntry) {
if (entry === unhandledEntry) {
throw createUnhandledEntryError(value);

@@ -136,0 +64,0 @@ }

@@ -1,2 +0,2 @@

import { Symbols } from "./Symbols";
import { handleUnexpected, handleNull, handleUndefined } from "./symbols";
//# sourceMappingURL=EnumValueVisitor.js.map

@@ -1,37 +0,9 @@

/**
* 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 {@link $enum} to publicly get/create an EnumWrapper
*
* @param enumObj - An enum-like object. See the {@link EnumLike} type for more explanation.
*/
import { isNonArrayIndexKey, getOwnEnumerableNonArrayIndexKeys } from "./objectKeysUtil";
var EnumWrapper = (function () {
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());
this.keysList = Object.freeze(getOwnEnumerableNonArrayIndexKeys(enumObj));
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) {

@@ -42,3 +14,2 @@ var key = this.keysList[index];

keysByValueMap.set(value, key);
// Type casting of "this" necessary to bypass readonly index signature for initialization.
this[index] = Object.freeze([key, value]);

@@ -49,3 +20,2 @@ }

this.size = this.length = length;
// Make the EnumWrapper instance immutable
Object.freeze(this);

@@ -60,18 +30,5 @@ }

});
/**
* @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 () {

@@ -97,10 +54,2 @@ var _this = 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.
*/
EnumWrapper.prototype.values = function () {

@@ -126,7 +75,2 @@ var _this = 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.
*/
EnumWrapper.prototype.entries = function () {

@@ -141,3 +85,2 @@ var _this = this;

done: isDone,
// NOTE: defensive copy not necessary because entries are "frozen"
value: _this[index]

@@ -154,22 +97,7 @@ };

};
/**
* 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) {

@@ -180,17 +108,5 @@ var entry = 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) {

@@ -202,50 +118,16 @@ var entry = this[index];

};
/**
* 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) &&
isNonArrayIndexKey(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) {

@@ -259,14 +141,4 @@ if (this.isKey(key)) {

};
/**
* 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;

@@ -278,18 +150,5 @@ }

};
/**
* 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) {

@@ -303,10 +162,2 @@ if (this.isValue(value)) {

};
/**
* 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) {

@@ -320,14 +171,3 @@ if (this.isValue(value)) {

};
/**
* 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;

@@ -341,14 +181,3 @@ if (result != null) {

};
/**
* 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;

@@ -362,30 +191,7 @@ if (result != null) {

};
/**
* 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];

@@ -400,13 +206,2 @@ }

export { EnumWrapper };
/**
* 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));
}
//# sourceMappingURL=EnumWrapper.js.map

@@ -5,2 +5,5 @@ export * from "./EnumWrapper";

export * from "./$enum";
export * from "./mapEnumValue";
export * from "./visitEnumValue";
export * from "./symbols";
//# sourceMappingURL=index.js.map
import { EnumValueMappee, EnumValueMappeeWithNull, EnumValueMappeeWithUndefined } from "./EnumValueMappee";
export function mapEnumValue(value) {
// NOTE: The run time type of EnumValueMappee created does not necessarily match
// the compile-time type. This results in unusual EnumValueMappee.with()
// implementations.
if (value === null) {

@@ -7,0 +4,0 @@ return new EnumValueMappeeWithNull();

import { EnumValueVisitee, EnumValueVisiteeWithNull, EnumValueVisiteeWithUndefined } from "./EnumValueVisitee";
export function visitEnumValue(value) {
// NOTE: The run time type of EnumValueVisitee created does not necessarily match
// the compile-time type. This results in unusual EnumValueVisitee.with()
// implementations.
if (value === null) {

@@ -7,0 +4,0 @@ return new EnumValueVisiteeWithNull();

@@ -1,3 +0,4 @@

import { EnumWrapper, EnumLike } from "./EnumWrapper";
import { Symbols } from "./Symbols";
import { EnumWrapper } from "./EnumWrapper";
import { StringKeyOf } from "./types";
import * as symbols from "./symbols";
import { visitEnumValue } from "./visitEnumValue";

@@ -13,3 +14,11 @@ import { mapEnumValue } from "./mapEnumValue";

*/
export declare function $enum<V extends number, T extends EnumLike<number, Extract<keyof T, string>>>(enumObj: T): EnumWrapper<number, T>;
export declare function $enum<V extends number, T extends Record<StringKeyOf<T>, number>>(enumObj: T): EnumWrapper<number, T>;
export declare namespace $enum {
var handleNull: typeof symbols.handleNull;
var handleUndefined: typeof symbols.handleUndefined;
var handleUnexpected: typeof symbols.handleUnexpected;
var unhandledEntry: typeof symbols.unhandledEntry;
var visitValue: typeof visitEnumValue;
var mapValue: typeof mapEnumValue;
}
/**

@@ -23,3 +32,11 @@ * Gets a cached EnumWrapper for an enum-like object with string values.

*/
export declare function $enum<T extends EnumLike<string, Extract<keyof T, string>>>(enumObj: T): EnumWrapper<string, T>;
export declare function $enum<T extends Record<StringKeyOf<T>, string>>(enumObj: T): EnumWrapper<string, T>;
export declare namespace $enum {
var handleNull: typeof symbols.handleNull;
var handleUndefined: typeof symbols.handleUndefined;
var handleUnexpected: typeof symbols.handleUnexpected;
var unhandledEntry: typeof symbols.unhandledEntry;
var visitValue: typeof visitEnumValue;
var mapValue: typeof mapEnumValue;
}
/**

@@ -35,29 +52,11 @@ * Gets a cached EnumWrapper for an enum-like object with a mixture of number

*/
export declare function $enum<T extends EnumLike<number | string, Extract<keyof T, string>>>(enumObj: T): EnumWrapper<number | string, T>;
export declare function $enum<T extends Record<StringKeyOf<T>, number | string>>(enumObj: T): EnumWrapper<number | string, T>;
export declare namespace $enum {
/**
* Convenient alias for {@link Symbols.unhandled}.
*/
const unhandled: typeof Symbols.unhandledEntry;
/**
* Convenient alias for {@link Symbols.handleNull}.
*/
const handleNull: typeof Symbols.handleNull;
/**
* Convenient alias for {@link Symbols.handleUndefined}.
*/
const handleUndefined: typeof Symbols.handleUndefined;
/**
* Convenient alias for {@link Symbols.handleUnexpected}.
*/
const handleUnexpected: typeof Symbols.handleUnexpected;
/**
* Convenient alias for {@link visitEnumValue}
*/
const visitValue: typeof visitEnumValue;
/**
* Convenient alias for {@link mapEnumValue}
*/
const mapValue: typeof mapEnumValue;
var handleNull: typeof symbols.handleNull;
var handleUndefined: typeof symbols.handleUndefined;
var handleUnexpected: typeof symbols.handleUnexpected;
var unhandledEntry: typeof symbols.unhandledEntry;
var visitValue: typeof visitEnumValue;
var mapValue: typeof mapEnumValue;
}
//# sourceMappingURL=$enum.d.ts.map

@@ -47,3 +47,3 @@ import { EnumValueMapper, EnumValueMapperWithNull, EnumValueMapperWithUndefined, EnumValueMapperWithNullAndUndefined } from "./EnumValueMapper";

* If the wrapped value is null, returns the mapper's
* {@link Symbols.handleNull} value.
* {@link handleNull} value.
* Otherwise, returns the value of the mapper's property whose name matches

@@ -77,3 +77,3 @@ * the wrapped value.

* If the wrapped value is undefined, returns the mapper's
* {@link Symbols.handleUndefined} value.
* {@link handleUndefined} value.
* Otherwise, returns the value of the mapper's property whose name matches

@@ -109,5 +109,5 @@ * the wrapped value.

* If the wrapped value is null, returns the mapper's
* {@link Symbols.handleNull} value.
* {@link handleNull} value.
* If the wrapped value is undefined, returns the mapper's
* {@link Symbols.handleUndefined} value.
* {@link handleUndefined} value.
* Otherwise, returns the value of the mapper's property whose name matches

@@ -114,0 +114,0 @@ * the wrapped value.

@@ -1,2 +0,2 @@

import { Symbols } from "./Symbols";
import { handleUnexpected, handleNull, handleUndefined, unhandledEntry } from "./symbols";
/**

@@ -10,3 +10,3 @@ * Core definition of all enum value mapper interfaces.

export declare type EnumValueMapperCore<E extends string | number, T> = {
[P in E]: T | typeof Symbols.unhandledEntry;
[P in E]: T | typeof unhandledEntry;
};

@@ -21,3 +21,3 @@ /**

export interface UnexpectedEnumValueMapper<T> {
[Symbols.handleUnexpected]?: T | typeof Symbols.unhandledEntry;
[handleUnexpected]?: T | typeof unhandledEntry;
}

@@ -32,3 +32,3 @@ /**

export interface NullEnumValueMapper<T> {
[Symbols.handleNull]: T | typeof Symbols.unhandledEntry;
[handleNull]: T | typeof unhandledEntry;
}

@@ -43,3 +43,3 @@ /**

export interface UndefinedEnumValueMapper<T> {
[Symbols.handleUndefined]: T | typeof Symbols.unhandledEntry;
[handleUndefined]: T | typeof unhandledEntry;
}

@@ -46,0 +46,0 @@ /**

@@ -1,2 +0,2 @@

import { Symbols } from "./Symbols";
import { handleUnexpected, handleNull, handleUndefined, unhandledEntry } from "./symbols";
/**

@@ -22,3 +22,3 @@ * Helper type to widen a number/string enum/literal type to plain string or number.

export declare type EnumValueVisitorCore<E extends string | number, R> = {
[P in E]: EnumValueVisitorHandler<P, R> | typeof Symbols.unhandledEntry;
[P in E]: EnumValueVisitorHandler<P, R> | typeof unhandledEntry;
};

@@ -32,3 +32,3 @@ /**

export interface NullEnumValueVisitor<R> {
[Symbols.handleNull]: EnumValueVisitorHandler<null, R> | typeof Symbols.unhandledEntry;
[handleNull]: EnumValueVisitorHandler<null, R> | typeof unhandledEntry;
}

@@ -42,3 +42,3 @@ /**

export interface UndefinedEnumValueVisitor<R> {
[Symbols.handleUndefined]: EnumValueVisitorHandler<undefined, R> | typeof Symbols.unhandledEntry;
[handleUndefined]: EnumValueVisitorHandler<undefined, R> | typeof unhandledEntry;
}

@@ -52,3 +52,3 @@ /**

export declare type EnumValueVisitor<E extends string | number, R> = EnumValueVisitorCore<E, R> & {
[Symbols.handleUnexpected]?: EnumValueVisitorHandler<WidenEnumType<E> | null | undefined, R> | typeof Symbols.unhandledEntry;
[handleUnexpected]?: EnumValueVisitorHandler<WidenEnumType<E> | null | undefined, R> | typeof unhandledEntry;
};

@@ -63,3 +63,3 @@ /**

export declare type EnumValueVisitorWithNull<E extends string | number, R> = EnumValueVisitorCore<E, R> & NullEnumValueVisitor<R> & {
[Symbols.handleUnexpected]?: EnumValueVisitorHandler<WidenEnumType<E> | undefined, R> | typeof Symbols.unhandledEntry;
[handleUnexpected]?: EnumValueVisitorHandler<WidenEnumType<E> | undefined, R> | typeof unhandledEntry;
};

@@ -74,3 +74,3 @@ /**

export declare type EnumValueVisitorWithUndefined<E extends string | number, R> = EnumValueVisitorCore<E, R> & UndefinedEnumValueVisitor<R> & {
[Symbols.handleUnexpected]?: EnumValueVisitorHandler<WidenEnumType<E> | null, R> | typeof Symbols.unhandledEntry;
[handleUnexpected]?: EnumValueVisitorHandler<WidenEnumType<E> | null, R> | typeof unhandledEntry;
};

@@ -85,4 +85,4 @@ /**

export declare type EnumValueVisitorWithNullAndUndefined<E extends string | number, R> = EnumValueVisitorCore<E, R> & NullEnumValueVisitor<R> & UndefinedEnumValueVisitor<R> & {
[Symbols.handleUnexpected]?: EnumValueVisitorHandler<WidenEnumType<E>, R> | typeof Symbols.unhandledEntry;
[handleUnexpected]?: EnumValueVisitorHandler<WidenEnumType<E>, R> | typeof unhandledEntry;
};
//# sourceMappingURL=EnumValueVisitor.d.ts.map

@@ -0,25 +1,9 @@

import { StringKeyOf } from "./types";
/**
* 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}).
* A generic wrapper for any enum-like object.
* 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}
* EnumWrapper cannot be directly instantiated. Use {@link $enum} to get/create an EnumWrapper
* instance.
*

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

*/
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>> {
export declare class EnumWrapper<V extends number | string = number | string, T extends Record<StringKeyOf<T>, V> = any> implements Iterable<EnumWrapper.Entry<T>>, ArrayLike<EnumWrapper.Entry<T>> {
private readonly enumObj;

@@ -62,3 +46,3 @@ /**

*/
readonly [key: number]: EnumWrapper.Entry<V, T>;
readonly [key: number]: EnumWrapper.Entry<T>;
/**

@@ -69,3 +53,3 @@ * Create a new EnumWrapper instance.

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

@@ -99,3 +83,3 @@ constructor(enumObj: T);

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

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

*/
[Symbol.iterator](): IterableIterator<EnumWrapper.Entry<V, T>>;
[Symbol.iterator](): IterableIterator<EnumWrapper.Entry<T>>;
/**

@@ -148,3 +132,3 @@ * Calls the provided iteratee on each item in this enum.

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

@@ -354,6 +338,5 @@ * Tests if the provided string is actually a valid key for this enum

* 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>]]>;
type Entry<T extends Record<StringKeyOf<T>, number | string> = any> = Readonly<[StringKeyOf<T>, T[StringKeyOf<T>]]>;
/**

@@ -371,3 +354,3 @@ * A function used in iterating all key/value entries in an enum.

*/
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 Iteratee<R = any, V extends number | string = number | string, T extends Record<StringKeyOf<T>, V> = any> = (this: any, value: T[StringKeyOf<T>], key: StringKeyOf<T>, enumWrapper: EnumWrapper<V, T>, index: number) => R;
}

@@ -379,3 +362,3 @@ /**

*/
export declare type NumberEnumWrapper<T extends EnumLike<number, StringKeyOf<T>> = any> = EnumWrapper<number, T>;
export declare type NumberEnumWrapper<T extends Record<StringKeyOf<T>, number> = any> = EnumWrapper<number, T>;
export declare namespace NumberEnumWrapper {

@@ -387,3 +370,3 @@ /**

*/
type Entry<T extends EnumLike<number, StringKeyOf<T>> = any> = EnumWrapper.Entry<number, T>;
type Entry<T extends Record<StringKeyOf<T>, number> = any> = EnumWrapper.Entry<T>;
/**

@@ -395,3 +378,3 @@ * Type alias for an {@link EnumWrapper.Iteratee} for any type of 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 Iteratee<R = any, T extends Record<StringKeyOf<T>, number> = any> = EnumWrapper.Iteratee<R, number, T>;
}

@@ -403,3 +386,3 @@ /**

*/
export declare type StringEnumWrapper<T extends EnumLike<string, StringKeyOf<T>> = any> = EnumWrapper<string, T>;
export declare type StringEnumWrapper<T extends Record<StringKeyOf<T>, string> = any> = EnumWrapper<string, T>;
export declare namespace StringEnumWrapper {

@@ -411,3 +394,3 @@ /**

*/
type Entry<T extends EnumLike<string, StringKeyOf<T>> = any> = EnumWrapper.Entry<string, T>;
type Entry<T extends Record<StringKeyOf<T>, string> = any> = EnumWrapper.Entry<T>;
/**

@@ -419,3 +402,3 @@ * Type alias for an {@link EnumWrapper.Iteratee} for any type of 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 Iteratee<R = any, T extends Record<StringKeyOf<T>, string> = any> = EnumWrapper.Iteratee<R, string, T>;
}

@@ -428,3 +411,3 @@ /**

*/
export declare type MixedEnumWrapper<T extends EnumLike<number | string, StringKeyOf<T>> = any> = EnumWrapper<number | string, T>;
export declare type MixedEnumWrapper<T extends Record<StringKeyOf<T>, number | string> = any> = EnumWrapper<number | string, T>;
export declare namespace MixedEnumWrapper {

@@ -437,3 +420,3 @@ /**

*/
type Entry<T extends EnumLike<number | string, StringKeyOf<T>> = any> = EnumWrapper.Entry<number | string, T>;
type Entry<T extends Record<StringKeyOf<T>, number | string> = any> = EnumWrapper.Entry<T>;
/**

@@ -446,5 +429,4 @@ * Type alias for an {@link EnumWrapper.Iteratee} for any type of enum-like object that contains a mix of

*/
type Iteratee<R = any, T extends EnumLike<number | string, StringKeyOf<T>> = any> = EnumWrapper.Iteratee<R, number | string, T>;
type Iteratee<R = any, T extends Record<StringKeyOf<T>, number | string> = any> = EnumWrapper.Iteratee<R, number | string, T>;
}
export {};
//# sourceMappingURL=EnumWrapper.d.ts.map

@@ -7,2 +7,5 @@ export * from "./EnumWrapper";

export * from "./$enum";
export * from "./mapEnumValue";
export * from "./visitEnumValue";
export * from "./symbols";
//# sourceMappingURL=index.d.ts.map
{
"name": "ts-enum-util",
"version": "4.0.0-alpha.20181015",
"version": "4.0.0-alpha.20190310",
"description": "TypeScript Enum Utilities",

@@ -31,4 +31,4 @@ "repository": {

"build:types": "tsc --project src/tsconfig.json --pretty --noErrorTruncation --emitDeclarationOnly true --declarationMap true --outDir dist/types",
"build:commonjs": "tsc --project src/tsconfig.json --pretty --noErrorTruncation --declaration false --outDir dist/commonjs",
"build:es": "tsc --project src/tsconfig.json --pretty --noErrorTruncation --declaration false -m es6 --outDir dist/es",
"build:commonjs": "tsc --project src/tsconfig.json --pretty --removeComments --noErrorTruncation --declaration false --outDir dist/commonjs",
"build:es": "tsc --project src/tsconfig.json --pretty --removeComments --noErrorTruncation --declaration false -m es6 --outDir dist/es",
"build": "npm run clean:dist && run-p build:types build:es build:commonjs",

@@ -38,5 +38,4 @@ "pack": "run-p clean:pack build && npm pack",

"jest:coverage": "npm run clean:coverage && jest --coverage",
"dtslint:v2_8_plus": "dtslint type_tests/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",
"dtslint": "run-s clean:dist build:types dtslint:v2_9_plus",
"test": "run-s compile prettier:test lint dtslint jest",

@@ -53,17 +52,17 @@ "test:coverage": "run-s compile prettier:test lint dtslint jest:coverage",

"devDependencies": {
"@types/jest": "23.3.3",
"@types/node": "10.11.4",
"coveralls": "3.0.2",
"dtslint": "0.3.0",
"jest": "23.6.0",
"npm-run-all": "4.1.3",
"prettier": "1.14.3",
"rimraf": "2.6.2",
"ts-jest": "23.10.3",
"tslint": "5.11.0",
"tslint-config-prettier": "1.15.0",
"typescript": "3.1.1"
"@types/jest": "24.0.9",
"@types/node": "10.12.18",
"coveralls": "3.0.3",
"dtslint": "0.5.3",
"jest": "24.1.0",
"npm-run-all": "4.1.5",
"prettier": "1.16.4",
"rimraf": "2.6.3",
"ts-jest": "24.0.0",
"tslint": "5.13.1",
"tslint-config-prettier": "1.18.0",
"typescript": "3.3.3333"
},
"peerDependencies": {
"typescript": ">= 2.8.1"
"typescript": ">= 2.9.1"
},

@@ -70,0 +69,0 @@ "keywords": [

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

<!-- TOC depthFrom:2 -->
<!-- TOC depthFrom:2 -->autoauto- [What is it?](#what-is-it)auto- [Other TypeScript Enum Projects](#other-typescript-enum-projects)auto- [Installation](#installation)auto- [Usage Examples](#usage-examples)auto - [Basic setup for all examples](#basic-setup-for-all-examples)auto - [Get an `EnumWrapper` instance for an enum](#get-an-enumwrapper-instance-for-an-enum)auto - [Get count of enum entries](#get-count-of-enum-entries)auto - [Get lists of enum data](#get-lists-of-enum-data)auto - [Lookup value by key](#lookup-value-by-key)auto - [Reverse lookup key by value](#reverse-lookup-key-by-value)auto - [Validate/convert enum keys](#validateconvert-enum-keys)auto - [Validate/convert enum values](#validateconvert-enum-values)auto - [Iteration and Mapping](#iteration-and-mapping)auto - [Wrapped enums are Array-Like](#wrapped-enums-are-array-like)auto - [Wrapped enums are Map-Like](#wrapped-enums-are-map-like)auto- [Requirements](#requirements)auto- [Limitations](#limitations)auto- [Known Issues](#known-issues)auto - [`WeakMap` Polyfill](#weakmap-polyfill)auto- [General Concepts](#general-concepts)auto - [Enum-Like Object](#enum-like-object)auto - [EnumWrapper](#enumwrapper)auto - [Specific Typing](#specific-typing)auto - [Map-Like Interface](#map-like-interface)auto - [Array-Like Interface](#array-like-interface)auto - [Guaranteed Order of Iteration](#guaranteed-order-of-iteration)auto - [Caching](#caching)auto- [API Reference](#api-reference)auto - [Terminology](#terminology)auto - [$enum](#enum)auto - [Types](#types)auto - [EnumWrapper](#enumwrapper-1)auto - [EnumWrapper.Entry](#enumwrapperentry)auto - [EnumWrapper.Iteratee](#enumwrapperiteratee)auto - [Array-Like Interface](#array-like-interface-1)auto - [EnumWrapper.prototype.length](#enumwrapperprototypelength)auto - [EnumWrapper.prototype.[index]](#enumwrapperprototypeindex)auto - [Map-Like Interface](#map-like-interface-1)auto - [EnumWrapper.prototype.size](#enumwrapperprototypesize)auto - [EnumWrapper.prototype.keys](#enumwrapperprototypekeys)auto - [EnumWrapper.prototype.values](#enumwrapperprototypevalues)auto - [EnumWrapper.prototype.entries](#enumwrapperprototypeentries)auto - [EnumWrapper.prototype.@@iterator](#enumwrapperprototypeiterator)auto - [EnumWrapper.prototype.forEach](#enumwrapperprototypeforeach)auto - [Iteration](#iteration)auto - [EnumWrapper.prototype.forEach](#enumwrapperprototypeforeach-1)auto - [EnumWrapper.prototype.map](#enumwrapperprototypemap)auto - [Get Arrays of Enum Data](#get-arrays-of-enum-data)auto - [EnumWrapper.prototype.getKeys](#enumwrapperprototypegetkeys)auto - [EnumWrapper.prototype.getValues](#enumwrapperprototypegetvalues)auto - [EnumWrapper.prototype.getEntries](#enumwrapperprototypegetentries)auto - [Key Validation/Typecasting](#key-validationtypecasting)auto - [EnumWrapper.prototype.isKey](#enumwrapperprototypeiskey)auto - [EnumWrapper.prototype.asKeyOrThrow](#enumwrapperprototypeaskeyorthrow)auto - [EnumWrapper.prototype.asKeyOrDefault](#enumwrapperprototypeaskeyordefault)auto - [Value Validation/Typecasting](#value-validationtypecasting)auto - [EnumWrapper.prototype.isValue](#enumwrapperprototypeisvalue)auto - [EnumWrapper.prototype.asValueOrThrow](#enumwrapperprototypeasvalueorthrow)auto - [EnumWrapper.prototype.asValueOrDefault](#enumwrapperprototypeasvalueordefault)auto - [Lookup Key by Value](#lookup-key-by-value)auto - [EnumWrapper.prototype.getKeyOrThrow](#enumwrapperprototypegetkeyorthrow)auto - [EnumWrapper.prototype.getKeyOrDefault](#enumwrapperprototypegetkeyordefault)auto - [Lookup Value by Key](#lookup-value-by-key)auto - [EnumWrapper.prototype.getValueOrThrow](#enumwrapperprototypegetvalueorthrow)auto - [EnumWrapper.prototype.getValueOrDefault](#enumwrapperprototypegetvalueordefault)autoauto<!-- /TOC -->
- [What is it?](#what-is-it)
- [Other TypeScript Enum Projects](#other-typescript-enum-projects)
- [Installation](#installation)
- [Usage Examples](#usage-examples)
- [Basic setup for all examples](#basic-setup-for-all-examples)
- [Get an `EnumWrapper` instance for an enum](#get-an-enumwrapper-instance-for-an-enum)
- [Get count of enum entries](#get-count-of-enum-entries)
- [Get lists of enum data](#get-lists-of-enum-data)
- [Lookup value by key](#lookup-value-by-key)
- [Reverse lookup key by value](#reverse-lookup-key-by-value)
- [Validate/convert enum keys](#validateconvert-enum-keys)
- [Validate/convert enum values](#validateconvert-enum-values)
- [Iteration and Mapping](#iteration-and-mapping)
- [Wrapped enums are Array-Like](#wrapped-enums-are-array-like)
- [Wrapped enums are Map-Like](#wrapped-enums-are-map-like)
- [Requirements](#requirements)
- [Limitations](#limitations)
- [Known Issues](#known-issues)
- [`WeakMap` Polyfill](#weakmap-polyfill)
- [General Concepts](#general-concepts)
- [Enum-Like Object](#enum-like-object)
- [EnumWrapper](#enumwrapper)
- [Specific Typing](#specific-typing)
- [Map-Like Interface](#map-like-interface)
- [Array-Like Interface](#array-like-interface)
- [Guaranteed Order of Iteration](#guaranteed-order-of-iteration)
- [Caching](#caching)
- [API Reference](#api-reference)
- [Terminology](#terminology)
- [$enum](#enum)
- [Types](#types)
- [EnumWrapper](#enumwrapper-1)
- [EnumWrapper.Entry](#enumwrapperentry)
- [EnumWrapper.Iteratee](#enumwrapperiteratee)
- [Array-Like Interface](#array-like-interface-1)
- [EnumWrapper.prototype.length](#enumwrapperprototypelength)
- [EnumWrapper.prototype.[index]](#enumwrapperprototypeindex)
- [Map-Like Interface](#map-like-interface-1)
- [EnumWrapper.prototype.size](#enumwrapperprototypesize)
- [EnumWrapper.prototype.keys](#enumwrapperprototypekeys)
- [EnumWrapper.prototype.values](#enumwrapperprototypevalues)
- [EnumWrapper.prototype.entries](#enumwrapperprototypeentries)
- [EnumWrapper.prototype.@@iterator](#enumwrapperprototypeiterator)
- [EnumWrapper.prototype.forEach](#enumwrapperprototypeforeach)
- [Iteration](#iteration)
- [EnumWrapper.prototype.forEach](#enumwrapperprototypeforeach-1)
- [EnumWrapper.prototype.map](#enumwrapperprototypemap)
- [Get Arrays of Enum Data](#get-arrays-of-enum-data)
- [EnumWrapper.prototype.getKeys](#enumwrapperprototypegetkeys)
- [EnumWrapper.prototype.getValues](#enumwrapperprototypegetvalues)
- [EnumWrapper.prototype.getEntries](#enumwrapperprototypegetentries)
- [Key Validation/Typecasting](#key-validationtypecasting)
- [EnumWrapper.prototype.isKey](#enumwrapperprototypeiskey)
- [EnumWrapper.prototype.asKeyOrThrow](#enumwrapperprototypeaskeyorthrow)
- [EnumWrapper.prototype.asKeyOrDefault](#enumwrapperprototypeaskeyordefault)
- [Value Validation/Typecasting](#value-validationtypecasting)
- [EnumWrapper.prototype.isValue](#enumwrapperprototypeisvalue)
- [EnumWrapper.prototype.asValueOrThrow](#enumwrapperprototypeasvalueorthrow)
- [EnumWrapper.prototype.asValueOrDefault](#enumwrapperprototypeasvalueordefault)
- [Lookup Key by Value](#lookup-key-by-value)
- [EnumWrapper.prototype.getKeyOrThrow](#enumwrapperprototypegetkeyorthrow)
- [EnumWrapper.prototype.getKeyOrDefault](#enumwrapperprototypegetkeyordefault)
- [Lookup Value by Key](#lookup-value-by-key)
- [EnumWrapper.prototype.getValueOrThrow](#enumwrapperprototypegetvalueorthrow)
- [EnumWrapper.prototype.getValueOrDefault](#enumwrapperprototypegetvalueordefault)
<!-- /TOC -->
## What is it?

@@ -134,3 +66,3 @@

Use the [$enum](#enum) function to get an `EnumWrapper` instance for a particular enum.
Use the [\$enum](#enum) function to get an `EnumWrapper` instance for a particular enum.
Read about how `EnumWrapper` instances are cached: [Caching](#caching).

@@ -406,3 +338,3 @@

You likely won't ever directly reference the `EnumWrapper` class because it's much more convenient to use the [$enum](#enum) function to obtain a reference to an `EnumWrapper` instance.
You likely won't ever directly reference the `EnumWrapper` class because it's much more convenient to use the [\$enum](#enum) function to obtain a reference to an `EnumWrapper` instance.

@@ -457,3 +389,3 @@ ### Specific Typing

`EnumWrapper` instances are cached using an ES6 `WeakMap` for quick subsequent retrieval via the [$enum](#enum) function. This allows you to easily access the `EnumWrapper` functionality for a given enum via the `$enum` function throughout your codebase without worrying about storing a reference to an `EnumWrapper` that is accessible by all of the relevant code.
`EnumWrapper` instances are cached using an ES6 `WeakMap` for quick subsequent retrieval via the [\$enum](#enum) function. This allows you to easily access the `EnumWrapper` functionality for a given enum via the `$enum` function throughout your codebase without worrying about storing a reference to an `EnumWrapper` that is accessible by all of the relevant code.

@@ -485,3 +417,3 @@ The use of the `WeakMap` means that even if you use `ts-enum-util` on temporary, dynamically-generated, enum-like objects, there will be no excessive cache bloat or memory leaks. A cached `EnumWrapper` instance will be garbage collected when the enum-like object it is mapped to is garbage collected.

### $enum
### \$enum

@@ -502,3 +434,3 @@ This is where it all begins. This method returns an [EnumWrapper](#enum-wrapper-1) instance that provides useful utilities for `enumObj`.

This is the class that implements all the enum utilities. It's a generic class that requires an overloaded helper function to properly instantiate, so the constructor is private. Use [$enum()](#enum) to get/create an instance of `EnumWrapper`.
This is the class that implements all the enum utilities. It's a generic class that requires an overloaded helper function to properly instantiate, so the constructor is private. Use [\$enum()](#enum) to get/create an instance of `EnumWrapper`.

@@ -687,6 +619,7 @@ ```ts

```ts
Returns `true` if the provided `key` is a valid key for the enum.
Also acts as a type guard to tell the compiler that the provided `key` is the more specific `KeyType` type.
```ts
EnumWrapper.prototype.isKey(

@@ -693,0 +626,0 @@ key: string | null | undefined

@@ -1,3 +0,4 @@

import { EnumWrapper, EnumLike } from "./EnumWrapper";
import { Symbols } from "./Symbols";
import { EnumWrapper } from "./EnumWrapper";
import { StringKeyOf } from "./types";
import * as symbols from "./symbols";
import { visitEnumValue } from "./visitEnumValue";

@@ -30,3 +31,3 @@ import { mapEnumValue } from "./mapEnumValue";

V extends number,
T extends EnumLike<number, Extract<keyof T, string>>
T extends Record<StringKeyOf<T>, number>
>(enumObj: T): EnumWrapper<number, T>;

@@ -41,3 +42,3 @@ /**

*/
export function $enum<T extends EnumLike<string, Extract<keyof T, string>>>(
export function $enum<T extends Record<StringKeyOf<T>, string>>(
enumObj: T

@@ -55,5 +56,5 @@ ): EnumWrapper<string, T>;

*/
export function $enum<
T extends EnumLike<number | string, Extract<keyof T, string>>
>(enumObj: T): EnumWrapper<number | string, T>;
export function $enum<T extends Record<StringKeyOf<T>, number | string>>(
enumObj: T
): EnumWrapper<number | string, T>;
export function $enum(enumObj: object): EnumWrapper {

@@ -70,35 +71,7 @@ let result = enumWrapperInstancesCache.get(enumObj);

export namespace $enum {
/**
* Convenient alias for {@link Symbols.unhandled}.
*/
export const unhandled: typeof Symbols.unhandledEntry =
Symbols.unhandledEntry;
/**
* Convenient alias for {@link Symbols.handleNull}.
*/
export const handleNull: typeof Symbols.handleNull = Symbols.handleNull;
/**
* Convenient alias for {@link Symbols.handleUndefined}.
*/
export const handleUndefined: typeof Symbols.handleUndefined =
Symbols.handleUndefined;
/**
* Convenient alias for {@link Symbols.handleUnexpected}.
*/
export const handleUnexpected: typeof Symbols.handleUnexpected =
Symbols.handleUnexpected;
/**
* Convenient alias for {@link visitEnumValue}
*/
export const visitValue = visitEnumValue;
/**
* Convenient alias for {@link mapEnumValue}
*/
export const mapValue = mapEnumValue;
}
$enum.handleNull = symbols.handleNull;
$enum.handleUndefined = symbols.handleUndefined;
$enum.handleUnexpected = symbols.handleUnexpected;
$enum.unhandledEntry = symbols.unhandledEntry;
$enum.visitValue = visitEnumValue;
$enum.mapValue = mapEnumValue;

@@ -9,3 +9,8 @@ import { createUnhandledEntryError } from "./createUnhandledEntryError";

} from "./EnumValueMapper";
import { Symbols } from "./Symbols";
import {
handleUnexpected,
handleNull,
handleUndefined,
unhandledEntry
} from "./symbols";

@@ -44,4 +49,4 @@ /**

);
} else if (mapper.hasOwnProperty(Symbols.handleUnexpected)) {
return processEntry(mapper[Symbols.handleUnexpected]!, this.value);
} else if (mapper.hasOwnProperty(handleUnexpected)) {
return processEntry(mapper[handleUnexpected]!, this.value);
} else {

@@ -70,3 +75,3 @@ throw new Error(`Unexpected value: ${this.value}`);

* If the wrapped value is null, returns the mapper's
* {@link Symbols.handleNull} value.
* {@link handleNull} value.
* Otherwise, returns the value of the mapper's property whose name matches

@@ -85,6 +90,6 @@ * the wrapped value.

// handleNull exists.
if (mapper.hasOwnProperty(Symbols.handleNull)) {
return processEntry(mapper[Symbols.handleNull], null);
} else if (mapper.hasOwnProperty(Symbols.handleUnexpected)) {
return processEntry(mapper[Symbols.handleUnexpected]!, null);
if (mapper.hasOwnProperty(handleNull)) {
return processEntry(mapper[handleNull], null);
} else if (mapper.hasOwnProperty(handleUnexpected)) {
return processEntry(mapper[handleUnexpected]!, null);
} else {

@@ -113,3 +118,3 @@ throw new Error(`Unexpected value: null`);

* If the wrapped value is undefined, returns the mapper's
* {@link Symbols.handleUndefined} value.
* {@link handleUndefined} value.
* Otherwise, returns the value of the mapper's property whose name matches

@@ -128,6 +133,6 @@ * the wrapped value.

// have to check if handleUndefined exists.
if (mapper.hasOwnProperty(Symbols.handleUndefined)) {
return processEntry(mapper[Symbols.handleUndefined], undefined);
} else if (mapper.hasOwnProperty(Symbols.handleUnexpected)) {
return processEntry(mapper[Symbols.handleUnexpected]!, undefined);
if (mapper.hasOwnProperty(handleUndefined)) {
return processEntry(mapper[handleUndefined], undefined);
} else if (mapper.hasOwnProperty(handleUnexpected)) {
return processEntry(mapper[handleUnexpected]!, undefined);
} else {

@@ -160,5 +165,5 @@ throw new Error(`Unexpected value: undefined`);

* If the wrapped value is null, returns the mapper's
* {@link Symbols.handleNull} value.
* {@link handleNull} value.
* If the wrapped value is undefined, returns the mapper's
* {@link Symbols.handleUndefined} value.
* {@link handleUndefined} value.
* Otherwise, returns the value of the mapper's property whose name matches

@@ -178,3 +183,3 @@ * the wrapped value.

* Common implementation for processing an entry of an enum value mapper.
* @param entry - Either the mapped value entry, or {@link Symbols.unhandledEntry}.
* @param entry - Either the mapped value entry, or {@link unhandledEntry}.
* @param value - The value being mapped.

@@ -185,6 +190,6 @@ * @return The provided entry, if it is not an unhandledEntry.

function processEntry<T>(
entry: T | typeof Symbols.unhandledEntry,
entry: T | typeof unhandledEntry,
value: string | number | null | undefined
): T {
if (entry === Symbols.unhandledEntry) {
if (entry === unhandledEntry) {
throw createUnhandledEntryError(value);

@@ -191,0 +196,0 @@ } else {

@@ -1,2 +0,7 @@

import { Symbols } from "./Symbols";
import {
handleUnexpected,
handleNull,
handleUndefined,
unhandledEntry
} from "./symbols";

@@ -11,3 +16,3 @@ /**

export type EnumValueMapperCore<E extends string | number, T> = {
[P in E]: T | typeof Symbols.unhandledEntry
[P in E]: T | typeof unhandledEntry
};

@@ -23,3 +28,3 @@

export interface UnexpectedEnumValueMapper<T> {
[Symbols.handleUnexpected]?: T | typeof Symbols.unhandledEntry;
[handleUnexpected]?: T | typeof unhandledEntry;
}

@@ -35,3 +40,3 @@

export interface NullEnumValueMapper<T> {
[Symbols.handleNull]: T | typeof Symbols.unhandledEntry;
[handleNull]: T | typeof unhandledEntry;
}

@@ -47,3 +52,3 @@

export interface UndefinedEnumValueMapper<T> {
[Symbols.handleUndefined]: T | typeof Symbols.unhandledEntry;
[handleUndefined]: T | typeof unhandledEntry;
}

@@ -50,0 +55,0 @@

@@ -1,2 +0,7 @@

import { Symbols } from "./Symbols";
import {
handleUnexpected,
handleNull,
handleUndefined,
unhandledEntry
} from "./symbols";
import { createUnhandledEntryError } from "./createUnhandledEntryError";

@@ -39,4 +44,4 @@ import {

return processEntry(handler, this.value);
} else if (visitor[Symbols.handleUnexpected]) {
return processEntry(visitor[Symbols.handleUnexpected]!, (this
} else if (visitor[handleUnexpected]) {
return processEntry(visitor[handleUnexpected]!, (this
.value as any) as WidenEnumType<E>);

@@ -74,7 +79,7 @@ } else {

// type being visited, so we actually have to check if handleNull exists.
if (visitor[Symbols.handleNull]) {
return processEntry(visitor[Symbols.handleNull], null);
} else if (visitor[Symbols.handleUnexpected]) {
if (visitor[handleNull]) {
return processEntry(visitor[handleNull], null);
} else if (visitor[handleUnexpected]) {
return processEntry(
(visitor as EnumValueVisitor<E, R>)[Symbols.handleUnexpected]!,
(visitor as EnumValueVisitor<E, R>)[handleUnexpected]!,
null

@@ -113,7 +118,7 @@ );

// type being visited, so we actually have to check if handleUndefined exists.
if (visitor[Symbols.handleUndefined]) {
return processEntry(visitor[Symbols.handleUndefined], undefined);
} else if (visitor[Symbols.handleUnexpected]) {
if (visitor[handleUndefined]) {
return processEntry(visitor[handleUndefined], undefined);
} else if (visitor[handleUnexpected]) {
return processEntry(
(visitor as EnumValueVisitor<E, R>)[Symbols.handleUnexpected]!,
(visitor as EnumValueVisitor<E, R>)[handleUnexpected]!,
undefined

@@ -164,6 +169,6 @@ );

function processEntry<E extends string | number | null | undefined, R>(
entry: EnumValueVisitorHandler<E, R> | typeof Symbols.unhandledEntry,
entry: EnumValueVisitorHandler<E, R> | typeof unhandledEntry,
value: E
): R {
if (entry === Symbols.unhandledEntry) {
if (entry === unhandledEntry) {
throw createUnhandledEntryError(value);

@@ -170,0 +175,0 @@ } else {

@@ -1,2 +0,7 @@

import { Symbols } from "./Symbols";
import {
handleUnexpected,
handleNull,
handleUndefined,
unhandledEntry
} from "./symbols";

@@ -30,3 +35,3 @@ /**

export type EnumValueVisitorCore<E extends string | number, R> = {
[P in E]: EnumValueVisitorHandler<P, R> | typeof Symbols.unhandledEntry
[P in E]: EnumValueVisitorHandler<P, R> | typeof unhandledEntry
};

@@ -41,5 +46,3 @@

export interface NullEnumValueVisitor<R> {
[Symbols.handleNull]:
| EnumValueVisitorHandler<null, R>
| typeof Symbols.unhandledEntry;
[handleNull]: EnumValueVisitorHandler<null, R> | typeof unhandledEntry;
}

@@ -54,5 +57,5 @@

export interface UndefinedEnumValueVisitor<R> {
[Symbols.handleUndefined]:
[handleUndefined]:
| EnumValueVisitorHandler<undefined, R>
| typeof Symbols.unhandledEntry;
| typeof unhandledEntry;
}

@@ -70,5 +73,5 @@

> = EnumValueVisitorCore<E, R> & {
[Symbols.handleUnexpected]?:
[handleUnexpected]?:
| EnumValueVisitorHandler<WidenEnumType<E> | null | undefined, R>
| typeof Symbols.unhandledEntry;
| typeof unhandledEntry;
};

@@ -88,5 +91,5 @@

NullEnumValueVisitor<R> & {
[Symbols.handleUnexpected]?:
[handleUnexpected]?:
| EnumValueVisitorHandler<WidenEnumType<E> | undefined, R>
| typeof Symbols.unhandledEntry;
| typeof unhandledEntry;
};

@@ -106,5 +109,5 @@

UndefinedEnumValueVisitor<R> & {
[Symbols.handleUnexpected]?:
[handleUnexpected]?:
| EnumValueVisitorHandler<WidenEnumType<E> | null, R>
| typeof Symbols.unhandledEntry;
| typeof unhandledEntry;
};

@@ -125,5 +128,5 @@

UndefinedEnumValueVisitor<R> & {
[Symbols.handleUnexpected]?:
[handleUnexpected]?:
| EnumValueVisitorHandler<WidenEnumType<E>, R>
| typeof Symbols.unhandledEntry;
| typeof unhandledEntry;
};

@@ -1,27 +0,14 @@

/**
* 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
};
import { StringKeyOf } from "./types";
import {
isNonArrayIndexKey,
getOwnEnumerableNonArrayIndexKeys
} from "./objectKeysUtil";
/**
* 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}).
* A generic wrapper for any enum-like object.
* 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}
* EnumWrapper cannot be directly instantiated. Use {@link $enum} to get/create an EnumWrapper
* instance.
*

@@ -33,7 +20,4 @@ * @template V - Type of the enum value.

V extends number | string = number | string,
T extends EnumLike<V, StringKeyOf<T>> = any
>
implements
Iterable<EnumWrapper.Entry<V, T>>,
ArrayLike<EnumWrapper.Entry<V, T>> {
T extends Record<StringKeyOf<T>, V> = any
> implements Iterable<EnumWrapper.Entry<T>>, ArrayLike<EnumWrapper.Entry<T>> {
/**

@@ -74,3 +58,3 @@ * List of all keys for this enum, in sorted order.

*/
readonly [key: number]: EnumWrapper.Entry<V, T>;
readonly [key: number]: EnumWrapper.Entry<T>;

@@ -82,13 +66,11 @@ /**

*
* @param enumObj - An enum-like object. See the {@link EnumLike} type for more explanation.
* @param enumObj - An enum-like object.
*/
public 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>[]);
// Include only own enumerable keys that are not array indexes.
// This is necessary to ignore the reverse-lookup entries that are automatically added
// by TypeScript to numeric enums.
this.keysList = Object.freeze(
getOwnEnumerableNonArrayIndexKeys(enumObj)
);

@@ -197,3 +179,3 @@ const length = this.keysList.length;

*/
public entries(): IterableIterator<EnumWrapper.Entry<V, T>> {
public entries(): IterableIterator<EnumWrapper.Entry<T>> {
let index = 0;

@@ -204,3 +186,3 @@

const isDone = index >= this.length;
const result: IteratorResult<EnumWrapper.Entry<V, T>> = {
const result: IteratorResult<EnumWrapper.Entry<T>> = {
done: isDone,

@@ -216,3 +198,3 @@ // NOTE: defensive copy not necessary because entries are "frozen"

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

@@ -228,3 +210,3 @@ }

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

@@ -312,3 +294,3 @@ }

*/
public getEntries(): EnumWrapper.Entry<V, T>[] {
public getEntries(): EnumWrapper.Entry<T>[] {
// Create an array from the indexed entries of "this".

@@ -328,3 +310,3 @@ // NOTE: no need for defensive copy of each entry because all entries are "frozen".

key != null &&
isNonIndexKey(key) &&
isNonArrayIndexKey(key) &&
this.enumObj.hasOwnProperty(key)

@@ -412,5 +394,3 @@ );

if (this.isKey(key)) {
// type cast required to work around TypeScript bug:
// https://github.com/Microsoft/TypeScript/issues/21950
return key as StringKeyOf<T>;
return key;
} else {

@@ -695,5 +675,3 @@ return defaultKey;

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>];
return this.enumObj[key];
} else {

@@ -708,8 +686,6 @@ return defaultValue;

* 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
T extends Record<StringKeyOf<T>, number | string> = any
> = Readonly<[StringKeyOf<T>, T[StringKeyOf<T>]]>;

@@ -732,3 +708,3 @@

V extends number | string = number | string,
T extends EnumLike<V, StringKeyOf<T>> = any
T extends Record<StringKeyOf<T>, V> = any
> = (

@@ -749,3 +725,3 @@ this: any,

export type NumberEnumWrapper<
T extends EnumLike<number, StringKeyOf<T>> = any
T extends Record<StringKeyOf<T>, number> = any
> = EnumWrapper<number, T>;

@@ -760,4 +736,4 @@

export type Entry<
T extends EnumLike<number, StringKeyOf<T>> = any
> = EnumWrapper.Entry<number, T>;
T extends Record<StringKeyOf<T>, number> = any
> = EnumWrapper.Entry<T>;

@@ -772,3 +748,3 @@ /**

R = any,
T extends EnumLike<number, StringKeyOf<T>> = any
T extends Record<StringKeyOf<T>, number> = any
> = EnumWrapper.Iteratee<R, number, T>;

@@ -783,3 +759,3 @@ }

export type StringEnumWrapper<
T extends EnumLike<string, StringKeyOf<T>> = any
T extends Record<StringKeyOf<T>, string> = any
> = EnumWrapper<string, T>;

@@ -794,4 +770,4 @@

export type Entry<
T extends EnumLike<string, StringKeyOf<T>> = any
> = EnumWrapper.Entry<string, T>;
T extends Record<StringKeyOf<T>, string> = any
> = EnumWrapper.Entry<T>;

@@ -806,3 +782,3 @@ /**

R = any,
T extends EnumLike<string, StringKeyOf<T>> = any
T extends Record<StringKeyOf<T>, string> = any
> = EnumWrapper.Iteratee<R, string, T>;

@@ -818,3 +794,3 @@ }

export type MixedEnumWrapper<
T extends EnumLike<number | string, StringKeyOf<T>> = any
T extends Record<StringKeyOf<T>, number | string> = any
> = EnumWrapper<number | string, T>;

@@ -830,4 +806,4 @@

export type Entry<
T extends EnumLike<number | string, StringKeyOf<T>> = any
> = EnumWrapper.Entry<number | string, T>;
T extends Record<StringKeyOf<T>, number | string> = any
> = EnumWrapper.Entry<T>;

@@ -843,16 +819,4 @@ /**

R = any,
T extends EnumLike<number | string, StringKeyOf<T>> = any
T extends Record<StringKeyOf<T>, number | string> = any
> = EnumWrapper.Iteratee<R, number | string, T>;
}
/**
* 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));
}

@@ -7,1 +7,4 @@ export * from "./EnumWrapper";

export * from "./$enum";
export * from "./mapEnumValue";
export * from "./visitEnumValue";
export * from "./symbols";

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

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

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

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

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

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

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

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

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc