@zajno/common 1.4.16 → 1.4.19
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/coverage/clover.xml +49 -35
- package/coverage/coverage-final.json +4 -3
- package/coverage/lcov-report/index.html +25 -25
- package/coverage/lcov-report/src/__tests__/helpers/index.html +1 -1
- package/coverage/lcov-report/src/__tests__/helpers/main.ts.html +1 -1
- package/coverage/lcov-report/src/async/arrays.ts.html +1 -1
- package/coverage/lcov-report/src/async/index.html +1 -1
- package/coverage/lcov-report/src/async/timeout.ts.html +12 -12
- package/coverage/lcov-report/src/cache.ts.html +1 -1
- package/coverage/lcov-report/src/dates/calc.ts.html +1 -1
- package/coverage/lcov-report/src/dates/convert.ts.html +1 -1
- package/coverage/lcov-report/src/dates/datex.ts.html +1 -1
- package/coverage/lcov-report/src/dates/format.ts.html +1 -1
- package/coverage/lcov-report/src/dates/index.html +1 -1
- package/coverage/lcov-report/src/dates/index.ts.html +1 -1
- package/coverage/lcov-report/src/dates/parse.ts.html +1 -1
- package/coverage/lcov-report/src/dates/period.ts.html +1 -1
- package/coverage/lcov-report/src/dates/shift.ts.html +1 -1
- package/coverage/lcov-report/src/dates/types.ts.html +1 -1
- package/coverage/lcov-report/src/dates/yearDate.ts.html +1 -1
- package/coverage/lcov-report/src/disposer.ts.html +1 -1
- package/coverage/lcov-report/src/enumHelper.ts.html +1 -1
- package/coverage/lcov-report/src/event.ts.html +1 -1
- package/coverage/lcov-report/src/fields/index.html +1 -1
- package/coverage/lcov-report/src/fields/update.ts.html +1 -1
- package/coverage/lcov-report/src/functions.ts.html +1 -1
- package/coverage/lcov-report/src/index.html +24 -9
- package/coverage/lcov-report/src/lazy.light.ts.html +1 -1
- package/coverage/lcov-report/src/logger/batch.ts.html +1 -1
- package/coverage/lcov-report/src/logger/console.ts.html +1 -1
- package/coverage/lcov-report/src/logger/index.html +1 -1
- package/coverage/lcov-report/src/logger/index.ts.html +1 -1
- package/coverage/lcov-report/src/logger/named.ts.html +1 -1
- package/coverage/lcov-report/src/logger/proxy.ts.html +1 -1
- package/coverage/lcov-report/src/math/arrays.ts.html +1 -1
- package/coverage/lcov-report/src/math/calc.ts.html +1 -1
- package/coverage/lcov-report/src/math/distribution.ts.html +1 -1
- package/coverage/lcov-report/src/math/index.html +1 -1
- package/coverage/lcov-report/src/math/index.ts.html +1 -1
- package/coverage/lcov-report/src/observersMap.ts.html +1 -1
- package/coverage/lcov-report/src/observingCache.ts.html +1 -1
- package/coverage/lcov-report/src/tempoCache.ts.html +134 -0
- package/coverage/lcov-report/src/transitionObserver.ts.html +1 -1
- package/coverage/lcov-report/src/types.ts.html +1 -1
- package/coverage/lcov-report/src/validation/ValidationErrors.ts.html +1 -1
- package/coverage/lcov-report/src/validation/creditCard.ts.html +1 -1
- package/coverage/lcov-report/src/validation/helpers.ts.html +32 -38
- package/coverage/lcov-report/src/validation/index.html +14 -14
- package/coverage/lcov-report/src/validation/index.ts.html +1 -1
- package/coverage/lcov-report/src/validation/types.ts.html +13 -4
- package/coverage/lcov-report/src/validation/validators.ts.html +1 -1
- package/coverage/lcov-report/src/validation/wrappers.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/CommonModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/FlagModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/LoadingModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/MultiSelectModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/NumberModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/SelectModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/TextModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/Validatable.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/index.html +1 -1
- package/coverage/lcov-report/src/viewModels/wrappers.ts.html +1 -1
- package/coverage/lcov-report/tempoCache.ts.html +134 -0
- package/coverage/lcov-report/timer.ts.html +200 -0
- package/coverage/lcov.info +82 -49
- package/lib/logger/file.d.ts +9 -8
- package/lib/logger/file.d.ts.map +1 -1
- package/lib/logger/file.js +44 -22
- package/lib/logger/file.js.map +1 -1
- package/lib/services/storage/web/sessionStorage.js +1 -1
- package/lib/services/storage/web/sessionStorage.js.map +1 -1
- package/lib/tempoCache.d.ts +10 -0
- package/lib/tempoCache.d.ts.map +1 -0
- package/lib/tempoCache.js +21 -0
- package/lib/tempoCache.js.map +1 -0
- package/lib/timeHelper.d.ts +0 -7
- package/lib/timeHelper.d.ts.map +1 -1
- package/lib/timeHelper.js +3 -19
- package/lib/timeHelper.js.map +1 -1
- package/lib/timer.d.ts +15 -0
- package/lib/timer.d.ts.map +1 -0
- package/lib/timer.js +36 -0
- package/lib/timer.js.map +1 -0
- package/lib/validation/helpers.d.ts +2 -6
- package/lib/validation/helpers.d.ts.map +1 -1
- package/lib/validation/helpers.js +6 -11
- package/lib/validation/helpers.js.map +1 -1
- package/lib/validation/types.d.ts +8 -2
- package/lib/validation/types.d.ts.map +1 -1
- package/lib/validation/types.js.map +1 -1
- package/lib/validation/wrappers.d.ts +3 -3
- package/package.json +1 -1
- package/src/__tests__/tempoCache.test.ts +31 -0
- package/src/logger/file.ts +46 -26
- package/src/services/storage/web/sessionStorage.ts +1 -1
- package/src/tempoCache.ts +18 -0
- package/src/timeHelper.ts +3 -18
- package/src/timer.ts +40 -0
- package/src/validation/helpers.ts +16 -18
- package/src/validation/types.ts +5 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sessionStorage.js","sourceRoot":"","sources":["../../../../src/services/storage/web/sessionStorage.ts"],"names":[],"mappings":";;;AAAA,0BAA0E;AAE1E,6CAA0C;AAE1C,mBAAmB;AAEnB,IAAI,OAAO,MAAM,KAAK,SAAS,EAAE;IAC7B,MAAM,IAAI,KAAK,CAAC,2DAA2D,CAAC,CAAC;CAChF;AAED,MAAM,iBAAkB,SAAQ,uBAAU;IACtC,IAAc,OAAO,KAAK,OAAO,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"sessionStorage.js","sourceRoot":"","sources":["../../../../src/services/storage/web/sessionStorage.ts"],"names":[],"mappings":";;;AAAA,0BAA0E;AAE1E,6CAA0C;AAE1C,mBAAmB;AAEnB,IAAI,OAAO,MAAM,KAAK,SAAS,EAAE;IAC7B,MAAM,IAAI,KAAK,CAAC,2DAA2D,CAAC,CAAC;CAChF;AAED,MAAM,iBAAkB,SAAQ,uBAAU;IACtC,IAAc,OAAO,KAAK,OAAO,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC;CAC5D;AAEY,QAAA,cAAc,GAAiB,IAAI,iBAAiB,EAAE,CAAC;AAEvD,QAAA,mBAAmB,GAAa,IAAI,uBAAmB,CAAC,sBAAc,CAAC,CAAC;AAErF,MAAa,iBAAkB,SAAQ,cAAU;IAC7C,YAAqB,GAAW;QAC5B,KAAK,CAAC,sBAAc,EAAE,GAAG,CAAC,CAAC;QADV,QAAG,GAAH,GAAG,CAAQ;IAEhC,CAAC;CACJ;AAJD,8CAIC;AAED,MAAa,0BAA8B,SAAQ,uBAAsB;IACrE,YACI,GAAW,EACX,KAAwB,EACxB,MAAyB;QAEzB,KAAK,CAAC,sBAAc,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;IAC9C,CAAC;CACJ;AARD,gEAQC;AAED,kBAAe,sBAAc,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class TempoCache<T> {
|
|
2
|
+
readonly factory: () => T;
|
|
3
|
+
readonly lifetimeMs: number;
|
|
4
|
+
private _expiringAt;
|
|
5
|
+
private _current;
|
|
6
|
+
constructor(factory: () => T, lifetimeMs: number);
|
|
7
|
+
get isExpired(): boolean;
|
|
8
|
+
get current(): T;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=tempoCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tempoCache.d.ts","sourceRoot":"","sources":["../src/tempoCache.ts"],"names":[],"mappings":"AACA,qBAAa,UAAU,CAAC,CAAC;IAKT,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAAE,QAAQ,CAAC,UAAU,EAAE,MAAM;IAHlE,OAAO,CAAC,WAAW,CAAa;IAChC,OAAO,CAAC,QAAQ,CAAgB;gBAEX,OAAO,EAAE,MAAM,CAAC,EAAW,UAAU,EAAE,MAAM;IAElE,IAAW,SAAS,YAA6C;IAEjE,IAAW,OAAO,MAMjB;CACJ"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TempoCache = void 0;
|
|
4
|
+
class TempoCache {
|
|
5
|
+
constructor(factory, lifetimeMs) {
|
|
6
|
+
this.factory = factory;
|
|
7
|
+
this.lifetimeMs = lifetimeMs;
|
|
8
|
+
this._expiringAt = 0; // already expired
|
|
9
|
+
this._current = undefined;
|
|
10
|
+
}
|
|
11
|
+
get isExpired() { return Date.now() >= this._expiringAt; }
|
|
12
|
+
get current() {
|
|
13
|
+
if (this.isExpired) {
|
|
14
|
+
this._current = this.factory();
|
|
15
|
+
this._expiringAt = Date.now() + this.lifetimeMs;
|
|
16
|
+
}
|
|
17
|
+
return this._current;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.TempoCache = TempoCache;
|
|
21
|
+
//# sourceMappingURL=tempoCache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tempoCache.js","sourceRoot":"","sources":["../src/tempoCache.ts"],"names":[],"mappings":";;;AACA,MAAa,UAAU;IAKnB,YAAqB,OAAgB,EAAW,UAAkB;QAA7C,YAAO,GAAP,OAAO,CAAS;QAAW,eAAU,GAAV,UAAU,CAAQ;QAH1D,gBAAW,GAAW,CAAC,CAAC,CAAC,kBAAkB;QAC3C,aAAQ,GAAM,SAAS,CAAC;IAEsC,CAAC;IAEvE,IAAW,SAAS,KAAK,OAAO,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC;IAEjE,IAAW,OAAO;QACd,IAAI,IAAI,CAAC,SAAS,EAAE;YAChB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;YAC/B,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC;SACnD;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC;IACzB,CAAC;CACJ;AAhBD,gCAgBC"}
|
package/lib/timeHelper.d.ts
CHANGED
|
@@ -4,11 +4,4 @@ export declare function formatMS(ms: number): string;
|
|
|
4
4
|
export declare function formatTime(n: number): string;
|
|
5
5
|
/** @deprecated */
|
|
6
6
|
export declare function secToFormattedMin(totalSec: number): string;
|
|
7
|
-
declare class Time {
|
|
8
|
-
private _now;
|
|
9
|
-
constructor();
|
|
10
|
-
get now(): number;
|
|
11
|
-
}
|
|
12
|
-
export declare const TimeHelper: Time;
|
|
13
|
-
export {};
|
|
14
7
|
//# sourceMappingURL=timeHelper.d.ts.map
|
package/lib/timeHelper.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timeHelper.d.ts","sourceRoot":"","sources":["../src/timeHelper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"timeHelper.d.ts","sourceRoot":"","sources":["../src/timeHelper.ts"],"names":[],"mappings":"AAIA,kBAAkB;AAClB,wBAAgB,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAU3C;AAED,kBAAkB;AAClB,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAM5C;AAED,kBAAkB;AAClB,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAW1D"}
|
package/lib/timeHelper.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
exports.secToFormattedMin = exports.formatTime = exports.formatMS = void 0;
|
|
4
|
+
// eslint-disable-next-line no-console
|
|
5
|
+
console.warn('"timeHelper" module is deprecated. Please use "dates" one.');
|
|
6
6
|
/** @deprecated */
|
|
7
7
|
function formatMS(ms) {
|
|
8
8
|
if (!ms && ms !== 0) {
|
|
@@ -34,20 +34,4 @@ function secToFormattedMin(totalSec) {
|
|
|
34
34
|
return res;
|
|
35
35
|
}
|
|
36
36
|
exports.secToFormattedMin = secToFormattedMin;
|
|
37
|
-
class Time {
|
|
38
|
-
constructor() {
|
|
39
|
-
this._now = new Date().getTime();
|
|
40
|
-
mobx_1.makeObservable(this);
|
|
41
|
-
// Update _now once an hour
|
|
42
|
-
setInterval(() => { this._now = new Date().getTime(); }, 1000 * 3600);
|
|
43
|
-
}
|
|
44
|
-
get now() {
|
|
45
|
-
return this._now;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
tslib_1.__decorate([
|
|
49
|
-
mobx_1.observable,
|
|
50
|
-
tslib_1.__metadata("design:type", Number)
|
|
51
|
-
], Time.prototype, "_now", void 0);
|
|
52
|
-
exports.TimeHelper = new Time();
|
|
53
37
|
//# sourceMappingURL=timeHelper.js.map
|
package/lib/timeHelper.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timeHelper.js","sourceRoot":"","sources":["../src/timeHelper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"timeHelper.js","sourceRoot":"","sources":["../src/timeHelper.ts"],"names":[],"mappings":";;;AACA,sCAAsC;AACtC,OAAO,CAAC,IAAI,CAAC,4DAA4D,CAAC,CAAC;AAE3E,kBAAkB;AAClB,SAAgB,QAAQ,CAAC,EAAU;IAC/B,IAAI,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,EAAE;QACjB,OAAO,EAAE,CAAC;KACb;IAED,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;IACzC,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;IAChD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,IAAI,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;IAEvD,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,GAAG,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;AACpG,CAAC;AAVD,4BAUC;AAED,kBAAkB;AAClB,SAAgB,UAAU,CAAC,CAAS;IAChC,IAAI,CAAC,GAAG,EAAE,EAAE;QACR,OAAO,GAAG,GAAG,CAAC,CAAC;KAClB;IAED,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAC;AACxB,CAAC;AAND,gCAMC;AAED,kBAAkB;AAClB,SAAgB,iBAAiB,CAAC,QAAgB;IAC9C,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC;IACtC,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;IAC5C,IAAI,GAAG,GAAG,GAAG,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;IAE/B,IAAI,GAAG,EAAE;QACL,GAAG,IAAI,GAAG,CAAC;QACX,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;KACtC;IAED,OAAO,GAAG,CAAC;AACf,CAAC;AAXD,8CAWC"}
|
package/lib/timer.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { IDisposable } from './disposer';
|
|
2
|
+
import { EventHandler, IEvent } from './event';
|
|
3
|
+
export declare class Timer implements IDisposable, IEvent<number> {
|
|
4
|
+
readonly interval: number;
|
|
5
|
+
private readonly _event;
|
|
6
|
+
private _now;
|
|
7
|
+
private _handle;
|
|
8
|
+
constructor(interval?: number);
|
|
9
|
+
get now(): number;
|
|
10
|
+
on(handler: EventHandler<number>): () => void;
|
|
11
|
+
off(handler: EventHandler<number>): void;
|
|
12
|
+
restart(): void;
|
|
13
|
+
dispose(): void;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=timer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timer.d.ts","sourceRoot":"","sources":["../src/timer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAS,YAAY,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEtD,qBAAa,KAAM,YAAW,WAAW,EAAE,MAAM,CAAC,MAAM,CAAC;IAMzC,QAAQ,CAAC,QAAQ;IAL7B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuB;IAE9C,OAAO,CAAC,IAAI,CAAsB;IAClC,OAAO,CAAC,OAAO,CAAQ;gBAEF,QAAQ,SAAc;IAI3C,IAAI,GAAG,WAEN;IAED,EAAE,CAAC,OAAO,EAAE,YAAY,CAAC,MAAM,CAAC,GAAG,MAAM,IAAI;IAI7C,GAAG,CAAC,OAAO,EAAE,YAAY,CAAC,MAAM,CAAC,GAAG,IAAI;IAIjC,OAAO;IAUP,OAAO,IAAI,IAAI;CAIzB"}
|
package/lib/timer.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Timer = void 0;
|
|
4
|
+
const event_1 = require("./event");
|
|
5
|
+
class Timer {
|
|
6
|
+
constructor(interval = 3600 * 1000) {
|
|
7
|
+
this.interval = interval;
|
|
8
|
+
this._event = new event_1.Event();
|
|
9
|
+
this._now = Date.now();
|
|
10
|
+
this._handle = null;
|
|
11
|
+
this.restart();
|
|
12
|
+
}
|
|
13
|
+
get now() {
|
|
14
|
+
return this._now;
|
|
15
|
+
}
|
|
16
|
+
on(handler) {
|
|
17
|
+
return this._event.on(handler);
|
|
18
|
+
}
|
|
19
|
+
off(handler) {
|
|
20
|
+
return this._event.off(handler);
|
|
21
|
+
}
|
|
22
|
+
restart() {
|
|
23
|
+
this.dispose();
|
|
24
|
+
// Update _now once an hour
|
|
25
|
+
this._handle = setInterval(() => {
|
|
26
|
+
this._now = Date.now();
|
|
27
|
+
this._event.trigger(this._now);
|
|
28
|
+
}, this.interval);
|
|
29
|
+
}
|
|
30
|
+
dispose() {
|
|
31
|
+
clearInterval(this._handle);
|
|
32
|
+
this._handle = null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.Timer = Timer;
|
|
36
|
+
//# sourceMappingURL=timer.js.map
|
package/lib/timer.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timer.js","sourceRoot":"","sources":["../src/timer.ts"],"names":[],"mappings":";;;AACA,mCAAsD;AAEtD,MAAa,KAAK;IAMd,YAAqB,WAAW,IAAI,GAAG,IAAI;QAAtB,aAAQ,GAAR,QAAQ,CAAc;QAL1B,WAAM,GAAG,IAAI,aAAK,EAAU,CAAC;QAEtC,SAAI,GAAW,IAAI,CAAC,GAAG,EAAE,CAAC;QAC1B,YAAO,GAAG,IAAI,CAAC;QAGnB,IAAI,CAAC,OAAO,EAAE,CAAC;IACnB,CAAC;IAED,IAAI,GAAG;QACH,OAAO,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;IAED,EAAE,CAAC,OAA6B;QAC5B,OAAO,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;IAED,GAAG,CAAC,OAA6B;QAC7B,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACpC,CAAC;IAEM,OAAO;QACV,IAAI,CAAC,OAAO,EAAE,CAAC;QAEf,2BAA2B;QAC3B,IAAI,CAAC,OAAO,GAAG,WAAW,CAAC,GAAG,EAAE;YAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YACvB,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACnC,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;IACtB,CAAC;IAEM,OAAO;QACV,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC5B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;IACxB,CAAC;CACJ;AApCD,sBAoCC"}
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import { ValidatorFunction } from './types';
|
|
2
1
|
import { ValidationErrors } from './ValidationErrors';
|
|
2
|
+
import { ValidationConfig, ValidationResults, ValidatorFunction } from './types';
|
|
3
3
|
export declare function createShouldBeEqualTo<T = string>(getter: () => T): ValidatorFunction<T>;
|
|
4
4
|
export declare function throwNotOk(result: ValidationErrors, message?: string): void;
|
|
5
|
-
export declare function validateObject<T>(obj: T, validators:
|
|
6
|
-
[P in keyof T]?: ValidatorFunction<T[P]>;
|
|
7
|
-
}): {
|
|
8
|
-
[P in keyof T]?: ValidationErrors;
|
|
9
|
-
};
|
|
5
|
+
export declare function validateObject<T, TErrors = ValidationErrors>(obj: T, validators: ValidationConfig<T, TErrors>, onlyTruethy?: boolean): ValidationResults<T, TErrors>;
|
|
10
6
|
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/validation/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/validation/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EACH,gBAAgB,EAEhB,iBAAiB,EACjB,iBAAiB,EACpB,MAAM,SAAS,CAAC;AAEjB,wBAAgB,qBAAqB,CAAC,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAEvF;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,gBAAgB,EAAE,OAAO,SAAqB,QAIhF;AAED,wBAAgB,cAAc,CAAC,CAAC,EAAE,OAAO,GAAG,gBAAgB,EACxD,GAAG,EAAE,CAAC,EACN,UAAU,EAAE,gBAAgB,CAAC,CAAC,EAAE,OAAO,CAAC,EACxC,WAAW,UAAQ,GACpB,iBAAiB,CAAC,CAAC,EAAE,OAAO,CAAC,CAuB/B"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.validateObject = exports.throwNotOk = exports.createShouldBeEqualTo = void 0;
|
|
4
|
-
const types_1 = require("./types");
|
|
5
4
|
const ValidationErrors_1 = require("./ValidationErrors");
|
|
5
|
+
const types_1 = require("./types");
|
|
6
6
|
function createShouldBeEqualTo(getter) {
|
|
7
7
|
return (val) => getter() === val ? ValidationErrors_1.ValidationErrors.None : ValidationErrors_1.ValidationErrors.ShouldBeEqualTo;
|
|
8
8
|
}
|
|
@@ -13,7 +13,7 @@ function throwNotOk(result, message = 'Validation error') {
|
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
exports.throwNotOk = throwNotOk;
|
|
16
|
-
function validateObject(obj, validators) {
|
|
16
|
+
function validateObject(obj, validators, onlyTruethy = false) {
|
|
17
17
|
const res = {};
|
|
18
18
|
Object.keys(obj).forEach(k => {
|
|
19
19
|
const kk = k;
|
|
@@ -22,15 +22,10 @@ function validateObject(obj, validators) {
|
|
|
22
22
|
return;
|
|
23
23
|
}
|
|
24
24
|
const v = obj[k];
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
// if (typeof v !== 'string') {
|
|
30
|
-
// return; // skip non-strings
|
|
31
|
-
// }
|
|
32
|
-
// str = v;
|
|
33
|
-
const err = validator(v);
|
|
25
|
+
if (onlyTruethy && !v) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const err = validator(v, obj);
|
|
34
29
|
if (err) {
|
|
35
30
|
res[kk] = err;
|
|
36
31
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/validation/helpers.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/validation/helpers.ts"],"names":[],"mappings":";;;AAAA,yDAAsD;AACtD,mCAKiB;AAEjB,SAAgB,qBAAqB,CAAa,MAAe;IAC7D,OAAO,CAAC,GAAM,EAAE,EAAE,CAAC,MAAM,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC,mCAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,mCAAgB,CAAC,eAAe,CAAC;AACnG,CAAC;AAFD,sDAEC;AAED,SAAgB,UAAU,CAAC,MAAwB,EAAE,OAAO,GAAG,kBAAkB;IAC7E,IAAI,MAAM,EAAE;QACR,MAAM,IAAI,uBAAe,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;KAC9C;AACL,CAAC;AAJD,gCAIC;AAED,SAAgB,cAAc,CAC1B,GAAM,EACN,UAAwC,EACxC,WAAW,GAAG,KAAK;IAGnB,MAAM,GAAG,GAAkC,EAAG,CAAC;IAE/C,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;QACzB,MAAM,EAAE,GAAG,CAAY,CAAC;QACxB,MAAM,SAAS,GAAgD,UAAU,CAAC,EAAE,CAAC,CAAC;QAC9E,IAAI,CAAC,SAAS,EAAE;YACZ,OAAO;SACV;QAED,MAAM,CAAC,GAAG,GAAG,CAAC,CAAY,CAAC,CAAC;QAC5B,IAAI,WAAW,IAAI,CAAC,CAAC,EAAE;YACnB,OAAO;SACV;QAED,MAAM,GAAG,GAAG,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;QAC9B,IAAI,GAAG,EAAE;YACL,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC;SACjB;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,GAAG,CAAC;AACf,CAAC;AA3BD,wCA2BC"}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { ValidationErrors } from './ValidationErrors';
|
|
2
|
-
export declare type ValidatorFunction<T = string, TErrors = ValidationErrors> = (val: T) => TErrors;
|
|
3
|
-
export declare type ValidatorFunctionAsync<T = string, TErrors = ValidationErrors> = (val: T) => Promise<TErrors>;
|
|
2
|
+
export declare type ValidatorFunction<T = string, TErrors = ValidationErrors, TContext = any> = (val: T, ctx?: TContext) => TErrors;
|
|
3
|
+
export declare type ValidatorFunctionAsync<T = string, TErrors = ValidationErrors, TContext = any> = (val: T, ctx?: TContext) => Promise<TErrors>;
|
|
4
|
+
export declare type ValidationConfig<T, TErrors> = {
|
|
5
|
+
[P in keyof T]?: ValidatorFunction<T[P], TErrors, T>;
|
|
6
|
+
};
|
|
7
|
+
export declare type ValidationResults<T, TErrors> = {
|
|
8
|
+
[P in keyof T]?: TErrors;
|
|
9
|
+
};
|
|
4
10
|
export declare type WrapperFunction = (val: ValidatorFunction) => ValidatorFunction;
|
|
5
11
|
export declare class ValidationError<TErrors = ValidationErrors> extends Error {
|
|
6
12
|
readonly code: TErrors;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/validation/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEtD,oBAAY,iBAAiB,CAAC,CAAC,GAAG,MAAM,EAAE,OAAO,GAAG,gBAAgB,IAAI,CAAC,GAAG,EAAE,CAAC,KAAK,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/validation/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEtD,oBAAY,iBAAiB,CAAC,CAAC,GAAG,MAAM,EAAE,OAAO,GAAG,gBAAgB,EAAE,QAAQ,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,QAAQ,KAAK,OAAO,CAAC;AAC5H,oBAAY,sBAAsB,CAAC,CAAC,GAAG,MAAM,EAAE,OAAO,GAAG,gBAAgB,EAAE,QAAQ,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,QAAQ,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;AAE1I,oBAAY,gBAAgB,CAAC,CAAC,EAAE,OAAO,IAAI;KAAG,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;CAAE,CAAC;AACpG,oBAAY,iBAAiB,CAAC,CAAC,EAAE,OAAO,IAAI;KAAG,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,OAAO;CAAE,CAAC;AAEzE,oBAAY,eAAe,GAAG,CAAC,GAAG,EAAE,iBAAiB,KAAK,iBAAiB,CAAC;AAE5E,qBAAa,eAAe,CAAC,OAAO,GAAG,gBAAgB,CAAE,SAAQ,KAAK;IAClE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAQ;gBAElB,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO;CAI7C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/validation/types.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/validation/types.ts"],"names":[],"mappings":";;;AAUA,MAAa,eAA4C,SAAQ,KAAK;IAGlE,YAAY,OAAe,EAAE,IAAa;QACtC,KAAK,CAAC,OAAO,CAAC,CAAC;QAHV,SAAI,GAAY,IAAI,CAAC;QAI1B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACrB,CAAC;CACJ;AAPD,0CAOC"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ValidatorFunction } from './types';
|
|
2
2
|
import { ValidationErrors } from './ValidationErrors';
|
|
3
3
|
export declare const Wrappers: {
|
|
4
|
-
required<T = string>(validator: ValidatorFunction<T, ValidationErrors>): ValidatorFunction<T, ValidationErrors>;
|
|
5
|
-
notRequired<T_1 = string>(validator: ValidatorFunction<T_1, ValidationErrors>): ValidatorFunction<T_1, ValidationErrors>;
|
|
6
|
-
thrower<T_2 = string>(validator: ValidatorFunction<T_2, ValidationErrors>): (val: T_2) => void;
|
|
4
|
+
required<T = string>(validator: ValidatorFunction<T, ValidationErrors, any>): ValidatorFunction<T, ValidationErrors, any>;
|
|
5
|
+
notRequired<T_1 = string>(validator: ValidatorFunction<T_1, ValidationErrors, any>): ValidatorFunction<T_1, ValidationErrors, any>;
|
|
6
|
+
thrower<T_2 = string>(validator: ValidatorFunction<T_2, ValidationErrors, any>): (val: T_2) => void;
|
|
7
7
|
};
|
|
8
8
|
//# sourceMappingURL=wrappers.d.ts.map
|
package/package.json
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { TempoCache } from '../tempoCache';
|
|
2
|
+
import { setTimeoutAsync } from '../async/timeout';
|
|
3
|
+
|
|
4
|
+
describe('TempoCache', () => {
|
|
5
|
+
it('just works', async () => {
|
|
6
|
+
let incrementer = 1;
|
|
7
|
+
|
|
8
|
+
const example = new TempoCache(() => Promise.resolve(incrementer++), 199);
|
|
9
|
+
|
|
10
|
+
expect(await example.current).toBe(1);
|
|
11
|
+
await expect(example.current).resolves.toBe(1);
|
|
12
|
+
|
|
13
|
+
await setTimeoutAsync(100);
|
|
14
|
+
|
|
15
|
+
await expect(example.current).resolves.toBe(1);
|
|
16
|
+
|
|
17
|
+
await setTimeoutAsync(200);
|
|
18
|
+
|
|
19
|
+
await expect(example.current).resolves.toBe(2);
|
|
20
|
+
|
|
21
|
+
await setTimeoutAsync(100);
|
|
22
|
+
|
|
23
|
+
await expect(example.current).resolves.toBe(2);
|
|
24
|
+
await expect(example.current).resolves.toBe(2);
|
|
25
|
+
|
|
26
|
+
await setTimeoutAsync(300);
|
|
27
|
+
|
|
28
|
+
await expect(example.current).resolves.toBe(3);
|
|
29
|
+
await expect(example.current).resolves.toBe(3);
|
|
30
|
+
});
|
|
31
|
+
});
|
package/src/logger/file.ts
CHANGED
|
@@ -1,46 +1,66 @@
|
|
|
1
|
+
|
|
2
|
+
/* eslint-disable no-console */
|
|
3
|
+
import * as Path from 'path';
|
|
1
4
|
import * as FS from 'fs';
|
|
2
5
|
import * as Util from 'util';
|
|
6
|
+
|
|
3
7
|
import { ILogger } from './abstractions';
|
|
4
|
-
import { Getter } from '../types';
|
|
5
8
|
|
|
6
9
|
export class FileLoggerNode implements ILogger {
|
|
7
|
-
private readonly _buffer: string[];
|
|
10
|
+
private readonly _buffer: string[] = [];
|
|
11
|
+
private _logFilePath: string = null;
|
|
12
|
+
|
|
13
|
+
constructor(readonly extraLogger: ILogger = console, readonly instantFlush = false) {
|
|
14
|
+
this.setLogName('');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
setLogFilePath(path: string) {
|
|
18
|
+
this._logFilePath = path;
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
8
21
|
|
|
9
|
-
|
|
10
|
-
|
|
22
|
+
setLogName(name: string) {
|
|
23
|
+
const n = name ? `-${name}` : '';
|
|
24
|
+
this._logFilePath = Path.resolve(__dirname, `../run${n}.${new Date().toISOString()}.log`);
|
|
25
|
+
return this;
|
|
11
26
|
}
|
|
12
27
|
|
|
13
|
-
|
|
14
|
-
|
|
28
|
+
private append(..._args: any[]) {
|
|
29
|
+
// @ts-ignore
|
|
30
|
+
const str = Util.format.apply(null, arguments) + '\n';
|
|
31
|
+
this._buffer.push(str);
|
|
32
|
+
|
|
33
|
+
if (this.instantFlush) {
|
|
34
|
+
this.flush();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
15
37
|
|
|
16
38
|
log = (...args: any[]) => {
|
|
17
|
-
this.
|
|
39
|
+
this.extraLogger?.log(...args);
|
|
40
|
+
this.append(...args);
|
|
18
41
|
};
|
|
19
42
|
|
|
20
|
-
warn(...args: any[]) {
|
|
21
|
-
this.
|
|
22
|
-
|
|
43
|
+
warn = (...args: any[]) => {
|
|
44
|
+
this.extraLogger?.warn(...args);
|
|
45
|
+
this.append(...args);
|
|
46
|
+
};
|
|
23
47
|
|
|
24
|
-
error(...args: any[]) {
|
|
25
|
-
this.
|
|
26
|
-
|
|
48
|
+
error = (...args: any[]) => {
|
|
49
|
+
this.extraLogger?.error(...args);
|
|
50
|
+
this.append(...args);
|
|
51
|
+
};
|
|
27
52
|
|
|
28
|
-
|
|
29
|
-
if (!this._buffer) {
|
|
53
|
+
flush() {
|
|
54
|
+
if (!this._buffer.length) {
|
|
30
55
|
return;
|
|
31
56
|
}
|
|
32
57
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const str = Util.format.apply(null, arguments) + '\n';
|
|
40
|
-
if (this._buffer) {
|
|
41
|
-
this._buffer.push(str);
|
|
42
|
-
} else {
|
|
43
|
-
FS.appendFileSync(this.logFilePath, str);
|
|
58
|
+
try {
|
|
59
|
+
FS.appendFileSync(this._logFilePath, this._buffer.join(''));
|
|
60
|
+
} catch (err) {
|
|
61
|
+
console.warn('Failed to flush file, error', err);
|
|
62
|
+
} finally {
|
|
63
|
+
this._buffer.length = 0;
|
|
44
64
|
}
|
|
45
65
|
}
|
|
46
66
|
}
|
|
@@ -9,7 +9,7 @@ if (typeof window === undefined) {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
class WebSessionStorage extends WebStorage {
|
|
12
|
-
protected get storage() { return window.
|
|
12
|
+
protected get storage() { return window.sessionStorage; }
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export const SessionStorage: IStorageSync = new WebSessionStorage();
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
export class TempoCache<T> {
|
|
3
|
+
|
|
4
|
+
private _expiringAt: number = 0; // already expired
|
|
5
|
+
private _current: T = undefined;
|
|
6
|
+
|
|
7
|
+
constructor(readonly factory: () => T, readonly lifetimeMs: number) { }
|
|
8
|
+
|
|
9
|
+
public get isExpired() { return Date.now() >= this._expiringAt; }
|
|
10
|
+
|
|
11
|
+
public get current() {
|
|
12
|
+
if (this.isExpired) {
|
|
13
|
+
this._current = this.factory();
|
|
14
|
+
this._expiringAt = Date.now() + this.lifetimeMs;
|
|
15
|
+
}
|
|
16
|
+
return this._current;
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/timeHelper.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
// eslint-disable-next-line no-console
|
|
3
|
+
console.warn('"timeHelper" module is deprecated. Please use "dates" one.');
|
|
2
4
|
|
|
3
5
|
/** @deprecated */
|
|
4
6
|
export function formatMS(ms: number): string {
|
|
@@ -35,20 +37,3 @@ export function secToFormattedMin(totalSec: number): string {
|
|
|
35
37
|
|
|
36
38
|
return res;
|
|
37
39
|
}
|
|
38
|
-
|
|
39
|
-
class Time {
|
|
40
|
-
@observable
|
|
41
|
-
private _now: number = new Date().getTime();
|
|
42
|
-
|
|
43
|
-
constructor() {
|
|
44
|
-
makeObservable(this);
|
|
45
|
-
// Update _now once an hour
|
|
46
|
-
setInterval(() => { this._now = new Date().getTime(); }, 1000 * 3600);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
get now() {
|
|
50
|
-
return this._now;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export const TimeHelper = new Time();
|
package/src/timer.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { IDisposable } from './disposer';
|
|
2
|
+
import { Event, EventHandler, IEvent } from './event';
|
|
3
|
+
|
|
4
|
+
export class Timer implements IDisposable, IEvent<number> {
|
|
5
|
+
private readonly _event = new Event<number>();
|
|
6
|
+
|
|
7
|
+
private _now: number = Date.now();
|
|
8
|
+
private _handle = null;
|
|
9
|
+
|
|
10
|
+
constructor(readonly interval = 3600 * 1000) {
|
|
11
|
+
this.restart();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
get now() {
|
|
15
|
+
return this._now;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
on(handler: EventHandler<number>): () => void {
|
|
19
|
+
return this._event.on(handler);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
off(handler: EventHandler<number>): void {
|
|
23
|
+
return this._event.off(handler);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public restart() {
|
|
27
|
+
this.dispose();
|
|
28
|
+
|
|
29
|
+
// Update _now once an hour
|
|
30
|
+
this._handle = setInterval(() => {
|
|
31
|
+
this._now = Date.now();
|
|
32
|
+
this._event.trigger(this._now);
|
|
33
|
+
}, this.interval);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public dispose(): void {
|
|
37
|
+
clearInterval(this._handle);
|
|
38
|
+
this._handle = null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import { ValidationError, ValidatorFunction } from './types';
|
|
2
1
|
import { ValidationErrors } from './ValidationErrors';
|
|
2
|
+
import {
|
|
3
|
+
ValidationConfig,
|
|
4
|
+
ValidationError,
|
|
5
|
+
ValidationResults,
|
|
6
|
+
ValidatorFunction,
|
|
7
|
+
} from './types';
|
|
3
8
|
|
|
4
9
|
export function createShouldBeEqualTo<T = string>(getter: () => T): ValidatorFunction<T> {
|
|
5
10
|
return (val: T) => getter() === val ? ValidationErrors.None : ValidationErrors.ShouldBeEqualTo;
|
|
@@ -11,33 +16,27 @@ export function throwNotOk(result: ValidationErrors, message = 'Validation error
|
|
|
11
16
|
}
|
|
12
17
|
}
|
|
13
18
|
|
|
14
|
-
export function validateObject<T>(
|
|
19
|
+
export function validateObject<T, TErrors = ValidationErrors>(
|
|
15
20
|
obj: T,
|
|
16
|
-
validators:
|
|
17
|
-
|
|
21
|
+
validators: ValidationConfig<T, TErrors>,
|
|
22
|
+
onlyTruethy = false,
|
|
23
|
+
): ValidationResults<T, TErrors> {
|
|
18
24
|
|
|
19
|
-
const res:
|
|
25
|
+
const res: ValidationResults<T, TErrors> = { };
|
|
20
26
|
|
|
21
27
|
Object.keys(obj).forEach(k => {
|
|
22
28
|
const kk = k as keyof T;
|
|
23
|
-
const validator: ValidatorFunction<T[typeof kk]> = validators[kk];
|
|
29
|
+
const validator: ValidatorFunction<T[typeof kk], TErrors, T> = validators[kk];
|
|
24
30
|
if (!validator) {
|
|
25
31
|
return;
|
|
26
32
|
}
|
|
27
33
|
|
|
28
34
|
const v = obj[k as keyof T];
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
// }
|
|
33
|
-
|
|
34
|
-
// if (typeof v !== 'string') {
|
|
35
|
-
// return; // skip non-strings
|
|
36
|
-
// }
|
|
37
|
-
|
|
38
|
-
// str = v;
|
|
35
|
+
if (onlyTruethy && !v) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
39
38
|
|
|
40
|
-
const err = validator(v);
|
|
39
|
+
const err = validator(v, obj);
|
|
41
40
|
if (err) {
|
|
42
41
|
res[kk] = err;
|
|
43
42
|
}
|
|
@@ -45,4 +44,3 @@ export function validateObject<T>(
|
|
|
45
44
|
|
|
46
45
|
return res;
|
|
47
46
|
}
|
|
48
|
-
|
package/src/validation/types.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { ValidationErrors } from './ValidationErrors';
|
|
2
2
|
|
|
3
|
-
export type ValidatorFunction<T = string, TErrors = ValidationErrors> = (val: T) => TErrors;
|
|
4
|
-
export type ValidatorFunctionAsync<T = string, TErrors = ValidationErrors> = (val: T) => Promise<TErrors>;
|
|
3
|
+
export type ValidatorFunction<T = string, TErrors = ValidationErrors, TContext = any> = (val: T, ctx?: TContext) => TErrors;
|
|
4
|
+
export type ValidatorFunctionAsync<T = string, TErrors = ValidationErrors, TContext = any> = (val: T, ctx?: TContext) => Promise<TErrors>;
|
|
5
|
+
|
|
6
|
+
export type ValidationConfig<T, TErrors> = { [P in keyof T]?: ValidatorFunction<T[P], TErrors, T> };
|
|
7
|
+
export type ValidationResults<T, TErrors> = { [P in keyof T]?: TErrors };
|
|
5
8
|
|
|
6
9
|
export type WrapperFunction = (val: ValidatorFunction) => ValidatorFunction;
|
|
7
10
|
|