@zajno/common 1.3.10 → 1.3.11

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 (65) hide show
  1. package/coverage/clover.xml +56 -41
  2. package/coverage/coverage-final.json +2 -1
  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 +15 -15
  22. package/coverage/lcov-report/src/fields/update.ts.html +69 -12
  23. package/coverage/lcov-report/src/functions.ts.html +122 -0
  24. package/coverage/lcov-report/src/index.html +24 -9
  25. package/coverage/lcov-report/src/lazy.light.ts.html +1 -1
  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 +1 -1
  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 +1 -1
  48. package/coverage/lcov-report/src/viewModels/MultiSelectModel.ts.html +1 -1
  49. package/coverage/lcov-report/src/viewModels/NumberModel.ts.html +1 -1
  50. package/coverage/lcov-report/src/viewModels/SelectModel.ts.html +1 -1
  51. package/coverage/lcov-report/src/viewModels/Validatable.ts.html +1 -1
  52. package/coverage/lcov-report/src/viewModels/index.html +1 -1
  53. package/coverage/lcov-report/src/viewModels/wrappers.ts.html +1 -1
  54. package/coverage/lcov.info +150 -97
  55. package/lib/fields/update.d.ts +8 -2
  56. package/lib/fields/update.d.ts.map +1 -1
  57. package/lib/fields/update.js +13 -1
  58. package/lib/fields/update.js.map +1 -1
  59. package/lib/functions.d.ts +1 -2
  60. package/lib/functions.d.ts.map +1 -1
  61. package/lib/functions.js.map +1 -1
  62. package/package.json +1 -1
  63. package/src/fields/update.ts +22 -3
  64. package/src/functions.ts +1 -3
  65. package/tsconfig.json +6 -4
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.updateArray = exports.updateFieldExtended = exports.updateField = void 0;
4
+ const functions_1 = require("../functions");
4
5
  function updateField(target, source, diff, key, hasChanged = null) {
5
6
  return updateFieldExtended(target, source, diff, t => t[key], (t, v) => t[key] = v, hasChanged);
6
7
  }
@@ -19,6 +20,7 @@ exports.updateFieldExtended = updateFieldExtended;
19
20
  const DefaultComparator = (v1, v2) => v1 === v2;
20
21
  const DefaultUpdater = (v1, v2) => Object.assign(v1, v2);
21
22
  function updateArray(target, source, options) {
23
+ var _a, _b, _c;
22
24
  if (!source) {
23
25
  return { changed: 0, result: target };
24
26
  }
@@ -28,11 +30,16 @@ function updateArray(target, source, options) {
28
30
  : [];
29
31
  const comparator = (options === null || options === void 0 ? void 0 : options.comparator) || DefaultComparator;
30
32
  const updater = (options === null || options === void 0 ? void 0 : options.updater) || DefaultUpdater;
33
+ const onDeleted = (_a = options === null || options === void 0 ? void 0 : options.hooks) === null || _a === void 0 ? void 0 : _a.onDeleted;
34
+ const onUpdate = (_b = options === null || options === void 0 ? void 0 : options.hooks) === null || _b === void 0 ? void 0 : _b.onUpdated;
35
+ const onAdded = (_c = options === null || options === void 0 ? void 0 : options.hooks) === null || _c === void 0 ? void 0 : _c.onAdded;
31
36
  // remove all missing elements
32
37
  if (!(options === null || options === void 0 ? void 0 : options.additive)) {
33
38
  for (let i = 0; i < result.length; ++i) {
34
39
  if (source.find(item => comparator(item, result[i])) == null) {
35
- result.splice(i, 1);
40
+ // DELETE
41
+ const removed = result.splice(i, 1);
42
+ functions_1.safeCall(onDeleted, removed[0]);
36
43
  ++changed;
37
44
  --i;
38
45
  }
@@ -44,10 +51,15 @@ function updateArray(target, source, options) {
44
51
  const existingItem = result[existingIndex];
45
52
  if (existingIndex < 0) {
46
53
  result.push(i);
54
+ functions_1.safeCall(onAdded, i);
47
55
  ++changed;
48
56
  }
49
57
  else if (typeof existingItem === 'object') {
58
+ const before = onUpdate != null ? { ...existingItem } : undefined;
50
59
  result[existingIndex] = updater(existingItem, i);
60
+ if (onUpdate != null) {
61
+ onUpdate(before, result[existingIndex]);
62
+ }
51
63
  }
52
64
  });
53
65
  const sorter = options === null || options === void 0 ? void 0 : options.sorter;
@@ -1 +1 @@
1
- {"version":3,"file":"update.js","sourceRoot":"","sources":["../../src/fields/update.ts"],"names":[],"mappings":";;;AAUA,SAAgB,WAAW,CAAI,MAAS,EAAE,MAAkB,EAAE,IAAgB,EAAE,GAAY,EAAE,aAAiC,IAAI;IAC/H,OAAO,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,UAAU,CAAC,CAAC;AACpG,CAAC;AAFD,kCAEC;AAED,SAAgB,mBAAmB,CAC/B,MAAS,EACT,MAAkB,EAClB,IAAgB,EAChB,GAAqB,EACrB,GAAqB,EACrB,aAAiC,IAAI;IAGrC,UAAU,GAAG,UAAU,IAAI,CAAC,CAAC,EAAK,EAAE,EAAK,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAErE,IAAI,OAAO,GAAG,IAAI,CAAC;IAEnB,IAAI,GAAG,CAAC,MAAM,CAAC,KAAK,SAAS,IAAI,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE;QACzD,OAAO,GAAG,IAAI,CAAC;QACf,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;QACvB,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;KAC5B;IAED,OAAO,OAAO,CAAC;AAEnB,CAAC;AArBD,kDAqBC;AAED,MAAM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AAM,CAAC,aAAa,CAAC,CAAC;QAC3C,IAAI,aAAa,GAAG,CAAC,EAAE;YACnB,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACf,oBAAQ,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;YACrB,EAAE,OAAO,CAAC;SACb;aAAM,IAAI,OAAO,YAAY,KAAK,QAAQ,EAAE;YACzC,MAAM,MAAM,GAAG,QAAQ,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,GAAG,YAAY,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;YAClE,MAAM,CAAC,aAAa,CAAC,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC;YACjD,IAAI,QAAQ,IAAI,IAAI,EAAE;gBAClB,QAAQ,CAAC,MAAO,EAAE,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC;aAC5C;SACJ;IACL,CAAC,CAAC,CAAC;IAEH,MAAM,MAAM,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAC;IAC/B,IAAI,MAAM,KAAK,IAAI,EAAE;QACjB,MAAM,CAAC,IAAI,CAAC,MAAM,IAAI,SAAS,CAAC,CAAC;KACpC;IAED,OAAO;QACH,MAAM;QACN,OAAO;KACV,CAAC;AACN,CAAC;AA5DD,kCA4DC"}
@@ -4,7 +4,6 @@ declare type PropertiesOf<T> = Exclude<{
4
4
  export declare type RemoveFunctionFields<T> = {
5
5
  readonly [P in PropertiesOf<T>]: T[P];
6
6
  };
7
- export declare function safeCall<T extends (...args: any) => any>(cb: T, ...args: Parameters<T>): ReturnType<T>;
8
- export declare type SortingFunction<T> = (i1: T, i2: T) => number;
7
+ export declare function safeCall<T extends (...args: any) => any>(cb: T | undefined, ...args: Parameters<T>): ReturnType<T> | void;
9
8
  export {};
10
9
  //# sourceMappingURL=functions.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"functions.d.ts","sourceRoot":"","sources":["../src/functions.ts"],"names":[],"mappings":"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C,GAAG,IAAI,CAIzH"}
@@ -1 +1 @@
1
- {"version":3,"file":"functions.js","sourceRoot":"","sources":["../src/functions.ts"],"names":[],"mappings":";;;AASA,SAAgB,QAAQ,CAAkC,EAAK,EAAE,GAAG,IAAmB;IACnF,IAAI,EAAE,EAAE;QACJ,OAAO,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;KAC/B;AACL,CAAC;AAJD,4BAIC"}
1
+ {"version":3,"file":"functions.js","sourceRoot":"","sources":["../src/functions.ts"],"names":[],"mappings":";;;AASA,SAAgB,QAAQ,CAAkC,EAAiB,EAAE,GAAG,IAAmB;IAC/F,IAAI,EAAE,EAAE;QACJ,OAAO,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;KAC/B;AACL,CAAC;AAJD,4BAIC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zajno/common",
3
- "version": "1.3.10",
3
+ "version": "1.3.11",
4
4
  "description": "Zajno's re-usable utilities for JS/TS projects",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,3 +1,4 @@
1
+ import { safeCall } from '../functions';
1
2
  import { Comparator } from '../types';
2
3
 
3
4
  export namespace Fields {
@@ -8,7 +9,7 @@ export namespace Fields {
8
9
  }
9
10
 
10
11
 
11
- export function updateField<T>(target: T, source: Partial<T>, diff: Partial<T>, key: keyof T, hasChanged: Fields.Comparer<T> = null): boolean {
12
+ export function updateField<T>(target: T, source: Partial<T>, diff: Partial<T>, key: keyof T, hasChanged: null | Fields.Comparer<T> = null): boolean {
12
13
  return updateFieldExtended(target, source, diff, t => t[key], (t, v) => t[key] = v, hasChanged);
13
14
  }
14
15
 
@@ -18,7 +19,7 @@ export function updateFieldExtended<T>(
18
19
  diff: Partial<T>,
19
20
  get: Fields.Getter<T>,
20
21
  set: Fields.Setter<T>,
21
- hasChanged: Fields.Comparer<T> = null,
22
+ hasChanged: null | Fields.Comparer<T> = null,
22
23
  ): boolean {
23
24
 
24
25
  hasChanged = hasChanged || ((v1: T, v2: T) => (get(v1) !== get(v2)));
@@ -44,6 +45,13 @@ export type UpdateArrayOptions<T> = {
44
45
  comparator?: Comparator<T>,
45
46
  updater?: Fields.Updater<T>,
46
47
  sorter?: Comparator<T, number> | null | undefined,
48
+ hooks?: UpdateArrayHooks<T>,
49
+ };
50
+
51
+ export type UpdateArrayHooks<T> = {
52
+ onAdded?: (item: T) => void,
53
+ onDeleted?: (item: T) => void,
54
+ onUpdated?: (previous: T, next: T) => void,
47
55
  };
48
56
 
49
57
  export function updateArray<T>(
@@ -62,12 +70,18 @@ export function updateArray<T>(
62
70
 
63
71
  const comparator = options?.comparator || DefaultComparator;
64
72
  const updater = options?.updater || DefaultUpdater;
73
+ const onDeleted = options?.hooks?.onDeleted;
74
+ const onUpdate = options?.hooks?.onUpdated;
75
+ const onAdded = options?.hooks?.onAdded;
65
76
 
66
77
  // remove all missing elements
67
78
  if (!options?.additive) {
68
79
  for (let i = 0; i < result.length; ++i) {
69
80
  if (source.find(item => comparator(item, result[i])) == null) {
70
- result.splice(i, 1);
81
+ // DELETE
82
+ const removed = result.splice(i, 1);
83
+ safeCall(onDeleted, removed[0]);
84
+
71
85
  ++changed;
72
86
  --i;
73
87
  }
@@ -80,9 +94,14 @@ export function updateArray<T>(
80
94
  const existingItem = result[existingIndex];
81
95
  if (existingIndex < 0) {
82
96
  result.push(i);
97
+ safeCall(onAdded, i);
83
98
  ++changed;
84
99
  } else if (typeof existingItem === 'object') {
100
+ const before = onUpdate != null ? { ...existingItem } : undefined;
85
101
  result[existingIndex] = updater(existingItem, i);
102
+ if (onUpdate != null) {
103
+ onUpdate(before!, result[existingIndex]);
104
+ }
86
105
  }
87
106
  });
88
107
 
package/src/functions.ts CHANGED
@@ -7,10 +7,8 @@ export type RemoveFunctionFields<T> = {
7
7
  readonly [P in PropertiesOf<T>]: T[P];
8
8
  };
9
9
 
10
- export function safeCall<T extends (...args: any) => any>(cb: T, ...args: Parameters<T>): ReturnType<T> {
10
+ export function safeCall<T extends (...args: any) => any>(cb: T | undefined, ...args: Parameters<T>): ReturnType<T> | void {
11
11
  if (cb) {
12
12
  return cb.apply(null, args);
13
13
  }
14
14
  }
15
-
16
- export type SortingFunction<T> = (i1: T, i2: T) => number;
package/tsconfig.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "compilerOptions": {
3
+ "strict": false,
4
+ "noUnusedLocals": true,
5
+ "strictBindCallApply": true,
3
6
  "outDir": "./lib",
4
- "emitDecoratorMetadata": true,
5
7
  "experimentalDecorators": true,
6
- "strictBindCallApply": true,
8
+ "emitDecoratorMetadata": true,
9
+ "allowSyntheticDefaultImports": true,
7
10
  "skipLibCheck": true,
8
11
  "sourceMap": true,
9
- "allowSyntheticDefaultImports": true,
10
12
  "esModuleInterop": true,
11
13
  "target": "ES2019",
12
14
  "module": "CommonJS",
@@ -18,7 +20,7 @@
18
20
  "importHelpers": true,
19
21
  "baseUrl": "src",
20
22
  "paths": {
21
- }
23
+ },
22
24
  },
23
25
  "include": [
24
26
  "./src"