1 line
5.1 KiB
Plaintext
1 line
5.1 KiB
Plaintext
|
{"version":3,"file":"Enum.js","sourceRoot":"","sources":["../src/Enum.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAa,IAAI;IACf,gBAAuB,CAAC;IAExB;;;;;;;;;;;;;;;;;OAiBG;IACI,MAAM,CAAC,gBAAgB,CAC5B,UAGC,EACD,GAAW;QAEX,8DAA8D;QAC9D,OAAO,UAAU,CAAC,GAAG,CAAQ,CAAC;IAChC,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,aAAa,CACzB,UAGC,EACD,GAAW;QAEX,8DAA8D;QAC9D,MAAM,MAAM,GAA2B,UAAU,CAAC,GAAG,CAAQ,CAAC;QAC9D,IAAI,MAAM,KAAK,SAAS,EAAE;YACxB,MAAM,IAAI,KAAK,CAAC,kBAAkB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;SACzE;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoCG;IACI,MAAM,CAAC,iBAAiB,CAC7B,UAAuB,EACvB,KAAa;QAEb,8DAA8D;QAC9D,OAAO,UAAU,CAAC,KAAK,CAAQ,CAAC;IAClC,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,cAAc,CAC1B,UAAuB,EACvB,KAAa;QAEb,8DAA8D;QAC9D,MAAM,MAAM,GAAwC,UAAU,CAAC,KAAK,CAAQ,CAAC;QAC7E,IAAI,MAAM,KAAK,SAAS,EAAE;YACxB,MAAM,IAAI,KAAK,CAAC,aAAa,KAAK,gCAAgC,CAAC,CAAC;SACrE;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AA/GD,oBA+GC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * A helper for looking up TypeScript `enum` keys/values.\n *\n * @remarks\n * TypeScript enums implement a lookup table for mapping between their keys and values:\n *\n * ```ts\n * enum Colors {\n * Red = 1\n * }\n *\n * // Prints \"Red\"\n * console.log(Colors[1]);\n *\n * // Prints \"1\"\n * console.log(Colors[\"Red]);\n * ```\n *\n * However the compiler's \"noImplicitAny\" validation has trouble with these mappings, because\n * there are so many possible types for the map elements:\n *\n * ```ts\n * function f(s: string): Colors | undefined {\n * // (TS 7015) Element implicitly has an 'any' type because\n * // index expression is not of type 'number'.\n * return Colors[s];\n * }\n * ```\n *\n * The `Enum` helper provides a more specific, strongly typed way to access members:\n *\n * ```ts\n * function f(s: string): Colors | undefined {\n * return Enum.tryGetValueByKey(Colors, s);\n * }\n * ```\n *\n * @public\n */\nexport class Enum {\n private constructor() {}\n\n /**\n * Returns an enum value, given its key. Returns `undefined` if no matching key is found.\n *\n * @example\n *\n * Example usage:\n * ```ts\n * enum Colors {\n * Red = 1\n * }\n *\n * // Prints \"1\"\n * console.log(Enum.tryGetValueByKey(Colors, \"Red\"));\n *\n * // Prints \"undefined\"\n * console.log(Enum.tryGetValueByKey(Colors, \"Black\"));\n * ```\n */\n public static tryGetValueByKey<TEnumValue>(\n enumObject: {\n [key: string]: TEnumValue | string;\n [key: number]: TEnumValue | string;\n },\n key: string\n ): TEnumValue | undefined {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return enumObject[key] as any;\n }\n\n /**\n * This API is similar to {@link Enum.tryGetValueByKey}, except that it throws an exception\n * if the key is undefined.\n */\n public static getValueByKey<TEnumValue>(\n enumObject: {\n [key: string]: TEnumValue | string;\n [key: number]: TEnumValue | string;\n },\n key: string\n ): TEnumValue {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const result: TEnumValue | undefined = enumObject[key] as any;\n if (result === undefined) {\n throw new Error(`The lookup key ${JSON.stringify(key)} is not defined`);\n }\n return result;\n }\n\n /**\n * Returns an enum string key, given its numeric value. Returns `undefined` if no matching value\n * is found.\n *\n * @remarks\n * The TypeScript compiler only creates a reverse mapping for enum members whose value is numeric.\n * For example:\n *\n * ```ts\n * enum E {\n * A = 1,\n * B = 'c'\n * }\n *\n * // Prints \"A\"\n * console.log(E[1]);\n *\n * // Prints \"undefined\"\n * console.log(E[\"c\"]);\n * ```\n *\n * @example\n *\n * Example usage:\n * ```ts\n * enum Colors {\n * Red = 1,\n * Blue = '
|