utilitish - v0.0.10
    Preparing search index...

    Function resolveSelector

    • Resolves a selector parameter into a function that extracts a value from an item.

      The selector can be:

      • a key string of the object (returns the value at that key),
      • a function transforming the item to the desired value,
      • or undefined, in which case a fallback function must be provided.

      Type Parameters

      • T

        The type of the input items.

      • R

        The type of the selected value.

      Parameters

      • Optionalselector: keyof T | ((item: T) => R)

        The selector key or function.

      • Optionalfallback: (item: T, index?: number) => R

        A fallback function if no selector is provided.

      • Optionalname: string = 'selector'

        Name used in error messages.

      Returns (item: T, index?: number) => R

      The resolved selector function.

      const sel1 = resolveSelector<{name: string}, string>('name');
      sel1({name: 'Alice'}); // 'Alice'

      const sel2 = resolveSelector<{value: number}, number>(item => item.value * 2);
      sel2({value: 5}); // 10

      const sel3 = resolveSelector(undefined, (item) => item.toString());
      sel3(42); // '42'

      If the selector is not a function or string key.

      If no selector is provided and no fallback is given.