@zajno/common 1.3.12 → 1.3.16

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.
Files changed (102) hide show
  1. package/coverage/clover.xml +66 -58
  2. package/coverage/coverage-final.json +6 -6
  3. package/coverage/lcov-report/index.html +26 -26
  4. package/coverage/lcov-report/src/__tests__/helpers/index.html +1 -1
  5. package/coverage/lcov-report/src/__tests__/helpers/main.ts.html +1 -1
  6. package/coverage/lcov-report/src/async/arrays.ts.html +1 -1
  7. package/coverage/lcov-report/src/async/index.html +1 -1
  8. package/coverage/lcov-report/src/dates/calc.ts.html +1 -1
  9. package/coverage/lcov-report/src/dates/convert.ts.html +1 -1
  10. package/coverage/lcov-report/src/dates/datex.ts.html +1 -1
  11. package/coverage/lcov-report/src/dates/format.ts.html +1 -1
  12. package/coverage/lcov-report/src/dates/index.html +1 -1
  13. package/coverage/lcov-report/src/dates/index.ts.html +1 -1
  14. package/coverage/lcov-report/src/dates/parse.ts.html +1 -1
  15. package/coverage/lcov-report/src/dates/period.ts.html +1 -1
  16. package/coverage/lcov-report/src/dates/shift.ts.html +1 -1
  17. package/coverage/lcov-report/src/dates/types.ts.html +1 -1
  18. package/coverage/lcov-report/src/dates/yearDate.ts.html +1 -1
  19. package/coverage/lcov-report/src/enumHelper.ts.html +1 -1
  20. package/coverage/lcov-report/src/event.ts.html +1 -1
  21. package/coverage/lcov-report/src/fields/index.html +1 -1
  22. package/coverage/lcov-report/src/fields/update.ts.html +1 -1
  23. package/coverage/lcov-report/src/functions.ts.html +1 -1
  24. package/coverage/lcov-report/src/index.html +15 -15
  25. package/coverage/lcov-report/src/lazy.light.ts.html +33 -21
  26. package/coverage/lcov-report/src/logger/batch.ts.html +1 -1
  27. package/coverage/lcov-report/src/logger/console.ts.html +1 -1
  28. package/coverage/lcov-report/src/logger/index.html +1 -1
  29. package/coverage/lcov-report/src/logger/index.ts.html +1 -1
  30. package/coverage/lcov-report/src/logger/named.ts.html +1 -1
  31. package/coverage/lcov-report/src/logger/proxy.ts.html +1 -1
  32. package/coverage/lcov-report/src/math/arrays.ts.html +1 -1
  33. package/coverage/lcov-report/src/math/calc.ts.html +1 -1
  34. package/coverage/lcov-report/src/math/distribution.ts.html +1 -1
  35. package/coverage/lcov-report/src/math/index.html +1 -1
  36. package/coverage/lcov-report/src/math/index.ts.html +1 -1
  37. package/coverage/lcov-report/src/transitionObserver.ts.html +1 -1
  38. package/coverage/lcov-report/src/types.ts.html +1 -1
  39. package/coverage/lcov-report/src/validation/ValidationErrors.ts.html +1 -1
  40. package/coverage/lcov-report/src/validation/creditCard.ts.html +1 -1
  41. package/coverage/lcov-report/src/validation/helpers.ts.html +1 -1
  42. package/coverage/lcov-report/src/validation/index.html +1 -1
  43. package/coverage/lcov-report/src/validation/index.ts.html +1 -1
  44. package/coverage/lcov-report/src/validation/types.ts.html +6 -6
  45. package/coverage/lcov-report/src/validation/validators.ts.html +1 -1
  46. package/coverage/lcov-report/src/validation/wrappers.ts.html +1 -1
  47. package/coverage/lcov-report/src/viewModels/FlagModel.ts.html +24 -9
  48. package/coverage/lcov-report/src/viewModels/MultiSelectModel.ts.html +1 -1
  49. package/coverage/lcov-report/src/viewModels/NumberModel.ts.html +24 -9
  50. package/coverage/lcov-report/src/viewModels/SelectModel.ts.html +10 -10
  51. package/coverage/lcov-report/src/viewModels/Validatable.ts.html +56 -50
  52. package/coverage/lcov-report/src/viewModels/index.html +44 -44
  53. package/coverage/lcov-report/src/viewModels/wrappers.ts.html +1 -1
  54. package/coverage/lcov.info +137 -121
  55. package/lib/disposer.d.ts.map +1 -1
  56. package/lib/disposer.js +3 -1
  57. package/lib/disposer.js.map +1 -1
  58. package/lib/lazy.d.ts +6 -2
  59. package/lib/lazy.d.ts.map +1 -1
  60. package/lib/lazy.js +22 -12
  61. package/lib/lazy.js.map +1 -1
  62. package/lib/lazy.light.d.ts +2 -1
  63. package/lib/lazy.light.d.ts.map +1 -1
  64. package/lib/lazy.light.js +11 -9
  65. package/lib/lazy.light.js.map +1 -1
  66. package/lib/pool.d.ts +12 -4
  67. package/lib/pool.d.ts.map +1 -1
  68. package/lib/pool.js +28 -5
  69. package/lib/pool.js.map +1 -1
  70. package/lib/validation/types.d.ts +5 -5
  71. package/lib/validation/types.d.ts.map +1 -1
  72. package/lib/validation/types.js.map +1 -1
  73. package/lib/validation/wrappers.d.ts +4 -3
  74. package/lib/validation/wrappers.d.ts.map +1 -1
  75. package/lib/viewModels/FlagModel.d.ts +1 -0
  76. package/lib/viewModels/FlagModel.d.ts.map +1 -1
  77. package/lib/viewModels/FlagModel.js +8 -1
  78. package/lib/viewModels/FlagModel.js.map +1 -1
  79. package/lib/viewModels/NumberModel.d.ts +1 -0
  80. package/lib/viewModels/NumberModel.d.ts.map +1 -1
  81. package/lib/viewModels/NumberModel.js +8 -1
  82. package/lib/viewModels/NumberModel.js.map +1 -1
  83. package/lib/viewModels/TextModel.d.ts +4 -3
  84. package/lib/viewModels/TextModel.d.ts.map +1 -1
  85. package/lib/viewModels/TextModel.js +25 -24
  86. package/lib/viewModels/TextModel.js.map +1 -1
  87. package/lib/viewModels/Validatable.d.ts +8 -10
  88. package/lib/viewModels/Validatable.d.ts.map +1 -1
  89. package/lib/viewModels/Validatable.js +14 -13
  90. package/lib/viewModels/Validatable.js.map +1 -1
  91. package/package.json +4 -3
  92. package/src/disposer.ts +4 -1
  93. package/src/lazy.light.ts +15 -11
  94. package/src/lazy.ts +22 -15
  95. package/src/pool.ts +31 -8
  96. package/src/validation/types.ts +5 -5
  97. package/src/viewModels/FlagModel.ts +6 -1
  98. package/src/viewModels/NumberModel.ts +6 -1
  99. package/src/viewModels/TextModel.ts +20 -15
  100. package/src/viewModels/Validatable.ts +23 -21
  101. package/yarn-error.log +3709 -0
  102. package/yarn.lock +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"Validatable.js","sourceRoot":"","sources":["../../src/viewModels/Validatable.ts"],"names":[],"mappings":";;;;AAAA,+BAA0D;AAC1D,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,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IACjC,CAAC;CACJ;AAxEG;IADC,iBAAU;;gDACmB;AA2D9B;IADC,aAAM;;;;6CAIN;AApEL,4CA8EC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zajno/common",
3
- "version": "1.3.12",
3
+ "version": "1.3.16",
4
4
  "description": "Zajno's re-usable utilities for JS/TS projects",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,7 +21,9 @@
21
21
  "lint": "eslint \"src/**/*.ts?(x)\"",
22
22
  "test": "yarn jest --clearCache && yarn jest"
23
23
  },
24
- "dependencies": {},
24
+ "dependencies": {
25
+ "rimraf": "^3"
26
+ },
25
27
  "devDependencies": {
26
28
  "@types/faker": "^5.5.9",
27
29
  "@types/jest": "^27.0.2",
@@ -32,7 +34,6 @@
32
34
  "fast-check": "^2.18.0",
33
35
  "jest": "^27.3.1",
34
36
  "mobx": "^6",
35
- "rimraf": "^3.0.2",
36
37
  "ts-jest": "^27.0.7",
37
38
  "ts-node": "^10.3.1",
38
39
  "tslib": "^2.2.0",
package/src/disposer.ts CHANGED
@@ -59,9 +59,12 @@ export class Disposer {
59
59
  );
60
60
  }
61
61
 
62
- this._disposers.forEach(d => d());
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
- return {
11
- _factory: factory,
12
- _instance: undefined as T,
11
+ const _factory = factory;
12
+ let _instance: T = undefined;
13
+
14
+ const res: LazyLight<T> = {
13
15
  get value() {
14
- if (this._instance === undefined) {
15
- this._instance = this._factory();
16
+ if (_instance === undefined) {
17
+ _instance = _factory();
16
18
  }
17
- return this._instance;
19
+ return _instance;
18
20
  },
19
- get hasValue() { return this._instance !== undefined; },
20
- reset() {
21
- this._instance = undefined;
21
+ get hasValue() { return _instance !== undefined; },
22
+ reset: () => {
23
+ _instance = undefined;
22
24
  },
25
+ dispose: () => res.reset(),
26
+ };
23
27
 
24
- } as LazyLight<T>;
28
+ return res;
25
29
  }
package/src/lazy.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { observable, transaction, makeObservable } from 'mobx';
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(res => {
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
- transaction(() => {
67
- this._busy = false;
68
- this._instance = this.initial;
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
- get(factory: () => T): T {
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
+ }
@@ -1,14 +1,14 @@
1
1
  import { ValidationErrors } from './ValidationErrors';
2
2
 
3
- export type ValidatorFunction<T = string> = (val: T) => ValidationErrors;
4
- export type ValidatorFunctionAsync<T = string> = (val: T) => Promise<ValidationErrors>;
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: ValidationErrors = null;
8
+ export class ValidationError<TErrors = ValidationErrors> extends Error {
9
+ readonly code: TErrors = null;
10
10
 
11
- constructor(message: string, code: ValidationErrors) {
11
+ constructor(message: string, code: TErrors) {
12
12
  super(message);
13
13
  this.code = code;
14
14
  }
@@ -27,9 +27,14 @@ export class FlagModel implements IFlagModel, IFlagModelReadonly {
27
27
  }
28
28
 
29
29
  set value(value: boolean) {
30
- this._value = value;
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._value = v; }
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, computed, makeObservable, reaction } from 'mobx';
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, ValidationConfig } from './Validatable';
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(config && config.validation);
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 = val;
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._focused = val;
93
- if (!val) {
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 = { [code: number]: string };
5
+ export type ValueValidator<T, TErrors = ValidationErrors> = ValidatorFunction<T, TErrors> | ValidatorFunctionAsync<T, TErrors>;
6
+ export type ValidationErrorsStrings<TErrors extends string | number = number> = Record<TErrors, string>;
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 = () => ValidationErrors.None;
13
+ const EmptyValidator = () => 0;
14
+
14
15
  export abstract class ValidatableModel<T = string> {
15
16
 
16
- private _validator: ValueValidator<Readonly<T>> = null;
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(config?: ValidationConfig<T>) {
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 valid = await this._validator(this.valueToValidate);
56
- this._validationError = valid === ValidationErrors.None
55
+ const validationResult = await this._validator(this.valueToValidate);
56
+ this._validationError = !validationResult
57
57
  ? null
58
- : new ValidationError('Unknown error', valid);
58
+ : new ValidationError('Unknown error', validationResult);
59
59
  } catch (err) {
60
60
  this._validationError = err as ValidationError;
61
61
  }
62
62
 
63
- if (!this._validationError) {
64
- this._error = null;
65
- } else {
66
- const code = this._validationError.code;
67
- this._error = this._strings && this._strings[code];
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