Function createSimpleMap

  • Create a map object which contains both the perperty key and value which both map to the requested generic mapValue with a type of V, E[key] => mapValue and E[value] => mapValue.

    Type Parameters

    • E

      Identifies the const enum type (eg. typeof Animal);

    • V

      Identifies the type of the mapping string; number; etc is not restructed to primitive types.

    Parameters

    • values: {
          [key in string | number | symbol]: [E[keyof E], V]
      }

      The values to populate on the new object

    Returns EnumTypeMap<E, V>

    A new frozen (immutable) object which contains a property for each key and value that returns the defiend mapped value.

    const enum Animal {
    Dog = 0,
    Cat = 1,
    Butterfly = 2,
    Bear = 3
    };
    // Creates a simple mapping to a string value
    const animalFamilyMap = createValueMap<typeof Animal, string>({
    Dog: [ Animal.Dog, "Canidae"],
    Cat: [ Animal.Cat, "Felidae"],
    Butterfly: [ Animal.Butterfly, "Papilionidae"],
    Bear: [ Animal.Bear, "Ursidae"]
    });
    // You end up with an object that maps everything to the name
    animalMap.Dog === "Canidae"; // true with typeof animalMap.Dog is "string"
    animalMap[0] === "Canidae"; // true with typeof animalMap[0] is "string"
    animalMap["Dog"] === "Canidae"; // true with typeof animalMap["Dog"] is "string"
    animalMap.Cat === "Felidae"; // true with typeof animalMap.Cat is "string"
    animalMap[1] === "Felidae"; // true with typeof animalMap[1] is "string"
    animalMap["Cat"] === "Felidae"; // true with typeof animalMap["Cat"] is "string"