Creates a function that maps each input string to a key from keys using a stable hash, so the same input always selects the same key.
keys
Preserves literal key types (for as const arrays). Empty input strings are supported.
as const
Non-empty list of string keys to choose from. The same input string always maps to the same key via a stable hash.
A function (value: string) => T[number] that picks a key from keys deterministically.
(value: string) => T[number]
const colors = ["red", "green", "blue"] as const;const getColor = createDeterministicKeySelector(colors);getColor("Albert"); // "blue"getColor("Barbara"); // "green"getColor("Charlie"); // "red"getColor("Albert"); // "blue" (stable across calls) Copy
const colors = ["red", "green", "blue"] as const;const getColor = createDeterministicKeySelector(colors);getColor("Albert"); // "blue"getColor("Barbara"); // "green"getColor("Charlie"); // "red"getColor("Albert"); // "blue" (stable across calls)
If called with an empty keys array.
Creates a function that maps each input string to a key from
keysusing a stable hash, so the same input always selects the same key.Preserves literal key types (for
as constarrays). Empty input strings are supported.