UNPKG

@tldraw/store

Version:

tldraw infinite canvas SDK (store).

8 lines (7 loc) 3.1 kB
{ "version": 3, "sources": ["../../src/lib/setUtils.ts"], "sourcesContent": ["import { CollectionDiff } from './Store'\n\n/**\n * Combine multiple sets into a single set containing only the common elements of all sets.\n * Returns the intersection of all provided sets - elements that exist in every set.\n * If no sets are provided, returns an empty set.\n *\n * @param sets - The sets to intersect. Can be an empty array.\n * @returns A new set containing only elements that exist in all input sets\n *\n * @example\n * ```ts\n * const set1 = new Set([1, 2, 3])\n * const set2 = new Set([2, 3, 4])\n * const set3 = new Set([3, 4, 5])\n *\n * const intersection = intersectSets([set1, set2, set3])\n * console.log(intersection) // Set {3}\n *\n * // Empty array returns empty set\n * const empty = intersectSets([])\n * console.log(empty) // Set {}\n * ```\n *\n * @public\n */\nexport function intersectSets<T>(sets: Set<T>[]) {\n\tif (sets.length === 0) return new Set<T>()\n\tconst first = sets[0]\n\tconst rest = sets.slice(1)\n\tconst result = new Set<T>()\n\n\tfor (const val of first) {\n\t\tif (rest.every((set) => set.has(val))) {\n\t\t\tresult.add(val)\n\t\t}\n\t}\n\n\treturn result\n}\n\n/**\n * Calculates a diff between two sets, identifying which elements were added or removed.\n * Returns undefined if the sets are identical (no changes detected).\n *\n * @param prev - The previous set to compare from\n * @param next - The next set to compare to\n * @returns A CollectionDiff object with `added` and/or `removed` sets, or undefined if no changes\n *\n * @example\n * ```ts\n * const prev = new Set(['a', 'b', 'c'])\n * const next = new Set(['b', 'c', 'd'])\n *\n * const diff = diffSets(prev, next)\n * console.log(diff)\n * // {\n * // added: Set {'d'},\n * // removed: Set {'a'}\n * // }\n *\n * // No changes returns undefined\n * const same = diffSets(prev, prev)\n * console.log(same) // undefined\n * ```\n *\n * @public\n */\nexport function diffSets<T>(prev: Set<T>, next: Set<T>): CollectionDiff<T> | undefined {\n\tconst result: CollectionDiff<T> = {}\n\n\tfor (const val of next) {\n\t\tif (!prev.has(val)) {\n\t\t\tresult.added ??= new Set()\n\t\t\tresult.added.add(val)\n\t\t}\n\t}\n\n\tfor (const val of prev) {\n\t\tif (!next.has(val)) {\n\t\t\tresult.removed ??= new Set()\n\t\t\tresult.removed.add(val)\n\t\t}\n\t}\n\n\treturn result.added || result.removed ? result : undefined\n}\n"], "mappings": "AA0BO,SAAS,cAAiB,MAAgB;AAChD,MAAI,KAAK,WAAW,EAAG,QAAO,oBAAI,IAAO;AACzC,QAAM,QAAQ,KAAK,CAAC;AACpB,QAAM,OAAO,KAAK,MAAM,CAAC;AACzB,QAAM,SAAS,oBAAI,IAAO;AAE1B,aAAW,OAAO,OAAO;AACxB,QAAI,KAAK,MAAM,CAAC,QAAQ,IAAI,IAAI,GAAG,CAAC,GAAG;AACtC,aAAO,IAAI,GAAG;AAAA,IACf;AAAA,EACD;AAEA,SAAO;AACR;AA6BO,SAAS,SAAY,MAAc,MAA6C;AACtF,QAAM,SAA4B,CAAC;AAEnC,aAAW,OAAO,MAAM;AACvB,QAAI,CAAC,KAAK,IAAI,GAAG,GAAG;AACnB,aAAO,UAAU,oBAAI,IAAI;AACzB,aAAO,MAAM,IAAI,GAAG;AAAA,IACrB;AAAA,EACD;AAEA,aAAW,OAAO,MAAM;AACvB,QAAI,CAAC,KAAK,IAAI,GAAG,GAAG;AACnB,aAAO,YAAY,oBAAI,IAAI;AAC3B,aAAO,QAAQ,IAAI,GAAG;AAAA,IACvB;AAAA,EACD;AAEA,SAAO,OAAO,SAAS,OAAO,UAAU,SAAS;AAClD;", "names": [] }