UNPKG

@mantine/hooks

Version:

A collection of 50+ hooks for state and UI management

70 lines (69 loc) 1.99 kB
"use client"; import { useRef, useState } from "react"; //#region packages/@mantine/hooks/src/use-set/use-set.ts function readonlySetLikeToSet(input) { if (input instanceof Set) return input; const result = /* @__PURE__ */ new Set(); const iterator = input.keys(); let next = iterator.next(); while (!next.done) { result.add(next.value); next = iterator.next(); } return result; } function useSet(values) { const [set, setSet] = useState(() => new Set(values)); const setRef = useRef(set); setRef.current = set; set.add = (...args) => { const res = Set.prototype.add.apply(setRef.current, args); setSet(new Set(setRef.current)); return res; }; set.clear = (...args) => { Set.prototype.clear.apply(setRef.current, args); setSet(new Set(setRef.current)); }; set.delete = (...args) => { const res = Set.prototype.delete.apply(setRef.current, args); setSet(new Set(setRef.current)); return res; }; set.union = (other) => { const result = new Set(setRef.current); readonlySetLikeToSet(other).forEach((item) => result.add(item)); return result; }; set.intersection = (other) => { const result = /* @__PURE__ */ new Set(); const otherSet = readonlySetLikeToSet(other); setRef.current.forEach((item) => { if (otherSet.has(item)) result.add(item); }); return result; }; set.difference = (other) => { const result = /* @__PURE__ */ new Set(); const otherSet = readonlySetLikeToSet(other); setRef.current.forEach((item) => { if (!otherSet.has(item)) result.add(item); }); return result; }; set.symmetricDifference = (other) => { const result = /* @__PURE__ */ new Set(); const otherSet = readonlySetLikeToSet(other); setRef.current.forEach((item) => { if (!otherSet.has(item)) result.add(item); }); otherSet.forEach((item) => { if (!setRef.current.has(item)) result.add(item); }); return result; }; return set; } //#endregion export { readonlySetLikeToSet, useSet }; //# sourceMappingURL=use-set.mjs.map