@naturalcycles/js-lib 14.144.1 → 14.145.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/dist/array/array.util.js +2 -2
- package/dist/datetime/localDate.js +0 -1
- package/dist/datetime/localTime.js +0 -1
- package/dist/decorators/createPromiseDecorator.js +0 -1
- package/dist/http/fetcher.d.ts +7 -0
- package/dist/http/fetcher.js +21 -0
- package/dist/http/fetcher.model.d.ts +1 -1
- package/dist/object/object.util.js +0 -1
- package/dist/object/sortObjectDeep.js +0 -1
- package/dist/promise/abortable.d.ts +2 -2
- package/dist/string/leven.js +0 -1
- package/dist/string/string.util.js +0 -1
- package/dist/vendor/is.d.ts +1 -1
- package/dist/vendor/is.js +1 -7
- package/dist-esm/array/array.util.js +2 -2
- package/dist-esm/datetime/localDate.js +0 -1
- package/dist-esm/datetime/localTime.js +0 -1
- package/dist-esm/decorators/createPromiseDecorator.js +0 -1
- package/dist-esm/http/fetcher.js +18 -0
- package/dist-esm/object/object.util.js +0 -1
- package/dist-esm/object/sortObjectDeep.js +0 -1
- package/dist-esm/string/leven.js +0 -1
- package/dist-esm/string/string.util.js +0 -1
- package/dist-esm/vendor/is.js +1 -7
- package/package.json +1 -1
- package/src/array/array.util.ts +2 -2
- package/src/datetime/localDate.ts +0 -2
- package/src/datetime/localTime.ts +0 -2
- package/src/decorators/createPromiseDecorator.ts +1 -1
- package/src/http/fetcher.model.ts +1 -1
- package/src/http/fetcher.ts +22 -0
- package/src/json-schema/jsonSchema.model.ts +0 -1
- package/src/object/object.util.ts +0 -1
- package/src/object/sortObjectDeep.ts +0 -1
- package/src/string/leven.ts +1 -1
- package/src/string/string.util.ts +0 -2
- package/src/vendor/is.ts +7 -10
package/dist/array/array.util.js
CHANGED
|
@@ -144,7 +144,7 @@ exports._groupBy = _groupBy;
|
|
|
144
144
|
function _sortBy(items, mapper, mutate = false, descending = false) {
|
|
145
145
|
const mod = descending ? -1 : 1;
|
|
146
146
|
return (mutate ? items : [...items]).sort((_a, _b) => {
|
|
147
|
-
const [a, b] = [_a, _b].map(mapper);
|
|
147
|
+
const [a, b] = [_a, _b].map(mapper);
|
|
148
148
|
if (typeof a === 'number' && typeof b === 'number')
|
|
149
149
|
return (a - b) * mod;
|
|
150
150
|
return String(a).localeCompare(String(b)) * mod;
|
|
@@ -155,7 +155,7 @@ exports._sortBy = _sortBy;
|
|
|
155
155
|
* Like items.find(), but it tries to find from the END of the array.
|
|
156
156
|
*/
|
|
157
157
|
function _findLast(items, predicate) {
|
|
158
|
-
return [...items].reverse().find(predicate);
|
|
158
|
+
return [...items].reverse().find(predicate);
|
|
159
159
|
}
|
|
160
160
|
exports._findLast = _findLast;
|
|
161
161
|
function _takeWhile(items, predicate) {
|
|
@@ -5,7 +5,6 @@ const assert_1 = require("../error/assert");
|
|
|
5
5
|
const localTime_1 = require("./localTime");
|
|
6
6
|
const MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
7
7
|
const DATE_REGEX = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
|
|
8
|
-
/* eslint-disable no-dupe-class-members */
|
|
9
8
|
/**
|
|
10
9
|
* @experimental
|
|
11
10
|
*/
|
|
@@ -13,7 +13,6 @@ const decorator_util_1 = require("./decorator.util");
|
|
|
13
13
|
*
|
|
14
14
|
* @experimental
|
|
15
15
|
*/
|
|
16
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
17
16
|
function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
18
17
|
const { decoratorName } = cfg;
|
|
19
18
|
return function decoratorFunction(target, propertyKey, pd) {
|
package/dist/http/fetcher.d.ts
CHANGED
|
@@ -30,6 +30,13 @@ export declare class Fetcher {
|
|
|
30
30
|
patchVoid: (url: string, opt?: FetcherOptions) => Promise<void>;
|
|
31
31
|
deleteVoid: (url: string, opt?: FetcherOptions) => Promise<void>;
|
|
32
32
|
headVoid: (url: string, opt?: FetcherOptions) => Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Returns raw fetchResponse.body, which is a ReadableStream<Uint8Array>
|
|
35
|
+
*
|
|
36
|
+
* More on streams and Node interop:
|
|
37
|
+
* https://css-tricks.com/web-streams-everywhere-and-fetch-for-node-js/
|
|
38
|
+
*/
|
|
39
|
+
getReadableStream(url: string, opt?: FetcherOptions): Promise<ReadableStream<Uint8Array>>;
|
|
33
40
|
fetch<T = unknown>(url: string, opt?: FetcherOptions): Promise<T>;
|
|
34
41
|
/**
|
|
35
42
|
* Returns FetcherResponse.
|
package/dist/http/fetcher.js
CHANGED
|
@@ -77,6 +77,19 @@ class Fetcher {
|
|
|
77
77
|
static create(cfg = {}) {
|
|
78
78
|
return new Fetcher(cfg);
|
|
79
79
|
}
|
|
80
|
+
// mode=readableStream
|
|
81
|
+
/**
|
|
82
|
+
* Returns raw fetchResponse.body, which is a ReadableStream<Uint8Array>
|
|
83
|
+
*
|
|
84
|
+
* More on streams and Node interop:
|
|
85
|
+
* https://css-tricks.com/web-streams-everywhere-and-fetch-for-node-js/
|
|
86
|
+
*/
|
|
87
|
+
async getReadableStream(url, opt) {
|
|
88
|
+
return await this.fetch(url, {
|
|
89
|
+
mode: 'readableStream',
|
|
90
|
+
...opt,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
80
93
|
async fetch(url, opt) {
|
|
81
94
|
const res = await this.doFetch(url, opt);
|
|
82
95
|
if (res.err) {
|
|
@@ -202,6 +215,14 @@ class Fetcher {
|
|
|
202
215
|
else if (mode === 'blob') {
|
|
203
216
|
res.body = res.fetchResponse.body ? await res.fetchResponse.blob() : {};
|
|
204
217
|
}
|
|
218
|
+
else if (mode === 'readableStream') {
|
|
219
|
+
res.body = res.fetchResponse.body;
|
|
220
|
+
if (res.body === null) {
|
|
221
|
+
res.err = new Error(`fetchResponse.body is null`);
|
|
222
|
+
res.ok = false;
|
|
223
|
+
return await this.onNotOkResponse(res, started, timeout);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
205
226
|
clearTimeout(timeout);
|
|
206
227
|
res.retryStatus.retryStopped = true;
|
|
207
228
|
// res.err can happen on JSON.parse error
|
|
@@ -160,4 +160,4 @@ export interface FetcherErrorResponse<BODY = unknown> {
|
|
|
160
160
|
signature: string;
|
|
161
161
|
}
|
|
162
162
|
export type FetcherResponse<BODY = unknown> = FetcherSuccessResponse<BODY> | FetcherErrorResponse<BODY>;
|
|
163
|
-
export type FetcherMode = 'json' | 'text' | 'void' | 'arrayBuffer' | 'blob';
|
|
163
|
+
export type FetcherMode = 'json' | 'text' | 'void' | 'arrayBuffer' | 'blob' | 'readableStream';
|
|
@@ -114,7 +114,6 @@ exports._mapValues = _mapValues;
|
|
|
114
114
|
* Does not support `mutate` flag.
|
|
115
115
|
*/
|
|
116
116
|
function _mapKeys(obj, mapper) {
|
|
117
|
-
// eslint-disable-next-line unicorn/prefer-object-from-entries
|
|
118
117
|
return Object.entries(obj).reduce((map, [k, v]) => {
|
|
119
118
|
map[mapper(k, v, obj)] = v;
|
|
120
119
|
return map;
|
|
@@ -11,8 +11,8 @@ import { AnyFunction } from '../types';
|
|
|
11
11
|
* @experimental
|
|
12
12
|
*/
|
|
13
13
|
export declare class Abortable {
|
|
14
|
-
onAbort?: AnyFunction
|
|
15
|
-
constructor(onAbort?: AnyFunction
|
|
14
|
+
onAbort?: AnyFunction | undefined;
|
|
15
|
+
constructor(onAbort?: AnyFunction | undefined);
|
|
16
16
|
aborted: boolean;
|
|
17
17
|
abort(): void;
|
|
18
18
|
clear(): void;
|
package/dist/string/leven.js
CHANGED
|
@@ -59,7 +59,6 @@ function _leven(first, second) {
|
|
|
59
59
|
for (index = 0; index < firstLength; index++) {
|
|
60
60
|
temporary2 = bCharacterCode === characterCodeCache[index] ? temporary : temporary + 1;
|
|
61
61
|
temporary = array[index];
|
|
62
|
-
// eslint-disable-next-line no-multi-assign
|
|
63
62
|
result = array[index] =
|
|
64
63
|
temporary > result
|
|
65
64
|
? temporary2 > result
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/* eslint-disable unicorn/prefer-string-slice */
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
3
|
exports._nl2br = exports._replaceAll = exports._substringBetweenLast = exports._substringAfterLast = exports._substringAfter = exports._substringBeforeLast = exports._substringBefore = exports._truncateMiddle = exports._truncate = exports._removeWhitespace = exports._split = exports._lowerFirst = exports._upperFirst = exports._capitalize = void 0;
|
|
5
4
|
/**
|
package/dist/vendor/is.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export declare namespace is {
|
|
|
17
17
|
var bigint: (value: unknown) => value is bigint;
|
|
18
18
|
var function_: (value: unknown) => value is Function;
|
|
19
19
|
var null_: (value: unknown) => value is null;
|
|
20
|
-
var class_: (value: unknown) => value is Class
|
|
20
|
+
var class_: (value: unknown) => value is Class;
|
|
21
21
|
var boolean: (value: unknown) => value is boolean;
|
|
22
22
|
var symbol: (value: unknown) => value is symbol;
|
|
23
23
|
var numericString: (value: unknown) => value is string;
|
package/dist/vendor/is.js
CHANGED
|
@@ -63,7 +63,6 @@ const primitiveTypeNames = [
|
|
|
63
63
|
function isPrimitiveTypeName(name) {
|
|
64
64
|
return primitiveTypeNames.includes(name);
|
|
65
65
|
}
|
|
66
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
67
66
|
function isOfType(type) {
|
|
68
67
|
return (value) => typeof value === type;
|
|
69
68
|
}
|
|
@@ -126,7 +125,6 @@ is.string = isOfType('string');
|
|
|
126
125
|
const isNumberType = isOfType('number');
|
|
127
126
|
is.number = (value) => isNumberType(value) && !is.nan(value);
|
|
128
127
|
is.bigint = isOfType('bigint');
|
|
129
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
130
128
|
is.function_ = isOfType('function');
|
|
131
129
|
is.null_ = (value) => value === null;
|
|
132
130
|
is.class_ = (value) => is.function_(value) && value.toString().startsWith('class ');
|
|
@@ -155,7 +153,6 @@ is.promise = (value) => is.nativePromise(value) || hasPromiseAPI(value);
|
|
|
155
153
|
is.generatorFunction = isObjectOfType('GeneratorFunction');
|
|
156
154
|
is.asyncGeneratorFunction = (value) => getObjectType(value) === 'AsyncGeneratorFunction';
|
|
157
155
|
is.asyncFunction = (value) => getObjectType(value) === 'AsyncFunction';
|
|
158
|
-
// eslint-disable-next-line no-prototype-builtins, @typescript-eslint/ban-types
|
|
159
156
|
is.boundFunction = (value) => is.function_(value) && !value.hasOwnProperty('prototype');
|
|
160
157
|
is.regExp = isObjectOfType('RegExp');
|
|
161
158
|
is.date = isObjectOfType('Date');
|
|
@@ -185,7 +182,7 @@ is.urlString = (value) => {
|
|
|
185
182
|
return false;
|
|
186
183
|
}
|
|
187
184
|
try {
|
|
188
|
-
new URL(value);
|
|
185
|
+
new URL(value);
|
|
189
186
|
return true;
|
|
190
187
|
}
|
|
191
188
|
catch {
|
|
@@ -344,7 +341,6 @@ exports.assert = {
|
|
|
344
341
|
string: (value) => assertType(is.string(value), 'string', value),
|
|
345
342
|
number: (value) => assertType(is.number(value), 'number', value),
|
|
346
343
|
bigint: (value) => assertType(is.bigint(value), 'bigint', value),
|
|
347
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
348
344
|
function_: (value) => assertType(is.function_(value), 'Function', value),
|
|
349
345
|
null_: (value) => assertType(is.null_(value), 'null', value),
|
|
350
346
|
class_: (value) => assertType(is.class_(value), AssertionTypeDescription.class_, value),
|
|
@@ -369,9 +365,7 @@ exports.assert = {
|
|
|
369
365
|
promise: (value) => assertType(is.promise(value), 'Promise', value),
|
|
370
366
|
generatorFunction: (value) => assertType(is.generatorFunction(value), 'GeneratorFunction', value),
|
|
371
367
|
asyncGeneratorFunction: (value) => assertType(is.asyncGeneratorFunction(value), 'AsyncGeneratorFunction', value),
|
|
372
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
373
368
|
asyncFunction: (value) => assertType(is.asyncFunction(value), 'AsyncFunction', value),
|
|
374
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
375
369
|
boundFunction: (value) => assertType(is.boundFunction(value), 'Function', value),
|
|
376
370
|
regExp: (value) => assertType(is.regExp(value), 'RegExp', value),
|
|
377
371
|
date: (value) => assertType(is.date(value), 'Date', value),
|
|
@@ -134,7 +134,7 @@ export function _groupBy(items, mapper) {
|
|
|
134
134
|
export function _sortBy(items, mapper, mutate = false, descending = false) {
|
|
135
135
|
const mod = descending ? -1 : 1;
|
|
136
136
|
return (mutate ? items : [...items]).sort((_a, _b) => {
|
|
137
|
-
const [a, b] = [_a, _b].map(mapper);
|
|
137
|
+
const [a, b] = [_a, _b].map(mapper);
|
|
138
138
|
if (typeof a === 'number' && typeof b === 'number')
|
|
139
139
|
return (a - b) * mod;
|
|
140
140
|
return String(a).localeCompare(String(b)) * mod;
|
|
@@ -144,7 +144,7 @@ export function _sortBy(items, mapper, mutate = false, descending = false) {
|
|
|
144
144
|
* Like items.find(), but it tries to find from the END of the array.
|
|
145
145
|
*/
|
|
146
146
|
export function _findLast(items, predicate) {
|
|
147
|
-
return [...items].reverse().find(predicate);
|
|
147
|
+
return [...items].reverse().find(predicate);
|
|
148
148
|
}
|
|
149
149
|
export function _takeWhile(items, predicate) {
|
|
150
150
|
let proceed = true;
|
|
@@ -2,7 +2,6 @@ import { _assert } from '../error/assert';
|
|
|
2
2
|
import { LocalTime } from './localTime';
|
|
3
3
|
const MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
4
4
|
const DATE_REGEX = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
|
|
5
|
-
/* eslint-disable no-dupe-class-members */
|
|
6
5
|
/**
|
|
7
6
|
* @experimental
|
|
8
7
|
*/
|
|
@@ -10,7 +10,6 @@ import { _getTargetMethodSignature } from './decorator.util';
|
|
|
10
10
|
*
|
|
11
11
|
* @experimental
|
|
12
12
|
*/
|
|
13
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
14
13
|
export function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
15
14
|
const { decoratorName } = cfg;
|
|
16
15
|
return function decoratorFunction(target, propertyKey, pd) {
|
package/dist-esm/http/fetcher.js
CHANGED
|
@@ -66,6 +66,16 @@ export class Fetcher {
|
|
|
66
66
|
static create(cfg = {}) {
|
|
67
67
|
return new Fetcher(cfg);
|
|
68
68
|
}
|
|
69
|
+
// mode=readableStream
|
|
70
|
+
/**
|
|
71
|
+
* Returns raw fetchResponse.body, which is a ReadableStream<Uint8Array>
|
|
72
|
+
*
|
|
73
|
+
* More on streams and Node interop:
|
|
74
|
+
* https://css-tricks.com/web-streams-everywhere-and-fetch-for-node-js/
|
|
75
|
+
*/
|
|
76
|
+
async getReadableStream(url, opt) {
|
|
77
|
+
return await this.fetch(url, Object.assign({ mode: 'readableStream' }, opt));
|
|
78
|
+
}
|
|
69
79
|
async fetch(url, opt) {
|
|
70
80
|
const res = await this.doFetch(url, opt);
|
|
71
81
|
if (res.err) {
|
|
@@ -227,6 +237,14 @@ export class Fetcher {
|
|
|
227
237
|
else if (mode === 'blob') {
|
|
228
238
|
res.body = res.fetchResponse.body ? await res.fetchResponse.blob() : {};
|
|
229
239
|
}
|
|
240
|
+
else if (mode === 'readableStream') {
|
|
241
|
+
res.body = res.fetchResponse.body;
|
|
242
|
+
if (res.body === null) {
|
|
243
|
+
res.err = new Error(`fetchResponse.body is null`);
|
|
244
|
+
res.ok = false;
|
|
245
|
+
return await this.onNotOkResponse(res, started, timeout);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
230
248
|
clearTimeout(timeout);
|
|
231
249
|
res.retryStatus.retryStopped = true;
|
|
232
250
|
// res.err can happen on JSON.parse error
|
|
@@ -102,7 +102,6 @@ export function _mapValues(obj, mapper, mutate = false) {
|
|
|
102
102
|
* Does not support `mutate` flag.
|
|
103
103
|
*/
|
|
104
104
|
export function _mapKeys(obj, mapper) {
|
|
105
|
-
// eslint-disable-next-line unicorn/prefer-object-from-entries
|
|
106
105
|
return Object.entries(obj).reduce((map, [k, v]) => {
|
|
107
106
|
map[mapper(k, v, obj)] = v;
|
|
108
107
|
return map;
|
package/dist-esm/string/leven.js
CHANGED
|
@@ -56,7 +56,6 @@ export function _leven(first, second) {
|
|
|
56
56
|
for (index = 0; index < firstLength; index++) {
|
|
57
57
|
temporary2 = bCharacterCode === characterCodeCache[index] ? temporary : temporary + 1;
|
|
58
58
|
temporary = array[index];
|
|
59
|
-
// eslint-disable-next-line no-multi-assign
|
|
60
59
|
result = array[index] =
|
|
61
60
|
temporary > result
|
|
62
61
|
? temporary2 > result
|
package/dist-esm/vendor/is.js
CHANGED
|
@@ -60,7 +60,6 @@ const primitiveTypeNames = [
|
|
|
60
60
|
function isPrimitiveTypeName(name) {
|
|
61
61
|
return primitiveTypeNames.includes(name);
|
|
62
62
|
}
|
|
63
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
64
63
|
function isOfType(type) {
|
|
65
64
|
return (value) => typeof value === type;
|
|
66
65
|
}
|
|
@@ -122,7 +121,6 @@ is.string = isOfType('string');
|
|
|
122
121
|
const isNumberType = isOfType('number');
|
|
123
122
|
is.number = (value) => isNumberType(value) && !is.nan(value);
|
|
124
123
|
is.bigint = isOfType('bigint');
|
|
125
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
126
124
|
is.function_ = isOfType('function');
|
|
127
125
|
is.null_ = (value) => value === null;
|
|
128
126
|
is.class_ = (value) => is.function_(value) && value.toString().startsWith('class ');
|
|
@@ -151,7 +149,6 @@ is.promise = (value) => is.nativePromise(value) || hasPromiseAPI(value);
|
|
|
151
149
|
is.generatorFunction = isObjectOfType('GeneratorFunction');
|
|
152
150
|
is.asyncGeneratorFunction = (value) => getObjectType(value) === 'AsyncGeneratorFunction';
|
|
153
151
|
is.asyncFunction = (value) => getObjectType(value) === 'AsyncFunction';
|
|
154
|
-
// eslint-disable-next-line no-prototype-builtins, @typescript-eslint/ban-types
|
|
155
152
|
is.boundFunction = (value) => is.function_(value) && !value.hasOwnProperty('prototype');
|
|
156
153
|
is.regExp = isObjectOfType('RegExp');
|
|
157
154
|
is.date = isObjectOfType('Date');
|
|
@@ -181,7 +178,7 @@ is.urlString = (value) => {
|
|
|
181
178
|
return false;
|
|
182
179
|
}
|
|
183
180
|
try {
|
|
184
|
-
new URL(value);
|
|
181
|
+
new URL(value);
|
|
185
182
|
return true;
|
|
186
183
|
}
|
|
187
184
|
catch (_a) {
|
|
@@ -341,7 +338,6 @@ export const assert = {
|
|
|
341
338
|
string: (value) => assertType(is.string(value), 'string', value),
|
|
342
339
|
number: (value) => assertType(is.number(value), 'number', value),
|
|
343
340
|
bigint: (value) => assertType(is.bigint(value), 'bigint', value),
|
|
344
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
345
341
|
function_: (value) => assertType(is.function_(value), 'Function', value),
|
|
346
342
|
null_: (value) => assertType(is.null_(value), 'null', value),
|
|
347
343
|
class_: (value) => assertType(is.class_(value), AssertionTypeDescription.class_, value),
|
|
@@ -366,9 +362,7 @@ export const assert = {
|
|
|
366
362
|
promise: (value) => assertType(is.promise(value), 'Promise', value),
|
|
367
363
|
generatorFunction: (value) => assertType(is.generatorFunction(value), 'GeneratorFunction', value),
|
|
368
364
|
asyncGeneratorFunction: (value) => assertType(is.asyncGeneratorFunction(value), 'AsyncGeneratorFunction', value),
|
|
369
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
370
365
|
asyncFunction: (value) => assertType(is.asyncFunction(value), 'AsyncFunction', value),
|
|
371
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
372
366
|
boundFunction: (value) => assertType(is.boundFunction(value), 'Function', value),
|
|
373
367
|
regExp: (value) => assertType(is.regExp(value), 'RegExp', value),
|
|
374
368
|
date: (value) => assertType(is.date(value), 'Date', value),
|
package/package.json
CHANGED
package/src/array/array.util.ts
CHANGED
|
@@ -149,7 +149,7 @@ export function _sortBy<T>(
|
|
|
149
149
|
): T[] {
|
|
150
150
|
const mod = descending ? -1 : 1
|
|
151
151
|
return (mutate ? items : [...items]).sort((_a, _b) => {
|
|
152
|
-
const [a, b] = [_a, _b].map(mapper)
|
|
152
|
+
const [a, b] = [_a, _b].map(mapper)
|
|
153
153
|
if (typeof a === 'number' && typeof b === 'number') return (a - b) * mod
|
|
154
154
|
return String(a).localeCompare(String(b)) * mod
|
|
155
155
|
})
|
|
@@ -159,7 +159,7 @@ export function _sortBy<T>(
|
|
|
159
159
|
* Like items.find(), but it tries to find from the END of the array.
|
|
160
160
|
*/
|
|
161
161
|
export function _findLast<T>(items: T[], predicate: Predicate<T>): T | undefined {
|
|
162
|
-
return [...items].reverse().find(predicate)
|
|
162
|
+
return [...items].reverse().find(predicate)
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
export function _takeWhile<T>(items: T[], predicate: Predicate<T>): T[] {
|
|
@@ -52,7 +52,7 @@ export interface PromiseDecoratorResp<PARAMS> {
|
|
|
52
52
|
*
|
|
53
53
|
* @experimental
|
|
54
54
|
*/
|
|
55
|
-
|
|
55
|
+
|
|
56
56
|
export function _createPromiseDecorator<RES = any, PARAMS = any>(
|
|
57
57
|
cfg: PromiseDecoratorCfg<RES, PARAMS>,
|
|
58
58
|
decoratorParams: PARAMS = {} as any,
|
|
@@ -192,4 +192,4 @@ export type FetcherResponse<BODY = unknown> =
|
|
|
192
192
|
| FetcherSuccessResponse<BODY>
|
|
193
193
|
| FetcherErrorResponse<BODY>
|
|
194
194
|
|
|
195
|
-
export type FetcherMode = 'json' | 'text' | 'void' | 'arrayBuffer' | 'blob'
|
|
195
|
+
export type FetcherMode = 'json' | 'text' | 'void' | 'arrayBuffer' | 'blob' | 'readableStream'
|
package/src/http/fetcher.ts
CHANGED
|
@@ -129,6 +129,20 @@ export class Fetcher {
|
|
|
129
129
|
deleteVoid!: (url: string, opt?: FetcherOptions) => Promise<void>
|
|
130
130
|
headVoid!: (url: string, opt?: FetcherOptions) => Promise<void>
|
|
131
131
|
|
|
132
|
+
// mode=readableStream
|
|
133
|
+
/**
|
|
134
|
+
* Returns raw fetchResponse.body, which is a ReadableStream<Uint8Array>
|
|
135
|
+
*
|
|
136
|
+
* More on streams and Node interop:
|
|
137
|
+
* https://css-tricks.com/web-streams-everywhere-and-fetch-for-node-js/
|
|
138
|
+
*/
|
|
139
|
+
async getReadableStream(url: string, opt?: FetcherOptions): Promise<ReadableStream<Uint8Array>> {
|
|
140
|
+
return await this.fetch(url, {
|
|
141
|
+
mode: 'readableStream',
|
|
142
|
+
...opt,
|
|
143
|
+
})
|
|
144
|
+
}
|
|
145
|
+
|
|
132
146
|
async fetch<T = unknown>(url: string, opt?: FetcherOptions): Promise<T> {
|
|
133
147
|
const res = await this.doFetch<T>(url, opt)
|
|
134
148
|
if (res.err) {
|
|
@@ -276,6 +290,14 @@ export class Fetcher {
|
|
|
276
290
|
res.body = res.fetchResponse.body ? await res.fetchResponse.arrayBuffer() : {}
|
|
277
291
|
} else if (mode === 'blob') {
|
|
278
292
|
res.body = res.fetchResponse.body ? await res.fetchResponse.blob() : {}
|
|
293
|
+
} else if (mode === 'readableStream') {
|
|
294
|
+
res.body = res.fetchResponse.body
|
|
295
|
+
|
|
296
|
+
if (res.body === null) {
|
|
297
|
+
res.err = new Error(`fetchResponse.body is null`)
|
|
298
|
+
res.ok = false
|
|
299
|
+
return await this.onNotOkResponse(res, started, timeout)
|
|
300
|
+
}
|
|
279
301
|
}
|
|
280
302
|
|
|
281
303
|
clearTimeout(timeout)
|
|
@@ -139,7 +139,6 @@ export function _mapKeys<T extends AnyObject>(
|
|
|
139
139
|
obj: T,
|
|
140
140
|
mapper: ObjectMapper<T, string>,
|
|
141
141
|
): StringMap<T[keyof T]> {
|
|
142
|
-
// eslint-disable-next-line unicorn/prefer-object-from-entries
|
|
143
142
|
return Object.entries(obj).reduce((map, [k, v]) => {
|
|
144
143
|
map[mapper(k, v, obj) as keyof T] = v
|
|
145
144
|
return map
|
package/src/string/leven.ts
CHANGED
|
@@ -70,7 +70,7 @@ export function _leven(first: string, second: string): number {
|
|
|
70
70
|
for (index = 0; index < firstLength; index++) {
|
|
71
71
|
temporary2 = bCharacterCode === characterCodeCache[index] ? temporary : temporary + 1
|
|
72
72
|
temporary = array[index]!
|
|
73
|
-
|
|
73
|
+
|
|
74
74
|
result = array[index] =
|
|
75
75
|
temporary > result
|
|
76
76
|
? temporary2 > result
|
package/src/vendor/is.ts
CHANGED
|
@@ -84,7 +84,6 @@ function isPrimitiveTypeName(name: unknown): name is PrimitiveTypeName {
|
|
|
84
84
|
|
|
85
85
|
export type TypeName = ObjectTypeName | PrimitiveTypeName
|
|
86
86
|
|
|
87
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
88
87
|
function isOfType<T extends Primitive | Function>(type: PrimitiveTypeName | 'function') {
|
|
89
88
|
return (value: unknown): value is T => typeof value === type
|
|
90
89
|
}
|
|
@@ -168,7 +167,6 @@ is.number = (value: unknown): value is number => isNumberType(value) && !is.nan(
|
|
|
168
167
|
|
|
169
168
|
is.bigint = isOfType<bigint>('bigint')
|
|
170
169
|
|
|
171
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
172
170
|
is.function_ = isOfType<Function>('function')
|
|
173
171
|
|
|
174
172
|
is.null_ = (value: unknown): value is null => value === null
|
|
@@ -228,7 +226,6 @@ is.asyncGeneratorFunction = (value: unknown): value is (...args: any[]) => Promi
|
|
|
228
226
|
is.asyncFunction = <T = unknown>(value: unknown): value is (...args: any[]) => Promise<T> =>
|
|
229
227
|
getObjectType(value) === 'AsyncFunction'
|
|
230
228
|
|
|
231
|
-
// eslint-disable-next-line no-prototype-builtins, @typescript-eslint/ban-types
|
|
232
229
|
is.boundFunction = (value: unknown): value is Function =>
|
|
233
230
|
is.function_(value) && !value.hasOwnProperty('prototype')
|
|
234
231
|
|
|
@@ -270,7 +267,7 @@ is.urlString = (value: unknown): value is string => {
|
|
|
270
267
|
}
|
|
271
268
|
|
|
272
269
|
try {
|
|
273
|
-
new URL(value)
|
|
270
|
+
new URL(value)
|
|
274
271
|
return true
|
|
275
272
|
} catch {
|
|
276
273
|
return false
|
|
@@ -512,7 +509,7 @@ interface Assert {
|
|
|
512
509
|
string: (value: unknown) => asserts value is string
|
|
513
510
|
number: (value: unknown) => asserts value is number
|
|
514
511
|
bigint: (value: unknown) => asserts value is bigint
|
|
515
|
-
|
|
512
|
+
|
|
516
513
|
function_: (value: unknown) => asserts value is Function
|
|
517
514
|
null_: (value: unknown) => asserts value is null
|
|
518
515
|
class_: (value: unknown) => asserts value is Class
|
|
@@ -536,9 +533,9 @@ interface Assert {
|
|
|
536
533
|
promise: <T = unknown>(value: unknown) => asserts value is Promise<T>
|
|
537
534
|
generatorFunction: (value: unknown) => asserts value is GeneratorFunction
|
|
538
535
|
asyncGeneratorFunction: (value: unknown) => asserts value is AsyncGeneratorFunction
|
|
539
|
-
|
|
536
|
+
|
|
540
537
|
asyncFunction: (value: unknown) => asserts value is Function
|
|
541
|
-
|
|
538
|
+
|
|
542
539
|
boundFunction: (value: unknown) => asserts value is Function
|
|
543
540
|
regExp: (value: unknown) => asserts value is RegExp
|
|
544
541
|
date: (value: unknown) => asserts value is Date
|
|
@@ -620,7 +617,7 @@ export const assert: Assert = {
|
|
|
620
617
|
assertType(is.number(value), 'number', value),
|
|
621
618
|
bigint: (value: unknown): asserts value is bigint =>
|
|
622
619
|
assertType(is.bigint(value), 'bigint', value),
|
|
623
|
-
|
|
620
|
+
|
|
624
621
|
function_: (value: unknown): asserts value is Function =>
|
|
625
622
|
assertType(is.function_(value), 'Function', value),
|
|
626
623
|
null_: (value: unknown): asserts value is null => assertType(is.null_(value), 'null', value),
|
|
@@ -666,10 +663,10 @@ export const assert: Assert = {
|
|
|
666
663
|
assertType(is.generatorFunction(value), 'GeneratorFunction', value),
|
|
667
664
|
asyncGeneratorFunction: (value: unknown): asserts value is AsyncGeneratorFunction =>
|
|
668
665
|
assertType(is.asyncGeneratorFunction(value), 'AsyncGeneratorFunction', value),
|
|
669
|
-
|
|
666
|
+
|
|
670
667
|
asyncFunction: (value: unknown): asserts value is Function =>
|
|
671
668
|
assertType(is.asyncFunction(value), 'AsyncFunction', value),
|
|
672
|
-
|
|
669
|
+
|
|
673
670
|
boundFunction: (value: unknown): asserts value is Function =>
|
|
674
671
|
assertType(is.boundFunction(value), 'Function', value),
|
|
675
672
|
regExp: (value: unknown): asserts value is RegExp =>
|