UNPKG

@thi.ng/associative

Version:

ES Map/Set-compatible implementations with customizable equality semantics & supporting operations

18 lines (17 loc) 427 B
import { empty } from "@thi.ng/object-utils/empty"; import { selectKeysObj } from "@thi.ng/object-utils/select-keys"; import { EquivMap } from "./equiv-map.js"; const indexed = (records, ks) => { const res = new EquivMap(); let x, ik, rv; for (x of records) { ik = selectKeysObj(x, ks); rv = res.get(ik); !rv && res.set(ik, rv = empty(records, Set)); rv.add(x); } return res; }; export { indexed };