ts-enum-util
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -29,15 +29,8 @@ "use strict"; | ||
this.enumObj = enumObj; | ||
/** | ||
* Map of enum value -> enum key. | ||
* Used for reverse key lookups. | ||
* NOTE: Performance tests show that using a Map (even if it's a slow polyfill) is faster than building a lookup | ||
* string key for values and using a plain Object: | ||
* {@link https://www.measurethat.net/Benchmarks/Show/2514/1/map-keyed-by-string-or-number} | ||
*/ | ||
this.keysByValueMap = new Map(); | ||
this.keysList = Object.keys(enumObj) | ||
this.keysList = Object.freeze(Object.keys(enumObj) | ||
.filter(isNonIndexKey) | ||
.sort(); | ||
.sort()); | ||
var length = this.keysList.length; | ||
this.valuesList = new Array(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 | ||
@@ -48,8 +41,12 @@ // Array.prototype.forEach | ||
var value = enumObj[key]; | ||
this.valuesList[index] = value; | ||
this.keysByValueMap.set(value, key); | ||
// type casting necessary to bypass readonly index signature for initialization | ||
this[index] = [key, value]; | ||
valuesList[index] = value; | ||
keysByValueMap.set(value, key); | ||
// Type casting of "this" necessary to bypass readonly index signature for initialization. | ||
this[index] = Object.freeze([key, value]); | ||
} | ||
this.valuesList = Object.freeze(valuesList); | ||
this.keysByValueMap = keysByValueMap; | ||
this.size = this.length = length; | ||
// Make the EnumWrapper instance immutable | ||
Object.freeze(this); | ||
} | ||
@@ -87,3 +84,3 @@ /** | ||
next: function () { | ||
var isDone = (index >= _this.length); | ||
var isDone = index >= _this.length; | ||
var result = { | ||
@@ -118,3 +115,3 @@ done: isDone, | ||
next: function () { | ||
var isDone = (index >= _this.length); | ||
var isDone = index >= _this.length; | ||
var result = { | ||
@@ -146,4 +143,3 @@ done: isDone, | ||
next: function () { | ||
var isDone = (index >= _this.length); | ||
var entry = _this[index]; | ||
var isDone = index >= _this.length; | ||
var result = { | ||
@@ -153,4 +149,4 @@ done: isDone, | ||
// https://github.com/Microsoft/TypeScript/issues/11375 | ||
// Create a defensive copy of the entry | ||
value: isDone ? undefined : [entry[0], entry[1]] | ||
// NOTE: defensive copy not necessary because entries are "frozen" | ||
value: isDone ? undefined : _this[index] | ||
}; | ||
@@ -219,3 +215,3 @@ ++index; | ||
EnumWrapper.prototype.getKeys = function () { | ||
// return defensive copy | ||
// need to return a copy of this.keysList so it can be returned as Array instead of ReadonlyArray. | ||
return this.keysList.slice(); | ||
@@ -231,3 +227,3 @@ }; | ||
EnumWrapper.prototype.getValues = function () { | ||
// return defensive copy | ||
// need to return a copy of this.valuesList so it can be returned as Array instead of ReadonlyArray. | ||
return this.valuesList.slice(); | ||
@@ -241,11 +237,5 @@ }; | ||
EnumWrapper.prototype.getEntries = function () { | ||
var length = this.length; | ||
var result = new Array(length); | ||
// According to multiple tests found on jsperf.com, a plain for loop is faster than using Array.prototype.map | ||
for (var index = 0; index < length; ++index) { | ||
var entry = this[index]; | ||
// Create a defensive copy of the entry | ||
result[index] = [entry[0], entry[1]]; | ||
} | ||
return result; | ||
// 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); | ||
}; | ||
@@ -259,3 +249,5 @@ /** | ||
EnumWrapper.prototype.isKey = function (key) { | ||
return key != null && isNonIndexKey(key) && this.enumObj.hasOwnProperty(key); | ||
return (key != null && | ||
isNonIndexKey(key) && | ||
this.enumObj.hasOwnProperty(key)); | ||
}; | ||
@@ -347,3 +339,3 @@ /** | ||
// for successful lookups. | ||
var result = (value != null) ? this.keysByValueMap.get(value) : undefined; | ||
var result = value != null ? this.keysByValueMap.get(value) : undefined; | ||
if (result != null) { | ||
@@ -368,3 +360,3 @@ return result; | ||
// 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; | ||
var result = value != null ? this.keysByValueMap.get(value) : undefined; | ||
if (result != null) { | ||
@@ -371,0 +363,0 @@ return result; |
@@ -27,15 +27,8 @@ /** | ||
this.enumObj = enumObj; | ||
/** | ||
* Map of enum value -> enum key. | ||
* Used for reverse key lookups. | ||
* NOTE: Performance tests show that using a Map (even if it's a slow polyfill) is faster than building a lookup | ||
* string key for values and using a plain Object: | ||
* {@link https://www.measurethat.net/Benchmarks/Show/2514/1/map-keyed-by-string-or-number} | ||
*/ | ||
this.keysByValueMap = new Map(); | ||
this.keysList = Object.keys(enumObj) | ||
this.keysList = Object.freeze(Object.keys(enumObj) | ||
.filter(isNonIndexKey) | ||
.sort(); | ||
.sort()); | ||
var length = this.keysList.length; | ||
this.valuesList = new Array(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 | ||
@@ -46,8 +39,12 @@ // Array.prototype.forEach | ||
var value = enumObj[key]; | ||
this.valuesList[index] = value; | ||
this.keysByValueMap.set(value, key); | ||
// type casting necessary to bypass readonly index signature for initialization | ||
this[index] = [key, value]; | ||
valuesList[index] = value; | ||
keysByValueMap.set(value, key); | ||
// Type casting of "this" necessary to bypass readonly index signature for initialization. | ||
this[index] = Object.freeze([key, value]); | ||
} | ||
this.valuesList = Object.freeze(valuesList); | ||
this.keysByValueMap = keysByValueMap; | ||
this.size = this.length = length; | ||
// Make the EnumWrapper instance immutable | ||
Object.freeze(this); | ||
} | ||
@@ -85,3 +82,3 @@ /** | ||
next: function () { | ||
var isDone = (index >= _this.length); | ||
var isDone = index >= _this.length; | ||
var result = { | ||
@@ -116,3 +113,3 @@ done: isDone, | ||
next: function () { | ||
var isDone = (index >= _this.length); | ||
var isDone = index >= _this.length; | ||
var result = { | ||
@@ -144,4 +141,3 @@ done: isDone, | ||
next: function () { | ||
var isDone = (index >= _this.length); | ||
var entry = _this[index]; | ||
var isDone = index >= _this.length; | ||
var result = { | ||
@@ -151,4 +147,4 @@ done: isDone, | ||
// https://github.com/Microsoft/TypeScript/issues/11375 | ||
// Create a defensive copy of the entry | ||
value: isDone ? undefined : [entry[0], entry[1]] | ||
// NOTE: defensive copy not necessary because entries are "frozen" | ||
value: isDone ? undefined : _this[index] | ||
}; | ||
@@ -217,3 +213,3 @@ ++index; | ||
EnumWrapper.prototype.getKeys = function () { | ||
// return defensive copy | ||
// need to return a copy of this.keysList so it can be returned as Array instead of ReadonlyArray. | ||
return this.keysList.slice(); | ||
@@ -229,3 +225,3 @@ }; | ||
EnumWrapper.prototype.getValues = function () { | ||
// return defensive copy | ||
// need to return a copy of this.valuesList so it can be returned as Array instead of ReadonlyArray. | ||
return this.valuesList.slice(); | ||
@@ -239,11 +235,5 @@ }; | ||
EnumWrapper.prototype.getEntries = function () { | ||
var length = this.length; | ||
var result = new Array(length); | ||
// According to multiple tests found on jsperf.com, a plain for loop is faster than using Array.prototype.map | ||
for (var index = 0; index < length; ++index) { | ||
var entry = this[index]; | ||
// Create a defensive copy of the entry | ||
result[index] = [entry[0], entry[1]]; | ||
} | ||
return result; | ||
// 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); | ||
}; | ||
@@ -257,3 +247,5 @@ /** | ||
EnumWrapper.prototype.isKey = function (key) { | ||
return key != null && isNonIndexKey(key) && this.enumObj.hasOwnProperty(key); | ||
return (key != null && | ||
isNonIndexKey(key) && | ||
this.enumObj.hasOwnProperty(key)); | ||
}; | ||
@@ -345,3 +337,3 @@ /** | ||
// for successful lookups. | ||
var result = (value != null) ? this.keysByValueMap.get(value) : undefined; | ||
var result = value != null ? this.keysByValueMap.get(value) : undefined; | ||
if (result != null) { | ||
@@ -366,3 +358,3 @@ return result; | ||
// 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; | ||
var result = value != null ? this.keysByValueMap.get(value) : undefined; | ||
if (result != null) { | ||
@@ -369,0 +361,0 @@ return result; |
@@ -23,3 +23,3 @@ /** | ||
*/ | ||
export declare class EnumWrapper<V extends number | string = number | string, T extends EnumLike<V, keyof T> = any> implements Iterable<EnumWrapper.Entry<V, T>>, ArrayLike<Readonly<EnumWrapper.Entry<V, T>>> { | ||
export declare class EnumWrapper<V extends number | string = number | string, T extends EnumLike<V, keyof T> = any> implements Iterable<EnumWrapper.Entry<V, T>>, ArrayLike<EnumWrapper.Entry<V, T>> { | ||
private readonly enumObj; | ||
@@ -26,0 +26,0 @@ /** |
{ | ||
"name": "ts-enum-util", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "TypeScript Enum Utilities", | ||
@@ -28,2 +28,4 @@ "repository": { | ||
"lint:fix": "npm run lint -- --fix", | ||
"prettier:test": "prettier --list-different \"{src,tests,type_tests}/**/*.ts\"", | ||
"prettier:fix": "prettier --write \"{src,tests,type_tests}/**/*.ts\"", | ||
"build:commonjs": "tsc --project src/tsconfig.json --pretty --noErrorTruncation", | ||
@@ -39,4 +41,4 @@ "build:move-types-from-commonjs": "copyfiles --up 2 dist/commonjs/*.d.ts dist/types && rimraf dist/commonjs/*.d.ts", | ||
"dtslint": "run-s dtslint:v2_3_plus dtslint:v2_4_plus", | ||
"test": "run-s compile lint dtslint jest", | ||
"test:coverage": "run-s compile lint dtslint jest:coverage", | ||
"test": "run-s compile prettier:test lint dtslint jest", | ||
"test:coverage": "run-s compile prettier:test lint dtslint jest:coverage", | ||
"build:travis": "run-p test:coverage build && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js" | ||
@@ -58,5 +60,7 @@ }, | ||
"npm-run-all": "4.1.2", | ||
"prettier": "1.11.1", | ||
"rimraf": "2.6.2", | ||
"ts-jest": "22.0.4", | ||
"tslint": "5.9.1", | ||
"tslint-config-prettier": "1.9.0", | ||
"typescript": "2.7.2" | ||
@@ -63,0 +67,0 @@ }, |
@@ -339,3 +339,3 @@ [![npm version](https://img.shields.io/npm/v/ts-enum-util.svg)](https://www.npmjs.com/package/ts-enum-util) | ||
## Requirements | ||
- *TypeScript 2.3+*: Code that uses `ts-enum-util` will not compile properly with TypeScript versions prior to 2.3. You'll need TypeScript 2.4+ to take andvantage of string enums, but you can still use `ts-enum-util` with [Enum-Like Objects](#enum-like-object) containing string values if you are stuck with TypeScript 2.3. | ||
- *TypeScript 2.3+*: Code that uses `ts-enum-util` will not compile properly with TypeScript versions prior to 2.3. You'll need TypeScript 2.4+ to take advantage of string enums, but you can still use `ts-enum-util` with [Enum-Like Objects](#enum-like-object) containing string values if you are stuck with TypeScript 2.3. | ||
- *ES6 Features*: The following ES6 features are used by `ts-enum-util`, so they must exist (either natively or via polyfill) in the run-time environment: | ||
@@ -342,0 +342,0 @@ - `Map` |
228
src/index.ts
@@ -8,3 +8,3 @@ /** | ||
export type EnumLike<V extends number | string, K extends string> = { | ||
[P in K]: V; | ||
[P in K]: V | ||
}; | ||
@@ -28,3 +28,6 @@ | ||
T extends EnumLike<V, keyof T> = any | ||
> implements Iterable<EnumWrapper.Entry<V, T>>, ArrayLike<Readonly<EnumWrapper.Entry<V, T>>> { | ||
> | ||
implements | ||
Iterable<EnumWrapper.Entry<V, T>>, | ||
ArrayLike<EnumWrapper.Entry<V, T>> { | ||
/** | ||
@@ -46,3 +49,3 @@ * Map of enum object -> EnumWrapper instance. | ||
*/ | ||
private readonly keysList: (keyof T)[]; | ||
private readonly keysList: ReadonlyArray<keyof T>; | ||
@@ -52,3 +55,3 @@ /** | ||
*/ | ||
private readonly valuesList: T[keyof T][]; | ||
private readonly valuesList: ReadonlyArray<T[keyof T]>; | ||
@@ -62,3 +65,3 @@ /** | ||
*/ | ||
private readonly keysByValueMap = new Map<V, keyof T>(); | ||
private readonly keysByValueMap: ReadonlyMap<V, keyof T>; | ||
@@ -113,5 +116,5 @@ /** | ||
*/ | ||
public static getCachedInstance<T extends EnumLike<number | string, keyof T>>( | ||
enumObj: T | ||
): EnumWrapper<number | string, T>; | ||
public static getCachedInstance< | ||
T extends EnumLike<number | string, keyof T> | ||
>(enumObj: T): EnumWrapper<number | string, T>; | ||
/** | ||
@@ -145,13 +148,16 @@ * Gets a cached EnumWrapper for an enum-like object. | ||
private constructor(private readonly enumObj: T) { | ||
this.keysList = 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( | ||
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() | ||
); | ||
const length = this.keysList.length; | ||
this.valuesList = new Array<T[keyof T]>(length); | ||
const valuesList = new Array<T[keyof T]>(length); | ||
const keysByValueMap = new Map<V, keyof T>(); | ||
@@ -164,9 +170,16 @@ // According to multiple tests found on jsperf.com, a plain for loop is faster than using | ||
this.valuesList[index] = value; | ||
this.keysByValueMap.set(value, key); | ||
// type casting necessary to bypass readonly index signature for initialization | ||
(this as any as EnumWrapper.Entry<V, T>[])[index] = [key, value]; | ||
valuesList[index] = value; | ||
keysByValueMap.set(value, key); | ||
// Type casting of "this" necessary to bypass readonly index signature for initialization. | ||
((this as any) as EnumWrapper.Entry<V, T>[])[index] = Object.freeze( | ||
[key, value] as EnumWrapper.Entry<V, T> | ||
); | ||
} | ||
this.valuesList = Object.freeze(valuesList); | ||
this.keysByValueMap = keysByValueMap; | ||
this.size = this.length = length; | ||
// Make the EnumWrapper instance immutable | ||
Object.freeze(this); | ||
} | ||
@@ -192,3 +205,3 @@ | ||
next: () => { | ||
const isDone = (index >= this.length); | ||
const isDone = index >= this.length; | ||
const result: IteratorResult<keyof T> = { | ||
@@ -198,3 +211,3 @@ done: isDone, | ||
// https://github.com/Microsoft/TypeScript/issues/11375 | ||
value: isDone ? undefined as any : this.keysList[index] | ||
value: isDone ? (undefined as any) : this.keysList[index] | ||
}; | ||
@@ -226,3 +239,3 @@ | ||
next: () => { | ||
const isDone = (index >= this.length); | ||
const isDone = index >= this.length; | ||
const result: IteratorResult<T[keyof T]> = { | ||
@@ -232,3 +245,3 @@ done: isDone, | ||
// https://github.com/Microsoft/TypeScript/issues/11375 | ||
value: isDone ? undefined as any : this.valuesList[index] | ||
value: isDone ? (undefined as any) : this.valuesList[index] | ||
}; | ||
@@ -257,4 +270,3 @@ | ||
next: () => { | ||
const isDone = (index >= this.length); | ||
const entry = this[index]; | ||
const isDone = index >= this.length; | ||
const result: IteratorResult<EnumWrapper.Entry<V, T>> = { | ||
@@ -264,4 +276,4 @@ done: isDone, | ||
// https://github.com/Microsoft/TypeScript/issues/11375 | ||
// Create a defensive copy of the entry | ||
value: isDone ? undefined as any : [entry[0], entry[1]] | ||
// NOTE: defensive copy not necessary because entries are "frozen" | ||
value: isDone ? (undefined as any) : this[index] | ||
}; | ||
@@ -297,3 +309,6 @@ | ||
*/ | ||
public forEach(iteratee: EnumWrapper.Iteratee<void, V, T>, context?: any): void { | ||
public forEach( | ||
iteratee: EnumWrapper.Iteratee<void, V, T>, | ||
context?: any | ||
): void { | ||
const length = this.length; | ||
@@ -327,3 +342,9 @@ | ||
const entry = this[index]; | ||
result[index] = iteratee.call(context, entry[1], entry[0], this, index); | ||
result[index] = iteratee.call( | ||
context, | ||
entry[1], | ||
entry[0], | ||
this, | ||
index | ||
); | ||
} | ||
@@ -340,3 +361,3 @@ | ||
public getKeys(): (keyof T)[] { | ||
// return defensive copy | ||
// need to return a copy of this.keysList so it can be returned as Array instead of ReadonlyArray. | ||
return this.keysList.slice(); | ||
@@ -353,3 +374,3 @@ } | ||
public getValues(): T[keyof T][] { | ||
// return defensive copy | ||
// need to return a copy of this.valuesList so it can be returned as Array instead of ReadonlyArray. | ||
return this.valuesList.slice(); | ||
@@ -364,13 +385,5 @@ } | ||
public getEntries(): EnumWrapper.Entry<V, T>[] { | ||
const length = this.length; | ||
const result = new Array<EnumWrapper.Entry<V, T>>(length); | ||
// According to multiple tests found on jsperf.com, a plain for loop is faster than using Array.prototype.map | ||
for (let index = 0; index < length; ++index) { | ||
const entry = this[index]; | ||
// Create a defensive copy of the entry | ||
result[index] = [entry[0], entry[1]]; | ||
} | ||
return result; | ||
// 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); | ||
} | ||
@@ -385,3 +398,7 @@ | ||
public isKey(key: string | null | undefined): key is keyof T { | ||
return key != null && isNonIndexKey(key) && this.enumObj.hasOwnProperty(key); | ||
return ( | ||
key != null && | ||
isNonIndexKey(key) && | ||
this.enumObj.hasOwnProperty(key) | ||
); | ||
} | ||
@@ -400,3 +417,5 @@ | ||
} else { | ||
throw new Error(`Unexpected key: ${key}. Expected one of: ${this.getValues()}`); | ||
throw new Error( | ||
`Unexpected key: ${key}. Expected one of: ${this.getValues()}` | ||
); | ||
} | ||
@@ -413,3 +432,6 @@ } | ||
*/ | ||
public asKeyOrDefault(key: string | null | undefined, defaultKey: keyof T): keyof T; | ||
public asKeyOrDefault( | ||
key: string | null | undefined, | ||
defaultKey: keyof T | ||
): keyof T; | ||
/** | ||
@@ -423,3 +445,6 @@ * Casts a string to a properly-typed key for this enum. | ||
*/ | ||
public asKeyOrDefault(key: string | null | undefined, defaultKey?: keyof T): keyof T | undefined; | ||
public asKeyOrDefault( | ||
key: string | null | undefined, | ||
defaultKey?: keyof T | ||
): keyof T | undefined; | ||
/** | ||
@@ -433,3 +458,6 @@ * Casts a string to a properly-typed key for this enum. | ||
*/ | ||
public asKeyOrDefault(key: string | null | undefined, defaultKey: string): string; | ||
public asKeyOrDefault( | ||
key: string | null | undefined, | ||
defaultKey: string | ||
): string; | ||
/** | ||
@@ -443,3 +471,6 @@ * Casts a string to a properly-typed key for this enum. | ||
*/ | ||
public asKeyOrDefault(key: string | null | undefined, defaultKey: string | undefined): string | undefined; | ||
public asKeyOrDefault( | ||
key: string | null | undefined, | ||
defaultKey: string | undefined | ||
): string | undefined; | ||
/** | ||
@@ -453,3 +484,6 @@ * Casts a string to a properly-typed key for this enum. | ||
*/ | ||
public asKeyOrDefault(key: string | null | undefined, defaultKey?: keyof T | string): string | undefined { | ||
public asKeyOrDefault( | ||
key: string | null | undefined, | ||
defaultKey?: keyof T | string | ||
): string | undefined { | ||
if (this.isKey(key)) { | ||
@@ -485,3 +519,5 @@ // type cast required to work around TypeScript bug: | ||
} else { | ||
throw new Error(`Unexpected value: ${value}. Expected one of: ${this.getValues()}`); | ||
throw new Error( | ||
`Unexpected value: ${value}. Expected one of: ${this.getValues()}` | ||
); | ||
} | ||
@@ -498,3 +534,6 @@ } | ||
*/ | ||
public asValueOrDefault(value: V | null | undefined, defaultValue: T[keyof T]): T[keyof T]; | ||
public asValueOrDefault( | ||
value: V | null | undefined, | ||
defaultValue: T[keyof T] | ||
): T[keyof T]; | ||
/** | ||
@@ -508,3 +547,6 @@ * Casts a value to a properly-typed value for this enum. | ||
*/ | ||
public asValueOrDefault(value: V | null | undefined, defaultValue?: T[keyof T]): T[keyof T] | undefined; | ||
public asValueOrDefault( | ||
value: V | null | undefined, | ||
defaultValue?: T[keyof T] | ||
): T[keyof T] | undefined; | ||
/** | ||
@@ -527,3 +569,6 @@ * Casts a value to a properly-typed value for this enum. | ||
*/ | ||
public asValueOrDefault(value: V | null | undefined, defaultValue: V | undefined): V | undefined; | ||
public asValueOrDefault( | ||
value: V | null | undefined, | ||
defaultValue: V | undefined | ||
): V | undefined; | ||
/** | ||
@@ -537,3 +582,6 @@ * Casts a value to a properly-typed value for this enum. | ||
*/ | ||
public asValueOrDefault(value: V | null | undefined, defaultValue?: T[keyof T] | V): V | undefined { | ||
public asValueOrDefault( | ||
value: V | null | undefined, | ||
defaultValue?: T[keyof T] | V | ||
): V | undefined { | ||
if (this.isValue(value)) { | ||
@@ -558,3 +606,4 @@ return value; | ||
// for successful lookups. | ||
const result = (value != null) ? this.keysByValueMap.get(value) : undefined; | ||
const result = | ||
value != null ? this.keysByValueMap.get(value) : undefined; | ||
@@ -564,3 +613,5 @@ if (result != null) { | ||
} else { | ||
throw new Error(`Unexpected value: ${value}. Expected one of: ${this.getValues()}`); | ||
throw new Error( | ||
`Unexpected value: ${value}. Expected one of: ${this.getValues()}` | ||
); | ||
} | ||
@@ -579,3 +630,6 @@ } | ||
*/ | ||
public getKeyOrDefault(value: V | null | undefined, defaultKey: keyof T): keyof T; | ||
public getKeyOrDefault( | ||
value: V | null | undefined, | ||
defaultKey: keyof T | ||
): keyof T; | ||
/** | ||
@@ -591,3 +645,6 @@ * Performs a reverse lookup from enum value to corresponding enum key. | ||
*/ | ||
public getKeyOrDefault(value: V | null | undefined, defaultKey?: keyof T): keyof T | undefined; | ||
public getKeyOrDefault( | ||
value: V | null | undefined, | ||
defaultKey?: keyof T | ||
): keyof T | undefined; | ||
/** | ||
@@ -603,3 +660,6 @@ * Performs a reverse lookup from enum value to corresponding enum key. | ||
*/ | ||
public getKeyOrDefault(value: V | null | undefined, defaultKey: string): string; | ||
public getKeyOrDefault( | ||
value: V | null | undefined, | ||
defaultKey: string | ||
): string; | ||
/** | ||
@@ -615,3 +675,6 @@ * Performs a reverse lookup from enum value to corresponding enum key. | ||
*/ | ||
public getKeyOrDefault(value: V | null | undefined, defaultKey: string | undefined): string | undefined; | ||
public getKeyOrDefault( | ||
value: V | null | undefined, | ||
defaultKey: string | undefined | ||
): string | undefined; | ||
/** | ||
@@ -627,5 +690,9 @@ * Performs a reverse lookup from enum value to corresponding enum key. | ||
*/ | ||
public getKeyOrDefault(value: V | null | undefined, defaultKey?: keyof T | string): string | undefined { | ||
public getKeyOrDefault( | ||
value: V | null | undefined, | ||
defaultKey?: keyof T | string | ||
): string | undefined { | ||
// NOTE: Intentionally not using isValue() to avoid making two key lookups into the map for successful lookups. | ||
const result = (value != null) ? this.keysByValueMap.get(value) : undefined; | ||
const result = | ||
value != null ? this.keysByValueMap.get(value) : undefined; | ||
@@ -661,3 +728,6 @@ if (result != null) { | ||
*/ | ||
public getValueOrDefault(key: string | null | undefined, defaultValue: T[keyof T]): T[keyof T]; | ||
public getValueOrDefault( | ||
key: string | null | undefined, | ||
defaultValue: T[keyof T] | ||
): T[keyof T]; | ||
/** | ||
@@ -671,3 +741,6 @@ * Gets the enum value for the provided key. | ||
*/ | ||
public getValueOrDefault(key: string | null | undefined, defaultValue?: T[keyof T]): T[keyof T] | undefined; | ||
public getValueOrDefault( | ||
key: string | null | undefined, | ||
defaultValue?: T[keyof T] | ||
): T[keyof T] | undefined; | ||
/** | ||
@@ -681,3 +754,6 @@ * Gets the enum value for the provided key. | ||
*/ | ||
public getValueOrDefault(key: string | null | undefined, defaultValue: V): V; | ||
public getValueOrDefault( | ||
key: string | null | undefined, | ||
defaultValue: V | ||
): V; | ||
/** | ||
@@ -691,3 +767,6 @@ * Gets the enum value for the provided key. | ||
*/ | ||
public getValueOrDefault(key: string | null | undefined, defaultValue: V | undefined): V | undefined; | ||
public getValueOrDefault( | ||
key: string | null | undefined, | ||
defaultValue: V | undefined | ||
): V | undefined; | ||
/** | ||
@@ -701,3 +780,6 @@ * Gets the enum value for the provided key. | ||
*/ | ||
public getValueOrDefault(key: string | null | undefined, defaultValue?: T[keyof T] | V): V | undefined { | ||
public getValueOrDefault( | ||
key: string | null | undefined, | ||
defaultValue?: T[keyof T] | V | ||
): V | undefined { | ||
// NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive | ||
@@ -743,3 +825,9 @@ // lookups for keys that match properties on Object.prototype, or keys that match the index keys of | ||
T extends EnumLike<V, keyof T> = any | ||
> = (this: any, value: T[keyof T], key: keyof T, enumWrapper: EnumWrapper<V, T>, index: number) => R; | ||
> = ( | ||
this: any, | ||
value: T[keyof T], | ||
key: keyof T, | ||
enumWrapper: EnumWrapper<V, T>, | ||
index: number | ||
) => R; | ||
} | ||
@@ -746,0 +834,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
149269
2160
13