@mic-rexjs/usecases-react 3.1.1 → 3.1.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.
- package/dist/index.min.js +1 -1
- package/es/hooks/useEntity/index.js +4 -1
- package/es/hooks/useStore/index.js +19 -5
- package/es/hooks/useUseCase/index.test.js +118 -77
- package/es/usecases/valueUseCase/index.js +14 -37
- package/es/usecases/valueUseCase/types.d.ts +1 -2
- package/lib/hooks/useEntity/index.js +4 -1
- package/lib/hooks/useStore/index.js +19 -5
- package/lib/hooks/useUseCase/index.test.js +133 -84
- package/lib/usecases/valueUseCase/index.js +27 -63
- package/package.json +1 -1
|
@@ -19,11 +19,18 @@ var valueUseCase = exports.valueUseCase = (0, _usecases.createUseCase)(function
|
|
|
19
19
|
var _utilsUseCase = (0, _usecases.utilsUseCase)(),
|
|
20
20
|
createKey = _utilsUseCase.createKey;
|
|
21
21
|
var prevKeyIndex = createKey();
|
|
22
|
-
var
|
|
22
|
+
var isObjectValue = function isObjectValue(entity) {
|
|
23
23
|
if ((0, _typeof2["default"])(entity) !== 'object' || entity === null) {
|
|
24
24
|
return false;
|
|
25
25
|
}
|
|
26
|
-
return
|
|
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;
|
|
27
34
|
};
|
|
28
35
|
var isValueChanged = function isValueChanged(entity, value) {
|
|
29
36
|
return entity !== value;
|
|
@@ -32,8 +39,8 @@ var valueUseCase = exports.valueUseCase = (0, _usecases.createUseCase)(function
|
|
|
32
39
|
if (entity === value) {
|
|
33
40
|
return true;
|
|
34
41
|
}
|
|
35
|
-
var isObject1 = (
|
|
36
|
-
var isObject2 = (
|
|
42
|
+
var isObject1 = isObjectValue(entity);
|
|
43
|
+
var isObject2 = isObjectValue(value);
|
|
37
44
|
var keys1 = isObject1 ? Object.keys(entity) : [];
|
|
38
45
|
var keys2 = isObject2 ? Object.keys(value) : [];
|
|
39
46
|
var hasCallback = typeof onMatchPropertyFailed === 'function';
|
|
@@ -45,21 +52,18 @@ var valueUseCase = exports.valueUseCase = (0, _usecases.createUseCase)(function
|
|
|
45
52
|
var keys = [].concat((0, _toConsumableArray2["default"])(keys1), (0, _toConsumableArray2["default"])(keys2));
|
|
46
53
|
for (var i = 0, length = keys.length; i < length; i++) {
|
|
47
54
|
var key = keys[i];
|
|
48
|
-
var isFromKeys2 = i >= length1;
|
|
49
55
|
var isInKey1 = keys1.includes(key);
|
|
56
|
+
var isFromKeys2 = i >= length1;
|
|
50
57
|
var isUsed = isFromKeys2 && isInKey1;
|
|
51
58
|
if (isUsed) {
|
|
52
59
|
continue;
|
|
53
60
|
}
|
|
54
61
|
var hasField1 = hasField(entity, key);
|
|
55
62
|
var hasField2 = hasField(value, key);
|
|
56
|
-
var onlyHasField1 = hasField1 && !hasField2;
|
|
57
|
-
var onlyHasField2 = !hasField1 && hasField2;
|
|
58
|
-
var onlyHasOneField = onlyHasField1 || onlyHasField2;
|
|
59
63
|
var value1 = hasField1 ? entity[key] : void 0;
|
|
60
64
|
var value2 = hasField2 ? value[key] : void 0;
|
|
61
65
|
var matched = value1 === value2;
|
|
62
|
-
if (
|
|
66
|
+
if (hasField1 && hasField2 && matched) {
|
|
63
67
|
continue;
|
|
64
68
|
}
|
|
65
69
|
allMatched = false;
|
|
@@ -140,22 +144,6 @@ var valueUseCase = exports.valueUseCase = (0, _usecases.createUseCase)(function
|
|
|
140
144
|
} while (list.length > 0);
|
|
141
145
|
return allMatched;
|
|
142
146
|
};
|
|
143
|
-
var diffValue = function diffValue(entity, value, onMatchPropertyFailed) {
|
|
144
|
-
var isArray1 = Array.isArray(entity);
|
|
145
|
-
var isArray2 = Array.isArray(value);
|
|
146
|
-
var isArray = isArray1 && isArray2;
|
|
147
|
-
var result = isArray ? [] : {};
|
|
148
|
-
if (isArray) {
|
|
149
|
-
var length1 = entity.length;
|
|
150
|
-
var length2 = value.length;
|
|
151
|
-
result.length = Math.max(length1, length2);
|
|
152
|
-
}
|
|
153
|
-
matchValue(entity, value, function (key, newSubValue, oldSubValue) {
|
|
154
|
-
result[key] = newSubValue;
|
|
155
|
-
onMatchPropertyFailed === null || onMatchPropertyFailed === void 0 || onMatchPropertyFailed(key, newSubValue, oldSubValue);
|
|
156
|
-
});
|
|
157
|
-
return result;
|
|
158
|
-
};
|
|
159
147
|
var recordValue = /*#__PURE__*/_regenerator["default"].mark(function recordValue(entity, value) {
|
|
160
148
|
var changed;
|
|
161
149
|
return _regenerator["default"].wrap(function (_context) {
|
|
@@ -178,76 +166,52 @@ var valueUseCase = exports.valueUseCase = (0, _usecases.createUseCase)(function
|
|
|
178
166
|
}
|
|
179
167
|
}, recordValue);
|
|
180
168
|
});
|
|
181
|
-
var recordValueDiff = /*#__PURE__*/_regenerator["default"].mark(function recordValueDiff(entity, value) {
|
|
182
|
-
var matched, result;
|
|
183
|
-
return _regenerator["default"].wrap(function (_context2) {
|
|
184
|
-
while (1) switch (_context2.prev = _context2.next) {
|
|
185
|
-
case 0:
|
|
186
|
-
matched = true;
|
|
187
|
-
result = diffValue(entity, value, function () {
|
|
188
|
-
matched = false;
|
|
189
|
-
});
|
|
190
|
-
if (matched) {
|
|
191
|
-
_context2.next = 1;
|
|
192
|
-
break;
|
|
193
|
-
}
|
|
194
|
-
_context2.next = 1;
|
|
195
|
-
return value;
|
|
196
|
-
case 1:
|
|
197
|
-
return _context2.abrupt("return", result);
|
|
198
|
-
case 2:
|
|
199
|
-
case "end":
|
|
200
|
-
return _context2.stop();
|
|
201
|
-
}
|
|
202
|
-
}, recordValueDiff);
|
|
203
|
-
});
|
|
204
169
|
var recordValueMatch = /*#__PURE__*/_regenerator["default"].mark(function recordValueMatch(entity, value) {
|
|
205
170
|
var matched;
|
|
206
|
-
return _regenerator["default"].wrap(function (
|
|
207
|
-
while (1) switch (
|
|
171
|
+
return _regenerator["default"].wrap(function (_context2) {
|
|
172
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
208
173
|
case 0:
|
|
209
174
|
matched = matchValue(entity, value);
|
|
210
175
|
if (!matched) {
|
|
211
|
-
|
|
176
|
+
_context2.next = 1;
|
|
212
177
|
break;
|
|
213
178
|
}
|
|
214
|
-
return
|
|
179
|
+
return _context2.abrupt("return", true);
|
|
215
180
|
case 1:
|
|
216
|
-
|
|
181
|
+
_context2.next = 2;
|
|
217
182
|
return value;
|
|
218
183
|
case 2:
|
|
219
|
-
return
|
|
184
|
+
return _context2.abrupt("return", false);
|
|
220
185
|
case 3:
|
|
221
186
|
case "end":
|
|
222
|
-
return
|
|
187
|
+
return _context2.stop();
|
|
223
188
|
}
|
|
224
189
|
}, recordValueMatch);
|
|
225
190
|
});
|
|
226
191
|
var recordValueMatchWith = /*#__PURE__*/_regenerator["default"].mark(function recordValueMatchWith(entity, value, key) {
|
|
227
192
|
var matched;
|
|
228
|
-
return _regenerator["default"].wrap(function (
|
|
229
|
-
while (1) switch (
|
|
193
|
+
return _regenerator["default"].wrap(function (_context3) {
|
|
194
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
230
195
|
case 0:
|
|
231
|
-
return
|
|
196
|
+
return _context3.delegateYield(recordValueMatch(entity, value), "t0", 1);
|
|
232
197
|
case 1:
|
|
233
|
-
matched =
|
|
198
|
+
matched = _context3.t0;
|
|
234
199
|
if (!matched) {
|
|
235
200
|
prevKeyIndex = key !== null && key !== void 0 ? key : createKey();
|
|
236
201
|
}
|
|
237
|
-
return
|
|
202
|
+
return _context3.abrupt("return", prevKeyIndex);
|
|
238
203
|
case 2:
|
|
239
204
|
case "end":
|
|
240
|
-
return
|
|
205
|
+
return _context3.stop();
|
|
241
206
|
}
|
|
242
207
|
}, recordValueMatchWith);
|
|
243
208
|
});
|
|
244
209
|
return _objectSpread(_objectSpread({}, entityReducers), {}, {
|
|
245
|
-
|
|
210
|
+
isObjectValue: isObjectValue,
|
|
246
211
|
isValueChanged: isValueChanged,
|
|
247
212
|
matchValue: matchValue,
|
|
248
213
|
matchProperty: matchProperty,
|
|
249
214
|
recordValue: recordValue,
|
|
250
|
-
recordValueDiff: recordValueDiff,
|
|
251
215
|
recordValueMatch: recordValueMatch,
|
|
252
216
|
recordValueMatchWith: recordValueMatchWith
|
|
253
217
|
});
|