@naturalcycles/js-lib 14.75.1 → 14.76.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/decorators/logMethod.decorator.js +1 -3
- package/dist/decorators/timeout.decorator.js +9 -1
- package/dist/error/try.d.ts +20 -0
- package/dist/error/try.js +47 -1
- package/dist/index.d.ts +20 -20
- package/dist/index.js +20 -52
- package/dist/promise/pTimeout.d.ts +10 -1
- package/dist/promise/pTimeout.js +43 -29
- package/dist/seq/seq.d.ts +1 -0
- package/dist/seq/seq.js +10 -0
- package/dist/unit/size.util.d.ts +6 -0
- package/dist/unit/size.util.js +33 -10
- package/dist-esm/decorators/logMethod.decorator.js +2 -4
- package/dist-esm/decorators/timeout.decorator.js +9 -1
- package/dist-esm/error/try.js +43 -0
- package/dist-esm/index.js +20 -20
- package/dist-esm/promise/pTimeout.js +40 -28
- package/dist-esm/seq/seq.js +10 -0
- package/dist-esm/unit/size.util.js +31 -9
- package/package.json +1 -1
- package/src/decorators/logMethod.decorator.ts +2 -4
- package/src/decorators/timeout.decorator.ts +12 -1
- package/src/error/try.ts +44 -0
- package/src/index.ts +20 -60
- package/src/promise/pTimeout.ts +41 -29
- package/src/seq/seq.ts +10 -0
- package/src/unit/size.util.ts +22 -6
|
@@ -20,9 +20,7 @@ const decorator_util_1 = require("./decorator.util");
|
|
|
20
20
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
21
21
|
function _LogMethod(opt = {}) {
|
|
22
22
|
return (target, key, descriptor) => {
|
|
23
|
-
|
|
24
|
-
throw new TypeError('@_LogMethod can be applied only to methods');
|
|
25
|
-
}
|
|
23
|
+
(0, __1._assert)(typeof descriptor.value === 'function', '@_LogMethod can be applied only to methods');
|
|
26
24
|
const originalFn = descriptor.value;
|
|
27
25
|
const keyStr = String(key);
|
|
28
26
|
const { avg, noLogArgs, logStart, logResult, noLogResultLength, logger = console } = opt;
|
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports._Timeout = void 0;
|
|
4
|
+
const assert_1 = require("../error/assert");
|
|
4
5
|
const pTimeout_1 = require("../promise/pTimeout");
|
|
6
|
+
const decorator_util_1 = require("./decorator.util");
|
|
5
7
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
6
8
|
function _Timeout(opt) {
|
|
7
9
|
return (target, key, descriptor) => {
|
|
10
|
+
(0, assert_1._assert)(typeof descriptor.value === 'function', '@_Timeout can be applied only to methods');
|
|
8
11
|
const originalFn = descriptor.value;
|
|
9
|
-
|
|
12
|
+
const keyStr = String(key);
|
|
13
|
+
descriptor.value = async function (...args) {
|
|
14
|
+
const ctx = this;
|
|
15
|
+
opt.name || (opt.name = (0, decorator_util_1._getMethodSignature)(ctx, keyStr));
|
|
16
|
+
return await (0, pTimeout_1.pTimeout)(originalFn.apply(this, args), opt);
|
|
17
|
+
};
|
|
10
18
|
return descriptor;
|
|
11
19
|
};
|
|
12
20
|
}
|
package/dist/error/try.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { AnyFunction } from '../types';
|
|
2
|
+
import { AppError } from './app.error';
|
|
1
3
|
/**
|
|
2
4
|
* Calls a function, returns a Tuple of [error, value].
|
|
3
5
|
* Allows to write shorter code that avoids `try/catch`.
|
|
@@ -22,3 +24,21 @@ export declare function _try<ERR = unknown, RETURN = void>(fn: () => RETURN): [e
|
|
|
22
24
|
* but you should check for `err` presense first!
|
|
23
25
|
*/
|
|
24
26
|
export declare function pTry<ERR = unknown, RETURN = void>(promise: Promise<RETURN>): Promise<[err: ERR | null, value: Awaited<RETURN>]>;
|
|
27
|
+
/**
|
|
28
|
+
* It is thrown when Error was expected, but didn't happen
|
|
29
|
+
* ("pass" happened instead).
|
|
30
|
+
* "Pass" means "no error".
|
|
31
|
+
*/
|
|
32
|
+
export declare class UnexpectedPassError extends AppError {
|
|
33
|
+
constructor();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Calls `fn`, expects is to throw, catches the expected error and returns.
|
|
37
|
+
* If error was NOT thrown - throws UnexpectedPassError instead.
|
|
38
|
+
*/
|
|
39
|
+
export declare function _expectedError<ERR = Error>(fn: AnyFunction): ERR;
|
|
40
|
+
/**
|
|
41
|
+
* Awaits passed `promise`, expects is to throw (reject), catches the expected error and returns.
|
|
42
|
+
* If error was NOT thrown - throws UnexpectedPassError instead.
|
|
43
|
+
*/
|
|
44
|
+
export declare function pExpectedError<ERR = Error>(promise: Promise<any>): Promise<ERR>;
|
package/dist/error/try.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.pTry = exports._try = void 0;
|
|
3
|
+
exports.pExpectedError = exports._expectedError = exports.UnexpectedPassError = exports.pTry = exports._try = void 0;
|
|
4
|
+
const app_error_1 = require("./app.error");
|
|
4
5
|
/**
|
|
5
6
|
* Calls a function, returns a Tuple of [error, value].
|
|
6
7
|
* Allows to write shorter code that avoids `try/catch`.
|
|
@@ -43,3 +44,48 @@ async function pTry(promise) {
|
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
46
|
exports.pTry = pTry;
|
|
47
|
+
/**
|
|
48
|
+
* It is thrown when Error was expected, but didn't happen
|
|
49
|
+
* ("pass" happened instead).
|
|
50
|
+
* "Pass" means "no error".
|
|
51
|
+
*/
|
|
52
|
+
class UnexpectedPassError extends app_error_1.AppError {
|
|
53
|
+
constructor() {
|
|
54
|
+
super('_expectedError passed unexpectedly');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.UnexpectedPassError = UnexpectedPassError;
|
|
58
|
+
/**
|
|
59
|
+
* Calls `fn`, expects is to throw, catches the expected error and returns.
|
|
60
|
+
* If error was NOT thrown - throws UnexpectedPassError instead.
|
|
61
|
+
*/
|
|
62
|
+
function _expectedError(fn) {
|
|
63
|
+
try {
|
|
64
|
+
fn();
|
|
65
|
+
// Unexpected!
|
|
66
|
+
throw new UnexpectedPassError();
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
if (err instanceof UnexpectedPassError)
|
|
70
|
+
throw err; // re-throw
|
|
71
|
+
return err; // this is expected!
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports._expectedError = _expectedError;
|
|
75
|
+
/**
|
|
76
|
+
* Awaits passed `promise`, expects is to throw (reject), catches the expected error and returns.
|
|
77
|
+
* If error was NOT thrown - throws UnexpectedPassError instead.
|
|
78
|
+
*/
|
|
79
|
+
async function pExpectedError(promise) {
|
|
80
|
+
try {
|
|
81
|
+
await promise;
|
|
82
|
+
// Unexpected!
|
|
83
|
+
throw new UnexpectedPassError();
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
if (err instanceof UnexpectedPassError)
|
|
87
|
+
throw err; // re-throw
|
|
88
|
+
return err; // this is expected!
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.pExpectedError = pExpectedError;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,27 +3,27 @@ export * from './lazy';
|
|
|
3
3
|
export * from './string/url.util';
|
|
4
4
|
export * from './array/range';
|
|
5
5
|
import { PromiseDecoratorCfg, PromiseDecoratorResp, _createPromiseDecorator } from './decorators/createPromiseDecorator';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
export * from './decorators/debounce';
|
|
7
|
+
export * from './decorators/debounce.decorator';
|
|
8
|
+
export * from './decorators/decorator.util';
|
|
9
|
+
export * from './decorators/logMethod.decorator';
|
|
10
|
+
export * from './decorators/memo.decorator';
|
|
11
11
|
import { MemoCache } from './decorators/memo.util';
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
export * from './decorators/memoFn';
|
|
13
|
+
export * from './decorators/retry.decorator';
|
|
14
|
+
export * from './decorators/timeout.decorator';
|
|
15
|
+
export * from './error/app.error';
|
|
16
|
+
export * from './error/assert';
|
|
17
17
|
import { Admin401ErrorData, Admin403ErrorData, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse } from './error/error.model';
|
|
18
18
|
export * from './error/error.util';
|
|
19
19
|
import { ErrorMode } from './error/errorMode';
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
export * from './error/http.error';
|
|
21
|
+
export * from './error/try';
|
|
22
22
|
import { TryCatchOptions, _TryCatch, _tryCatch } from './error/tryCatch';
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
export * from './json-schema/from-data/generateJsonSchemaFromData';
|
|
24
|
+
export * from './json-schema/jsonSchema.cnst';
|
|
25
25
|
import { JsonSchema, JsonSchemaAllOf, JsonSchemaAny, JsonSchemaAnyOf, JsonSchemaArray, JsonSchemaBoolean, JsonSchemaConst, JsonSchemaEnum, JsonSchemaNot, JsonSchemaNull, JsonSchemaNumber, JsonSchemaRootObject, JsonSchemaObject, JsonSchemaOneOf, JsonSchemaRef, JsonSchemaString, JsonSchemaTuple } from './json-schema/jsonSchema.model';
|
|
26
|
-
|
|
26
|
+
export * from './json-schema/jsonSchema.util';
|
|
27
27
|
import { jsonSchema, JsonSchemaAnyBuilder, JsonSchemaBuilder } from './json-schema/jsonSchemaBuilder';
|
|
28
28
|
export * from './math/math.util';
|
|
29
29
|
export * from './math/sma';
|
|
@@ -43,21 +43,21 @@ import { pMap, PMapOptions } from './promise/pMap';
|
|
|
43
43
|
export * from './promise/pProps';
|
|
44
44
|
import { pRetry, PRetryOptions } from './promise/pRetry';
|
|
45
45
|
export * from './promise/pState';
|
|
46
|
-
import { pTimeout, PTimeoutOptions } from './promise/pTimeout';
|
|
46
|
+
import { pTimeout, pTimeoutFn, PTimeoutOptions } from './promise/pTimeout';
|
|
47
47
|
export * from './promise/pTuple';
|
|
48
48
|
export * from './string/case';
|
|
49
49
|
export * from './string/json.util';
|
|
50
50
|
export * from './string/string.util';
|
|
51
51
|
import { JsonStringifyFunction, StringifyAnyOptions, _stringifyAny } from './string/stringifyAny';
|
|
52
|
-
|
|
52
|
+
export * from './time/time.util';
|
|
53
53
|
import { Class, ConditionalExcept, ConditionalPick, Merge, Promisable, ReadonlyDeep, Simplify } from './typeFest';
|
|
54
54
|
import { AsyncMapper, AsyncPredicate, BaseDBEntity, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, Saved, Unsaved, BatchResult, InstanceId, IsoDate, IsoDateTime, KeyValueTuple, Mapper, ObjectMapper, ObjectPredicate, Predicate, PromiseMap, AnyObject, AnyFunction, Reviver, SavedDBEntity, StringMap, UnixTimestamp, ValueOf, ValuesOf, AbortableMapper, AbortableAsyncPredicate, AbortableAsyncMapper, AbortablePredicate, END, SKIP, _noop, _objectKeys, _passNothingPredicate, _passthroughMapper, _passthroughPredicate, _passUndefinedMapper, _stringMapEntries, _stringMapValues } from './types';
|
|
55
|
-
|
|
55
|
+
export * from './unit/size.util';
|
|
56
56
|
import { is } from './vendor/is';
|
|
57
57
|
import { CommonLogLevel, CommonLogFunction, CommonLogger, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, CommonLogWithLevelFunction, commonLoggerCreate } from './log/commonLogger';
|
|
58
|
-
|
|
58
|
+
export * from './string/safeJsonStringify';
|
|
59
59
|
import { PQueue, PQueueCfg } from './promise/pQueue';
|
|
60
60
|
export * from './seq/seq';
|
|
61
61
|
export * from './math/stack.util';
|
|
62
62
|
export type { AbortableMapper, AbortablePredicate, AbortableAsyncPredicate, AbortableAsyncMapper, PQueueCfg, MemoCache, PromiseDecoratorCfg, PromiseDecoratorResp, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, Admin401ErrorData, Admin403ErrorData, StringMap, PromiseMap, AnyObject, AnyFunction, ValuesOf, ValueOf, KeyValueTuple, ObjectMapper, ObjectPredicate, InstanceId, IsoDate, IsoDateTime, Reviver, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, Simplify, ConditionalPick, ConditionalExcept, Class, UnixTimestamp, BaseDBEntity, SavedDBEntity, Saved, Unsaved, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, JsonSchema, JsonSchemaAny, JsonSchemaOneOf, JsonSchemaAllOf, JsonSchemaAnyOf, JsonSchemaNot, JsonSchemaRef, JsonSchemaConst, JsonSchemaEnum, JsonSchemaString, JsonSchemaNumber, JsonSchemaBoolean, JsonSchemaNull, JsonSchemaRootObject, JsonSchemaObject, JsonSchemaArray, JsonSchemaTuple, JsonSchemaBuilder, CommonLogLevel, CommonLogWithLevelFunction, CommonLogFunction, CommonLogger, };
|
|
63
|
-
export { is,
|
|
63
|
+
export { is, _createPromiseDecorator, _stringMapValues, _stringMapEntries, _objectKeys, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, ErrorMode, pDefer, AggregatedError, pRetry, pTimeout, pTimeoutFn, _tryCatch, _TryCatch, _stringifyAny, jsonSchema, JsonSchemaAnyBuilder, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, commonLoggerCreate, PQueue, END, SKIP, };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.SKIP = exports.END = exports.PQueue = exports._safeJsonStringify = exports.commonLoggerCreate = exports.commonLoggerPrefix = exports.commonLoggerPipe = exports.commonLogLevelNumber = exports.commonLoggerNoop = exports.commonLoggerMinLevel = exports.generateJsonSchemaFromData = exports.JSON_SCHEMA_ORDER = void 0;
|
|
3
|
+
exports.SKIP = exports.END = exports.PQueue = exports.commonLoggerCreate = exports.commonLoggerPrefix = exports.commonLoggerPipe = exports.commonLogLevelNumber = exports.commonLoggerNoop = exports.commonLoggerMinLevel = exports.JsonSchemaAnyBuilder = exports.jsonSchema = exports._stringifyAny = exports._TryCatch = exports._tryCatch = exports.pTimeoutFn = exports.pTimeout = exports.pRetry = exports.AggregatedError = exports.pDefer = exports.ErrorMode = exports._noop = exports._passNothingPredicate = exports._passthroughPredicate = exports._passUndefinedMapper = exports._passthroughMapper = exports.pMap = exports._objectKeys = exports._stringMapEntries = exports._stringMapValues = exports._createPromiseDecorator = exports.is = void 0;
|
|
5
4
|
const tslib_1 = require("tslib");
|
|
6
5
|
(0, tslib_1.__exportStar)(require("./array/array.util"), exports);
|
|
7
6
|
(0, tslib_1.__exportStar)(require("./lazy"), exports);
|
|
@@ -9,52 +8,27 @@ const tslib_1 = require("tslib");
|
|
|
9
8
|
(0, tslib_1.__exportStar)(require("./array/range"), exports);
|
|
10
9
|
const createPromiseDecorator_1 = require("./decorators/createPromiseDecorator");
|
|
11
10
|
Object.defineProperty(exports, "_createPromiseDecorator", { enumerable: true, get: function () { return createPromiseDecorator_1._createPromiseDecorator; } });
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const memo_decorator_1 = require("./decorators/memo.decorator");
|
|
23
|
-
Object.defineProperty(exports, "_Memo", { enumerable: true, get: function () { return memo_decorator_1._Memo; } });
|
|
24
|
-
const memoFn_1 = require("./decorators/memoFn");
|
|
25
|
-
Object.defineProperty(exports, "_memoFn", { enumerable: true, get: function () { return memoFn_1._memoFn; } });
|
|
26
|
-
const retry_decorator_1 = require("./decorators/retry.decorator");
|
|
27
|
-
Object.defineProperty(exports, "_Retry", { enumerable: true, get: function () { return retry_decorator_1._Retry; } });
|
|
28
|
-
const timeout_decorator_1 = require("./decorators/timeout.decorator");
|
|
29
|
-
Object.defineProperty(exports, "_Timeout", { enumerable: true, get: function () { return timeout_decorator_1._Timeout; } });
|
|
30
|
-
const app_error_1 = require("./error/app.error");
|
|
31
|
-
Object.defineProperty(exports, "AppError", { enumerable: true, get: function () { return app_error_1.AppError; } });
|
|
32
|
-
const assert_1 = require("./error/assert");
|
|
33
|
-
Object.defineProperty(exports, "AssertionError", { enumerable: true, get: function () { return assert_1.AssertionError; } });
|
|
34
|
-
Object.defineProperty(exports, "_assert", { enumerable: true, get: function () { return assert_1._assert; } });
|
|
35
|
-
Object.defineProperty(exports, "_assertDeepEquals", { enumerable: true, get: function () { return assert_1._assertDeepEquals; } });
|
|
36
|
-
Object.defineProperty(exports, "_assertEquals", { enumerable: true, get: function () { return assert_1._assertEquals; } });
|
|
37
|
-
Object.defineProperty(exports, "_assertIsError", { enumerable: true, get: function () { return assert_1._assertIsError; } });
|
|
38
|
-
Object.defineProperty(exports, "_assertIsNumber", { enumerable: true, get: function () { return assert_1._assertIsNumber; } });
|
|
39
|
-
Object.defineProperty(exports, "_assertIsString", { enumerable: true, get: function () { return assert_1._assertIsString; } });
|
|
40
|
-
Object.defineProperty(exports, "_assertTypeOf", { enumerable: true, get: function () { return assert_1._assertTypeOf; } });
|
|
11
|
+
(0, tslib_1.__exportStar)(require("./decorators/debounce"), exports);
|
|
12
|
+
(0, tslib_1.__exportStar)(require("./decorators/debounce.decorator"), exports);
|
|
13
|
+
(0, tslib_1.__exportStar)(require("./decorators/decorator.util"), exports);
|
|
14
|
+
(0, tslib_1.__exportStar)(require("./decorators/logMethod.decorator"), exports);
|
|
15
|
+
(0, tslib_1.__exportStar)(require("./decorators/memo.decorator"), exports);
|
|
16
|
+
(0, tslib_1.__exportStar)(require("./decorators/memoFn"), exports);
|
|
17
|
+
(0, tslib_1.__exportStar)(require("./decorators/retry.decorator"), exports);
|
|
18
|
+
(0, tslib_1.__exportStar)(require("./decorators/timeout.decorator"), exports);
|
|
19
|
+
(0, tslib_1.__exportStar)(require("./error/app.error"), exports);
|
|
20
|
+
(0, tslib_1.__exportStar)(require("./error/assert"), exports);
|
|
41
21
|
(0, tslib_1.__exportStar)(require("./error/error.util"), exports);
|
|
42
22
|
const errorMode_1 = require("./error/errorMode");
|
|
43
23
|
Object.defineProperty(exports, "ErrorMode", { enumerable: true, get: function () { return errorMode_1.ErrorMode; } });
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const try_1 = require("./error/try");
|
|
47
|
-
Object.defineProperty(exports, "_try", { enumerable: true, get: function () { return try_1._try; } });
|
|
48
|
-
Object.defineProperty(exports, "pTry", { enumerable: true, get: function () { return try_1.pTry; } });
|
|
24
|
+
(0, tslib_1.__exportStar)(require("./error/http.error"), exports);
|
|
25
|
+
(0, tslib_1.__exportStar)(require("./error/try"), exports);
|
|
49
26
|
const tryCatch_1 = require("./error/tryCatch");
|
|
50
27
|
Object.defineProperty(exports, "_TryCatch", { enumerable: true, get: function () { return tryCatch_1._TryCatch; } });
|
|
51
28
|
Object.defineProperty(exports, "_tryCatch", { enumerable: true, get: function () { return tryCatch_1._tryCatch; } });
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
Object.defineProperty(exports, "JSON_SCHEMA_ORDER", { enumerable: true, get: function () { return jsonSchema_cnst_1.JSON_SCHEMA_ORDER; } });
|
|
56
|
-
const jsonSchema_util_1 = require("./json-schema/jsonSchema.util");
|
|
57
|
-
Object.defineProperty(exports, "mergeJsonSchemaObjects", { enumerable: true, get: function () { return jsonSchema_util_1.mergeJsonSchemaObjects; } });
|
|
29
|
+
(0, tslib_1.__exportStar)(require("./json-schema/from-data/generateJsonSchemaFromData"), exports);
|
|
30
|
+
(0, tslib_1.__exportStar)(require("./json-schema/jsonSchema.cnst"), exports);
|
|
31
|
+
(0, tslib_1.__exportStar)(require("./json-schema/jsonSchema.util"), exports);
|
|
58
32
|
const jsonSchemaBuilder_1 = require("./json-schema/jsonSchemaBuilder");
|
|
59
33
|
Object.defineProperty(exports, "jsonSchema", { enumerable: true, get: function () { return jsonSchemaBuilder_1.jsonSchema; } });
|
|
60
34
|
Object.defineProperty(exports, "JsonSchemaAnyBuilder", { enumerable: true, get: function () { return jsonSchemaBuilder_1.JsonSchemaAnyBuilder; } });
|
|
@@ -82,15 +56,14 @@ Object.defineProperty(exports, "pRetry", { enumerable: true, get: function () {
|
|
|
82
56
|
(0, tslib_1.__exportStar)(require("./promise/pState"), exports);
|
|
83
57
|
const pTimeout_1 = require("./promise/pTimeout");
|
|
84
58
|
Object.defineProperty(exports, "pTimeout", { enumerable: true, get: function () { return pTimeout_1.pTimeout; } });
|
|
59
|
+
Object.defineProperty(exports, "pTimeoutFn", { enumerable: true, get: function () { return pTimeout_1.pTimeoutFn; } });
|
|
85
60
|
(0, tslib_1.__exportStar)(require("./promise/pTuple"), exports);
|
|
86
61
|
(0, tslib_1.__exportStar)(require("./string/case"), exports);
|
|
87
62
|
(0, tslib_1.__exportStar)(require("./string/json.util"), exports);
|
|
88
63
|
(0, tslib_1.__exportStar)(require("./string/string.util"), exports);
|
|
89
64
|
const stringifyAny_1 = require("./string/stringifyAny");
|
|
90
65
|
Object.defineProperty(exports, "_stringifyAny", { enumerable: true, get: function () { return stringifyAny_1._stringifyAny; } });
|
|
91
|
-
|
|
92
|
-
Object.defineProperty(exports, "_ms", { enumerable: true, get: function () { return time_util_1._ms; } });
|
|
93
|
-
Object.defineProperty(exports, "_since", { enumerable: true, get: function () { return time_util_1._since; } });
|
|
66
|
+
(0, tslib_1.__exportStar)(require("./time/time.util"), exports);
|
|
94
67
|
const types_1 = require("./types");
|
|
95
68
|
Object.defineProperty(exports, "END", { enumerable: true, get: function () { return types_1.END; } });
|
|
96
69
|
Object.defineProperty(exports, "SKIP", { enumerable: true, get: function () { return types_1.SKIP; } });
|
|
@@ -102,11 +75,7 @@ Object.defineProperty(exports, "_passthroughPredicate", { enumerable: true, get:
|
|
|
102
75
|
Object.defineProperty(exports, "_passUndefinedMapper", { enumerable: true, get: function () { return types_1._passUndefinedMapper; } });
|
|
103
76
|
Object.defineProperty(exports, "_stringMapEntries", { enumerable: true, get: function () { return types_1._stringMapEntries; } });
|
|
104
77
|
Object.defineProperty(exports, "_stringMapValues", { enumerable: true, get: function () { return types_1._stringMapValues; } });
|
|
105
|
-
|
|
106
|
-
Object.defineProperty(exports, "_gb", { enumerable: true, get: function () { return size_util_1._gb; } });
|
|
107
|
-
Object.defineProperty(exports, "_hb", { enumerable: true, get: function () { return size_util_1._hb; } });
|
|
108
|
-
Object.defineProperty(exports, "_kb", { enumerable: true, get: function () { return size_util_1._kb; } });
|
|
109
|
-
Object.defineProperty(exports, "_mb", { enumerable: true, get: function () { return size_util_1._mb; } });
|
|
78
|
+
(0, tslib_1.__exportStar)(require("./unit/size.util"), exports);
|
|
110
79
|
const is_1 = require("./vendor/is");
|
|
111
80
|
Object.defineProperty(exports, "is", { enumerable: true, get: function () { return is_1.is; } });
|
|
112
81
|
const commonLogger_1 = require("./log/commonLogger");
|
|
@@ -116,8 +85,7 @@ Object.defineProperty(exports, "commonLogLevelNumber", { enumerable: true, get:
|
|
|
116
85
|
Object.defineProperty(exports, "commonLoggerPipe", { enumerable: true, get: function () { return commonLogger_1.commonLoggerPipe; } });
|
|
117
86
|
Object.defineProperty(exports, "commonLoggerPrefix", { enumerable: true, get: function () { return commonLogger_1.commonLoggerPrefix; } });
|
|
118
87
|
Object.defineProperty(exports, "commonLoggerCreate", { enumerable: true, get: function () { return commonLogger_1.commonLoggerCreate; } });
|
|
119
|
-
|
|
120
|
-
Object.defineProperty(exports, "_safeJsonStringify", { enumerable: true, get: function () { return safeJsonStringify_1._safeJsonStringify; } });
|
|
88
|
+
(0, tslib_1.__exportStar)(require("./string/safeJsonStringify"), exports);
|
|
121
89
|
const pQueue_1 = require("./promise/pQueue");
|
|
122
90
|
Object.defineProperty(exports, "PQueue", { enumerable: true, get: function () { return pQueue_1.PQueue; } });
|
|
123
91
|
(0, tslib_1.__exportStar)(require("./seq/seq"), exports);
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { AppError } from '../error/app.error';
|
|
1
2
|
import { AnyFunction } from '../types';
|
|
3
|
+
export declare class TimeoutError extends AppError {
|
|
4
|
+
}
|
|
2
5
|
export interface PTimeoutOptions {
|
|
3
6
|
/**
|
|
4
7
|
* Timeout in milliseconds.
|
|
@@ -20,4 +23,10 @@ export interface PTimeoutOptions {
|
|
|
20
23
|
* Throws an Error if the Function is not resolved in a certain time.
|
|
21
24
|
* If the Function rejects - passes this rejection further.
|
|
22
25
|
*/
|
|
23
|
-
export declare function
|
|
26
|
+
export declare function pTimeoutFn<T extends AnyFunction>(fn: T, opt: PTimeoutOptions): T;
|
|
27
|
+
/**
|
|
28
|
+
* Decorates a Function with a timeout and immediately calls it.
|
|
29
|
+
* Throws an Error if the Function is not resolved in a certain time.
|
|
30
|
+
* If the Function rejects - passes this rejection further.
|
|
31
|
+
*/
|
|
32
|
+
export declare function pTimeout<T>(promise: Promise<T>, opt: PTimeoutOptions): Promise<T>;
|
package/dist/promise/pTimeout.js
CHANGED
|
@@ -1,41 +1,55 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.pTimeout = void 0;
|
|
3
|
+
exports.pTimeout = exports.pTimeoutFn = exports.TimeoutError = void 0;
|
|
4
|
+
const app_error_1 = require("../error/app.error");
|
|
5
|
+
class TimeoutError extends app_error_1.AppError {
|
|
6
|
+
}
|
|
7
|
+
exports.TimeoutError = TimeoutError;
|
|
4
8
|
/**
|
|
5
9
|
* Decorates a Function with a timeout.
|
|
6
10
|
* Throws an Error if the Function is not resolved in a certain time.
|
|
7
11
|
* If the Function rejects - passes this rejection further.
|
|
8
12
|
*/
|
|
9
|
-
function
|
|
10
|
-
|
|
13
|
+
function pTimeoutFn(fn, opt) {
|
|
14
|
+
opt.name || (opt.name = fn.name);
|
|
15
|
+
return async function pTimeoutInternalFn(...args) {
|
|
16
|
+
return await pTimeout(fn.apply(this, args), opt);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
exports.pTimeoutFn = pTimeoutFn;
|
|
20
|
+
/**
|
|
21
|
+
* Decorates a Function with a timeout and immediately calls it.
|
|
22
|
+
* Throws an Error if the Function is not resolved in a certain time.
|
|
23
|
+
* If the Function rejects - passes this rejection further.
|
|
24
|
+
*/
|
|
25
|
+
async function pTimeout(promise, opt) {
|
|
26
|
+
// todo: check how we can automatically infer function name (only applicable to named functions)
|
|
11
27
|
const { timeout, name, onTimeout } = opt;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
resolve(onTimeout());
|
|
20
|
-
}
|
|
21
|
-
catch (err) {
|
|
22
|
-
reject(err);
|
|
23
|
-
}
|
|
24
|
-
return;
|
|
28
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
29
|
+
return await new Promise(async (resolve, reject) => {
|
|
30
|
+
// Prepare the timeout timer
|
|
31
|
+
const timer = setTimeout(() => {
|
|
32
|
+
if (onTimeout) {
|
|
33
|
+
try {
|
|
34
|
+
resolve(onTimeout());
|
|
25
35
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
resolve(await fn.apply(this, args));
|
|
31
|
-
}
|
|
32
|
-
catch (err) {
|
|
33
|
-
reject(err);
|
|
34
|
-
}
|
|
35
|
-
finally {
|
|
36
|
-
clearTimeout(timer);
|
|
36
|
+
catch (err) {
|
|
37
|
+
reject(err);
|
|
38
|
+
}
|
|
39
|
+
return;
|
|
37
40
|
}
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
reject(new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`));
|
|
42
|
+
}, timeout);
|
|
43
|
+
// Execute the Function
|
|
44
|
+
try {
|
|
45
|
+
resolve(await promise);
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
reject(err);
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
clearTimeout(timer);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
40
54
|
}
|
|
41
55
|
exports.pTimeout = pTimeout;
|
package/dist/seq/seq.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export declare class Sequence<T> implements Iterable<T> {
|
|
|
23
23
|
some(predicate: AbortablePredicate<T>): boolean;
|
|
24
24
|
every(predicate: AbortablePredicate<T>): boolean;
|
|
25
25
|
toArray(): T[];
|
|
26
|
+
forEach(fn: (v: T, i: number) => void): void;
|
|
26
27
|
}
|
|
27
28
|
/**
|
|
28
29
|
* Convenience function to create a Sequence.
|
package/dist/seq/seq.js
CHANGED
|
@@ -130,6 +130,16 @@ class Sequence {
|
|
|
130
130
|
a.push(v);
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
|
+
forEach(fn) {
|
|
134
|
+
let i = -1;
|
|
135
|
+
// eslint-disable-next-line no-constant-condition
|
|
136
|
+
while (true) {
|
|
137
|
+
const v = this.next();
|
|
138
|
+
if (v === types_1.END)
|
|
139
|
+
return;
|
|
140
|
+
fn(v, ++i);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
133
143
|
}
|
|
134
144
|
exports.Sequence = Sequence;
|
|
135
145
|
/**
|
package/dist/unit/size.util.d.ts
CHANGED
|
@@ -5,3 +5,9 @@ export declare function _kb(b: number): number;
|
|
|
5
5
|
* Byte size to Human byte size string
|
|
6
6
|
*/
|
|
7
7
|
export declare function _hb(b?: number): string;
|
|
8
|
+
/**
|
|
9
|
+
* hc stands for "human count", similar to "human bytes" `_hb` function.
|
|
10
|
+
* Helpful to print big numbers, as it adds `K` (kilo), `M` (mega), etc to make
|
|
11
|
+
* them more readable.
|
|
12
|
+
*/
|
|
13
|
+
export declare function _hc(c?: number): string;
|
package/dist/unit/size.util.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._hb = exports._kb = exports._mb = exports._gb = void 0;
|
|
3
|
+
exports._hc = exports._hb = exports._kb = exports._mb = exports._gb = void 0;
|
|
4
4
|
function _gb(b) {
|
|
5
|
-
return Math.round(b /
|
|
5
|
+
return Math.round(b / 1024 ** 3);
|
|
6
6
|
}
|
|
7
7
|
exports._gb = _gb;
|
|
8
8
|
function _mb(b) {
|
|
9
|
-
return Math.round(b /
|
|
9
|
+
return Math.round(b / 1024 ** 2);
|
|
10
10
|
}
|
|
11
11
|
exports._mb = _mb;
|
|
12
12
|
function _kb(b) {
|
|
@@ -17,12 +17,35 @@ exports._kb = _kb;
|
|
|
17
17
|
* Byte size to Human byte size string
|
|
18
18
|
*/
|
|
19
19
|
function _hb(b = 0) {
|
|
20
|
-
if (b <
|
|
21
|
-
return `${Math.round(b)} byte
|
|
22
|
-
if (b <
|
|
23
|
-
return `${
|
|
24
|
-
if (b <
|
|
25
|
-
return `${
|
|
26
|
-
|
|
20
|
+
if (b < 1024)
|
|
21
|
+
return `${Math.round(b)} byte`;
|
|
22
|
+
if (b < 1024 ** 2)
|
|
23
|
+
return `${(b / 1024).toPrecision(3)} Kb`;
|
|
24
|
+
if (b < 1024 ** 3)
|
|
25
|
+
return `${(b / 1024 ** 2).toPrecision(3)} Mb`;
|
|
26
|
+
if (b < 1024 ** 4)
|
|
27
|
+
return `${(b / 1024 ** 3).toPrecision(3)} Gb`;
|
|
28
|
+
if (b < 1024 ** 5)
|
|
29
|
+
return `${(b / 1024 ** 4).toPrecision(3)} Tb`;
|
|
30
|
+
return `${Math.round(b / 1024 ** 4)} Tb`;
|
|
27
31
|
}
|
|
28
32
|
exports._hb = _hb;
|
|
33
|
+
/**
|
|
34
|
+
* hc stands for "human count", similar to "human bytes" `_hb` function.
|
|
35
|
+
* Helpful to print big numbers, as it adds `K` (kilo), `M` (mega), etc to make
|
|
36
|
+
* them more readable.
|
|
37
|
+
*/
|
|
38
|
+
function _hc(c = 0) {
|
|
39
|
+
if (c < 10 ** 4)
|
|
40
|
+
return String(c);
|
|
41
|
+
if (c < 10 ** 6)
|
|
42
|
+
return (c / 10 ** 3).toPrecision(3) + ' K';
|
|
43
|
+
if (c < 10 ** 9)
|
|
44
|
+
return (c / 10 ** 6).toPrecision(3) + ' M'; // million
|
|
45
|
+
if (c < 10 ** 12)
|
|
46
|
+
return (c / 10 ** 9).toPrecision(3) + ' B'; // billion
|
|
47
|
+
if (c < 10 ** 15)
|
|
48
|
+
return (c / 10 ** 12).toPrecision(3) + ' T'; // trillion
|
|
49
|
+
return Math.round(c / 10 ** 12) + ' T';
|
|
50
|
+
}
|
|
51
|
+
exports._hc = _hc;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SimpleMovingAverage, _stringifyAny } from '..';
|
|
1
|
+
import { SimpleMovingAverage, _stringifyAny, _assert } from '..';
|
|
2
2
|
import { _ms } from '../time/time.util';
|
|
3
3
|
import { _getArgsSignature, _getMethodSignature } from './decorator.util';
|
|
4
4
|
/**
|
|
@@ -17,9 +17,7 @@ import { _getArgsSignature, _getMethodSignature } from './decorator.util';
|
|
|
17
17
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
18
18
|
export function _LogMethod(opt = {}) {
|
|
19
19
|
return (target, key, descriptor) => {
|
|
20
|
-
|
|
21
|
-
throw new TypeError('@_LogMethod can be applied only to methods');
|
|
22
|
-
}
|
|
20
|
+
_assert(typeof descriptor.value === 'function', '@_LogMethod can be applied only to methods');
|
|
23
21
|
const originalFn = descriptor.value;
|
|
24
22
|
const keyStr = String(key);
|
|
25
23
|
const { avg, noLogArgs, logStart, logResult, noLogResultLength, logger = console } = opt;
|
|
@@ -1,9 +1,17 @@
|
|
|
1
|
+
import { _assert } from '../error/assert';
|
|
1
2
|
import { pTimeout } from '../promise/pTimeout';
|
|
3
|
+
import { _getMethodSignature } from './decorator.util';
|
|
2
4
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
3
5
|
export function _Timeout(opt) {
|
|
4
6
|
return (target, key, descriptor) => {
|
|
7
|
+
_assert(typeof descriptor.value === 'function', '@_Timeout can be applied only to methods');
|
|
5
8
|
const originalFn = descriptor.value;
|
|
6
|
-
|
|
9
|
+
const keyStr = String(key);
|
|
10
|
+
descriptor.value = async function (...args) {
|
|
11
|
+
const ctx = this;
|
|
12
|
+
opt.name || (opt.name = _getMethodSignature(ctx, keyStr));
|
|
13
|
+
return await pTimeout(originalFn.apply(this, args), opt);
|
|
14
|
+
};
|
|
7
15
|
return descriptor;
|
|
8
16
|
};
|
|
9
17
|
}
|
package/dist-esm/error/try.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AppError } from './app.error';
|
|
1
2
|
/**
|
|
2
3
|
* Calls a function, returns a Tuple of [error, value].
|
|
3
4
|
* Allows to write shorter code that avoids `try/catch`.
|
|
@@ -38,3 +39,45 @@ export async function pTry(promise) {
|
|
|
38
39
|
return [err, undefined];
|
|
39
40
|
}
|
|
40
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* It is thrown when Error was expected, but didn't happen
|
|
44
|
+
* ("pass" happened instead).
|
|
45
|
+
* "Pass" means "no error".
|
|
46
|
+
*/
|
|
47
|
+
export class UnexpectedPassError extends AppError {
|
|
48
|
+
constructor() {
|
|
49
|
+
super('_expectedError passed unexpectedly');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Calls `fn`, expects is to throw, catches the expected error and returns.
|
|
54
|
+
* If error was NOT thrown - throws UnexpectedPassError instead.
|
|
55
|
+
*/
|
|
56
|
+
export function _expectedError(fn) {
|
|
57
|
+
try {
|
|
58
|
+
fn();
|
|
59
|
+
// Unexpected!
|
|
60
|
+
throw new UnexpectedPassError();
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
if (err instanceof UnexpectedPassError)
|
|
64
|
+
throw err; // re-throw
|
|
65
|
+
return err; // this is expected!
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Awaits passed `promise`, expects is to throw (reject), catches the expected error and returns.
|
|
70
|
+
* If error was NOT thrown - throws UnexpectedPassError instead.
|
|
71
|
+
*/
|
|
72
|
+
export async function pExpectedError(promise) {
|
|
73
|
+
try {
|
|
74
|
+
await promise;
|
|
75
|
+
// Unexpected!
|
|
76
|
+
throw new UnexpectedPassError();
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
if (err instanceof UnexpectedPassError)
|
|
80
|
+
throw err; // re-throw
|
|
81
|
+
return err; // this is expected!
|
|
82
|
+
}
|
|
83
|
+
}
|
package/dist-esm/index.js
CHANGED
|
@@ -3,24 +3,24 @@ export * from './lazy';
|
|
|
3
3
|
export * from './string/url.util';
|
|
4
4
|
export * from './array/range';
|
|
5
5
|
import { _createPromiseDecorator, } from './decorators/createPromiseDecorator';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
6
|
+
export * from './decorators/debounce';
|
|
7
|
+
export * from './decorators/debounce.decorator';
|
|
8
|
+
export * from './decorators/decorator.util';
|
|
9
|
+
export * from './decorators/logMethod.decorator';
|
|
10
|
+
export * from './decorators/memo.decorator';
|
|
11
|
+
export * from './decorators/memoFn';
|
|
12
|
+
export * from './decorators/retry.decorator';
|
|
13
|
+
export * from './decorators/timeout.decorator';
|
|
14
|
+
export * from './error/app.error';
|
|
15
|
+
export * from './error/assert';
|
|
16
16
|
export * from './error/error.util';
|
|
17
17
|
import { ErrorMode } from './error/errorMode';
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
export * from './error/http.error';
|
|
19
|
+
export * from './error/try';
|
|
20
20
|
import { _TryCatch, _tryCatch } from './error/tryCatch';
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
export * from './json-schema/from-data/generateJsonSchemaFromData';
|
|
22
|
+
export * from './json-schema/jsonSchema.cnst';
|
|
23
|
+
export * from './json-schema/jsonSchema.util';
|
|
24
24
|
import { jsonSchema, JsonSchemaAnyBuilder, } from './json-schema/jsonSchemaBuilder';
|
|
25
25
|
export * from './math/math.util';
|
|
26
26
|
export * from './math/sma';
|
|
@@ -40,19 +40,19 @@ import { pMap } from './promise/pMap';
|
|
|
40
40
|
export * from './promise/pProps';
|
|
41
41
|
import { pRetry } from './promise/pRetry';
|
|
42
42
|
export * from './promise/pState';
|
|
43
|
-
import { pTimeout } from './promise/pTimeout';
|
|
43
|
+
import { pTimeout, pTimeoutFn } from './promise/pTimeout';
|
|
44
44
|
export * from './promise/pTuple';
|
|
45
45
|
export * from './string/case';
|
|
46
46
|
export * from './string/json.util';
|
|
47
47
|
export * from './string/string.util';
|
|
48
48
|
import { _stringifyAny } from './string/stringifyAny';
|
|
49
|
-
|
|
49
|
+
export * from './time/time.util';
|
|
50
50
|
import { END, SKIP, _noop, _objectKeys, _passNothingPredicate, _passthroughMapper, _passthroughPredicate, _passUndefinedMapper, _stringMapEntries, _stringMapValues, } from './types';
|
|
51
|
-
|
|
51
|
+
export * from './unit/size.util';
|
|
52
52
|
import { is } from './vendor/is';
|
|
53
53
|
import { commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, commonLoggerCreate, } from './log/commonLogger';
|
|
54
|
-
|
|
54
|
+
export * from './string/safeJsonStringify';
|
|
55
55
|
import { PQueue } from './promise/pQueue';
|
|
56
56
|
export * from './seq/seq';
|
|
57
57
|
export * from './math/stack.util';
|
|
58
|
-
export { is,
|
|
58
|
+
export { is, _createPromiseDecorator, _stringMapValues, _stringMapEntries, _objectKeys, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, ErrorMode, pDefer, AggregatedError, pRetry, pTimeout, pTimeoutFn, _tryCatch, _TryCatch, _stringifyAny, jsonSchema, JsonSchemaAnyBuilder, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, commonLoggerCreate, PQueue, END, SKIP, };
|
|
@@ -1,37 +1,49 @@
|
|
|
1
|
+
import { AppError } from '../error/app.error';
|
|
2
|
+
export class TimeoutError extends AppError {
|
|
3
|
+
}
|
|
1
4
|
/**
|
|
2
5
|
* Decorates a Function with a timeout.
|
|
3
6
|
* Throws an Error if the Function is not resolved in a certain time.
|
|
4
7
|
* If the Function rejects - passes this rejection further.
|
|
5
8
|
*/
|
|
6
|
-
export function
|
|
7
|
-
|
|
9
|
+
export function pTimeoutFn(fn, opt) {
|
|
10
|
+
opt.name || (opt.name = fn.name);
|
|
11
|
+
return async function pTimeoutInternalFn(...args) {
|
|
12
|
+
return await pTimeout(fn.apply(this, args), opt);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Decorates a Function with a timeout and immediately calls it.
|
|
17
|
+
* Throws an Error if the Function is not resolved in a certain time.
|
|
18
|
+
* If the Function rejects - passes this rejection further.
|
|
19
|
+
*/
|
|
20
|
+
export async function pTimeout(promise, opt) {
|
|
21
|
+
// todo: check how we can automatically infer function name (only applicable to named functions)
|
|
8
22
|
const { timeout, name, onTimeout } = opt;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
resolve(onTimeout());
|
|
17
|
-
}
|
|
18
|
-
catch (err) {
|
|
19
|
-
reject(err);
|
|
20
|
-
}
|
|
21
|
-
return;
|
|
23
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
24
|
+
return await new Promise(async (resolve, reject) => {
|
|
25
|
+
// Prepare the timeout timer
|
|
26
|
+
const timer = setTimeout(() => {
|
|
27
|
+
if (onTimeout) {
|
|
28
|
+
try {
|
|
29
|
+
resolve(onTimeout());
|
|
22
30
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
resolve(await fn.apply(this, args));
|
|
28
|
-
}
|
|
29
|
-
catch (err) {
|
|
30
|
-
reject(err);
|
|
31
|
-
}
|
|
32
|
-
finally {
|
|
33
|
-
clearTimeout(timer);
|
|
31
|
+
catch (err) {
|
|
32
|
+
reject(err);
|
|
33
|
+
}
|
|
34
|
+
return;
|
|
34
35
|
}
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
reject(new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`));
|
|
37
|
+
}, timeout);
|
|
38
|
+
// Execute the Function
|
|
39
|
+
try {
|
|
40
|
+
resolve(await promise);
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
reject(err);
|
|
44
|
+
}
|
|
45
|
+
finally {
|
|
46
|
+
clearTimeout(timer);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
37
49
|
}
|
package/dist-esm/seq/seq.js
CHANGED
|
@@ -127,6 +127,16 @@ export class Sequence {
|
|
|
127
127
|
a.push(v);
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
|
+
forEach(fn) {
|
|
131
|
+
let i = -1;
|
|
132
|
+
// eslint-disable-next-line no-constant-condition
|
|
133
|
+
while (true) {
|
|
134
|
+
const v = this.next();
|
|
135
|
+
if (v === END)
|
|
136
|
+
return;
|
|
137
|
+
fn(v, ++i);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
130
140
|
}
|
|
131
141
|
/**
|
|
132
142
|
* Convenience function to create a Sequence.
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export function _gb(b) {
|
|
2
|
-
return Math.round(b /
|
|
2
|
+
return Math.round(b / 1024 ** 3);
|
|
3
3
|
}
|
|
4
4
|
export function _mb(b) {
|
|
5
|
-
return Math.round(b /
|
|
5
|
+
return Math.round(b / 1024 ** 2);
|
|
6
6
|
}
|
|
7
7
|
export function _kb(b) {
|
|
8
8
|
return Math.round(b / 1024);
|
|
@@ -11,11 +11,33 @@ export function _kb(b) {
|
|
|
11
11
|
* Byte size to Human byte size string
|
|
12
12
|
*/
|
|
13
13
|
export function _hb(b = 0) {
|
|
14
|
-
if (b <
|
|
15
|
-
return `${Math.round(b)} byte
|
|
16
|
-
if (b <
|
|
17
|
-
return `${
|
|
18
|
-
if (b <
|
|
19
|
-
return `${
|
|
20
|
-
|
|
14
|
+
if (b < 1024)
|
|
15
|
+
return `${Math.round(b)} byte`;
|
|
16
|
+
if (b < 1024 ** 2)
|
|
17
|
+
return `${(b / 1024).toPrecision(3)} Kb`;
|
|
18
|
+
if (b < 1024 ** 3)
|
|
19
|
+
return `${(b / 1024 ** 2).toPrecision(3)} Mb`;
|
|
20
|
+
if (b < 1024 ** 4)
|
|
21
|
+
return `${(b / 1024 ** 3).toPrecision(3)} Gb`;
|
|
22
|
+
if (b < 1024 ** 5)
|
|
23
|
+
return `${(b / 1024 ** 4).toPrecision(3)} Tb`;
|
|
24
|
+
return `${Math.round(b / 1024 ** 4)} Tb`;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* hc stands for "human count", similar to "human bytes" `_hb` function.
|
|
28
|
+
* Helpful to print big numbers, as it adds `K` (kilo), `M` (mega), etc to make
|
|
29
|
+
* them more readable.
|
|
30
|
+
*/
|
|
31
|
+
export function _hc(c = 0) {
|
|
32
|
+
if (c < 10 ** 4)
|
|
33
|
+
return String(c);
|
|
34
|
+
if (c < 10 ** 6)
|
|
35
|
+
return (c / 10 ** 3).toPrecision(3) + ' K';
|
|
36
|
+
if (c < 10 ** 9)
|
|
37
|
+
return (c / 10 ** 6).toPrecision(3) + ' M'; // million
|
|
38
|
+
if (c < 10 ** 12)
|
|
39
|
+
return (c / 10 ** 9).toPrecision(3) + ' B'; // billion
|
|
40
|
+
if (c < 10 ** 15)
|
|
41
|
+
return (c / 10 ** 12).toPrecision(3) + ' T'; // trillion
|
|
42
|
+
return Math.round(c / 10 ** 12) + ' T';
|
|
21
43
|
}
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SimpleMovingAverage, _stringifyAny, CommonLogger } from '..'
|
|
1
|
+
import { SimpleMovingAverage, _stringifyAny, CommonLogger, _assert } from '..'
|
|
2
2
|
import { _ms } from '../time/time.util'
|
|
3
3
|
import { _getArgsSignature, _getMethodSignature } from './decorator.util'
|
|
4
4
|
|
|
@@ -69,9 +69,7 @@ export interface LogMethodOptions {
|
|
|
69
69
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
70
70
|
export function _LogMethod(opt: LogMethodOptions = {}): MethodDecorator {
|
|
71
71
|
return (target, key, descriptor) => {
|
|
72
|
-
|
|
73
|
-
throw new TypeError('@_LogMethod can be applied only to methods')
|
|
74
|
-
}
|
|
72
|
+
_assert(typeof descriptor.value === 'function', '@_LogMethod can be applied only to methods')
|
|
75
73
|
|
|
76
74
|
const originalFn = descriptor.value
|
|
77
75
|
const keyStr = String(key)
|
|
@@ -1,10 +1,21 @@
|
|
|
1
|
+
import { _assert } from '../error/assert'
|
|
1
2
|
import { pTimeout, PTimeoutOptions } from '../promise/pTimeout'
|
|
3
|
+
import { _getMethodSignature } from './decorator.util'
|
|
2
4
|
|
|
3
5
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
4
6
|
export function _Timeout(opt: PTimeoutOptions): MethodDecorator {
|
|
5
7
|
return (target, key, descriptor) => {
|
|
8
|
+
_assert(typeof descriptor.value === 'function', '@_Timeout can be applied only to methods')
|
|
9
|
+
|
|
6
10
|
const originalFn = descriptor.value
|
|
7
|
-
|
|
11
|
+
const keyStr = String(key)
|
|
12
|
+
|
|
13
|
+
descriptor.value = async function (this: typeof target, ...args: any[]) {
|
|
14
|
+
const ctx = this
|
|
15
|
+
opt.name ||= _getMethodSignature(ctx, keyStr)
|
|
16
|
+
return await pTimeout(originalFn.apply(this, args), opt)
|
|
17
|
+
} as any
|
|
18
|
+
|
|
8
19
|
return descriptor
|
|
9
20
|
}
|
|
10
21
|
}
|
package/src/error/try.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { AnyFunction } from '../types'
|
|
2
|
+
import { AppError } from './app.error'
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Calls a function, returns a Tuple of [error, value].
|
|
3
6
|
* Allows to write shorter code that avoids `try/catch`.
|
|
@@ -42,3 +45,44 @@ export async function pTry<ERR = unknown, RETURN = void>(
|
|
|
42
45
|
return [err as ERR, undefined as any]
|
|
43
46
|
}
|
|
44
47
|
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* It is thrown when Error was expected, but didn't happen
|
|
51
|
+
* ("pass" happened instead).
|
|
52
|
+
* "Pass" means "no error".
|
|
53
|
+
*/
|
|
54
|
+
export class UnexpectedPassError extends AppError {
|
|
55
|
+
constructor() {
|
|
56
|
+
super('_expectedError passed unexpectedly')
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Calls `fn`, expects is to throw, catches the expected error and returns.
|
|
62
|
+
* If error was NOT thrown - throws UnexpectedPassError instead.
|
|
63
|
+
*/
|
|
64
|
+
export function _expectedError<ERR = Error>(fn: AnyFunction): ERR {
|
|
65
|
+
try {
|
|
66
|
+
fn()
|
|
67
|
+
// Unexpected!
|
|
68
|
+
throw new UnexpectedPassError()
|
|
69
|
+
} catch (err) {
|
|
70
|
+
if (err instanceof UnexpectedPassError) throw err // re-throw
|
|
71
|
+
return err as ERR // this is expected!
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Awaits passed `promise`, expects is to throw (reject), catches the expected error and returns.
|
|
77
|
+
* If error was NOT thrown - throws UnexpectedPassError instead.
|
|
78
|
+
*/
|
|
79
|
+
export async function pExpectedError<ERR = Error>(promise: Promise<any>): Promise<ERR> {
|
|
80
|
+
try {
|
|
81
|
+
await promise
|
|
82
|
+
// Unexpected!
|
|
83
|
+
throw new UnexpectedPassError()
|
|
84
|
+
} catch (err) {
|
|
85
|
+
if (err instanceof UnexpectedPassError) throw err // re-throw
|
|
86
|
+
return err as ERR // this is expected!
|
|
87
|
+
}
|
|
88
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -7,26 +7,17 @@ import {
|
|
|
7
7
|
PromiseDecoratorResp,
|
|
8
8
|
_createPromiseDecorator,
|
|
9
9
|
} from './decorators/createPromiseDecorator'
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
export * from './decorators/debounce'
|
|
11
|
+
export * from './decorators/debounce.decorator'
|
|
12
|
+
export * from './decorators/decorator.util'
|
|
13
|
+
export * from './decorators/logMethod.decorator'
|
|
14
|
+
export * from './decorators/memo.decorator'
|
|
15
15
|
import { MemoCache } from './decorators/memo.util'
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
AssertionError,
|
|
22
|
-
_assert,
|
|
23
|
-
_assertDeepEquals,
|
|
24
|
-
_assertEquals,
|
|
25
|
-
_assertIsError,
|
|
26
|
-
_assertIsNumber,
|
|
27
|
-
_assertIsString,
|
|
28
|
-
_assertTypeOf,
|
|
29
|
-
} from './error/assert'
|
|
16
|
+
export * from './decorators/memoFn'
|
|
17
|
+
export * from './decorators/retry.decorator'
|
|
18
|
+
export * from './decorators/timeout.decorator'
|
|
19
|
+
export * from './error/app.error'
|
|
20
|
+
export * from './error/assert'
|
|
30
21
|
import {
|
|
31
22
|
Admin401ErrorData,
|
|
32
23
|
Admin403ErrorData,
|
|
@@ -37,11 +28,11 @@ import {
|
|
|
37
28
|
} from './error/error.model'
|
|
38
29
|
export * from './error/error.util'
|
|
39
30
|
import { ErrorMode } from './error/errorMode'
|
|
40
|
-
|
|
41
|
-
|
|
31
|
+
export * from './error/http.error'
|
|
32
|
+
export * from './error/try'
|
|
42
33
|
import { TryCatchOptions, _TryCatch, _tryCatch } from './error/tryCatch'
|
|
43
|
-
|
|
44
|
-
|
|
34
|
+
export * from './json-schema/from-data/generateJsonSchemaFromData'
|
|
35
|
+
export * from './json-schema/jsonSchema.cnst'
|
|
45
36
|
import {
|
|
46
37
|
JsonSchema,
|
|
47
38
|
JsonSchemaAllOf,
|
|
@@ -61,7 +52,7 @@ import {
|
|
|
61
52
|
JsonSchemaString,
|
|
62
53
|
JsonSchemaTuple,
|
|
63
54
|
} from './json-schema/jsonSchema.model'
|
|
64
|
-
|
|
55
|
+
export * from './json-schema/jsonSchema.util'
|
|
65
56
|
import {
|
|
66
57
|
jsonSchema,
|
|
67
58
|
JsonSchemaAnyBuilder,
|
|
@@ -85,13 +76,13 @@ import { pMap, PMapOptions } from './promise/pMap'
|
|
|
85
76
|
export * from './promise/pProps'
|
|
86
77
|
import { pRetry, PRetryOptions } from './promise/pRetry'
|
|
87
78
|
export * from './promise/pState'
|
|
88
|
-
import { pTimeout, PTimeoutOptions } from './promise/pTimeout'
|
|
79
|
+
import { pTimeout, pTimeoutFn, PTimeoutOptions } from './promise/pTimeout'
|
|
89
80
|
export * from './promise/pTuple'
|
|
90
81
|
export * from './string/case'
|
|
91
82
|
export * from './string/json.util'
|
|
92
83
|
export * from './string/string.util'
|
|
93
84
|
import { JsonStringifyFunction, StringifyAnyOptions, _stringifyAny } from './string/stringifyAny'
|
|
94
|
-
|
|
85
|
+
export * from './time/time.util'
|
|
95
86
|
import {
|
|
96
87
|
Class,
|
|
97
88
|
ConditionalExcept,
|
|
@@ -144,7 +135,7 @@ import {
|
|
|
144
135
|
_stringMapEntries,
|
|
145
136
|
_stringMapValues,
|
|
146
137
|
} from './types'
|
|
147
|
-
|
|
138
|
+
export * from './unit/size.util'
|
|
148
139
|
import { is } from './vendor/is'
|
|
149
140
|
import {
|
|
150
141
|
CommonLogLevel,
|
|
@@ -158,7 +149,7 @@ import {
|
|
|
158
149
|
CommonLogWithLevelFunction,
|
|
159
150
|
commonLoggerCreate,
|
|
160
151
|
} from './log/commonLogger'
|
|
161
|
-
|
|
152
|
+
export * from './string/safeJsonStringify'
|
|
162
153
|
import { PQueue, PQueueCfg } from './promise/pQueue'
|
|
163
154
|
export * from './seq/seq'
|
|
164
155
|
export * from './math/stack.util'
|
|
@@ -245,28 +236,10 @@ export type {
|
|
|
245
236
|
|
|
246
237
|
export {
|
|
247
238
|
is,
|
|
248
|
-
_Memo,
|
|
249
|
-
_memoFn,
|
|
250
|
-
_LogMethod,
|
|
251
|
-
_getArgsSignature,
|
|
252
239
|
_createPromiseDecorator,
|
|
253
|
-
AppError,
|
|
254
|
-
HttpError,
|
|
255
|
-
AssertionError,
|
|
256
|
-
_assert,
|
|
257
|
-
_assertEquals,
|
|
258
|
-
_assertDeepEquals,
|
|
259
|
-
_assertIsError,
|
|
260
|
-
_assertIsString,
|
|
261
|
-
_assertIsNumber,
|
|
262
|
-
_assertTypeOf,
|
|
263
240
|
_stringMapValues,
|
|
264
241
|
_stringMapEntries,
|
|
265
242
|
_objectKeys,
|
|
266
|
-
_debounce,
|
|
267
|
-
_throttle,
|
|
268
|
-
_Debounce,
|
|
269
|
-
_Throttle,
|
|
270
243
|
pMap,
|
|
271
244
|
_passthroughMapper,
|
|
272
245
|
_passUndefinedMapper,
|
|
@@ -278,31 +251,18 @@ export {
|
|
|
278
251
|
AggregatedError,
|
|
279
252
|
pRetry,
|
|
280
253
|
pTimeout,
|
|
281
|
-
|
|
282
|
-
_Timeout,
|
|
254
|
+
pTimeoutFn,
|
|
283
255
|
_tryCatch,
|
|
284
256
|
_TryCatch,
|
|
285
|
-
_try,
|
|
286
|
-
pTry,
|
|
287
257
|
_stringifyAny,
|
|
288
|
-
_ms,
|
|
289
|
-
_since,
|
|
290
|
-
_hb,
|
|
291
|
-
_gb,
|
|
292
|
-
_mb,
|
|
293
|
-
_kb,
|
|
294
|
-
mergeJsonSchemaObjects,
|
|
295
258
|
jsonSchema,
|
|
296
259
|
JsonSchemaAnyBuilder,
|
|
297
|
-
JSON_SCHEMA_ORDER,
|
|
298
|
-
generateJsonSchemaFromData,
|
|
299
260
|
commonLoggerMinLevel,
|
|
300
261
|
commonLoggerNoop,
|
|
301
262
|
commonLogLevelNumber,
|
|
302
263
|
commonLoggerPipe,
|
|
303
264
|
commonLoggerPrefix,
|
|
304
265
|
commonLoggerCreate,
|
|
305
|
-
_safeJsonStringify,
|
|
306
266
|
PQueue,
|
|
307
267
|
END,
|
|
308
268
|
SKIP,
|
package/src/promise/pTimeout.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { AppError } from '../error/app.error'
|
|
1
2
|
import { AnyFunction } from '../types'
|
|
2
3
|
|
|
4
|
+
export class TimeoutError extends AppError {}
|
|
5
|
+
|
|
3
6
|
export interface PTimeoutOptions {
|
|
4
7
|
/**
|
|
5
8
|
* Timeout in milliseconds.
|
|
@@ -24,37 +27,46 @@ export interface PTimeoutOptions {
|
|
|
24
27
|
* Throws an Error if the Function is not resolved in a certain time.
|
|
25
28
|
* If the Function rejects - passes this rejection further.
|
|
26
29
|
*/
|
|
27
|
-
export function
|
|
28
|
-
|
|
30
|
+
export function pTimeoutFn<T extends AnyFunction>(fn: T, opt: PTimeoutOptions): T {
|
|
31
|
+
opt.name ||= fn.name
|
|
32
|
+
|
|
33
|
+
return async function pTimeoutInternalFn(this: any, ...args: any[]) {
|
|
34
|
+
return await pTimeout(fn.apply(this, args), opt)
|
|
35
|
+
} as any
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Decorates a Function with a timeout and immediately calls it.
|
|
40
|
+
* Throws an Error if the Function is not resolved in a certain time.
|
|
41
|
+
* If the Function rejects - passes this rejection further.
|
|
42
|
+
*/
|
|
43
|
+
export async function pTimeout<T>(promise: Promise<T>, opt: PTimeoutOptions): Promise<T> {
|
|
44
|
+
// todo: check how we can automatically infer function name (only applicable to named functions)
|
|
29
45
|
const { timeout, name, onTimeout } = opt
|
|
30
46
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
reject(err)
|
|
41
|
-
}
|
|
42
|
-
return
|
|
47
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
48
|
+
return await new Promise(async (resolve, reject) => {
|
|
49
|
+
// Prepare the timeout timer
|
|
50
|
+
const timer = setTimeout(() => {
|
|
51
|
+
if (onTimeout) {
|
|
52
|
+
try {
|
|
53
|
+
resolve(onTimeout())
|
|
54
|
+
} catch (err) {
|
|
55
|
+
reject(err)
|
|
43
56
|
}
|
|
44
|
-
|
|
45
|
-
reject(
|
|
46
|
-
new Error(`"${name || fn.name || 'pTimeout function'}" timed out after ${timeout} ms`),
|
|
47
|
-
)
|
|
48
|
-
}, timeout)
|
|
49
|
-
|
|
50
|
-
// Execute the Function
|
|
51
|
-
try {
|
|
52
|
-
resolve(await fn.apply(this, args))
|
|
53
|
-
} catch (err) {
|
|
54
|
-
reject(err)
|
|
55
|
-
} finally {
|
|
56
|
-
clearTimeout(timer)
|
|
57
|
+
return
|
|
57
58
|
}
|
|
58
|
-
|
|
59
|
-
|
|
59
|
+
|
|
60
|
+
reject(new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`))
|
|
61
|
+
}, timeout)
|
|
62
|
+
|
|
63
|
+
// Execute the Function
|
|
64
|
+
try {
|
|
65
|
+
resolve(await promise)
|
|
66
|
+
} catch (err) {
|
|
67
|
+
reject(err)
|
|
68
|
+
} finally {
|
|
69
|
+
clearTimeout(timer)
|
|
70
|
+
}
|
|
71
|
+
})
|
|
60
72
|
}
|
package/src/seq/seq.ts
CHANGED
|
@@ -140,6 +140,16 @@ export class Sequence<T> implements Iterable<T> {
|
|
|
140
140
|
a.push(v)
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
|
+
|
|
144
|
+
forEach(fn: (v: T, i: number) => void): void {
|
|
145
|
+
let i = -1
|
|
146
|
+
// eslint-disable-next-line no-constant-condition
|
|
147
|
+
while (true) {
|
|
148
|
+
const v = this.next()
|
|
149
|
+
if (v === END) return
|
|
150
|
+
fn(v, ++i)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
143
153
|
}
|
|
144
154
|
|
|
145
155
|
/**
|
package/src/unit/size.util.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export function _gb(b: number): number {
|
|
2
|
-
return Math.round(b /
|
|
2
|
+
return Math.round(b / 1024 ** 3)
|
|
3
3
|
}
|
|
4
4
|
|
|
5
5
|
export function _mb(b: number): number {
|
|
6
|
-
return Math.round(b /
|
|
6
|
+
return Math.round(b / 1024 ** 2)
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export function _kb(b: number): number {
|
|
@@ -14,8 +14,24 @@ export function _kb(b: number): number {
|
|
|
14
14
|
* Byte size to Human byte size string
|
|
15
15
|
*/
|
|
16
16
|
export function _hb(b = 0): string {
|
|
17
|
-
if (b <
|
|
18
|
-
if (b <
|
|
19
|
-
if (b <
|
|
20
|
-
return `${
|
|
17
|
+
if (b < 1024) return `${Math.round(b)} byte`
|
|
18
|
+
if (b < 1024 ** 2) return `${(b / 1024).toPrecision(3)} Kb`
|
|
19
|
+
if (b < 1024 ** 3) return `${(b / 1024 ** 2).toPrecision(3)} Mb`
|
|
20
|
+
if (b < 1024 ** 4) return `${(b / 1024 ** 3).toPrecision(3)} Gb`
|
|
21
|
+
if (b < 1024 ** 5) return `${(b / 1024 ** 4).toPrecision(3)} Tb`
|
|
22
|
+
return `${Math.round(b / 1024 ** 4)} Tb`
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* hc stands for "human count", similar to "human bytes" `_hb` function.
|
|
27
|
+
* Helpful to print big numbers, as it adds `K` (kilo), `M` (mega), etc to make
|
|
28
|
+
* them more readable.
|
|
29
|
+
*/
|
|
30
|
+
export function _hc(c = 0): string {
|
|
31
|
+
if (c < 10 ** 4) return String(c)
|
|
32
|
+
if (c < 10 ** 6) return (c / 10 ** 3).toPrecision(3) + ' K'
|
|
33
|
+
if (c < 10 ** 9) return (c / 10 ** 6).toPrecision(3) + ' M' // million
|
|
34
|
+
if (c < 10 ** 12) return (c / 10 ** 9).toPrecision(3) + ' B' // billion
|
|
35
|
+
if (c < 10 ** 15) return (c / 10 ** 12).toPrecision(3) + ' T' // trillion
|
|
36
|
+
return Math.round(c / 10 ** 12) + ' T'
|
|
21
37
|
}
|