Function numberEnumToStringKey

Returns the validated & existing enum "string key" part of a "numeric" enum (the string at left hand side), given either side (the string or number part) of a "numeric" enum.

Features / Notes:

  • The number part can be given as a number or parseable string number.
  • The string part can be given the exact string key, or lower, upper or capitalized form of the string. // @todo: make this optional
  • It should maintain the right types of Enum / Enum keys, eg keyof TEnumType as a string

z.numberEnumToNumberVal() for the reverse function

@todo: Curry this function, maintain types:

  • Type Parameters

    • TEnumType extends Record<string, string | number>

    Parameters

    • theEnum: TEnumType

      a numeric enum eg enum AB {a: 1, b: 2}

    • value: string | number

      the value of the enum to convert to a number (eg B or 2 or '2' or AB.a or AB.b). Strings are considered in lower, upper & capitalized forms.

    Returns undefined | keyof TEnumType

    keyof TEnumType as a string