@tsdotnet/linq
Version:
A familiar set of functions that operate on JavaScript iterables (ES2015+) in a similar way to .NET's LINQ does with enumerables.
33 lines (30 loc) • 1.06 kB
JavaScript
import { ArgumentNullException } from '@tsdotnet/exceptions';
import identity from '../identity.js';
function toMap(keySelector, valueSelector = identity, mappingBehavior = -1) {
if (!keySelector)
throw new ArgumentNullException('keySelector');
if (typeof valueSelector == 'number') {
mappingBehavior = valueSelector;
valueSelector = undefined;
}
if (!valueSelector)
valueSelector = identity;
return function (sequence) {
const result = new Map();
let i = 0;
for (const e of sequence) {
const key = keySelector(e, i++);
if (result.has(key))
switch (mappingBehavior) {
case 0:
continue;
case -1:
throw new Error('MappingMode.Throw: more than one of the same key encountered.');
}
result.set(key, valueSelector(e, i - 1));
}
return result;
};
}
export { toMap as default };
//# sourceMappingURL=toMap.js.map