@vbarbarosh/node-helpers 3.21.1 → 3.23.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/format_progress.js +11 -4
- package/src/format_progress.test.js +17 -0
- package/src/http_get_stream.js +2 -0
- package/src/http_get_stream_range.js +2 -0
- package/src/is_number.js +6 -0
- package/src/is_number.test.js +20 -0
- package/src/is_number_gt.js +8 -0
- package/src/make_int.js +15 -0
- package/src/make_number.js +15 -0
- package/src/make_number.test.js +20 -0
package/package.json
CHANGED
package/src/format_progress.js
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
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
5
|
|
|
5
6
|
function format_progress({percents, total, done, rate, eta, duration})
|
|
6
7
|
{
|
|
7
|
-
const bps = rate ? `${format_bytes(rate)}/s` : '~';
|
|
8
|
-
if (total) {
|
|
9
|
-
const eta_str = eta ? format_seconds(eta) : '~';
|
|
8
|
+
const bps = is_number_gt(rate, 0) ? `${format_bytes(rate)}/s` : '~';
|
|
9
|
+
if (is_number_gt(total, 0)) {
|
|
10
|
+
const eta_str = is_number_gt(eta, 0) ? format_seconds(eta) : '~';
|
|
10
11
|
return `${format_percents(percents)} | ${format_bytes(done)} of ${format_bytes(total)} at ${bps} ETA ${eta_str} duration=${format_seconds(duration)}`;
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
+
if (is_number_gt(done, 0)) {
|
|
14
|
+
return `${format_bytes(done)} of ~ at ${bps} duration=${format_seconds(duration)}`;
|
|
15
|
+
}
|
|
16
|
+
if (is_number_gt(duration, 0)) {
|
|
17
|
+
return `~ duration=${format_seconds(duration)}`;
|
|
18
|
+
}
|
|
19
|
+
return '~';
|
|
13
20
|
}
|
|
14
21
|
|
|
15
22
|
module.exports = format_progress;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const format_progress = require('./format_progress');
|
|
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% | 5.0MB of 15.0MB at ~ ETA ~ duration=00:00:01', {percents: 0.2525, total: 15*1024*1024, done: 5*1024*1024, rate: 0, eta: 0, duration: 1}],
|
|
8
|
+
['25.25% | 5.0MB of 15.0MB at 10.0MB/s ETA 00:00:05 duration=00:00:01', {percents: 0.2525, total: 15*1024*1024, done: 5*1024*1024, rate: 10*1024*1024, eta: 5, duration: 1}],
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
describe('format_progress', function () {
|
|
12
|
+
items.forEach(function ([expected, input]) {
|
|
13
|
+
it(expected, function () {
|
|
14
|
+
assert.strictEqual(format_progress(input), expected);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
});
|
package/src/http_get_stream.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
const axios = require('axios');
|
|
2
|
+
const make_int = require('./make_int');
|
|
2
3
|
|
|
3
4
|
async function http_get_stream(url, options)
|
|
4
5
|
{
|
|
5
6
|
const res = await axios.get(url, {responseType: 'stream', ...options});
|
|
6
7
|
const out = res.data;
|
|
7
8
|
out.headers = res.headers;
|
|
9
|
+
out.total = make_int(res.headers['content-length'], null, 0);
|
|
8
10
|
return out;
|
|
9
11
|
}
|
|
10
12
|
|
|
@@ -9,6 +9,7 @@ async function http_get_stream_range(url, first, last)
|
|
|
9
9
|
const headers = {Range: `bytes=${a}-${b}`};
|
|
10
10
|
const res = await axios.get(url, {responseType: 'stream', headers});
|
|
11
11
|
const out = res.data;
|
|
12
|
+
out.headers = res.headers;
|
|
12
13
|
if (res.headers['content-range']) {
|
|
13
14
|
out.content_range = parse_http_content_range(res.headers['content-range']);
|
|
14
15
|
}
|
|
@@ -22,6 +23,7 @@ async function http_get_stream_range(url, first, last)
|
|
|
22
23
|
else if (b && out.content_range.last !== b) {
|
|
23
24
|
out.destroy(new Error(`Last byte of a returned range (${format_thousands(out.content_range.last)}) is not as expected: [${format_thousands(b)}]`));
|
|
24
25
|
}
|
|
26
|
+
out.total = out.content_range.total;
|
|
25
27
|
return out;
|
|
26
28
|
}
|
|
27
29
|
|
package/src/is_number.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
});
|
package/src/make_int.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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;
|
|
@@ -0,0 +1,15 @@
|
|
|
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;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const make_number = require('./make_number');
|
|
3
|
+
|
|
4
|
+
const items = [
|
|
5
|
+
['-inf → 0', -Infinity, 0],
|
|
6
|
+
['+inf → 0', +Infinity, 0],
|
|
7
|
+
['null → 0', null, 0],
|
|
8
|
+
['{} → 0', {}, 0],
|
|
9
|
+
['Symbol() → 0', Symbol(), 0],
|
|
10
|
+
['Function → 0', () => 1, 0],
|
|
11
|
+
['BigInt(1) → 1', 1n, 1],
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
describe('make_number', function () {
|
|
15
|
+
items.forEach(function ([title, input, expected]) {
|
|
16
|
+
it(title, function () {
|
|
17
|
+
assert.deepStrictEqual(make_number(input), expected);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
});
|