@vbarbarosh/node-helpers 3.64.0 → 3.66.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 +1 -1
- 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/src/fs_rmrf.js +6 -3
- package/src/shell_curl_progress.js +1 -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.0",
|
|
6
6
|
"description": "A set of helpers for JavaScript/Node.js",
|
|
7
7
|
"unpkg": "dist/browser.js",
|
|
8
8
|
"jsdelivr": "dist/browser.js",
|
|
@@ -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
|
});
|
package/src/fs_rmrf.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
const Promise = require('bluebird');
|
|
2
2
|
const fs_lstat = require('./fs_lstat');
|
|
3
|
-
const
|
|
3
|
+
const fs_path_join = require('./fs_path_join');
|
|
4
4
|
const fs_readdir = require('./fs_readdir');
|
|
5
5
|
const fs_rm = require('./fs_rm');
|
|
6
6
|
const fs_rmdir = require('./fs_rmdir');
|
|
7
|
+
const ignore = require('./ignore');
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Remove a file or directory, along with all nested files and directories, recursively.
|
|
10
11
|
*/
|
|
11
|
-
async function fs_rmrf(path)
|
|
12
|
+
async function fs_rmrf(path, progress = ignore)
|
|
12
13
|
{
|
|
13
14
|
const lstat = await fs_lstat(path).catch(() => null);
|
|
14
15
|
if (lstat === null) {
|
|
@@ -17,10 +18,12 @@ async function fs_rmrf(path)
|
|
|
17
18
|
|
|
18
19
|
if (lstat.isDirectory()) {
|
|
19
20
|
const names = await fs_readdir(path);
|
|
20
|
-
await Promise.all(names.map(v => fs_rmrf(
|
|
21
|
+
await Promise.all(names.map(v => fs_rmrf(fs_path_join(path, v), progress)));
|
|
22
|
+
progress('rmdir', path);
|
|
21
23
|
await fs_rmdir(path);
|
|
22
24
|
}
|
|
23
25
|
else {
|
|
26
|
+
progress('rm', path);
|
|
24
27
|
await fs_rm(path);
|
|
25
28
|
}
|
|
26
29
|
}
|