@vbarbarosh/node-helpers 3.63.2 → 3.65.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.63.2",
5
+ "version": "3.65.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "unpkg": "dist/browser.js",
8
8
  "jsdelivr": "dist/browser.js",
@@ -28,6 +28,7 @@
28
28
  "url": "https://github.com/vbarbarosh/node-helpers/issues"
29
29
  },
30
30
  "peerDependencies": {
31
+ "@vbarbarosh/type-helpers": "^0.1.0",
31
32
  "axios": "^1.7.9",
32
33
  "bluebird": "^3.7.2",
33
34
  "csv-parse": "^5.6.0",
@@ -1,7 +1,7 @@
1
1
  const format_bytes = require('./format_bytes');
2
2
  const format_percents = require('./format_percents');
3
3
  const format_seconds = require('./format_seconds');
4
- const is_number_gt = require('./is_number_gt');
4
+ const is_number_gt = require('@vbarbarosh/type-helpers/src/is_number_gt');
5
5
 
6
6
  function format_progress_bytes({percents, total, done, rate, eta, duration})
7
7
  {
@@ -1,7 +1,7 @@
1
1
  const format_kilo = require('./format_kilo');
2
2
  const format_percents = require('./format_percents');
3
3
  const format_seconds = require('./format_seconds');
4
- const is_number_gt = require('./is_number_gt');
4
+ const is_number_gt = require('@vbarbarosh/type-helpers/src/is_number_gt');
5
5
 
6
6
  function format_progress_kilo({percents, total, done, rate, eta, duration})
7
7
  {
@@ -0,0 +1,8 @@
1
+ const fs = require('fs');
2
+
3
+ function fs_copy_excl(src, dest)
4
+ {
5
+ return fs.promises.copyFile(src, dest, fs.constants.COPYFILE_EXCL);
6
+ }
7
+
8
+ module.exports = fs_copy_excl;
@@ -0,0 +1,8 @@
1
+ const path = require('path');
2
+
3
+ function fs_path_relative(from, to)
4
+ {
5
+ return path.relative(from, to);
6
+ }
7
+
8
+ module.exports = fs_path_relative;
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
  }
@@ -1,5 +1,5 @@
1
1
  const axios = require('axios');
2
- const make_int = require('./make_int');
2
+ const make_int = require('@vbarbarosh/type-helpers/src/make_int');
3
3
 
4
4
  async function http_get_stream(url, options)
5
5
  {
package/src/parallel.js CHANGED
@@ -1,11 +1,20 @@
1
1
  const NotImplemented = require('./errors/NotImplemented');
2
+ const Promise = require('bluebird');
3
+ const is_async_function = require('@vbarbarosh/type-helpers/src/is_async_function');
4
+ const is_async_generator = require('@vbarbarosh/type-helpers/src/is_async_generator');
2
5
 
3
6
  /**
4
7
  * - Run until `spawn` return `null`.
5
8
  * - Keep no more than `concurrency` number of workers at a time.
9
+ *
10
+ * ⚠️ Warning: `spawn` should not be async function (async functions are always return `promise`).
11
+ * Instead, it should be a simple function returning either `null` or a `promise`.
6
12
  */
7
13
  async function parallel({concurrency, spawn, progress})
8
14
  {
15
+ if (is_async_function(spawn) || is_async_generator(spawn)) {
16
+ throw new Error('[spawn] should not be async function. Instead, it should be a simple function returning either [null] or a [promise].');
17
+ }
9
18
  const running = [];
10
19
  return new Promise(function (resolve, reject) {
11
20
  let failed = false;
@@ -0,0 +1,11 @@
1
+ const assert = require('assert');
2
+ const parallel = require('./parallel');
3
+
4
+ describe('parallel', function () {
5
+ it('should reject when [spawn] is async function', function () {
6
+ assert.rejects(parallel({spawn: async function () {}}));
7
+ })
8
+ it('should reject when [spawn] is async generator', function () {
9
+ assert.rejects(parallel({spawn: async function* () {}}));
10
+ })
11
+ });
@@ -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
package/src/is_number.js DELETED
@@ -1,6 +0,0 @@
1
- function is_number(value)
2
- {
3
- return (typeof value === 'number') && isFinite(value);
4
- }
5
-
6
- module.exports = is_number;
@@ -1,20 +0,0 @@
1
- const assert = require('assert');
2
- const is_number = require('./is_number');
3
-
4
- const items = [
5
- ['-inf → false', -Infinity, false],
6
- ['+inf → false', +Infinity, false],
7
- ['null → false', null, false],
8
- ['{} → false', {}, false],
9
- ['Symbol() → false', Symbol(), false],
10
- ['Function → false', () => 1, false],
11
- ['BigInt(1) → false', 1n, false],
12
- ];
13
-
14
- describe('is_number', function () {
15
- items.forEach(function ([title, input, expected]) {
16
- it(title, function () {
17
- assert.deepStrictEqual(is_number(input), expected);
18
- });
19
- });
20
- });
@@ -1,8 +0,0 @@
1
- const is_number = require('./is_number');
2
-
3
- function is_number_gt(value, min)
4
- {
5
- return is_number(value) && value > min;
6
- }
7
-
8
- module.exports = is_number_gt;
package/src/make_int.js DELETED
@@ -1,15 +0,0 @@
1
- function make_int(value, default_value = 0, min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER)
2
- {
3
- switch (typeof value) {
4
- case 'bigint':
5
- return Math.max(min, Math.min(max, Number(value)));
6
- case 'symbol':
7
- return default_value;
8
- }
9
- if (value === null || value === undefined || !isFinite(value)) {
10
- return default_value;
11
- }
12
- return Math.max(min, Math.min(max, Math.round(value === 0 ? value : (+value||default_value))));
13
- }
14
-
15
- module.exports = make_int;
@@ -1,15 +0,0 @@
1
- function make_number(value, default_value = 0, min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER)
2
- {
3
- switch (typeof value) {
4
- case 'bigint':
5
- return Number(value);
6
- case 'symbol':
7
- return default_value;
8
- }
9
- if (value === null || value === undefined || !isFinite(value)) {
10
- return default_value;
11
- }
12
- return Math.max(min, Math.min(max, Math.round(value === 0 ? value : (+value||default_value))));
13
- }
14
-
15
- module.exports = make_number;
@@ -1,25 +0,0 @@
1
- const assert = require('assert');
2
- const make_number = require('./make_number');
3
-
4
- const items = [
5
- ['null → 0', null, 0],
6
- ['undefined → 0', undefined, 0],
7
- ['{} → 0', {}, 0],
8
- ['nan → 0', NaN, 0],
9
- ['-inf → 0', -Infinity, 0],
10
- ['+inf → 0', +Infinity, 0],
11
- ['false → 0', false, 0],
12
- ['true → 1', true, 1],
13
- ['Symbol() → 0', Symbol(), 0],
14
- ['Function → 0', () => 1, 0],
15
- ['BigInt(1) → 1', 1n, 1],
16
- ['"123" → 123', '123', 123],
17
- ];
18
-
19
- describe('make_number', function () {
20
- items.forEach(function ([title, input, expected]) {
21
- it(title, function () {
22
- assert.deepStrictEqual(make_number(input), expected);
23
- });
24
- });
25
- });