diff options
Diffstat (limited to 'alarm/node_modules/graphql/jsutils/keyValMap.mjs')
-rw-r--r-- | alarm/node_modules/graphql/jsutils/keyValMap.mjs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/alarm/node_modules/graphql/jsutils/keyValMap.mjs b/alarm/node_modules/graphql/jsutils/keyValMap.mjs new file mode 100644 index 0000000..5061c66 --- /dev/null +++ b/alarm/node_modules/graphql/jsutils/keyValMap.mjs @@ -0,0 +1,23 @@ +/** + * Creates a keyed JS object from an array, given a function to produce the keys + * and a function to produce the values from each item in the array. + * + * const phoneBook = [ + * { name: 'Jon', num: '555-1234' }, + * { name: 'Jenny', num: '867-5309' } + * ] + * + * // { Jon: '555-1234', Jenny: '867-5309' } + * const phonesByName = keyValMap( + * phoneBook, + * entry => entry.name, + * entry => entry.num + * ) + * + */ +export default function keyValMap(list, keyFn, valFn) { + return list.reduce(function (map, item) { + map[keyFn(item)] = valFn(item); + return map; + }, Object.create(null)); +} |