keyMap.d.ts 787 B

1234567891011121314151617181920212223242526272829303132
  1. import type { ObjMap } from './ObjMap';
  2. /**
  3. * Creates a keyed JS object from an array, given a function to produce the keys
  4. * for each value in the array.
  5. *
  6. * This provides a convenient lookup for the array items if the key function
  7. * produces unique results.
  8. * ```ts
  9. * const phoneBook = [
  10. * { name: 'Jon', num: '555-1234' },
  11. * { name: 'Jenny', num: '867-5309' }
  12. * ]
  13. *
  14. * const entriesByName = keyMap(
  15. * phoneBook,
  16. * entry => entry.name
  17. * )
  18. *
  19. * // {
  20. * // Jon: { name: 'Jon', num: '555-1234' },
  21. * // Jenny: { name: 'Jenny', num: '867-5309' }
  22. * // }
  23. *
  24. * const jennyEntry = entriesByName['Jenny']
  25. *
  26. * // { name: 'Jenny', num: '857-6309' }
  27. * ```
  28. */
  29. export declare function keyMap<T>(
  30. list: ReadonlyArray<T>,
  31. keyFn: (item: T) => string,
  32. ): ObjMap<T>;