@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 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.64.0",
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
- var length = array.length,
5
- out = [array.slice()],
6
- c = new Array(length).fill(0),
7
- i = 1, k, p;
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
- k = i % 2 && c[i];
15
+ kk = i % 2 && c[i];
12
16
  p = array[i];
13
- array[i] = array[k];
14
- array[k] = p;
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
  });
@@ -0,0 +1,7 @@
1
+ function date_add_hours(date, hours)
2
+ {
3
+ date.setHours(date.getHours() + hours);
4
+ return date;
5
+ }
6
+
7
+ module.exports = date_add_hours;
@@ -0,0 +1,7 @@
1
+ function date_add_milliseconds(date, milliseconds)
2
+ {
3
+ date.setMilliseconds(date.getMilliseconds() + milliseconds);
4
+ return date;
5
+ }
6
+
7
+ module.exports = date_add_milliseconds;
@@ -0,0 +1,7 @@
1
+ function date_add_minutes(date, minutes)
2
+ {
3
+ date.setMinutes(date.getMinutes() + minutes);
4
+ return date;
5
+ }
6
+
7
+ module.exports = date_add_minutes;
@@ -0,0 +1,7 @@
1
+ function date_add_seconds(date, seconds)
2
+ {
3
+ date.setSeconds(date.getSeconds() + seconds);
4
+ return date;
5
+ }
6
+
7
+ module.exports = date_add_seconds;
@@ -0,0 +1,6 @@
1
+ function date_diff_seconds(a, b)
2
+ {
3
+ return Math.floor((a.getTime() - b.getTime())/1000);
4
+ }
5
+
6
+ module.exports = date_diff_seconds;
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 fs_path_resolve = require('./fs_path_resolve');
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(fs_path_resolve(path, v))));
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
  }
@@ -4,6 +4,7 @@ const stream = require('stream');
4
4
  const stream_curl_progress = require('./stream_curl_progress');
5
5
  const stream_each = require('./stream_each');
6
6
 
7
+ // 🦋 TODO ...options
7
8
  async function shell_curl_progress(args, {options, user_friendly_status})
8
9
  {
9
10
  // $ man curl