@vbarbarosh/node-helpers 3.47.2 → 3.49.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.47.2",
5
+ "version": "3.49.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
@@ -0,0 +1,6 @@
1
+ function format_date(d)
2
+ {
3
+ return d.getFullYear() + `/0${d.getMonth()+1}/0${d.getDate()} 0${d.getHours()}:0${d.getMinutes()}:0${d.getSeconds()}`.replace(/0(\d\d)/g, '$1');
4
+ }
5
+
6
+ module.exports = format_date;
@@ -0,0 +1,8 @@
1
+ const axios = require('axios');
2
+
3
+ function http_get_utf8(url, options)
4
+ {
5
+ return axios.get(url, {responseType: 'text', responseEncoding: 'utf8', ...options}).then(v => v.data);
6
+ }
7
+
8
+ module.exports = http_get_utf8;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @link https://github.com/jshttp/range-parser
3
+ */
4
+ function http_range_parse(range, total)
5
+ {
6
+ const m = range.match(/^bytes\s*=\s*(\d*)-(\d*)$/);
7
+
8
+ if (!m || (!m[1] && !m[2])) {
9
+ throw new Error(`Cannot parse range "${range}". Only "bytes=FIRST-LAST", "bytes=FIRST-", or "bytes=-LAST" are supported.`);
10
+ }
11
+
12
+ let first = +m[1];
13
+ let last = +m[2];
14
+
15
+ if (!m[1]) { // -LAST
16
+ first = total - last;
17
+ last = total - 1;
18
+ }
19
+ else if (!m[2]) { // FIRST-
20
+ last = total - 1;
21
+ }
22
+
23
+ if (first < 0 || first > last || first >= total || last < 0 || last < first || last >= total) {
24
+ throw new Error(`Invalid range: first=${first}, last=${last}, total=${total}, expr="${range}"`);
25
+ }
26
+
27
+ return {first, last};
28
+ }
29
+
30
+ module.exports = http_range_parse;
@@ -6,10 +6,10 @@ const stream_map_parallel = require('./stream_map_parallel');
6
6
 
7
7
  describe('stream_map_parallel', function () {
8
8
  it('basic', async function () {
9
- const r = Array(100).fill().map((v,i) => i);
10
- const out = [];
9
+ const actual = [];
10
+ const expected = Array(100).fill().map((v,i) => i);
11
11
  await stream.promises.pipeline(
12
- stream.Readable.from(r),
12
+ stream.Readable.from(expected),
13
13
  stream_map_parallel({
14
14
  concurrency: 5,
15
15
  handler: async function (num) {
@@ -18,9 +18,9 @@ describe('stream_map_parallel', function () {
18
18
  },
19
19
  }),
20
20
  stream_each(function (item) {
21
- out.push(item);
21
+ actual.push(item);
22
22
  }),
23
23
  );
24
- assert.deepStrictEqual(out, r);
24
+ assert.deepStrictEqual(actual, expected);
25
25
  });
26
26
  });