@etsoo/shared 1.1.90 → 1.1.92

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.
@@ -64,6 +64,7 @@ test('Tests for getDataChanges', () => {
64
64
  amount: '',
65
65
  enabled: true,
66
66
  value: undefined,
67
+ date: new Date('2023/03/18'),
67
68
  ids: [1, 2],
68
69
  data: { d1: 1, d2: false, d3: 1.2, d4: 'Hello' }
69
70
  };
@@ -74,6 +75,7 @@ test('Tests for getDataChanges', () => {
74
75
  brand: 'ETSOO',
75
76
  price: 6,
76
77
  amount: 0,
78
+ date: '2023/03/18',
77
79
  enabled: true,
78
80
  ids: [1, 2],
79
81
  data: { d1: 1, d3: 1.2, d4: 'Hello', d2: false }
@@ -84,6 +86,28 @@ test('Tests for getDataChanges', () => {
84
86
  expect(input.amount).toBeUndefined();
85
87
  });
86
88
 
89
+ test('Tests for object array getDataChanges', () => {
90
+ const input = {
91
+ id: 1,
92
+ ids: [1, 2],
93
+ items: [
94
+ { id: 1, label: 'a' },
95
+ { id: 2, label: 'b' }
96
+ ]
97
+ };
98
+ const initData = {
99
+ id: 1,
100
+ ids: [1],
101
+ items: [
102
+ { id: 1, label: 'a' },
103
+ { id: 2, label: 'b' }
104
+ ]
105
+ };
106
+ const fields = Utils.getDataChanges(input, initData);
107
+ expect(fields).toStrictEqual(['ids']);
108
+ expect(input.items).toBeUndefined();
109
+ });
110
+
87
111
  test('Tests for exclude', () => {
88
112
  const options = [
89
113
  { id1: 1, name: 'a' },
@@ -99,7 +99,6 @@ var DataTypes;
99
99
  * @returns Converted value
100
100
  */
101
101
  function convert(input, target) {
102
- var _a;
103
102
  // null or undefined
104
103
  if (input == null)
105
104
  return undefined;
@@ -107,7 +106,9 @@ var DataTypes;
107
106
  if (Array.isArray(target)) {
108
107
  // Element item
109
108
  const elementItem = target.length > 0 ? target[0] : input;
110
- const elementType = (_a = getBasicNameByValue(elementItem, true)) !== null && _a !== void 0 ? _a : 'unknown[]';
109
+ const elementType = getBasicNameByValue(elementItem, true);
110
+ if (elementType == null)
111
+ return input;
111
112
  return convertByType(input, elementType);
112
113
  }
113
114
  // Target type
@@ -140,7 +141,7 @@ var DataTypes;
140
141
  ? input.split(/,\s*/g) // Support comma separated array
141
142
  : [input];
142
143
  // Element type
143
- const elementType = (targetType.substr(0, targetType.length - 2));
144
+ const elementType = (targetType.slice(0, targetType.length - 2));
144
145
  // Convert type
145
146
  return (inputArray
146
147
  .map((item) => convertByType(item, elementType))
package/lib/cjs/Utils.js CHANGED
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.Utils = void 0;
16
16
  const DataTypes_1 = require("./DataTypes");
17
17
  const lodash_isequal_1 = __importDefault(require("lodash.isequal"));
18
+ const DateUtils_1 = require("./DateUtils");
18
19
  String.prototype.containChinese = function () {
19
20
  const regExp = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
20
21
  return regExp.test(this);
@@ -222,6 +223,7 @@ var Utils;
222
223
  // Changed fields
223
224
  const changes = [];
224
225
  Object.entries(input).forEach(([key, value]) => {
226
+ var _a;
225
227
  // Ignore fields, no process
226
228
  if (ignoreFields.includes(key))
227
229
  return;
@@ -233,6 +235,16 @@ var Utils;
233
235
  return;
234
236
  }
235
237
  if (initValue != null) {
238
+ // Date when meets string
239
+ if (value instanceof Date) {
240
+ if (value.valueOf() ===
241
+ ((_a = DateUtils_1.DateUtils.parse(initValue)) === null || _a === void 0 ? void 0 : _a.valueOf())) {
242
+ Reflect.deleteProperty(input, key);
243
+ return;
244
+ }
245
+ changes.push(key);
246
+ return;
247
+ }
236
248
  const newValue = DataTypes_1.DataTypes.convert(value, initValue);
237
249
  if (Utils.equals(newValue, initValue)) {
238
250
  Reflect.deleteProperty(input, key);
@@ -96,7 +96,6 @@ export var DataTypes;
96
96
  * @returns Converted value
97
97
  */
98
98
  function convert(input, target) {
99
- var _a;
100
99
  // null or undefined
101
100
  if (input == null)
102
101
  return undefined;
@@ -104,7 +103,9 @@ export var DataTypes;
104
103
  if (Array.isArray(target)) {
105
104
  // Element item
106
105
  const elementItem = target.length > 0 ? target[0] : input;
107
- const elementType = (_a = getBasicNameByValue(elementItem, true)) !== null && _a !== void 0 ? _a : 'unknown[]';
106
+ const elementType = getBasicNameByValue(elementItem, true);
107
+ if (elementType == null)
108
+ return input;
108
109
  return convertByType(input, elementType);
109
110
  }
110
111
  // Target type
@@ -137,7 +138,7 @@ export var DataTypes;
137
138
  ? input.split(/,\s*/g) // Support comma separated array
138
139
  : [input];
139
140
  // Element type
140
- const elementType = (targetType.substr(0, targetType.length - 2));
141
+ const elementType = (targetType.slice(0, targetType.length - 2));
141
142
  // Convert type
142
143
  return (inputArray
143
144
  .map((item) => convertByType(item, elementType))
package/lib/mjs/Utils.js CHANGED
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { DataTypes } from './DataTypes';
11
11
  import isEqual from 'lodash.isequal';
12
+ import { DateUtils } from './DateUtils';
12
13
  String.prototype.containChinese = function () {
13
14
  const regExp = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
14
15
  return regExp.test(this);
@@ -216,6 +217,7 @@ export var Utils;
216
217
  // Changed fields
217
218
  const changes = [];
218
219
  Object.entries(input).forEach(([key, value]) => {
220
+ var _a;
219
221
  // Ignore fields, no process
220
222
  if (ignoreFields.includes(key))
221
223
  return;
@@ -227,6 +229,16 @@ export var Utils;
227
229
  return;
228
230
  }
229
231
  if (initValue != null) {
232
+ // Date when meets string
233
+ if (value instanceof Date) {
234
+ if (value.valueOf() ===
235
+ ((_a = DateUtils.parse(initValue)) === null || _a === void 0 ? void 0 : _a.valueOf())) {
236
+ Reflect.deleteProperty(input, key);
237
+ return;
238
+ }
239
+ changes.push(key);
240
+ return;
241
+ }
230
242
  const newValue = DataTypes.convert(value, initValue);
231
243
  if (Utils.equals(newValue, initValue)) {
232
244
  Reflect.deleteProperty(input, key);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.90",
3
+ "version": "1.1.92",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -54,7 +54,7 @@
54
54
  },
55
55
  "homepage": "https://github.com/ETSOO/Shared#readme",
56
56
  "devDependencies": {
57
- "@types/jest": "^29.4.0",
57
+ "@types/jest": "^29.5.0",
58
58
  "@types/lodash.isequal": "^4.5.6",
59
59
  "jest": "^29.5.0",
60
60
  "jest-environment-jsdom": "^29.5.0",
package/src/DataTypes.ts CHANGED
@@ -356,9 +356,9 @@ export namespace DataTypes {
356
356
  if (Array.isArray(target)) {
357
357
  // Element item
358
358
  const elementItem = target.length > 0 ? target[0] : input;
359
- const elementType =
360
- getBasicNameByValue(elementItem, true) ?? 'unknown[]';
359
+ const elementType = getBasicNameByValue(elementItem, true);
361
360
 
361
+ if (elementType == null) return <T>input;
362
362
  return <any>convertByType(input, elementType);
363
363
  }
364
364
 
@@ -400,7 +400,7 @@ export namespace DataTypes {
400
400
 
401
401
  // Element type
402
402
  const elementType = <BasicNames>(
403
- targetType.substr(0, targetType.length - 2)
403
+ targetType.slice(0, targetType.length - 2)
404
404
  );
405
405
 
406
406
  // Convert type
package/src/Utils.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { DataTypes } from './DataTypes';
2
2
  import isEqual from 'lodash.isequal';
3
+ import { DateUtils } from './DateUtils';
3
4
 
4
5
  declare global {
5
6
  interface String {
@@ -327,6 +328,19 @@ export namespace Utils {
327
328
  }
328
329
 
329
330
  if (initValue != null) {
331
+ // Date when meets string
332
+ if (value instanceof Date) {
333
+ if (
334
+ value.valueOf() ===
335
+ DateUtils.parse(initValue)?.valueOf()
336
+ ) {
337
+ Reflect.deleteProperty(input, key);
338
+ return;
339
+ }
340
+ changes.push(key);
341
+ return;
342
+ }
343
+
330
344
  const newValue = DataTypes.convert(value, initValue);
331
345
  if (Utils.equals(newValue, initValue)) {
332
346
  Reflect.deleteProperty(input, key);