@trenskow/combinations 0.1.1 → 0.1.3
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/index.d.ts +6 -0
- package/lib/index.js +35 -1
- package/package.json +4 -4
- package/test/index.js +10 -0
package/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// For license see LICENSE.
|
|
7
7
|
//
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
const combinations = (array) => {
|
|
10
10
|
|
|
11
11
|
const result = [[]];
|
|
12
12
|
|
|
@@ -38,3 +38,37 @@ export default (array) => {
|
|
|
38
38
|
return result;
|
|
39
39
|
|
|
40
40
|
};
|
|
41
|
+
|
|
42
|
+
// Takes an array of arrays and returns all combinations of single items, one from each array.
|
|
43
|
+
combinations.single = (array) => {
|
|
44
|
+
|
|
45
|
+
const result = [];
|
|
46
|
+
|
|
47
|
+
function backtrack(
|
|
48
|
+
current,
|
|
49
|
+
remaining
|
|
50
|
+
) {
|
|
51
|
+
|
|
52
|
+
if (remaining.length === 0) {
|
|
53
|
+
result.push(current);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const [ first, ...rest ] = remaining;
|
|
58
|
+
|
|
59
|
+
first.forEach((item) => {
|
|
60
|
+
backtrack(
|
|
61
|
+
[ ...current, item ],
|
|
62
|
+
rest
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
backtrack([], array);
|
|
69
|
+
|
|
70
|
+
return result;
|
|
71
|
+
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export default combinations;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trenskow/combinations",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Small package that iterates an array and returns all ordering combinations.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"array",
|
|
@@ -24,12 +24,12 @@
|
|
|
24
24
|
"test": "node ./node_modules/mocha/bin/mocha.js ./test/index.js"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@eslint/compat": "^2.0.
|
|
27
|
+
"@eslint/compat": "^2.0.5",
|
|
28
28
|
"@eslint/eslintrc": "^3.3.5",
|
|
29
29
|
"@eslint/js": "^10.0.1",
|
|
30
30
|
"chai": "^6.2.2",
|
|
31
|
-
"eslint": "^10.
|
|
32
|
-
"globals": "^17.
|
|
31
|
+
"eslint": "^10.3.0",
|
|
32
|
+
"globals": "^17.6.0",
|
|
33
33
|
"mocha": "^11.7.5"
|
|
34
34
|
}
|
|
35
35
|
}
|
package/test/index.js
CHANGED
|
@@ -33,4 +33,14 @@ describe('@trenskow/combinations', () => {
|
|
|
33
33
|
]);
|
|
34
34
|
});
|
|
35
35
|
|
|
36
|
+
it('should come back with all combinations of single items.', () => {
|
|
37
|
+
expect(combinations.single([[ 1, 2 ], [ 3, 4 ]])).to.deep.equal([
|
|
38
|
+
[ 1, 3 ],
|
|
39
|
+
[ 1, 4 ],
|
|
40
|
+
[ 2, 3 ],
|
|
41
|
+
[ 2, 4 ]
|
|
42
|
+
]);
|
|
43
|
+
|
|
44
|
+
});
|
|
45
|
+
|
|
36
46
|
});
|