@thi.ng/associative
Version:
ES Map/Set-compatible implementations with customizable equality semantics & supporting operations
26 lines (25 loc) • 540 B
JavaScript
import { empty } from "@thi.ng/object-utils/empty";
import { __combineSet } from "./internal/combine.js";
import { into } from "./into.js";
const intersection = (a, b, out) => {
out = out || empty(a, Set);
if (a === b) {
return into(out, a);
}
if (b.size < a.size) {
return intersection(b, a, out);
}
for (const i of b) {
if (a.has(i)) {
out.add(i);
}
}
return out;
};
function intersectionR(src) {
return __combineSet(intersectionR, intersection, src);
}
export {
intersection,
intersectionR
};