@zajno/common 1.3.13 → 1.4.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/coverage/clover.xml +66 -58
- package/coverage/coverage-final.json +6 -6
- package/coverage/lcov-report/index.html +26 -26
- 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/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/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 +15 -15
- package/coverage/lcov-report/src/lazy.light.ts.html +33 -21
- 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/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 +1 -1
- package/coverage/lcov-report/src/validation/index.html +1 -1
- package/coverage/lcov-report/src/validation/index.ts.html +1 -1
- package/coverage/lcov-report/src/validation/types.ts.html +6 -6
- 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/FlagModel.ts.html +24 -9
- package/coverage/lcov-report/src/viewModels/MultiSelectModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/NumberModel.ts.html +24 -9
- package/coverage/lcov-report/src/viewModels/SelectModel.ts.html +10 -10
- package/coverage/lcov-report/src/viewModels/Validatable.ts.html +56 -50
- package/coverage/lcov-report/src/viewModels/index.html +44 -44
- package/coverage/lcov-report/src/viewModels/wrappers.ts.html +1 -1
- package/coverage/lcov.info +137 -121
- package/lib/disposer.d.ts.map +1 -1
- package/lib/disposer.js +3 -1
- package/lib/disposer.js.map +1 -1
- package/lib/lazy.d.ts +6 -2
- package/lib/lazy.d.ts.map +1 -1
- package/lib/lazy.js +22 -12
- package/lib/lazy.js.map +1 -1
- package/lib/lazy.light.d.ts +2 -1
- package/lib/lazy.light.d.ts.map +1 -1
- package/lib/lazy.light.js +11 -9
- package/lib/lazy.light.js.map +1 -1
- package/lib/pool.d.ts +12 -4
- package/lib/pool.d.ts.map +1 -1
- package/lib/pool.js +28 -5
- package/lib/pool.js.map +1 -1
- package/lib/validation/types.d.ts +5 -5
- package/lib/validation/types.d.ts.map +1 -1
- package/lib/validation/types.js.map +1 -1
- package/lib/validation/wrappers.d.ts +4 -3
- package/lib/validation/wrappers.d.ts.map +1 -1
- package/lib/viewModels/CommonModel.d.ts +14 -0
- package/lib/viewModels/CommonModel.d.ts.map +1 -0
- package/lib/viewModels/CommonModel.js +48 -0
- package/lib/viewModels/CommonModel.js.map +1 -0
- package/lib/viewModels/FlagModel.d.ts +1 -0
- package/lib/viewModels/FlagModel.d.ts.map +1 -1
- package/lib/viewModels/FlagModel.js +8 -1
- package/lib/viewModels/FlagModel.js.map +1 -1
- package/lib/viewModels/NumberModel.d.ts +1 -0
- package/lib/viewModels/NumberModel.d.ts.map +1 -1
- package/lib/viewModels/NumberModel.js +8 -1
- package/lib/viewModels/NumberModel.js.map +1 -1
- package/lib/viewModels/TextModel.d.ts +4 -3
- package/lib/viewModels/TextModel.d.ts.map +1 -1
- package/lib/viewModels/TextModel.js +25 -24
- package/lib/viewModels/TextModel.js.map +1 -1
- package/lib/viewModels/Validatable.d.ts +8 -10
- package/lib/viewModels/Validatable.d.ts.map +1 -1
- package/lib/viewModels/Validatable.js +14 -13
- package/lib/viewModels/Validatable.js.map +1 -1
- package/lib/viewModels/index.d.ts +1 -0
- package/lib/viewModels/index.d.ts.map +1 -1
- package/lib/viewModels/index.js +1 -0
- package/lib/viewModels/index.js.map +1 -1
- package/package.json +1 -1
- package/src/disposer.ts +4 -1
- package/src/lazy.light.ts +15 -11
- package/src/lazy.ts +22 -15
- package/src/pool.ts +31 -8
- package/src/validation/types.ts +5 -5
- package/src/viewModels/CommonModel.ts +46 -0
- package/src/viewModels/FlagModel.ts +6 -1
- package/src/viewModels/NumberModel.ts +6 -1
- package/src/viewModels/TextModel.ts +20 -15
- package/src/viewModels/Validatable.ts +23 -21
- package/src/viewModels/index.ts +1 -0
- package/yarn.lock +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Validatable.d.ts","sourceRoot":"","sources":["../../src/viewModels/Validatable.ts"],"names":[],"mappings":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E5B,mBAAmB,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM,GAAG,gBAAgB,EAAE,MAAM,CAAC,EAAE,gBAAgB,CAAC,CAAC,EAAE,OAAO,CAAC;IAMvG,YAAY,CAAC,KAAK,EAAE,CAAC;IAQ5B,QAAQ;IAyBR,YAAY;IAMlB,KAAK;WAKQ,aAAa,CAAC,YAAY,EAAE,aAAa,CAAC,QAAQ,CAAC,gBAAgB,CAAC,CAAC,EAAE,UAAU,UAAO;CAQxG"}
|
|
@@ -5,15 +5,14 @@ const tslib_1 = require("tslib");
|
|
|
5
5
|
const mobx_1 = require("mobx");
|
|
6
6
|
const validation_1 = require("../validation");
|
|
7
7
|
const arrays_1 = require("../async/arrays");
|
|
8
|
-
const EmptyValidator = () =>
|
|
8
|
+
const EmptyValidator = () => 0;
|
|
9
9
|
class ValidatableModel {
|
|
10
|
-
constructor(
|
|
10
|
+
constructor() {
|
|
11
11
|
this._validator = null;
|
|
12
12
|
this._strings = null;
|
|
13
13
|
this._error = null;
|
|
14
14
|
this._validationError = null;
|
|
15
15
|
mobx_1.makeObservable(this);
|
|
16
|
-
this.setValidationConfig(config);
|
|
17
16
|
}
|
|
18
17
|
get isValid() { return !this._error; }
|
|
19
18
|
get error() { return this._error; }
|
|
@@ -34,21 +33,23 @@ class ValidatableModel {
|
|
|
34
33
|
return true;
|
|
35
34
|
}
|
|
36
35
|
try {
|
|
37
|
-
const
|
|
38
|
-
this._validationError =
|
|
36
|
+
const validationResult = await this._validator(this.valueToValidate);
|
|
37
|
+
this._validationError = !validationResult
|
|
39
38
|
? null
|
|
40
|
-
: new validation_1.ValidationError('Unknown error',
|
|
39
|
+
: new validation_1.ValidationError('Unknown error', validationResult);
|
|
41
40
|
}
|
|
42
41
|
catch (err) {
|
|
43
42
|
this._validationError = err;
|
|
44
43
|
}
|
|
45
|
-
|
|
46
|
-
this.
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
44
|
+
mobx_1.runInAction(() => {
|
|
45
|
+
if (!this._validationError) {
|
|
46
|
+
this._error = null;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
const code = this._validationError.code;
|
|
50
|
+
this._error = this._strings && this._strings[code];
|
|
51
|
+
}
|
|
52
|
+
});
|
|
52
53
|
return this._validationError == null;
|
|
53
54
|
}
|
|
54
55
|
async getIsInvalid() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Validatable.js","sourceRoot":"","sources":["../../src/viewModels/Validatable.ts"],"names":[],"mappings":";;;;AAAA,+
|
|
1
|
+
{"version":3,"file":"Validatable.js","sourceRoot":"","sources":["../../src/viewModels/Validatable.ts"],"names":[],"mappings":";;;;AAAA,+BAAuE;AACvE,8CAA6G;AAC7G,4CAA4C;AAU5C,MAAM,cAAc,GAAG,GAAG,EAAE,CAAC,CAAC,CAAC;AAE/B,MAAsB,gBAAgB;IAUlC;QARQ,eAAU,GAAqC,IAAI,CAAC;QACpD,aAAQ,GAAiC,IAAI,CAAC;QAG9C,WAAM,GAAW,IAAI,CAAC;QAEtB,qBAAgB,GAAoB,IAAI,CAAC;QAG7C,qBAAc,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;IAID,IAAI,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAEtC,IAAI,KAAK,KAAK,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAE5B,mBAAmB,CAAqD,MAAqC;QAChH,IAAI,CAAC,UAAU,GAAG,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,SAAS,KAAI,cAAc,CAAC;QACtD,IAAI,CAAC,QAAQ,GAAG,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,CAAC;QAC/B,OAAO,IAAI,CAAC;IAChB,CAAC;IAEM,KAAK,CAAC,YAAY,CAAC,KAAQ;QAC9B,IAAI,IAAI,CAAC,UAAU,EAAE;YACjB,MAAM,GAA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AI,UAAU,EAAE;YACZ,OAAO,kBAAS,CAAC,YAAY,EAAE,KAAK,EAAC,CAAC,EAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;SACpE;QAED,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;QACvE,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IACjC,CAAC;CACJ;AAxEG;IADC,iBAAU;;gDACmB;AA2D9B;IADC,aAAM;;;;6CAIN;AApEL,4CA8EC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/viewModels/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAE3B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AAEnC,cAAc,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/viewModels/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAE3B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AAEnC,cAAc,eAAe,CAAC"}
|
package/lib/viewModels/index.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
tslib_1.__exportStar(require("./types"), exports);
|
|
5
5
|
tslib_1.__exportStar(require("./Validatable"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./CommonModel"), exports);
|
|
6
7
|
tslib_1.__exportStar(require("./TextModel"), exports);
|
|
7
8
|
tslib_1.__exportStar(require("./FlagModel"), exports);
|
|
8
9
|
tslib_1.__exportStar(require("./NumberModel"), exports);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/viewModels/index.ts"],"names":[],"mappings":";;;AAAA,kDAAwB;AACxB,wDAA8B;AAC9B,sDAA4B;AAC5B,sDAA4B;AAC5B,wDAA8B;AAC9B,qDAA2B;AAE3B,wDAA8B;AAC9B,6DAAmC;AAEnC,wDAA8B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/viewModels/index.ts"],"names":[],"mappings":";;;AAAA,kDAAwB;AACxB,wDAA8B;AAC9B,wDAA8B;AAC9B,sDAA4B;AAC5B,sDAA4B;AAC5B,wDAA8B;AAC9B,qDAA2B;AAE3B,wDAA8B;AAC9B,6DAAmC;AAEnC,wDAA8B"}
|
package/package.json
CHANGED
package/src/disposer.ts
CHANGED
|
@@ -59,9 +59,12 @@ export class Disposer {
|
|
|
59
59
|
);
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
this._disposers.
|
|
62
|
+
const copy = this._disposers.slice().reverse();
|
|
63
63
|
this._disposers.length = 0;
|
|
64
64
|
this._map.clear();
|
|
65
|
+
|
|
66
|
+
// this should separate side effects
|
|
67
|
+
copy.forEach(d => d());
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
70
|
|
package/src/lazy.light.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import type { IDisposable } from './disposer';
|
|
1
2
|
|
|
2
|
-
export type LazyLight<T> = {
|
|
3
|
+
export type LazyLight<T> = IDisposable & {
|
|
3
4
|
readonly value: T;
|
|
4
5
|
readonly hasValue: boolean;
|
|
5
6
|
|
|
@@ -7,19 +8,22 @@ export type LazyLight<T> = {
|
|
|
7
8
|
};
|
|
8
9
|
|
|
9
10
|
export function createLazy<T>(factory: () => T) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const _factory = factory;
|
|
12
|
+
let _instance: T = undefined;
|
|
13
|
+
|
|
14
|
+
const res: LazyLight<T> = {
|
|
13
15
|
get value() {
|
|
14
|
-
if (
|
|
15
|
-
|
|
16
|
+
if (_instance === undefined) {
|
|
17
|
+
_instance = _factory();
|
|
16
18
|
}
|
|
17
|
-
return
|
|
19
|
+
return _instance;
|
|
18
20
|
},
|
|
19
|
-
get hasValue() { return
|
|
20
|
-
reset() {
|
|
21
|
-
|
|
21
|
+
get hasValue() { return _instance !== undefined; },
|
|
22
|
+
reset: () => {
|
|
23
|
+
_instance = undefined;
|
|
22
24
|
},
|
|
25
|
+
dispose: () => res.reset(),
|
|
26
|
+
};
|
|
23
27
|
|
|
24
|
-
|
|
28
|
+
return res;
|
|
25
29
|
}
|
package/src/lazy.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { IDisposable } from 'disposer';
|
|
2
|
+
import { observable, makeObservable, action } from 'mobx';
|
|
2
3
|
|
|
3
|
-
export default class Lazy<T> {
|
|
4
|
+
export default class Lazy<T> implements IDisposable {
|
|
4
5
|
|
|
5
6
|
@observable.ref
|
|
6
7
|
private _instance: T = null;
|
|
@@ -25,9 +26,11 @@ export default class Lazy<T> {
|
|
|
25
26
|
reset() {
|
|
26
27
|
this._instance = null;
|
|
27
28
|
}
|
|
29
|
+
|
|
30
|
+
dispose = () => this.reset();
|
|
28
31
|
}
|
|
29
32
|
|
|
30
|
-
export class LazyPromise<T> {
|
|
33
|
+
export class LazyPromise<T> implements IDisposable {
|
|
31
34
|
|
|
32
35
|
@observable.ref
|
|
33
36
|
private _instance: T = null;
|
|
@@ -50,23 +53,27 @@ export class LazyPromise<T> {
|
|
|
50
53
|
get() {
|
|
51
54
|
if (!this._loaded && !this._busy) {
|
|
52
55
|
this._busy = true;
|
|
53
|
-
this._factory().then(
|
|
54
|
-
transaction(() => {
|
|
55
|
-
this._busy = false;
|
|
56
|
-
this._loaded = true;
|
|
57
|
-
this._instance = res;
|
|
58
|
-
});
|
|
59
|
-
});
|
|
56
|
+
this._factory().then(this.setInstance);
|
|
60
57
|
}
|
|
61
58
|
|
|
62
59
|
return this._instance;
|
|
63
60
|
}
|
|
64
61
|
|
|
62
|
+
@action
|
|
63
|
+
private setInstance = (res: T) => {
|
|
64
|
+
const wasBusy = this._busy;
|
|
65
|
+
|
|
66
|
+
this._busy = false;
|
|
67
|
+
this._loaded = wasBusy;
|
|
68
|
+
this._instance = wasBusy ? res : null;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
@action
|
|
65
72
|
reset() {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
this._loaded = false;
|
|
70
|
-
});
|
|
73
|
+
this._busy = false;
|
|
74
|
+
this._instance = this.initial;
|
|
75
|
+
this._loaded = false;
|
|
71
76
|
}
|
|
77
|
+
|
|
78
|
+
dispose = () => this.reset();
|
|
72
79
|
}
|
package/src/pool.ts
CHANGED
|
@@ -1,24 +1,47 @@
|
|
|
1
|
+
import { safeCall } from 'functions';
|
|
2
|
+
import type { IDisposable } from './disposer';
|
|
1
3
|
|
|
2
4
|
export class Pool<T> {
|
|
3
5
|
|
|
4
|
-
private _container: T[] = [];
|
|
6
|
+
private readonly _container: T[] = [];
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
constructor(readonly factory: () => T) { }
|
|
9
|
+
|
|
10
|
+
public get(): T {
|
|
7
11
|
if (this._container.length > 0) {
|
|
8
12
|
return this._container.pop();
|
|
9
13
|
} else {
|
|
10
|
-
return factory();
|
|
14
|
+
return this.factory();
|
|
11
15
|
}
|
|
12
16
|
}
|
|
13
17
|
|
|
14
|
-
release(e: T): void
|
|
15
|
-
release(e: T[]): void;
|
|
16
|
-
|
|
17
|
-
release(e: T | T[]) {
|
|
18
|
+
public release(e: T | T[]): void {
|
|
18
19
|
if (Array.isArray(e)) {
|
|
19
20
|
this._container.push(...e);
|
|
20
|
-
} else {
|
|
21
|
+
} else if (e) {
|
|
21
22
|
this._container.push(e);
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
25
|
}
|
|
26
|
+
|
|
27
|
+
export class PoolDisposable<T extends IDisposable> extends Pool<T> {
|
|
28
|
+
|
|
29
|
+
constructor(factory: () => T, readonly initializer: (item: T) => void = null) {
|
|
30
|
+
super(factory);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public get(): T {
|
|
34
|
+
const res = super.get();
|
|
35
|
+
safeCall(this.initializer, res);
|
|
36
|
+
return res;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public release(e: T | T[]): void {
|
|
40
|
+
if (Array.isArray(e)) {
|
|
41
|
+
e.forEach(ee => ee.dispose());
|
|
42
|
+
} else if (e) {
|
|
43
|
+
e.dispose();
|
|
44
|
+
}
|
|
45
|
+
return super.release(e);
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/validation/types.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { ValidationErrors } from './ValidationErrors';
|
|
2
2
|
|
|
3
|
-
export type ValidatorFunction<T = string> = (val: T) =>
|
|
4
|
-
export type ValidatorFunctionAsync<T = string> = (val: T) => Promise<
|
|
3
|
+
export type ValidatorFunction<T = string, TErrors = ValidationErrors> = (val: T) => TErrors;
|
|
4
|
+
export type ValidatorFunctionAsync<T = string, TErrors = ValidationErrors> = (val: T) => Promise<TErrors>;
|
|
5
5
|
|
|
6
6
|
export type WrapperFunction = (val: ValidatorFunction) => ValidatorFunction;
|
|
7
7
|
|
|
8
|
-
export class ValidationError extends Error {
|
|
9
|
-
readonly code:
|
|
8
|
+
export class ValidationError<TErrors = ValidationErrors> extends Error {
|
|
9
|
+
readonly code: TErrors = null;
|
|
10
10
|
|
|
11
|
-
constructor(message: string, code:
|
|
11
|
+
constructor(message: string, code: TErrors) {
|
|
12
12
|
super(message);
|
|
13
13
|
this.code = code;
|
|
14
14
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { action, makeObservable, observable } from 'mobx';
|
|
2
|
+
import { ValidatableModel } from './Validatable';
|
|
3
|
+
|
|
4
|
+
export class CommonModel<T = any> extends ValidatableModel<T> {
|
|
5
|
+
|
|
6
|
+
@observable.ref
|
|
7
|
+
private _value: T = null;
|
|
8
|
+
|
|
9
|
+
private _defaultValue: T = null;
|
|
10
|
+
private _validateOnChange = false;
|
|
11
|
+
|
|
12
|
+
constructor(v: T = null) {
|
|
13
|
+
super();
|
|
14
|
+
makeObservable(this);
|
|
15
|
+
this._value = v;
|
|
16
|
+
this._defaultValue = v;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
validateOnChange(enable = true) {
|
|
20
|
+
this._validateOnChange = enable;
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
protected get valueToValidate(): Readonly<T> {
|
|
25
|
+
return this._value;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public get value() { return this._value; }
|
|
29
|
+
public set value(v: T) {
|
|
30
|
+
this.setValue(v);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@action
|
|
34
|
+
public readonly setValue = (value: T) => {
|
|
35
|
+
this._value = value;
|
|
36
|
+
|
|
37
|
+
if (this._validateOnChange) {
|
|
38
|
+
this.validate();
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
reset = () => {
|
|
43
|
+
super.reset();
|
|
44
|
+
this.value = this._defaultValue;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -27,9 +27,14 @@ export class FlagModel implements IFlagModel, IFlagModelReadonly {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
set value(value: boolean) {
|
|
30
|
-
this.
|
|
30
|
+
this.setValue(value);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
@action
|
|
34
|
+
public readonly setValue = (value: boolean) => {
|
|
35
|
+
this._value = value;
|
|
36
|
+
};
|
|
37
|
+
|
|
33
38
|
get isDefault() { return this._value === false; }
|
|
34
39
|
|
|
35
40
|
@action
|
|
@@ -20,7 +20,12 @@ export class NumberModel implements INumberModel, IValueModel<number> {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
get value() { return this._value; }
|
|
23
|
-
set value(v: number) { this.
|
|
23
|
+
set value(v: number) { this.setValue(v); }
|
|
24
|
+
|
|
25
|
+
@action
|
|
26
|
+
public readonly setValue = (v: number) => {
|
|
27
|
+
this._value = v;
|
|
28
|
+
};
|
|
24
29
|
|
|
25
30
|
get isDefault() { return this._value === this._initial; }
|
|
26
31
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { observable,
|
|
1
|
+
import { observable, makeObservable, reaction, action } from 'mobx';
|
|
2
2
|
import { Getter } from '../types';
|
|
3
3
|
import { IValueModel } from './types';
|
|
4
4
|
import logger from '../logger';
|
|
5
|
-
import { ValidatableModel
|
|
5
|
+
import { ValidatableModel } from './Validatable';
|
|
6
6
|
import { IResetableModel } from 'viewModels';
|
|
7
7
|
|
|
8
8
|
export type TextInputConfig = {
|
|
@@ -11,7 +11,6 @@ export type TextInputConfig = {
|
|
|
11
11
|
value?: Getter<string>;
|
|
12
12
|
async?: boolean;
|
|
13
13
|
|
|
14
|
-
validation?: ValidationConfig<string>;
|
|
15
14
|
noSubscribe?: boolean;
|
|
16
15
|
};
|
|
17
16
|
|
|
@@ -40,23 +39,20 @@ export class Text {
|
|
|
40
39
|
get value() { return this._value; }
|
|
41
40
|
}
|
|
42
41
|
|
|
43
|
-
export class TextInputVM extends ValidatableModel implements IValueModel<string>, IResetableModel {
|
|
42
|
+
export class TextInputVM extends ValidatableModel<string> implements IValueModel<string>, IResetableModel {
|
|
44
43
|
@observable
|
|
45
44
|
private _value = '';
|
|
46
45
|
|
|
47
46
|
@observable
|
|
48
47
|
private _focused = false;
|
|
49
48
|
|
|
50
|
-
@observable
|
|
51
49
|
private _name: string = null;
|
|
52
|
-
|
|
53
|
-
@observable
|
|
54
50
|
private _title: string = null;
|
|
55
51
|
|
|
56
52
|
private readonly _valueObserving: () => void = null;
|
|
57
53
|
|
|
58
54
|
constructor(config?: TextInputConfig) {
|
|
59
|
-
super(
|
|
55
|
+
super();
|
|
60
56
|
makeObservable(this);
|
|
61
57
|
config = config || {};
|
|
62
58
|
|
|
@@ -72,14 +68,18 @@ export class TextInputVM extends ValidatableModel implements IValueModel<string>
|
|
|
72
68
|
get title() { return this._title; }
|
|
73
69
|
|
|
74
70
|
set value(val) {
|
|
71
|
+
this.setValue(val);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@action
|
|
75
|
+
public readonly setValue = (value: string) => {
|
|
75
76
|
if (!this._valueObserving) {
|
|
76
|
-
this._value =
|
|
77
|
+
this._value = value;
|
|
77
78
|
} else {
|
|
78
79
|
logger.warn('[TextInputViewModel] Setting value is not allowed when value is observing');
|
|
79
80
|
}
|
|
80
|
-
}
|
|
81
|
+
};
|
|
81
82
|
|
|
82
|
-
@computed
|
|
83
83
|
get isEmpty() {
|
|
84
84
|
return !this._value;
|
|
85
85
|
}
|
|
@@ -88,15 +88,20 @@ export class TextInputVM extends ValidatableModel implements IValueModel<string>
|
|
|
88
88
|
return this._focused;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
-
set focused(val) {
|
|
92
|
-
this.
|
|
93
|
-
|
|
91
|
+
set focused(val: boolean) {
|
|
92
|
+
this.setFocused(val);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
@action
|
|
96
|
+
public readonly setFocused = (value = true) => {
|
|
97
|
+
this._focused = value;
|
|
98
|
+
if (!value) {
|
|
94
99
|
this.onBlur();
|
|
95
100
|
return;
|
|
96
101
|
}
|
|
97
102
|
|
|
98
103
|
super.reset();
|
|
99
|
-
}
|
|
104
|
+
};
|
|
100
105
|
|
|
101
106
|
protected get valueToValidate() { return (this.value ?? '').trim(); }
|
|
102
107
|
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import { observable, makeObservable, action } from 'mobx';
|
|
1
|
+
import { observable, makeObservable, action, runInAction } from 'mobx';
|
|
2
2
|
import { ValidatorFunction, ValidatorFunctionAsync, ValidationErrors, ValidationError } from '../validation';
|
|
3
3
|
import { someAsync } from '../async/arrays';
|
|
4
4
|
|
|
5
|
-
export type ValueValidator<T> = ValidatorFunction<T> | ValidatorFunctionAsync<T>;
|
|
6
|
-
export type ValidationErrorsStrings
|
|
5
|
+
export type ValueValidator<T, TErrors = ValidationErrors> = ValidatorFunction<T, TErrors> | ValidatorFunctionAsync<T, TErrors>;
|
|
6
|
+
export type ValidationErrorsStrings<TErrors extends string | number = number> = Partial<Omit<Record<TErrors, string>, 0 | null>>;
|
|
7
7
|
|
|
8
|
-
export type ValidationConfig<T> = {
|
|
9
|
-
validator: ValueValidator<Readonly<T
|
|
10
|
-
errors: ValidationErrorsStrings
|
|
8
|
+
export type ValidationConfig<T, TErrors extends string | number = ValidationErrors> = {
|
|
9
|
+
validator: ValueValidator<Readonly<T>, TErrors>,
|
|
10
|
+
errors: ValidationErrorsStrings<TErrors>,
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
const EmptyValidator = () =>
|
|
13
|
+
const EmptyValidator = () => 0;
|
|
14
|
+
|
|
14
15
|
export abstract class ValidatableModel<T = string> {
|
|
15
16
|
|
|
16
|
-
private _validator: ValueValidator<Readonly<T
|
|
17
|
-
private _strings: ValidationErrorsStrings = null;
|
|
17
|
+
private _validator: ValueValidator<Readonly<T>, any> = null;
|
|
18
|
+
private _strings: ValidationErrorsStrings<any> = null;
|
|
18
19
|
|
|
19
20
|
@observable
|
|
20
21
|
private _error: string = null;
|
|
21
22
|
|
|
22
23
|
private _validationError: ValidationError = null;
|
|
23
24
|
|
|
24
|
-
constructor(
|
|
25
|
+
constructor() {
|
|
25
26
|
makeObservable(this);
|
|
26
|
-
this.setValidationConfig(config);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
protected abstract get valueToValidate(): Readonly<T>;
|
|
@@ -32,7 +32,7 @@ export abstract class ValidatableModel<T = string> {
|
|
|
32
32
|
|
|
33
33
|
get error() { return this._error; }
|
|
34
34
|
|
|
35
|
-
public setValidationConfig(config?: ValidationConfig<T>) {
|
|
35
|
+
public setValidationConfig<TErrors extends string | number = ValidationErrors>(config?: ValidationConfig<T, TErrors>) {
|
|
36
36
|
this._validator = config?.validator || EmptyValidator;
|
|
37
37
|
this._strings = config?.errors;
|
|
38
38
|
return this;
|
|
@@ -52,20 +52,22 @@ export abstract class ValidatableModel<T = string> {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
try {
|
|
55
|
-
const
|
|
56
|
-
this._validationError =
|
|
55
|
+
const validationResult = await this._validator(this.valueToValidate);
|
|
56
|
+
this._validationError = !validationResult
|
|
57
57
|
? null
|
|
58
|
-
: new ValidationError('Unknown error',
|
|
58
|
+
: new ValidationError('Unknown error', validationResult);
|
|
59
59
|
} catch (err) {
|
|
60
60
|
this._validationError = err as ValidationError;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
|
|
64
|
-
this.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
63
|
+
runInAction(() => {
|
|
64
|
+
if (!this._validationError) {
|
|
65
|
+
this._error = null;
|
|
66
|
+
} else {
|
|
67
|
+
const code = this._validationError.code;
|
|
68
|
+
this._error = this._strings && this._strings[code];
|
|
69
|
+
}
|
|
70
|
+
});
|
|
69
71
|
return this._validationError == null;
|
|
70
72
|
}
|
|
71
73
|
|
package/src/viewModels/index.ts
CHANGED
package/yarn.lock
CHANGED
|
@@ -3046,7 +3046,7 @@ reusify@^1.0.4:
|
|
|
3046
3046
|
resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76"
|
|
3047
3047
|
integrity sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==
|
|
3048
3048
|
|
|
3049
|
-
rimraf@^3.0.0, rimraf@^3.0.2:
|
|
3049
|
+
rimraf@^3, rimraf@^3.0.0, rimraf@^3.0.2:
|
|
3050
3050
|
version "3.0.2"
|
|
3051
3051
|
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a"
|
|
3052
3052
|
integrity sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==
|