UNPKG

@walts81/linq-ts

Version:

Typescript/Javascript LINQ implementation library

17 lines (16 loc) 705 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toDictionary = void 0; const _common_1 = require("./_common"); const get_value_as_string_1 = require("./get-value-as-string"); Array.prototype.toDictionary = toDictionary; function toDictionary(getKey, getValue, throwOnDuplicateKey = true) { return this.reduce((prev, current, i) => { const key = (0, get_value_as_string_1.getValueAsString)(getKey(current, i)); if (Object.keys(prev).indexOf(key) > -1 && throwOnDuplicateKey) throw new _common_1.DuplicateKeyException(key); prev[key] = getValue(current, i); return prev; }, {}); } exports.toDictionary = toDictionary;