@mic-rexjs/usecases-react 3.3.0 → 3.6.1
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/dist/index.min.js +1 -1
- package/dist/index.min.js.LICENSE.txt +0 -2
- package/es/components/RenderingEventTrigger/index.d.ts +2 -0
- package/es/components/RenderingEventTrigger/index.js +7 -0
- package/es/components/RenderingEventTrigger/types.d.ts +3 -0
- package/es/hooks/useContextualItem/index.js +2 -3
- package/es/hooks/useDepsKey/index.js +19 -9
- package/es/hooks/useEntity/index.d.ts +1 -4
- package/es/hooks/useEntity/index.js +59 -50
- package/es/hooks/useEntityInitializing/index.d.ts +2 -0
- package/es/hooks/useEntityInitializing/index.js +16 -0
- package/es/hooks/useExec/index.d.ts +2 -1
- package/es/hooks/useExec/index.js +3 -3
- package/es/hooks/useExec/types.d.ts +3 -0
- package/es/hooks/useProvider/index.d.ts +1 -1
- package/es/hooks/useProvider/index.js +5 -2
- package/es/hooks/useReducers/index.js +8 -6
- package/es/hooks/useStore/index.d.ts +1 -2
- package/es/hooks/useStore/index.js +55 -69
- package/es/hooks/useUseCase/index.js +5 -6
- package/es/hooks/useUseCase/index.test.js +92 -35
- package/es/index.d.ts +2 -2
- package/es/index.js +1 -1
- package/es/methods/runEntityInitializer/index.d.ts +3 -0
- package/es/methods/runEntityInitializer/index.js +67 -0
- package/es/methods/triggerWatchers/index.js +3 -3
- package/es/usecases/lifecycleEventUseCase/index.d.ts +3 -0
- package/es/usecases/lifecycleEventUseCase/index.js +34 -0
- package/es/usecases/lifecycleEventUseCase/types.d.ts +36 -0
- package/es/usecases/lifecycleEventUseCase/types.js +1 -0
- package/es/usecases/lifecycleUseCase/index.d.ts +2 -0
- package/es/usecases/lifecycleUseCase/index.js +21 -0
- package/es/usecases/lifecycleUseCase/types.d.ts +5 -0
- package/es/usecases/lifecycleUseCase/types.js +1 -0
- package/es/usecases/methodUseCase/index.js +4 -4
- package/es/usecases/objectUtilsUseCase/index.d.ts +2 -0
- package/es/usecases/objectUtilsUseCase/index.js +131 -0
- package/es/usecases/objectUtilsUseCase/types.d.ts +14 -0
- package/es/usecases/objectUtilsUseCase/types.js +1 -0
- package/lib/components/RenderingEventTrigger/index.js +13 -0
- package/lib/hooks/useContextualItem/index.js +3 -4
- package/lib/hooks/useDepsKey/index.js +19 -10
- package/lib/hooks/useEntity/index.js +59 -50
- package/lib/hooks/useEntityInitializing/index.js +22 -0
- package/lib/hooks/useExec/index.js +3 -3
- package/lib/hooks/useProvider/index.js +5 -2
- package/lib/hooks/useReducers/index.js +9 -7
- package/lib/hooks/useStore/index.js +54 -67
- package/lib/hooks/useUseCase/index.js +6 -7
- package/lib/hooks/useUseCase/index.test.js +86 -35
- package/lib/index.js +3 -3
- package/lib/methods/runEntityInitializer/index.js +73 -0
- package/lib/methods/triggerWatchers/index.js +3 -3
- package/lib/usecases/lifecycleEventUseCase/index.js +40 -0
- package/lib/usecases/lifecycleEventUseCase/types.js +5 -0
- package/lib/usecases/{lifeCycleUseCase → lifecycleUseCase}/index.js +14 -12
- package/lib/usecases/lifecycleUseCase/types.js +5 -0
- package/lib/usecases/methodUseCase/index.js +4 -4
- package/lib/usecases/objectUtilsUseCase/index.js +140 -0
- package/lib/usecases/objectUtilsUseCase/types.js +5 -0
- package/package.json +3 -3
- package/es/usecases/lifeCycleUseCase/index.d.ts +0 -2
- package/es/usecases/lifeCycleUseCase/index.js +0 -18
- package/es/usecases/lifeCycleUseCase/types.d.ts +0 -9
- package/es/usecases/valueUseCase/index.d.ts +0 -2
- package/es/usecases/valueUseCase/index.js +0 -168
- package/es/usecases/valueUseCase/types.d.ts +0 -21
- package/lib/usecases/valueUseCase/index.js +0 -219
- /package/es/{usecases/lifeCycleUseCase → components/RenderingEventTrigger}/types.js +0 -0
- /package/es/{usecases/valueUseCase → hooks/useExec}/types.js +0 -0
- /package/lib/{usecases/lifeCycleUseCase → components/RenderingEventTrigger}/types.js +0 -0
- /package/lib/{usecases/valueUseCase → hooks/useExec}/types.js +0 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.objectUtilsUseCase = void 0;
|
|
8
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
9
|
+
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
|
10
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
11
|
+
var objectUtilsUseCase = exports.objectUtilsUseCase = function objectUtilsUseCase() {
|
|
12
|
+
var isObject = function isObject(target) {
|
|
13
|
+
if ((0, _typeof2["default"])(target) !== 'object' || target === null) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
};
|
|
18
|
+
var hasField = function hasField(target, field) {
|
|
19
|
+
var isObj = isObject(target);
|
|
20
|
+
if (isObj) {
|
|
21
|
+
return Object.hasOwn(target, field);
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
};
|
|
25
|
+
var isEqual = function isEqual(target1, target2, onFailed) {
|
|
26
|
+
if (target1 === target2) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
var isObject1 = isObject(target1);
|
|
30
|
+
var isObject2 = isObject(target2);
|
|
31
|
+
var keys1 = isObject1 ? Object.keys(target1) : [];
|
|
32
|
+
var keys2 = isObject2 ? Object.keys(target2) : [];
|
|
33
|
+
var hasCallback = typeof onFailed === 'function';
|
|
34
|
+
if (!isObject1 && !isObject2) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
var allMatched = true;
|
|
38
|
+
var length1 = keys1.length;
|
|
39
|
+
var keys = [].concat((0, _toConsumableArray2["default"])(keys1), (0, _toConsumableArray2["default"])(keys2));
|
|
40
|
+
for (var i = 0, length = keys.length; i < length; i++) {
|
|
41
|
+
var key = keys[i];
|
|
42
|
+
var isInKey1 = keys1.includes(key);
|
|
43
|
+
var isFromKeys2 = i >= length1;
|
|
44
|
+
var isUsed = isFromKeys2 && isInKey1;
|
|
45
|
+
if (isUsed) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
var hasField1 = hasField(target1, key);
|
|
49
|
+
var hasField2 = hasField(target2, key);
|
|
50
|
+
var value1 = hasField1 ? target1[key] : void 0;
|
|
51
|
+
var value2 = hasField2 ? target2[key] : void 0;
|
|
52
|
+
var matched = value1 === value2;
|
|
53
|
+
if (hasField1 && hasField2 && matched) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
allMatched = false;
|
|
57
|
+
if (hasCallback) {
|
|
58
|
+
onFailed(key, value2, value1);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
return allMatched;
|
|
64
|
+
};
|
|
65
|
+
var matchProperty = function matchProperty(target1, target2, fieldPath, onFailed) {
|
|
66
|
+
var allMatched = true;
|
|
67
|
+
var currentFieldPath = fieldPath;
|
|
68
|
+
var list = [{
|
|
69
|
+
fieldPaths: [],
|
|
70
|
+
newEntity: target2,
|
|
71
|
+
newValue: target2,
|
|
72
|
+
oldEntity: target1,
|
|
73
|
+
oldValue: target1
|
|
74
|
+
}];
|
|
75
|
+
do {
|
|
76
|
+
var _list$shift = list.shift(),
|
|
77
|
+
oldValue = _list$shift.oldValue,
|
|
78
|
+
newValue = _list$shift.newValue,
|
|
79
|
+
fieldPaths = _list$shift.fieldPaths;
|
|
80
|
+
var _ref = currentFieldPath.match(/^([^.]+)\.(.+)$/) || [],
|
|
81
|
+
_ref2 = (0, _slicedToArray2["default"])(_ref, 3),
|
|
82
|
+
_ref2$ = _ref2[1],
|
|
83
|
+
key = _ref2$ === void 0 ? currentFieldPath : _ref2$,
|
|
84
|
+
_ref2$2 = _ref2[2],
|
|
85
|
+
subFieldPath = _ref2$2 === void 0 ? '' : _ref2$2;
|
|
86
|
+
var hasOldField = hasField(oldValue, key);
|
|
87
|
+
var hasNewField = hasField(newValue, key);
|
|
88
|
+
if (hasOldField || hasNewField) {
|
|
89
|
+
var _ref3 = hasOldField ? [oldValue[key]] : [],
|
|
90
|
+
_ref4 = (0, _slicedToArray2["default"])(_ref3, 1),
|
|
91
|
+
oldSubValue = _ref4[0];
|
|
92
|
+
var _ref5 = hasNewField ? [newValue[key]] : [],
|
|
93
|
+
_ref6 = (0, _slicedToArray2["default"])(_ref5, 1),
|
|
94
|
+
newSubValue = _ref6[0];
|
|
95
|
+
var result = {
|
|
96
|
+
fieldPaths: [].concat((0, _toConsumableArray2["default"])(fieldPaths), [key]),
|
|
97
|
+
newEntity: target2,
|
|
98
|
+
newValue: newSubValue,
|
|
99
|
+
oldEntity: target1,
|
|
100
|
+
oldValue: oldSubValue
|
|
101
|
+
};
|
|
102
|
+
if (subFieldPath) {
|
|
103
|
+
currentFieldPath = subFieldPath;
|
|
104
|
+
list.push(result);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (newSubValue === oldSubValue) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
allMatched = false;
|
|
111
|
+
onFailed === null || onFailed === void 0 || onFailed(result);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (/^\d+^/.test(key)) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
var oldArray = Array.isArray(oldValue) ? oldValue : [];
|
|
118
|
+
var newArray = Array.isArray(newValue) ? newValue : [];
|
|
119
|
+
var oldLength = oldArray.length;
|
|
120
|
+
var newLength = newArray.length;
|
|
121
|
+
var length = Math.max(oldLength, newLength);
|
|
122
|
+
for (var i = 0; i < length; i++) {
|
|
123
|
+
list.push({
|
|
124
|
+
fieldPaths: [].concat((0, _toConsumableArray2["default"])(fieldPaths), ["".concat(i)]),
|
|
125
|
+
newEntity: target2,
|
|
126
|
+
newValue: newArray[i],
|
|
127
|
+
oldEntity: target1,
|
|
128
|
+
oldValue: oldArray[i]
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
} while (list.length > 0);
|
|
132
|
+
return allMatched;
|
|
133
|
+
};
|
|
134
|
+
return {
|
|
135
|
+
hasField: hasField,
|
|
136
|
+
isObject: isObject,
|
|
137
|
+
isEqual: isEqual,
|
|
138
|
+
matchProperty: matchProperty
|
|
139
|
+
};
|
|
140
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mic-rexjs/usecases-react",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.1",
|
|
4
4
|
"description": "React-based solution for use usecases of Clean Architecture",
|
|
5
5
|
"private": false,
|
|
6
6
|
"sideEffects": false,
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"license": "MIT",
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@mic-rexjs/usecases": "^4.
|
|
38
|
+
"@mic-rexjs/usecases": "^4.8.0",
|
|
39
39
|
"react": ">=16.8.0"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"@eslint/js": "^10.0.1",
|
|
55
55
|
"@ianvs/prettier-plugin-sort-imports": "^4.7.1",
|
|
56
56
|
"@jest/globals": "^30.3.0",
|
|
57
|
-
"@mic-rexjs/usecases": "^4.0
|
|
57
|
+
"@mic-rexjs/usecases": "^4.8.0",
|
|
58
58
|
"@testing-library/dom": "^10.4.1",
|
|
59
59
|
"@testing-library/react": "^16.3.2",
|
|
60
60
|
"@types/node": "^25.6.0",
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { entityUseCase } from '@mic-rexjs/usecases';
|
|
2
|
-
export const lifeCycleUseCase = function () {
|
|
3
|
-
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
4
|
-
const onBind = options.onBind,
|
|
5
|
-
onUnbind = options.onUnbind;
|
|
6
|
-
const entityReducers = entityUseCase();
|
|
7
|
-
const bindEntity = entity => {
|
|
8
|
-
onBind === null || onBind === void 0 || onBind(entity);
|
|
9
|
-
};
|
|
10
|
-
const unbindEntity = entity => {
|
|
11
|
-
onUnbind === null || onUnbind === void 0 || onUnbind(entity);
|
|
12
|
-
};
|
|
13
|
-
return {
|
|
14
|
-
...entityReducers,
|
|
15
|
-
bindEntity,
|
|
16
|
-
unbindEntity
|
|
17
|
-
};
|
|
18
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { EntityReducers } from '@mic-rexjs/usecases';
|
|
2
|
-
export interface LifeCycleUseCaseOptions<T> {
|
|
3
|
-
onBind?(entity: T): void;
|
|
4
|
-
onUnbind?(entity: T): void;
|
|
5
|
-
}
|
|
6
|
-
export type LifeCycleReducers<T> = EntityReducers<T, {
|
|
7
|
-
bindEntity(entity: T): void;
|
|
8
|
-
unbindEntity(entity: T): void;
|
|
9
|
-
}>;
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
-
import { createUseCase, entityUseCase, utilsUseCase } from '@mic-rexjs/usecases';
|
|
3
|
-
export const valueUseCase = createUseCase(() => {
|
|
4
|
-
return () => {
|
|
5
|
-
const entityReducers = entityUseCase();
|
|
6
|
-
const _utilsUseCase = utilsUseCase(),
|
|
7
|
-
createKey = _utilsUseCase.createKey;
|
|
8
|
-
let prevKeyIndex = createKey();
|
|
9
|
-
const isObjectValue = entity => {
|
|
10
|
-
if (typeof entity !== 'object' || entity === null) {
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
return true;
|
|
14
|
-
};
|
|
15
|
-
const hasField = (entity, field) => {
|
|
16
|
-
const isObject = isObjectValue(entity);
|
|
17
|
-
if (isObject) {
|
|
18
|
-
return Object.hasOwn(entity, field);
|
|
19
|
-
}
|
|
20
|
-
return false;
|
|
21
|
-
};
|
|
22
|
-
const isValueChanged = (entity, value) => {
|
|
23
|
-
return entity !== value;
|
|
24
|
-
};
|
|
25
|
-
const matchValue = (entity, value, onMatchPropertyFailed) => {
|
|
26
|
-
if (entity === value) {
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
29
|
-
const isObject1 = isObjectValue(entity);
|
|
30
|
-
const isObject2 = isObjectValue(value);
|
|
31
|
-
const keys1 = isObject1 ? Object.keys(entity) : [];
|
|
32
|
-
const keys2 = isObject2 ? Object.keys(value) : [];
|
|
33
|
-
const hasCallback = typeof onMatchPropertyFailed === 'function';
|
|
34
|
-
if (!isObject1 && !isObject2) {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
let allMatched = true;
|
|
38
|
-
const length1 = keys1.length;
|
|
39
|
-
const keys = [...keys1, ...keys2];
|
|
40
|
-
for (let i = 0, length = keys.length; i < length; i++) {
|
|
41
|
-
const key = keys[i];
|
|
42
|
-
const isInKey1 = keys1.includes(key);
|
|
43
|
-
const isFromKeys2 = i >= length1;
|
|
44
|
-
const isUsed = isFromKeys2 && isInKey1;
|
|
45
|
-
if (isUsed) {
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
const hasField1 = hasField(entity, key);
|
|
49
|
-
const hasField2 = hasField(value, key);
|
|
50
|
-
const value1 = hasField1 ? entity[key] : void 0;
|
|
51
|
-
const value2 = hasField2 ? value[key] : void 0;
|
|
52
|
-
const matched = value1 === value2;
|
|
53
|
-
if (hasField1 && hasField2 && matched) {
|
|
54
|
-
continue;
|
|
55
|
-
}
|
|
56
|
-
allMatched = false;
|
|
57
|
-
if (hasCallback) {
|
|
58
|
-
onMatchPropertyFailed(key, value2, value1);
|
|
59
|
-
continue;
|
|
60
|
-
}
|
|
61
|
-
break;
|
|
62
|
-
}
|
|
63
|
-
return allMatched;
|
|
64
|
-
};
|
|
65
|
-
const matchProperty = (entity, value, fieldPath, onFailed) => {
|
|
66
|
-
let allMatched = true;
|
|
67
|
-
let currentFieldPath = fieldPath;
|
|
68
|
-
const list = [{
|
|
69
|
-
fieldPaths: [],
|
|
70
|
-
newEntity: value,
|
|
71
|
-
newValue: value,
|
|
72
|
-
oldEntity: entity,
|
|
73
|
-
oldValue: entity
|
|
74
|
-
}];
|
|
75
|
-
do {
|
|
76
|
-
const _list$shift = list.shift(),
|
|
77
|
-
oldValue = _list$shift.oldValue,
|
|
78
|
-
newValue = _list$shift.newValue,
|
|
79
|
-
fieldPaths = _list$shift.fieldPaths;
|
|
80
|
-
const _ref = currentFieldPath.match(/^([^.]+)\.(.+)$/) || [],
|
|
81
|
-
_ref2 = _slicedToArray(_ref, 3),
|
|
82
|
-
_ref2$ = _ref2[1],
|
|
83
|
-
key = _ref2$ === void 0 ? currentFieldPath : _ref2$,
|
|
84
|
-
_ref2$2 = _ref2[2],
|
|
85
|
-
subFieldPath = _ref2$2 === void 0 ? '' : _ref2$2;
|
|
86
|
-
const hasOldField = hasField(oldValue, key);
|
|
87
|
-
const hasNewField = hasField(newValue, key);
|
|
88
|
-
if (hasOldField || hasNewField) {
|
|
89
|
-
const _ref3 = hasOldField ? [oldValue[key]] : [],
|
|
90
|
-
_ref4 = _slicedToArray(_ref3, 1),
|
|
91
|
-
oldSubValue = _ref4[0];
|
|
92
|
-
const _ref5 = hasNewField ? [newValue[key]] : [],
|
|
93
|
-
_ref6 = _slicedToArray(_ref5, 1),
|
|
94
|
-
newSubValue = _ref6[0];
|
|
95
|
-
const result = {
|
|
96
|
-
fieldPaths: [...fieldPaths, key],
|
|
97
|
-
newEntity: value,
|
|
98
|
-
newValue: newSubValue,
|
|
99
|
-
oldEntity: entity,
|
|
100
|
-
oldValue: oldSubValue
|
|
101
|
-
};
|
|
102
|
-
if (subFieldPath) {
|
|
103
|
-
currentFieldPath = subFieldPath;
|
|
104
|
-
list.push(result);
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
if (newSubValue === oldSubValue) {
|
|
108
|
-
continue;
|
|
109
|
-
}
|
|
110
|
-
allMatched = false;
|
|
111
|
-
onFailed === null || onFailed === void 0 || onFailed(result);
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
114
|
-
if (/^\d+^/.test(key)) {
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
const oldArray = Array.isArray(oldValue) ? oldValue : [];
|
|
118
|
-
const newArray = Array.isArray(newValue) ? newValue : [];
|
|
119
|
-
const oldLength = oldArray.length;
|
|
120
|
-
const newLength = newArray.length;
|
|
121
|
-
const length = Math.max(oldLength, newLength);
|
|
122
|
-
for (let i = 0; i < length; i++) {
|
|
123
|
-
list.push({
|
|
124
|
-
fieldPaths: [...fieldPaths, `${i}`],
|
|
125
|
-
newEntity: value,
|
|
126
|
-
newValue: newArray[i],
|
|
127
|
-
oldEntity: entity,
|
|
128
|
-
oldValue: oldArray[i]
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
} while (list.length > 0);
|
|
132
|
-
return allMatched;
|
|
133
|
-
};
|
|
134
|
-
const recordValue = function* (entity, value) {
|
|
135
|
-
const changed = isValueChanged(entity, value);
|
|
136
|
-
if (!changed) {
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
yield value;
|
|
140
|
-
return true;
|
|
141
|
-
};
|
|
142
|
-
const recordValueMatch = function* (entity, value) {
|
|
143
|
-
const matched = matchValue(entity, value);
|
|
144
|
-
if (matched) {
|
|
145
|
-
return true;
|
|
146
|
-
}
|
|
147
|
-
yield value;
|
|
148
|
-
return false;
|
|
149
|
-
};
|
|
150
|
-
const recordValueMatchWith = function* (entity, value, key) {
|
|
151
|
-
const matched = yield* recordValueMatch(entity, value);
|
|
152
|
-
if (!matched) {
|
|
153
|
-
prevKeyIndex = key !== null && key !== void 0 ? key : createKey();
|
|
154
|
-
}
|
|
155
|
-
return prevKeyIndex;
|
|
156
|
-
};
|
|
157
|
-
return {
|
|
158
|
-
...entityReducers,
|
|
159
|
-
isObjectValue,
|
|
160
|
-
isValueChanged,
|
|
161
|
-
matchValue,
|
|
162
|
-
matchProperty,
|
|
163
|
-
recordValue,
|
|
164
|
-
recordValueMatch,
|
|
165
|
-
recordValueMatchWith
|
|
166
|
-
};
|
|
167
|
-
};
|
|
168
|
-
});
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { EntityGenerator, EntityReducers } from '@mic-rexjs/usecases';
|
|
2
|
-
import React from 'react';
|
|
3
|
-
import { MatchPropertyFailedResult } from '../../entities/matchPropertyFailedResult/types';
|
|
4
|
-
export interface MatchPropertyFailedCallback<T> {
|
|
5
|
-
(result: MatchPropertyFailedResult<T>): void;
|
|
6
|
-
}
|
|
7
|
-
export interface MatchValuePropertyFailedCallback<T> {
|
|
8
|
-
<K extends keyof T>(key: K, newValue: T[K], oldValue: T[K]): void;
|
|
9
|
-
}
|
|
10
|
-
export type ValueReducers<T> = EntityReducers<T, {
|
|
11
|
-
isObjectValue(entity: T): boolean;
|
|
12
|
-
isValueChanged<S extends T>(entity: S, value: S): boolean;
|
|
13
|
-
matchValue<S extends T>(entity: S, value: S, onMatchPropertyFailed?: MatchValuePropertyFailedCallback<S>): boolean;
|
|
14
|
-
matchProperty<S extends T>(entity: S, value: S, fieldPath: string, onFailed?: MatchPropertyFailedCallback<S>): boolean;
|
|
15
|
-
recordValue<S extends T>(entity: S, value: S): EntityGenerator<S, boolean>;
|
|
16
|
-
recordValueMatch<S extends T>(entity: S, value: S): EntityGenerator<S, boolean>;
|
|
17
|
-
recordValueMatchWith<S extends T>(entity: S, value: S, key?: React.Key): EntityGenerator<S, React.Key>;
|
|
18
|
-
}>;
|
|
19
|
-
export interface ValueUseCase {
|
|
20
|
-
<T>(): ValueReducers<T>;
|
|
21
|
-
}
|
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports.valueUseCase = void 0;
|
|
8
|
-
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
9
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
10
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
11
|
-
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
|
12
|
-
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
13
|
-
var _usecases = require("@mic-rexjs/usecases");
|
|
14
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
15
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
16
|
-
var valueUseCase = exports.valueUseCase = (0, _usecases.createUseCase)(function () {
|
|
17
|
-
return function () {
|
|
18
|
-
var entityReducers = (0, _usecases.entityUseCase)();
|
|
19
|
-
var _utilsUseCase = (0, _usecases.utilsUseCase)(),
|
|
20
|
-
createKey = _utilsUseCase.createKey;
|
|
21
|
-
var prevKeyIndex = createKey();
|
|
22
|
-
var isObjectValue = function isObjectValue(entity) {
|
|
23
|
-
if ((0, _typeof2["default"])(entity) !== 'object' || entity === null) {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
return true;
|
|
27
|
-
};
|
|
28
|
-
var hasField = function hasField(entity, field) {
|
|
29
|
-
var isObject = isObjectValue(entity);
|
|
30
|
-
if (isObject) {
|
|
31
|
-
return Object.hasOwn(entity, field);
|
|
32
|
-
}
|
|
33
|
-
return false;
|
|
34
|
-
};
|
|
35
|
-
var isValueChanged = function isValueChanged(entity, value) {
|
|
36
|
-
return entity !== value;
|
|
37
|
-
};
|
|
38
|
-
var matchValue = function matchValue(entity, value, onMatchPropertyFailed) {
|
|
39
|
-
if (entity === value) {
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
|
-
var isObject1 = isObjectValue(entity);
|
|
43
|
-
var isObject2 = isObjectValue(value);
|
|
44
|
-
var keys1 = isObject1 ? Object.keys(entity) : [];
|
|
45
|
-
var keys2 = isObject2 ? Object.keys(value) : [];
|
|
46
|
-
var hasCallback = typeof onMatchPropertyFailed === 'function';
|
|
47
|
-
if (!isObject1 && !isObject2) {
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
var allMatched = true;
|
|
51
|
-
var length1 = keys1.length;
|
|
52
|
-
var keys = [].concat((0, _toConsumableArray2["default"])(keys1), (0, _toConsumableArray2["default"])(keys2));
|
|
53
|
-
for (var i = 0, length = keys.length; i < length; i++) {
|
|
54
|
-
var key = keys[i];
|
|
55
|
-
var isInKey1 = keys1.includes(key);
|
|
56
|
-
var isFromKeys2 = i >= length1;
|
|
57
|
-
var isUsed = isFromKeys2 && isInKey1;
|
|
58
|
-
if (isUsed) {
|
|
59
|
-
continue;
|
|
60
|
-
}
|
|
61
|
-
var hasField1 = hasField(entity, key);
|
|
62
|
-
var hasField2 = hasField(value, key);
|
|
63
|
-
var value1 = hasField1 ? entity[key] : void 0;
|
|
64
|
-
var value2 = hasField2 ? value[key] : void 0;
|
|
65
|
-
var matched = value1 === value2;
|
|
66
|
-
if (hasField1 && hasField2 && matched) {
|
|
67
|
-
continue;
|
|
68
|
-
}
|
|
69
|
-
allMatched = false;
|
|
70
|
-
if (hasCallback) {
|
|
71
|
-
onMatchPropertyFailed(key, value2, value1);
|
|
72
|
-
continue;
|
|
73
|
-
}
|
|
74
|
-
break;
|
|
75
|
-
}
|
|
76
|
-
return allMatched;
|
|
77
|
-
};
|
|
78
|
-
var matchProperty = function matchProperty(entity, value, fieldPath, onFailed) {
|
|
79
|
-
var allMatched = true;
|
|
80
|
-
var currentFieldPath = fieldPath;
|
|
81
|
-
var list = [{
|
|
82
|
-
fieldPaths: [],
|
|
83
|
-
newEntity: value,
|
|
84
|
-
newValue: value,
|
|
85
|
-
oldEntity: entity,
|
|
86
|
-
oldValue: entity
|
|
87
|
-
}];
|
|
88
|
-
do {
|
|
89
|
-
var _list$shift = list.shift(),
|
|
90
|
-
oldValue = _list$shift.oldValue,
|
|
91
|
-
newValue = _list$shift.newValue,
|
|
92
|
-
fieldPaths = _list$shift.fieldPaths;
|
|
93
|
-
var _ref = currentFieldPath.match(/^([^.]+)\.(.+)$/) || [],
|
|
94
|
-
_ref2 = (0, _slicedToArray2["default"])(_ref, 3),
|
|
95
|
-
_ref2$ = _ref2[1],
|
|
96
|
-
key = _ref2$ === void 0 ? currentFieldPath : _ref2$,
|
|
97
|
-
_ref2$2 = _ref2[2],
|
|
98
|
-
subFieldPath = _ref2$2 === void 0 ? '' : _ref2$2;
|
|
99
|
-
var hasOldField = hasField(oldValue, key);
|
|
100
|
-
var hasNewField = hasField(newValue, key);
|
|
101
|
-
if (hasOldField || hasNewField) {
|
|
102
|
-
var _ref3 = hasOldField ? [oldValue[key]] : [],
|
|
103
|
-
_ref4 = (0, _slicedToArray2["default"])(_ref3, 1),
|
|
104
|
-
oldSubValue = _ref4[0];
|
|
105
|
-
var _ref5 = hasNewField ? [newValue[key]] : [],
|
|
106
|
-
_ref6 = (0, _slicedToArray2["default"])(_ref5, 1),
|
|
107
|
-
newSubValue = _ref6[0];
|
|
108
|
-
var result = {
|
|
109
|
-
fieldPaths: [].concat((0, _toConsumableArray2["default"])(fieldPaths), [key]),
|
|
110
|
-
newEntity: value,
|
|
111
|
-
newValue: newSubValue,
|
|
112
|
-
oldEntity: entity,
|
|
113
|
-
oldValue: oldSubValue
|
|
114
|
-
};
|
|
115
|
-
if (subFieldPath) {
|
|
116
|
-
currentFieldPath = subFieldPath;
|
|
117
|
-
list.push(result);
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
if (newSubValue === oldSubValue) {
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
123
|
-
allMatched = false;
|
|
124
|
-
onFailed === null || onFailed === void 0 || onFailed(result);
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
if (/^\d+^/.test(key)) {
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
var oldArray = Array.isArray(oldValue) ? oldValue : [];
|
|
131
|
-
var newArray = Array.isArray(newValue) ? newValue : [];
|
|
132
|
-
var oldLength = oldArray.length;
|
|
133
|
-
var newLength = newArray.length;
|
|
134
|
-
var length = Math.max(oldLength, newLength);
|
|
135
|
-
for (var i = 0; i < length; i++) {
|
|
136
|
-
list.push({
|
|
137
|
-
fieldPaths: [].concat((0, _toConsumableArray2["default"])(fieldPaths), ["".concat(i)]),
|
|
138
|
-
newEntity: value,
|
|
139
|
-
newValue: newArray[i],
|
|
140
|
-
oldEntity: entity,
|
|
141
|
-
oldValue: oldArray[i]
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
} while (list.length > 0);
|
|
145
|
-
return allMatched;
|
|
146
|
-
};
|
|
147
|
-
var recordValue = /*#__PURE__*/_regenerator["default"].mark(function recordValue(entity, value) {
|
|
148
|
-
var changed;
|
|
149
|
-
return _regenerator["default"].wrap(function (_context) {
|
|
150
|
-
while (1) switch (_context.prev = _context.next) {
|
|
151
|
-
case 0:
|
|
152
|
-
changed = isValueChanged(entity, value);
|
|
153
|
-
if (changed) {
|
|
154
|
-
_context.next = 1;
|
|
155
|
-
break;
|
|
156
|
-
}
|
|
157
|
-
return _context.abrupt("return", false);
|
|
158
|
-
case 1:
|
|
159
|
-
_context.next = 2;
|
|
160
|
-
return value;
|
|
161
|
-
case 2:
|
|
162
|
-
return _context.abrupt("return", true);
|
|
163
|
-
case 3:
|
|
164
|
-
case "end":
|
|
165
|
-
return _context.stop();
|
|
166
|
-
}
|
|
167
|
-
}, recordValue);
|
|
168
|
-
});
|
|
169
|
-
var recordValueMatch = /*#__PURE__*/_regenerator["default"].mark(function recordValueMatch(entity, value) {
|
|
170
|
-
var matched;
|
|
171
|
-
return _regenerator["default"].wrap(function (_context2) {
|
|
172
|
-
while (1) switch (_context2.prev = _context2.next) {
|
|
173
|
-
case 0:
|
|
174
|
-
matched = matchValue(entity, value);
|
|
175
|
-
if (!matched) {
|
|
176
|
-
_context2.next = 1;
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
return _context2.abrupt("return", true);
|
|
180
|
-
case 1:
|
|
181
|
-
_context2.next = 2;
|
|
182
|
-
return value;
|
|
183
|
-
case 2:
|
|
184
|
-
return _context2.abrupt("return", false);
|
|
185
|
-
case 3:
|
|
186
|
-
case "end":
|
|
187
|
-
return _context2.stop();
|
|
188
|
-
}
|
|
189
|
-
}, recordValueMatch);
|
|
190
|
-
});
|
|
191
|
-
var recordValueMatchWith = /*#__PURE__*/_regenerator["default"].mark(function recordValueMatchWith(entity, value, key) {
|
|
192
|
-
var matched;
|
|
193
|
-
return _regenerator["default"].wrap(function (_context3) {
|
|
194
|
-
while (1) switch (_context3.prev = _context3.next) {
|
|
195
|
-
case 0:
|
|
196
|
-
return _context3.delegateYield(recordValueMatch(entity, value), "t0", 1);
|
|
197
|
-
case 1:
|
|
198
|
-
matched = _context3.t0;
|
|
199
|
-
if (!matched) {
|
|
200
|
-
prevKeyIndex = key !== null && key !== void 0 ? key : createKey();
|
|
201
|
-
}
|
|
202
|
-
return _context3.abrupt("return", prevKeyIndex);
|
|
203
|
-
case 2:
|
|
204
|
-
case "end":
|
|
205
|
-
return _context3.stop();
|
|
206
|
-
}
|
|
207
|
-
}, recordValueMatchWith);
|
|
208
|
-
});
|
|
209
|
-
return _objectSpread(_objectSpread({}, entityReducers), {}, {
|
|
210
|
-
isObjectValue: isObjectValue,
|
|
211
|
-
isValueChanged: isValueChanged,
|
|
212
|
-
matchValue: matchValue,
|
|
213
|
-
matchProperty: matchProperty,
|
|
214
|
-
recordValue: recordValue,
|
|
215
|
-
recordValueMatch: recordValueMatch,
|
|
216
|
-
recordValueMatchWith: recordValueMatchWith
|
|
217
|
-
});
|
|
218
|
-
};
|
|
219
|
-
});
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|