UNPKG

@beenotung/tslib

Version:
65 lines 1.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.incMap = exports.mapGetArray = exports.mapGetSet = exports.mapGetMap = exports.mapGetOrSetDefault = exports.reduceMap = exports.mapToArray = exports.mapMap = exports.collectMap = void 0; function collectMap(map, mapper) { const res = new Map(); map.forEach((value, key) => { const a = mapper(value, key); const vs = res.get(a); if (vs) { vs.push(value); } else { res.set(a, [value]); } }); return res; } exports.collectMap = collectMap; /**@deprecated renamed to collectMap */ exports.mapMap = collectMap; function mapToArray(map, f) { const res = []; map.forEach((v, k, m) => res.push(f(v, k, m))); return res; } exports.mapToArray = mapToArray; function reduceMap(map, mapper, initial) { let acc = initial; map.forEach((value, key) => { acc = mapper(acc, value, key); }); return acc; } exports.reduceMap = reduceMap; function mapGetOrSetDefault(map, key, f) { if (map.has(key)) { return map.get(key); } const res = f(); map.set(key, res); return res; } exports.mapGetOrSetDefault = mapGetOrSetDefault; function mapGetMap(map, key) { return mapGetOrSetDefault(map, key, () => new Map()); } exports.mapGetMap = mapGetMap; function mapGetSet(map, key) { return mapGetOrSetDefault(map, key, () => new Set()); } exports.mapGetSet = mapGetSet; function mapGetArray(map, key) { return mapGetOrSetDefault(map, key, () => []); } exports.mapGetArray = mapGetArray; function incMap(map, key) { if (map.has(key)) { map.set(key, map.get(key) + 1); } else { map.set(key, 1); } } exports.incMap = incMap; //# sourceMappingURL=map.js.map