@walts81/linq-ts
Version:
Typescript/Javascript LINQ implementation library
16 lines (15 loc) • 539 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toMap = void 0;
const _common_1 = require("./_common");
Array.prototype.toMap = toMap;
function toMap(getKey, getValue, throwOnDuplicateKey = true) {
return this.reduce((prev, current, i) => {
const key = getKey(current, i);
if (prev.has(key) && throwOnDuplicateKey)
throw new _common_1.DuplicateKeyException();
prev.set(key, getValue(current, i));
return prev;
}, new Map());
}
exports.toMap = toMap;