@vbarbarosh/node-helpers 3.65.0 → 3.66.1
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 +2 -2
- package/src/array_permutations.js +33 -8
- package/src/array_permutations.test.js +10 -0
- package/src/date_add_hours.js +7 -0
- package/src/date_add_milliseconds.js +7 -0
- package/src/date_add_minutes.js +7 -0
- package/src/date_add_seconds.js +7 -0
- package/src/date_diff_seconds.js +6 -0
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"author": "Vladimir Barbarosh <vladimir.barbarosh@gmail.com>",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"name": "@vbarbarosh/node-helpers",
|
|
5
|
-
"version": "3.
|
|
5
|
+
"version": "3.66.1",
|
|
6
6
|
"description": "A set of helpers for JavaScript/Node.js",
|
|
7
7
|
"unpkg": "dist/browser.js",
|
|
8
8
|
"jsdelivr": "dist/browser.js",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"url": "https://github.com/vbarbarosh/node-helpers/issues"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@vbarbarosh/type-helpers": "^0.
|
|
31
|
+
"@vbarbarosh/type-helpers": "^0.2.0",
|
|
32
32
|
"axios": "^1.7.9",
|
|
33
33
|
"bluebird": "^3.7.2",
|
|
34
34
|
"csv-parse": "^5.6.0",
|
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
// https://stackoverflow.com/a/37580979/23502239
|
|
2
|
-
function array_permutations(array)
|
|
2
|
+
function array_permutations(array, k = array.length)
|
|
3
3
|
{
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
if (k !== array.length) {
|
|
5
|
+
return array_permutations_k(array, k);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
let length = array.length;
|
|
9
|
+
let out = [array.slice()];
|
|
10
|
+
let c = new Array(length).fill(0);
|
|
11
|
+
let i = 1, kk, p;
|
|
8
12
|
|
|
9
13
|
while (i < length) {
|
|
10
14
|
if (c[i] < i) {
|
|
11
|
-
|
|
15
|
+
kk = i % 2 && c[i];
|
|
12
16
|
p = array[i];
|
|
13
|
-
array[i] = array[
|
|
14
|
-
array[
|
|
17
|
+
array[i] = array[kk];
|
|
18
|
+
array[kk] = p;
|
|
15
19
|
++c[i];
|
|
16
20
|
i = 1;
|
|
17
21
|
out.push(array.slice());
|
|
@@ -24,4 +28,25 @@ function array_permutations(array)
|
|
|
24
28
|
return out;
|
|
25
29
|
}
|
|
26
30
|
|
|
31
|
+
function array_permutations_k(array, k)
|
|
32
|
+
{
|
|
33
|
+
if (k === 0) {
|
|
34
|
+
return [
|
|
35
|
+
[]
|
|
36
|
+
];
|
|
37
|
+
}
|
|
38
|
+
if (array.length < k) {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const out = [];
|
|
43
|
+
for (let i = 0; i < array.length; i++) {
|
|
44
|
+
const rest = array.slice(0, i).concat(array.slice(i + 1));
|
|
45
|
+
for (const perm of array_permutations(rest, k - 1)) {
|
|
46
|
+
out.push([array[i], ...perm]);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
|
|
27
52
|
module.exports = array_permutations;
|
|
@@ -7,4 +7,14 @@ describe('array_permutations', function () {
|
|
|
7
7
|
assert.deepStrictEqual(array_permutations([1,2]), [[1,2],[2,1]]);
|
|
8
8
|
assert.deepStrictEqual(array_permutations([1,2,3]), [[1,2,3],[2,1,3],[3,1,2],[1,3,2],[2,3,1],[3,2,1]]);
|
|
9
9
|
});
|
|
10
|
+
it('should handle k', function () {
|
|
11
|
+
assert.deepStrictEqual(array_permutations([1,2], 1), [[1],[2]]);
|
|
12
|
+
assert.deepStrictEqual(array_permutations([1,2,3], 2), [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]);
|
|
13
|
+
assert.deepStrictEqual(array_permutations([1,2,3,4], 3), [
|
|
14
|
+
[1,2,3],[1,2,4],[1,3,2],[1,3,4],[1,4,2],[1,4,3],
|
|
15
|
+
[2,1,3],[2,1,4],[2,3,1],[2,3,4],[2,4,1],[2,4,3],
|
|
16
|
+
[3,1,2],[3,1,4],[3,2,1],[3,2,4],[3,4,1],[3,4,2],
|
|
17
|
+
[4,1,2],[4,1,3],[4,2,1],[4,2,3],[4,3,1],[4,3,2]
|
|
18
|
+
]);
|
|
19
|
+
});
|
|
10
20
|
});
|