@zajno/common 1.5.0 → 1.5.3

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 (92) hide show
  1. package/coverage/clover.xml +57 -49
  2. package/coverage/coverage-final.json +4 -4
  3. package/coverage/lcov-report/async/index.html +1 -1
  4. package/coverage/lcov-report/async/timeout.ts.html +12 -12
  5. package/coverage/lcov-report/index.html +21 -21
  6. package/coverage/lcov-report/src/__tests__/helpers/index.html +1 -1
  7. package/coverage/lcov-report/src/__tests__/helpers/main.ts.html +1 -1
  8. package/coverage/lcov-report/src/async/arrays.ts.html +1 -1
  9. package/coverage/lcov-report/src/async/index.html +1 -1
  10. package/coverage/lcov-report/src/async/timeout.ts.html +10 -10
  11. package/coverage/lcov-report/src/cache.ts.html +1 -1
  12. package/coverage/lcov-report/src/dates/calc.ts.html +1 -1
  13. package/coverage/lcov-report/src/dates/convert.ts.html +1 -1
  14. package/coverage/lcov-report/src/dates/datex.ts.html +1 -1
  15. package/coverage/lcov-report/src/dates/format.ts.html +1 -1
  16. package/coverage/lcov-report/src/dates/index.html +1 -1
  17. package/coverage/lcov-report/src/dates/index.ts.html +1 -1
  18. package/coverage/lcov-report/src/dates/parse.ts.html +1 -1
  19. package/coverage/lcov-report/src/dates/period.ts.html +1 -1
  20. package/coverage/lcov-report/src/dates/shift.ts.html +1 -1
  21. package/coverage/lcov-report/src/dates/types.ts.html +1 -1
  22. package/coverage/lcov-report/src/dates/yearDate.ts.html +1 -1
  23. package/coverage/lcov-report/src/disposer.ts.html +1 -1
  24. package/coverage/lcov-report/src/enumHelper.ts.html +1 -1
  25. package/coverage/lcov-report/src/event.ts.html +1 -1
  26. package/coverage/lcov-report/src/fields/index.html +1 -1
  27. package/coverage/lcov-report/src/fields/update.ts.html +1 -1
  28. package/coverage/lcov-report/src/functions.ts.html +1 -1
  29. package/coverage/lcov-report/src/index.html +1 -1
  30. package/coverage/lcov-report/src/lazy.light.ts.html +1 -1
  31. package/coverage/lcov-report/src/logger/batch.ts.html +1 -1
  32. package/coverage/lcov-report/src/logger/console.ts.html +1 -1
  33. package/coverage/lcov-report/src/logger/index.html +1 -1
  34. package/coverage/lcov-report/src/logger/index.ts.html +1 -1
  35. package/coverage/lcov-report/src/logger/named.ts.html +1 -1
  36. package/coverage/lcov-report/src/logger/proxy.ts.html +1 -1
  37. package/coverage/lcov-report/src/math/arrays.ts.html +1 -1
  38. package/coverage/lcov-report/src/math/calc.ts.html +1 -1
  39. package/coverage/lcov-report/src/math/distribution.ts.html +1 -1
  40. package/coverage/lcov-report/src/math/index.html +1 -1
  41. package/coverage/lcov-report/src/math/index.ts.html +1 -1
  42. package/coverage/lcov-report/src/observersMap.ts.html +1 -1
  43. package/coverage/lcov-report/src/observingCache.ts.html +1 -1
  44. package/coverage/lcov-report/src/tempoCache.ts.html +1 -1
  45. package/coverage/lcov-report/src/transitionObserver.ts.html +1 -1
  46. package/coverage/lcov-report/src/types.ts.html +1 -1
  47. package/coverage/lcov-report/src/validation/ValidationErrors.ts.html +1 -1
  48. package/coverage/lcov-report/src/validation/creditCard.ts.html +1 -1
  49. package/coverage/lcov-report/src/validation/helpers.ts.html +7 -4
  50. package/coverage/lcov-report/src/validation/index.html +4 -4
  51. package/coverage/lcov-report/src/validation/index.ts.html +1 -1
  52. package/coverage/lcov-report/src/validation/types.ts.html +1 -1
  53. package/coverage/lcov-report/src/validation/validators.ts.html +1 -1
  54. package/coverage/lcov-report/src/validation/wrappers.ts.html +1 -1
  55. package/coverage/lcov-report/src/viewModels/CommonModel.ts.html +1 -1
  56. package/coverage/lcov-report/src/viewModels/FlagModel.ts.html +1 -1
  57. package/coverage/lcov-report/src/viewModels/LoadingModel.ts.html +106 -34
  58. package/coverage/lcov-report/src/viewModels/MultiSelectModel.ts.html +1 -1
  59. package/coverage/lcov-report/src/viewModels/NumberModel.ts.html +13 -13
  60. package/coverage/lcov-report/src/viewModels/SelectModel.ts.html +1 -1
  61. package/coverage/lcov-report/src/viewModels/TextModel.ts.html +1 -1
  62. package/coverage/lcov-report/src/viewModels/Validatable.ts.html +1 -1
  63. package/coverage/lcov-report/src/viewModels/index.html +19 -19
  64. package/coverage/lcov-report/src/viewModels/wrappers.ts.html +1 -1
  65. package/coverage/lcov-report/validation/index.html +1 -1
  66. package/coverage/lcov-report/validation/index.ts.html +6 -6
  67. package/coverage/lcov-report/viewModels/CommonModel.ts.html +1 -1
  68. package/coverage/lcov-report/viewModels/LoadingModel.ts.html +129 -21
  69. package/coverage/lcov-report/viewModels/SelectModel.ts.html +54 -21
  70. package/coverage/lcov-report/viewModels/TextModel.ts.html +1 -1
  71. package/coverage/lcov-report/viewModels/index.html +23 -83
  72. package/coverage/lcov.info +100 -77
  73. package/lib/services/storage/index.d.ts +4 -0
  74. package/lib/services/storage/index.d.ts.map +1 -1
  75. package/lib/services/storage/index.js +13 -1
  76. package/lib/services/storage/index.js.map +1 -1
  77. package/lib/validation/helpers.d.ts +1 -1
  78. package/lib/validation/helpers.d.ts.map +1 -1
  79. package/lib/validation/helpers.js +2 -2
  80. package/lib/validation/helpers.js.map +1 -1
  81. package/lib/viewModels/LoadingModel.d.ts +12 -3
  82. package/lib/viewModels/LoadingModel.d.ts.map +1 -1
  83. package/lib/viewModels/LoadingModel.js +33 -16
  84. package/lib/viewModels/LoadingModel.js.map +1 -1
  85. package/lib/viewModels/types.d.ts +4 -1
  86. package/lib/viewModels/types.d.ts.map +1 -1
  87. package/package.json +1 -1
  88. package/src/services/storage/index.ts +17 -1
  89. package/src/validation/helpers.ts +2 -1
  90. package/src/viewModels/LoadingModel.ts +41 -17
  91. package/src/viewModels/__tests__/common.test.ts +27 -8
  92. package/src/viewModels/types.ts +5 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zajno/common",
3
- "version": "1.5.0",
3
+ "version": "1.5.3",
4
4
  "description": "Zajno's re-usable utilities for JS/TS projects",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,6 +28,14 @@ export class KeyStorage {
28
28
 
29
29
  get value() { return this.storage.getValue(this.key); }
30
30
  set value(v: string) { this.storage.setValue(this.key, v); }
31
+
32
+ public clean() {
33
+ this.storage.removeValue(this.key);
34
+ }
35
+
36
+ public getHasValue() {
37
+ return this.storage.hasValue(this.key);
38
+ }
31
39
  }
32
40
 
33
41
  export class KeyStorageConverted<T> {
@@ -37,11 +45,19 @@ export class KeyStorageConverted<T> {
37
45
  storage: IStorageSync,
38
46
  key: string,
39
47
  readonly input: (v: T) => string = (v => JSON.stringify(v)),
40
- readonly output: (s: string) => T = (s => JSON.parse(s) as T),
48
+ readonly output: (s: string) => T = (s => JSON.parse(s || null) as T),
41
49
  ) {
42
50
  this._storage = new KeyStorage(storage, key);
43
51
  }
44
52
 
45
53
  get value(): T { return this.output(this._storage.value); }
46
54
  set value(v: T) { this._storage.value = this.input(v); }
55
+
56
+ public clean() {
57
+ this._storage.clean();
58
+ }
59
+
60
+ public getHasValue() {
61
+ return this._storage.getHasValue();
62
+ }
47
63
  }
@@ -20,6 +20,7 @@ export function validateObject<T, TErrors = ValidationErrors>(
20
20
  obj: T,
21
21
  validators: ValidationConfig<T, TErrors>,
22
22
  onlyTruethy = false,
23
+ context?: T,
23
24
  ): ValidationResults<T, TErrors> {
24
25
 
25
26
  const res: ValidationResults<T, TErrors> = { };
@@ -36,7 +37,7 @@ export function validateObject<T, TErrors = ValidationErrors>(
36
37
  return;
37
38
  }
38
39
 
39
- const err = validator(v, obj);
40
+ const err = validator(v, context || obj);
40
41
  if (err) {
41
42
  res[kk] = err;
42
43
  }
@@ -1,28 +1,52 @@
1
1
  import { NumberModel } from './NumberModel';
2
+ import { IResetableModel, IValueModel } from './types';
2
3
 
3
- export class LoadingModel extends NumberModel {
4
+ export class LoadingModel implements IValueModel<boolean>, IResetableModel {
4
5
 
5
- public get isLoading() { return this.value > 0; }
6
+ private readonly _number = new NumberModel(0);
7
+
8
+ public get isLoading() { return this._number.value > 0; }
9
+
10
+ public get value(): boolean { return this.isLoading; }
11
+ public set value(v: boolean) {
12
+ if (v) {
13
+ this._number.increment();
14
+ } else {
15
+ this._number.decrement();
16
+ }
17
+ }
6
18
 
7
19
  public async useLoading<T>(cb: () => (T | Promise<T>)): Promise<T>;
8
20
  public async useLoading<T>(cb: () => (T | Promise<T>), exclusive: false): Promise<T>;
9
- public async useLoading<T>(cb: () => (T | Promise<T>), exclusive: true | 'throw'): Promise<T | false>;
10
-
11
- public async useLoading<T>(cb: () => (T | Promise<T>), exclusive: boolean | 'throw' = false): Promise<T | false> {
12
- if (exclusive && this.isLoading) {
13
- if (exclusive === 'throw') {
14
- throw new Error('Operation cannot be started because another one is in progress already.');
15
- }
16
- return false;
17
- }
21
+ public async useLoading<T>(cb: () => (T | Promise<T>), exclusive: true): Promise<T | false>;
22
+ public async useLoading<T>(cb: () => (T | Promise<T>), exclusive: 'throw'): Promise<T>;
23
+
24
+ public useLoading<T>(cb: () => (T | Promise<T>), exclusive: boolean | 'throw' = false): Promise<T | false> {
25
+ return withLoading(this, cb, exclusive as any);
26
+ }
27
+
28
+ public reset = () => this._number.reset();
29
+ }
18
30
 
19
- this.increment();
31
+ export function withLoading<T>(model: IValueModel<boolean>, cb: () => (T | Promise<T>)): Promise<T>;
32
+ export function withLoading<T>(model: IValueModel<boolean>, cb: () => (T | Promise<T>), exclusive: false): Promise<T>;
33
+ export function withLoading<T>(model: IValueModel<boolean>, cb: () => (T | Promise<T>), exclusive: true): Promise<T | false>;
34
+ export function withLoading<T>(model: IValueModel<boolean>, cb: () => (T | Promise<T>), exclusive: 'throw'): Promise<T>;
20
35
 
21
- try {
22
- const res = await cb();
23
- return res;
24
- } finally {
25
- this.decrement();
36
+ export async function withLoading<T>(this: void, model: IValueModel<boolean>, cb: () => (T | Promise<T>), exclusive: boolean | 'throw' = false): Promise<T | false> {
37
+ if (exclusive && model.value) {
38
+ if (exclusive === 'throw') {
39
+ throw new Error('Operation cannot be started because another one is in progress already.');
26
40
  }
41
+ return false;
42
+ }
43
+
44
+ model.value = true;
45
+
46
+ try {
47
+ const res = await cb();
48
+ return res;
49
+ } finally {
50
+ model.value = false;
27
51
  }
28
52
  }
@@ -51,19 +51,18 @@ describe('CommonModel', () => {
51
51
  });
52
52
  });
53
53
 
54
- describe('LoadingModel', () => {
54
+ describe('LoadingModel works', () => {
55
+ const worker = async () => {
56
+ await setTimeoutAsync(100);
57
+ return 100;
58
+ };
55
59
 
56
- it('works', async () => {
60
+ it('basic', async () => {
57
61
 
58
62
  const m = new LoadingModel();
59
- expect(m.value).toBe(0);
63
+ expect(m.value).toBe(false);
60
64
  expect(m.isLoading).toBeFalsy();
61
65
 
62
- const worker = async () => {
63
- await setTimeoutAsync(100);
64
- return 100;
65
- };
66
-
67
66
  const promise = m.useLoading(worker);
68
67
  expect(m.isLoading).toBeTruthy();
69
68
 
@@ -71,6 +70,26 @@ describe('LoadingModel', () => {
71
70
 
72
71
  expect(m.isLoading).toBeFalsy();
73
72
  });
73
+
74
+ it('with exclusive', async () => {
75
+ const m = new LoadingModel();
76
+
77
+ const first = m.useLoading(worker, true);
78
+ expect(m.isLoading).toBeTruthy();
79
+
80
+ const second = m.useLoading(worker, true);
81
+ expect(m.isLoading).toBeTruthy();
82
+
83
+ await expect(() => m.useLoading(worker, 'throw')).rejects.toThrow();
84
+
85
+ expect(m.isLoading).toBeTruthy();
86
+
87
+ await expect(second).resolves.toBe(false);
88
+ await expect(first).resolves.toBe(100);
89
+
90
+ expect(m.value).toBe(false);
91
+ expect(m.isLoading).toBeFalsy();
92
+ });
74
93
  });
75
94
 
76
95
  describe('Others', () => {
@@ -1,5 +1,9 @@
1
1
 
2
- export interface IValueModel<TValue> {
2
+ export interface IValueModelReadonly<TValue> {
3
+ readonly value: TValue;
4
+ }
5
+
6
+ export interface IValueModel<TValue> extends IValueModelReadonly<TValue> {
3
7
  value: TValue;
4
8
  }
5
9