graphql
Version:
A Query Language and Runtime which can target any service.
1 lines • 1.25 kB
Source Map (JSON)
{"version":3,"file":"keyMap.js","sourceRoot":"","sources":["../../src/jsutils/keyMap.ts"],"names":[],"mappings":"AAsBA,MAAM,UAAU,MAAM,CACpB,IAAsB,EACtB,KAA0B;IAE1B,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACnC,KAAK,MAAM,IAAI,IAAI,IAAI,EAAE,CAAC;QACxB,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC;IAC7B,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type { ObjMap } from './ObjMap.ts';\n\n/**\n * Creates a keyed JS object from an array, given a function to produce the keys\n * for each value in the array.\n *\n * This provides a convenient lookup for the array items if the key function\n * produces unique results.\n * @internal\n * @example\n * ```ts\n * const phoneBook = [\n * { name: 'Jon', num: '555-1234' },\n * { name: 'Jenny', num: '867-5309' },\n * ];\n *\n * const entriesByName = keyMap(phoneBook, (entry) => entry.name);\n *\n * Object.keys(entriesByName); // => ['Jon', 'Jenny']\n * entriesByName['Jenny']; // => { name: 'Jenny', num: '867-5309' }\n * ```\n */\nexport function keyMap<T>(\n list: ReadonlyArray<T>,\n keyFn: (item: T) => string,\n): ObjMap<T> {\n const result = Object.create(null);\n for (const item of list) {\n result[keyFn(item)] = item;\n }\n return result;\n}\n"]}