@vbarbarosh/node-helpers 3.37.1 → 3.38.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.37.1",
5
+ "version": "3.38.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
package/src/fastdl.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const UserFriendlyError = require('./errors/UserFriendlyError');
2
2
  const format_bytes = require('./format_bytes');
3
- const format_progress = require('./format_progress');
3
+ const format_progress_bytes = require('./format_progress_bytes');
4
4
  const format_thousands = require('./format_thousands');
5
5
  const fs = require('fs');
6
6
  const fs_path_basename = require('./fs_path_basename');
@@ -40,7 +40,7 @@ async function fastdl({file, read_stream_with_range, concurrency = 60, user_frie
40
40
  const timer = setInterval(tick, 1000);
41
41
  function tick() {
42
42
  progress.refresh();
43
- user_friendly_status(`${format_progress(progress)} connections=${connections}`);
43
+ user_friendly_status(`${format_progress_bytes(progress)} connections=${connections}`);
44
44
  }
45
45
 
46
46
  user_friendly_status(`${format_bytes(total)} [${format_thousands(total)} bytes] to download`);
@@ -16,7 +16,7 @@ const items = [
16
16
  describe('format_bytes', function () {
17
17
  items.forEach(function ([input, expected]) {
18
18
  it(`${input} → ${expected}`, function () {
19
- assert.strictEqual(expected, format_bytes(input));
19
+ assert.strictEqual(format_bytes(input), expected);
20
20
  });
21
21
  });
22
22
  });
@@ -0,0 +1,17 @@
1
+ function format_kilo(num)
2
+ {
3
+ const sizes = ['', 'K', 'M', 'G', 'T', 'P'];
4
+ if (typeof num !== 'number' || Number.isNaN(num)) {
5
+ return 'n/a';
6
+ }
7
+ if (!num) {
8
+ return '0';
9
+ }
10
+ if (num < 1000) {
11
+ return num.toString();
12
+ }
13
+ const i = parseInt(Math.floor(Math.log(num) / Math.log(1000)), 10);
14
+ return `${(num / (1000 ** i)).toFixed(2)}`.replace(/0$/, '').replace(/\.0$/, '') + sizes[i];
15
+ }
16
+
17
+ module.exports = format_kilo;
@@ -0,0 +1,23 @@
1
+ const assert = require('assert');
2
+ const format_kilo = require('./format_kilo');
3
+
4
+ const items = [
5
+ ['', 'n/a'],
6
+ [null, 'n/a'],
7
+ [undefined, 'n/a'],
8
+ [0, '0'],
9
+ [1, '1'],
10
+ [900, '900'],
11
+ [1500, '1.5K'],
12
+ [2000, '2K'],
13
+ [35.45*1000*1000, '35.45M'],
14
+ [3.92*1000*1000*1000, '3.92G'],
15
+ ];
16
+
17
+ describe('format_kilo', function () {
18
+ items.forEach(function ([input, expected]) {
19
+ it(`${input} → ${expected}`, function () {
20
+ assert.strictEqual(format_kilo(input), expected);
21
+ });
22
+ });
23
+ });
@@ -16,7 +16,7 @@ const items = [
16
16
  describe('format_percents', function () {
17
17
  items.forEach(function ([input, expected]) {
18
18
  it(`${input} → ${expected}`, function () {
19
- assert.strictEqual(expected, format_percents(input));
19
+ assert.strictEqual(format_percents(input), expected);
20
20
  });
21
21
  });
22
22
  });
@@ -3,7 +3,7 @@ const format_percents = require('./format_percents');
3
3
  const format_seconds = require('./format_seconds');
4
4
  const is_number_gt = require('./is_number_gt');
5
5
 
6
- function format_progress({percents, total, done, rate, eta, duration})
6
+ function format_progress_bytes({percents, total, done, rate, eta, duration})
7
7
  {
8
8
  const bps = is_number_gt(rate, 0) ? `${format_bytes(rate)}/s` : '~';
9
9
  if (done > total) {
@@ -22,4 +22,4 @@ function format_progress({percents, total, done, rate, eta, duration})
22
22
  return '~';
23
23
  }
24
24
 
25
- module.exports = format_progress;
25
+ module.exports = format_progress_bytes;
@@ -1,5 +1,5 @@
1
1
  const assert = require('assert');
2
- const format_progress = require('./format_progress');
2
+ const format_progress_bytes = require('./format_progress_bytes');
3
3
 
4
4
  const items = [
5
5
  ['~', {percents: 0, total: 0, done: 0, rate: 0, eta: 0, duration: 0}],
@@ -10,10 +10,10 @@ const items = [
10
10
  ['15.0MB at 10.0MB/s duration=00:00:01', {percents: 5.2525, total: 5*1024*1024, done: 15*1024*1024, rate: 10*1024*1024, eta: 5, duration: 1}],
11
11
  ];
12
12
 
13
- describe('format_progress', function () {
13
+ describe('format_progress_bytes', function () {
14
14
  items.forEach(function ([expected, input]) {
15
15
  it(expected, function () {
16
- assert.strictEqual(format_progress(input), expected);
16
+ assert.strictEqual(format_progress_bytes(input), expected);
17
17
  });
18
18
  });
19
19
  });
@@ -0,0 +1,25 @@
1
+ const format_kilo = require('./format_kilo');
2
+ const format_percents = require('./format_percents');
3
+ const format_seconds = require('./format_seconds');
4
+ const is_number_gt = require('./is_number_gt');
5
+
6
+ function format_progress_kilo({percents, total, done, rate, eta, duration})
7
+ {
8
+ const speed = is_number_gt(rate, 0) ? `${format_kilo(rate)}/s` : '~';
9
+ if (done > total) {
10
+ return `${format_kilo(done)} at ${speed} duration=${format_seconds(duration)}`;
11
+ }
12
+ if (is_number_gt(total, 0)) {
13
+ const eta_str = is_number_gt(eta, 0) ? format_seconds(eta) : '~';
14
+ return `${format_percents(percents)} | ${format_kilo(done)} of ${format_kilo(total)} at ${speed} ETA ${eta_str} duration=${format_seconds(duration)}`;
15
+ }
16
+ if (is_number_gt(done, 0)) {
17
+ return `${format_kilo(done)} of ~ at ${speed} duration=${format_seconds(duration)}`;
18
+ }
19
+ if (is_number_gt(duration, 0)) {
20
+ return `~ duration=${format_seconds(duration)}`;
21
+ }
22
+ return '~';
23
+ }
24
+
25
+ module.exports = format_progress_kilo;
@@ -0,0 +1,19 @@
1
+ const assert = require('assert');
2
+ const format_progress_kilo = require('./format_progress_kilo');
3
+
4
+ const items = [
5
+ ['~', {percents: 0, total: 0, done: 0, rate: 0, eta: 0, duration: 0}],
6
+ ['~ duration=00:00:01', {percents: 0, total: 0, done: 0, rate: 0, eta: 0, duration: 1}],
7
+ ['25.25% | 5M of 15M at ~ ETA ~ duration=00:00:01', {percents: 0.2525, total: 15*1000*1000, done: 5*1000*1000, rate: 0, eta: 0, duration: 1}],
8
+ ['25.25% | 5M of 15M at 10M/s ETA 00:00:05 duration=00:00:01', {percents: 0.2525, total: 15*1000*1000, done: 5*1000*1000, rate: 10*1000*1000, eta: 5, duration: 1}],
9
+ // edge case: done > total
10
+ ['15M at 10M/s duration=00:00:01', {percents: 5.2525, total: 5*1000*1000, done: 15*1000*1000, rate: 10*1000*1000, eta: 5, duration: 1}],
11
+ ];
12
+
13
+ describe('format_progress_kilo', function () {
14
+ items.forEach(function ([expected, input]) {
15
+ it(expected, function () {
16
+ assert.strictEqual(format_progress_kilo(input), expected);
17
+ });
18
+ });
19
+ });
@@ -36,7 +36,7 @@ const items = [
36
36
  describe('format_seconds', function () {
37
37
  items.forEach(function ([expected, ...args]) {
38
38
  it(`${args} → ${expected}`, function () {
39
- assert.strictEqual(expected, format_seconds(...args));
39
+ assert.strictEqual(format_seconds(...args), expected);
40
40
  });
41
41
  });
42
42
  });
@@ -22,7 +22,7 @@ const items = [
22
22
  describe('format_thousands', function () {
23
23
  items.forEach(function ([expected, ...args]) {
24
24
  it(`${args} → ${expected}`, function () {
25
- assert.strictEqual(expected, format_thousands(...args));
25
+ assert.strictEqual(format_thousands(...args), expected);
26
26
  });
27
27
  });
28
28
  });
@@ -1,4 +1,4 @@
1
- const format_progress = require('./format_progress');
1
+ const format_progress_bytes = require('./format_progress_bytes');
2
2
  const make_progress = require('./make_progress');
3
3
  const stream = require('stream');
4
4
 
@@ -33,7 +33,7 @@ function stream_progress({objectMode = false, total, user_friendly_status = s =>
33
33
  });
34
34
  function tick() {
35
35
  progress.update(done);
36
- user_friendly_status(format_progress(progress));
36
+ user_friendly_status(format_progress_bytes(progress));
37
37
  }
38
38
  }
39
39