UNPKG

map-fns

Version:

<h1 align="center"> <code>map-fns</code> </h1>

60 lines (53 loc) 2.22 kB
'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; /** * Creates a new `map` populated with every item in the original `map` with every item in * `items` added to the map. The key that items behind are determined by the `keyProperty`. * * ```tsx * const map: Record<string, { id: string, value: number } }> = { * a: { id: "a", value: 1 }, * }; * * const result = addListToMap(map, [{ id: "b", value: 2 }], "id"); * * console.log(result); // { a: { id: "a", value: 1 }, b: { id: "b", value: 2 } }; * ``` * * The original `map` is not modified. * * @param map - The map to copy and modify. * @param items - The items to add to the map * @param keyProperty - The property of the items that contains the map key to use (e.g. `id`). * @returns A new map with the items added to it. */ function addListToMap(map, items, keyProperty) { var obj = __assign({}, map); for (var _i = 0, items_1 = items; _i < items_1.length; _i++) { var item = items_1[_i]; obj[item[keyProperty]] = item; } return obj; } module.exports = addListToMap;