@slimlib/refine-partition 1.0.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,27 +1,24 @@
1
1
  {
2
2
  "type": "module",
3
- "version": "1.0.3",
3
+ "version": "2.0.0",
4
4
  "name": "@slimlib/refine-partition",
5
5
  "description": "Simple refine partition implementation",
6
6
  "license": "MIT",
7
7
  "author": "Konstantin Shutkin",
8
- "main": "./dist/index.cjs",
9
- "module": "./dist/index.mjs",
10
8
  "exports": {
11
9
  ".": {
12
- "types": "./dist/index.d.ts",
13
- "import": "./dist/index.mjs",
14
- "require": "./dist/index.cjs",
15
- "default": "./dist/index.mjs"
10
+ "types": "./types/index.d.ts",
11
+ "default": "./src/index.js"
16
12
  },
17
13
  "./package.json": "./package.json"
18
14
  },
19
- "types": "./dist/index.d.ts",
15
+ "types": "./types/index.d.ts",
20
16
  "files": [
21
- "dist"
17
+ "src",
18
+ "types"
22
19
  ],
23
20
  "engines": {
24
- "node": ">=15"
21
+ "node": ">=22"
25
22
  },
26
23
  "repository": {
27
24
  "type": "git",
package/src/index.js ADDED
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @template T
3
+ * @returns {(newPartitionCandidate?: Iterable<T>) => Iterable<Iterable<T>>}
4
+ */
5
+ export default function () {
6
+ /** @type {Set<Set<T>>} */
7
+ const processed = new Set();
8
+
9
+ return newPartitionCandidate => {
10
+ if (newPartitionCandidate) {
11
+ let remainingElements = new Set(newPartitionCandidate);
12
+
13
+ for (const partitionItem of [...processed]) {
14
+ const intersection = partitionItem.intersection(remainingElements);
15
+ if (intersection.size > 0) {
16
+ // Remove intersection elements from remaining candidate elements
17
+ remainingElements = remainingElements.difference(intersection);
18
+ // Only modify processed if partition is partially consumed
19
+ if (intersection.size < partitionItem.size) {
20
+ // Partial overlap - split partition into remainder and intersection
21
+ processed.delete(partitionItem);
22
+ const remainder = partitionItem.difference(intersection);
23
+ processed.add(remainder);
24
+ processed.add(intersection);
25
+ }
26
+ // If intersection.size === partitionItem.size, partition is fully consumed
27
+ // No need to delete/add since partitionItem already contains all intersection elements
28
+ }
29
+ }
30
+
31
+ // Add elements that didn't intersect with any existing partition
32
+ if (remainingElements.size > 0) {
33
+ processed.add(remainingElements);
34
+ }
35
+ }
36
+ return /** @type {Iterable<Iterable<T>>} */ (processed);
37
+ };
38
+ }
@@ -0,0 +1,7 @@
1
+ declare module '@slimlib/refine-partition' {
2
+ export default function _default<T>(): (newPartitionCandidate?: Iterable<T>) => Iterable<Iterable<T>>;
3
+
4
+ export {};
5
+ }
6
+
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,8 @@
1
+ {
2
+ "version": 3,
3
+ "file": "index.d.ts",
4
+ "names": [],
5
+ "sources": [],
6
+ "sourcesContent": [],
7
+ "mappings": ""
8
+ }
package/dist/index.cjs DELETED
@@ -1,40 +0,0 @@
1
- 'use strict';
2
-
3
- function index () {
4
- const processed = new Set();
5
- return (newPartitionCandidate) => {
6
- if (newPartitionCandidate) {
7
- const intersections = new Map();
8
- for (const element of newPartitionCandidate) {
9
- for (const partitionItem of processed) {
10
- if (partitionItem.has(element)) {
11
- let intersection = intersections.get(partitionItem);
12
- if (intersection === undefined) {
13
- intersection = new Set();
14
- intersections.set(partitionItem, intersection);
15
- }
16
- intersection.add(element);
17
- partitionItem.delete(element);
18
- if (partitionItem.size === 0) {
19
- processed.delete(partitionItem);
20
- }
21
- }
22
- }
23
- }
24
- const newPartitionItem = new Set(newPartitionCandidate);
25
- for (const intersection of intersections.values()) {
26
- processed.add(intersection);
27
- for (const element of intersection) {
28
- newPartitionItem.delete(element);
29
- }
30
- }
31
- intersections.clear();
32
- if (newPartitionItem.size > 0) {
33
- processed.add(newPartitionItem);
34
- }
35
- }
36
- return processed;
37
- };
38
- }
39
-
40
- module.exports = index;
package/dist/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export default function <T>(): (newPartitionCandidate?: Iterable<T>) => Iterable<Iterable<T>>;
package/dist/index.mjs DELETED
@@ -1,38 +0,0 @@
1
- function index () {
2
- const processed = new Set();
3
- return (newPartitionCandidate) => {
4
- if (newPartitionCandidate) {
5
- const intersections = new Map();
6
- for (const element of newPartitionCandidate) {
7
- for (const partitionItem of processed) {
8
- if (partitionItem.has(element)) {
9
- let intersection = intersections.get(partitionItem);
10
- if (intersection === undefined) {
11
- intersection = new Set();
12
- intersections.set(partitionItem, intersection);
13
- }
14
- intersection.add(element);
15
- partitionItem.delete(element);
16
- if (partitionItem.size === 0) {
17
- processed.delete(partitionItem);
18
- }
19
- }
20
- }
21
- }
22
- const newPartitionItem = new Set(newPartitionCandidate);
23
- for (const intersection of intersections.values()) {
24
- processed.add(intersection);
25
- for (const element of intersection) {
26
- newPartitionItem.delete(element);
27
- }
28
- }
29
- intersections.clear();
30
- if (newPartitionItem.size > 0) {
31
- processed.add(newPartitionItem);
32
- }
33
- }
34
- return processed;
35
- };
36
- }
37
-
38
- export { index as default };