@slimlib/refine-partition 1.0.2 → 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 +8 -14
- package/src/index.js +38 -0
- package/types/index.d.ts +7 -0
- package/types/index.d.ts.map +8 -0
- package/dist/index.cjs +0 -40
- package/dist/index.d.ts +0 -1
- package/dist/index.mjs +0 -38
package/package.json
CHANGED
|
@@ -1,25 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
|
-
"version": "
|
|
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
|
-
"
|
|
13
|
-
"default": "./
|
|
10
|
+
"types": "./types/index.d.ts",
|
|
11
|
+
"default": "./src/index.js"
|
|
14
12
|
},
|
|
15
13
|
"./package.json": "./package.json"
|
|
16
14
|
},
|
|
17
|
-
"types": "./
|
|
15
|
+
"types": "./types/index.d.ts",
|
|
18
16
|
"files": [
|
|
19
|
-
"
|
|
17
|
+
"src",
|
|
18
|
+
"types"
|
|
20
19
|
],
|
|
21
20
|
"engines": {
|
|
22
|
-
"node": ">=
|
|
21
|
+
"node": ">=22"
|
|
23
22
|
},
|
|
24
23
|
"repository": {
|
|
25
24
|
"type": "git",
|
|
@@ -34,10 +33,5 @@
|
|
|
34
33
|
"refine",
|
|
35
34
|
"partition",
|
|
36
35
|
"refine partition"
|
|
37
|
-
]
|
|
38
|
-
"scripts": {
|
|
39
|
-
"build": "pkgbld-internal",
|
|
40
|
-
"test": "jest --collectCoverage",
|
|
41
|
-
"lint": "eslint ./src"
|
|
42
|
-
}
|
|
36
|
+
]
|
|
43
37
|
}
|
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
|
+
}
|
package/types/index.d.ts
ADDED
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 };
|