@etsoo/shared 1.1.91 → 1.1.93

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/README.md CHANGED
@@ -96,6 +96,14 @@ ETSOO Extended abstract history class
96
96
  |pushState|Adds an entry to the history stack|
97
97
  |replaceState|Modifies the current history entry|
98
98
 
99
+ ## ArrayUtils
100
+ Array related utilities
101
+
102
+ |Name|Description|
103
+ |---:|---|
104
+ |differences|Array 1 items do not exist in Array 2 or reverse match|
105
+ |toUnique|Make all items are unique|
106
+
99
107
  ## DataTypes
100
108
  Data type definitions and type safe functions. ListItemType and ListItemType1 are sugar types.
101
109
 
@@ -244,7 +252,6 @@ String and other related utilities
244
252
  |Name|Description|
245
253
  |---:|---|
246
254
  |addBlankItem|Add blank item to collection|
247
- |arrayDifferences|Array 1 items do not exist in Array 2 or reverse match|
248
255
  |charsToNumber|Base64 chars to number|
249
256
  |containChinese|Check the input string contains Chinese character or not|
250
257
  |correctTypes|Correct object's property value type|
@@ -0,0 +1,22 @@
1
+ import { ArrayUtils } from '../src/ArrayUtils';
2
+
3
+ test('Tests for simple type toUnique', () => {
4
+ const result = [1, 1, 3, 5, 5].toUnique();
5
+ expect(result).toStrictEqual([1, 3, 5]);
6
+ });
7
+
8
+ test('Tests for object type toUnique', () => {
9
+ const result = [
10
+ { id: 1, label: 'a' },
11
+ { id: 2, label: 'b' },
12
+ { id: 2, label: 'b' }
13
+ ].toUnique();
14
+ expect(result.length).toBe(2);
15
+ });
16
+
17
+ test('Tests for differences', () => {
18
+ const a1 = ['a', 'b', 'c', 'e'];
19
+ const a2 = ['a', 'c', 'd'];
20
+ expect(ArrayUtils.differences(a1, a2)).toEqual(['b', 'e']);
21
+ expect(ArrayUtils.differences(a1, a2, true)).toEqual(['b', 'e', 'd']);
22
+ });
@@ -21,13 +21,6 @@ test('Tests for containChinese', () => {
21
21
  expect('ぁ-ん'.containJapanese()).toBeTruthy();
22
22
  });
23
23
 
24
- test('Tests for arrayDifferences', () => {
25
- const a1 = ['a', 'b', 'c', 'e'];
26
- const a2 = ['a', 'c', 'd'];
27
- expect(Utils.arrayDifferences(a1, a2)).toEqual(['b', 'e']);
28
- expect(Utils.arrayDifferences(a1, a2, true)).toEqual(['b', 'e', 'd']);
29
- });
30
-
31
24
  test('Tests for correctTypes', () => {
32
25
  const input = {
33
26
  id: '1',
@@ -64,6 +57,7 @@ test('Tests for getDataChanges', () => {
64
57
  amount: '',
65
58
  enabled: true,
66
59
  value: undefined,
60
+ date: new Date('2023/03/18'),
67
61
  ids: [1, 2],
68
62
  data: { d1: 1, d2: false, d3: 1.2, d4: 'Hello' }
69
63
  };
@@ -74,6 +68,7 @@ test('Tests for getDataChanges', () => {
74
68
  brand: 'ETSOO',
75
69
  price: 6,
76
70
  amount: 0,
71
+ date: '2023/03/18',
77
72
  enabled: true,
78
73
  ids: [1, 2],
79
74
  data: { d1: 1, d3: 1.2, d4: 'Hello', d2: false }
@@ -0,0 +1,27 @@
1
+ declare global {
2
+ interface Array<T> {
3
+ /**
4
+ * Items do not exist in target array or reverse match
5
+ * @param target Target array
6
+ * @param round A round for both matches
7
+ */
8
+ different(target: Array<T>, round?: boolean): Array<T>;
9
+ /**
10
+ * Make all items are unique
11
+ * @param this Input array
12
+ */
13
+ toUnique(): Array<T>;
14
+ }
15
+ }
16
+ /**
17
+ * Array Utilities
18
+ */
19
+ export declare namespace ArrayUtils {
20
+ /**
21
+ * Array 1 items do not exist in Array 2 or reverse match
22
+ * @param a1 Array 1
23
+ * @param a2 Array 2
24
+ * @param round A round for both matches
25
+ */
26
+ function differences<T>(a1: T[], a2: T[], round?: boolean): T[];
27
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ArrayUtils = void 0;
7
+ const lodash_isequal_1 = __importDefault(require("lodash.isequal"));
8
+ Array.prototype.different = function (target, round) {
9
+ return ArrayUtils.differences(this, target, round);
10
+ };
11
+ Array.prototype.toUnique = function () {
12
+ if (this.length === 0 || typeof this[0] !== 'object')
13
+ return Array.from(new Set(this));
14
+ const newArray = [];
15
+ this.forEach((item) => {
16
+ if (newArray.some((newItem) => (0, lodash_isequal_1.default)(item, newItem)))
17
+ return;
18
+ newArray.push(item);
19
+ });
20
+ return newArray;
21
+ };
22
+ /**
23
+ * Array Utilities
24
+ */
25
+ var ArrayUtils;
26
+ (function (ArrayUtils) {
27
+ /**
28
+ * Array 1 items do not exist in Array 2 or reverse match
29
+ * @param a1 Array 1
30
+ * @param a2 Array 2
31
+ * @param round A round for both matches
32
+ */
33
+ function differences(a1, a2, round) {
34
+ const diff = a1.filter((x) => !a2.includes(x));
35
+ if (round)
36
+ return [...diff, ...a2.filter((x) => !a1.includes(x))];
37
+ return diff;
38
+ }
39
+ ArrayUtils.differences = differences;
40
+ })(ArrayUtils = exports.ArrayUtils || (exports.ArrayUtils = {}));
@@ -72,13 +72,6 @@ export declare namespace Utils {
72
72
  * @param blankLabel Blank label, default is ---
73
73
  */
74
74
  function addBlankItem<T extends object>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): T[];
75
- /**
76
- * Array 1 items do not exist in Array 2 or reverse match
77
- * @param a1 Array 1
78
- * @param a2 Array 2
79
- * @param round A round for both matches
80
- */
81
- function arrayDifferences<T>(a1: T[], a2: T[], round?: boolean): T[];
82
75
  /**
83
76
  * Base64 chars to number
84
77
  * @param base64Chars Base64 chars
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);
@@ -97,19 +98,6 @@ var Utils;
97
98
  return options;
98
99
  }
99
100
  Utils.addBlankItem = addBlankItem;
100
- /**
101
- * Array 1 items do not exist in Array 2 or reverse match
102
- * @param a1 Array 1
103
- * @param a2 Array 2
104
- * @param round A round for both matches
105
- */
106
- function arrayDifferences(a1, a2, round) {
107
- const diff = a1.filter((x) => !a2.includes(x));
108
- if (round)
109
- return [...diff, ...a2.filter((x) => !a1.includes(x))];
110
- return diff;
111
- }
112
- Utils.arrayDifferences = arrayDifferences;
113
101
  /**
114
102
  * Base64 chars to number
115
103
  * @param base64Chars Base64 chars
@@ -222,6 +210,7 @@ var Utils;
222
210
  // Changed fields
223
211
  const changes = [];
224
212
  Object.entries(input).forEach(([key, value]) => {
213
+ var _a;
225
214
  // Ignore fields, no process
226
215
  if (ignoreFields.includes(key))
227
216
  return;
@@ -233,6 +222,16 @@ var Utils;
233
222
  return;
234
223
  }
235
224
  if (initValue != null) {
225
+ // Date when meets string
226
+ if (value instanceof Date) {
227
+ if (value.valueOf() ===
228
+ ((_a = DateUtils_1.DateUtils.parse(initValue)) === null || _a === void 0 ? void 0 : _a.valueOf())) {
229
+ Reflect.deleteProperty(input, key);
230
+ return;
231
+ }
232
+ changes.push(key);
233
+ return;
234
+ }
236
235
  const newValue = DataTypes_1.DataTypes.convert(value, initValue);
237
236
  if (Utils.equals(newValue, initValue)) {
238
237
  Reflect.deleteProperty(input, key);
@@ -7,6 +7,7 @@ export * from './types/FormData';
7
7
  export * from './storage/IStorage';
8
8
  export * from './storage/WindowStorage';
9
9
  export * from './ActionResult';
10
+ export * from './ArrayUtils';
10
11
  export * from './DataTypes';
11
12
  export * from './ColorUtils';
12
13
  export * from './DateUtils';
package/lib/cjs/index.js CHANGED
@@ -23,6 +23,7 @@ __exportStar(require("./types/FormData"), exports);
23
23
  __exportStar(require("./storage/IStorage"), exports);
24
24
  __exportStar(require("./storage/WindowStorage"), exports);
25
25
  __exportStar(require("./ActionResult"), exports);
26
+ __exportStar(require("./ArrayUtils"), exports);
26
27
  __exportStar(require("./DataTypes"), exports);
27
28
  __exportStar(require("./ColorUtils"), exports);
28
29
  __exportStar(require("./DateUtils"), exports);
@@ -0,0 +1,27 @@
1
+ declare global {
2
+ interface Array<T> {
3
+ /**
4
+ * Items do not exist in target array or reverse match
5
+ * @param target Target array
6
+ * @param round A round for both matches
7
+ */
8
+ different(target: Array<T>, round?: boolean): Array<T>;
9
+ /**
10
+ * Make all items are unique
11
+ * @param this Input array
12
+ */
13
+ toUnique(): Array<T>;
14
+ }
15
+ }
16
+ /**
17
+ * Array Utilities
18
+ */
19
+ export declare namespace ArrayUtils {
20
+ /**
21
+ * Array 1 items do not exist in Array 2 or reverse match
22
+ * @param a1 Array 1
23
+ * @param a2 Array 2
24
+ * @param round A round for both matches
25
+ */
26
+ function differences<T>(a1: T[], a2: T[], round?: boolean): T[];
27
+ }
@@ -0,0 +1,34 @@
1
+ import isEqual from 'lodash.isequal';
2
+ Array.prototype.different = function (target, round) {
3
+ return ArrayUtils.differences(this, target, round);
4
+ };
5
+ Array.prototype.toUnique = function () {
6
+ if (this.length === 0 || typeof this[0] !== 'object')
7
+ return Array.from(new Set(this));
8
+ const newArray = [];
9
+ this.forEach((item) => {
10
+ if (newArray.some((newItem) => isEqual(item, newItem)))
11
+ return;
12
+ newArray.push(item);
13
+ });
14
+ return newArray;
15
+ };
16
+ /**
17
+ * Array Utilities
18
+ */
19
+ export var ArrayUtils;
20
+ (function (ArrayUtils) {
21
+ /**
22
+ * Array 1 items do not exist in Array 2 or reverse match
23
+ * @param a1 Array 1
24
+ * @param a2 Array 2
25
+ * @param round A round for both matches
26
+ */
27
+ function differences(a1, a2, round) {
28
+ const diff = a1.filter((x) => !a2.includes(x));
29
+ if (round)
30
+ return [...diff, ...a2.filter((x) => !a1.includes(x))];
31
+ return diff;
32
+ }
33
+ ArrayUtils.differences = differences;
34
+ })(ArrayUtils || (ArrayUtils = {}));
@@ -72,13 +72,6 @@ export declare namespace Utils {
72
72
  * @param blankLabel Blank label, default is ---
73
73
  */
74
74
  function addBlankItem<T extends object>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): T[];
75
- /**
76
- * Array 1 items do not exist in Array 2 or reverse match
77
- * @param a1 Array 1
78
- * @param a2 Array 2
79
- * @param round A round for both matches
80
- */
81
- function arrayDifferences<T>(a1: T[], a2: T[], round?: boolean): T[];
82
75
  /**
83
76
  * Base64 chars to number
84
77
  * @param base64Chars Base64 chars
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);
@@ -91,19 +92,6 @@ export var Utils;
91
92
  return options;
92
93
  }
93
94
  Utils.addBlankItem = addBlankItem;
94
- /**
95
- * Array 1 items do not exist in Array 2 or reverse match
96
- * @param a1 Array 1
97
- * @param a2 Array 2
98
- * @param round A round for both matches
99
- */
100
- function arrayDifferences(a1, a2, round) {
101
- const diff = a1.filter((x) => !a2.includes(x));
102
- if (round)
103
- return [...diff, ...a2.filter((x) => !a1.includes(x))];
104
- return diff;
105
- }
106
- Utils.arrayDifferences = arrayDifferences;
107
95
  /**
108
96
  * Base64 chars to number
109
97
  * @param base64Chars Base64 chars
@@ -216,6 +204,7 @@ export var Utils;
216
204
  // Changed fields
217
205
  const changes = [];
218
206
  Object.entries(input).forEach(([key, value]) => {
207
+ var _a;
219
208
  // Ignore fields, no process
220
209
  if (ignoreFields.includes(key))
221
210
  return;
@@ -227,6 +216,16 @@ export var Utils;
227
216
  return;
228
217
  }
229
218
  if (initValue != null) {
219
+ // Date when meets string
220
+ if (value instanceof Date) {
221
+ if (value.valueOf() ===
222
+ ((_a = DateUtils.parse(initValue)) === null || _a === void 0 ? void 0 : _a.valueOf())) {
223
+ Reflect.deleteProperty(input, key);
224
+ return;
225
+ }
226
+ changes.push(key);
227
+ return;
228
+ }
230
229
  const newValue = DataTypes.convert(value, initValue);
231
230
  if (Utils.equals(newValue, initValue)) {
232
231
  Reflect.deleteProperty(input, key);
@@ -7,6 +7,7 @@ export * from './types/FormData';
7
7
  export * from './storage/IStorage';
8
8
  export * from './storage/WindowStorage';
9
9
  export * from './ActionResult';
10
+ export * from './ArrayUtils';
10
11
  export * from './DataTypes';
11
12
  export * from './ColorUtils';
12
13
  export * from './DateUtils';
package/lib/mjs/index.js CHANGED
@@ -7,6 +7,7 @@ export * from './types/FormData';
7
7
  export * from './storage/IStorage';
8
8
  export * from './storage/WindowStorage';
9
9
  export * from './ActionResult';
10
+ export * from './ArrayUtils';
10
11
  export * from './DataTypes';
11
12
  export * from './ColorUtils';
12
13
  export * from './DateUtils';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.91",
3
+ "version": "1.1.93",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -59,7 +59,7 @@
59
59
  "jest": "^29.5.0",
60
60
  "jest-environment-jsdom": "^29.5.0",
61
61
  "ts-jest": "^29.0.5",
62
- "typescript": "^4.9.5"
62
+ "typescript": "^5.0.2"
63
63
  },
64
64
  "dependencies": {
65
65
  "lodash.isequal": "^4.5.0"
@@ -0,0 +1,55 @@
1
+ import isEqual from 'lodash.isequal';
2
+
3
+ declare global {
4
+ interface Array<T> {
5
+ /**
6
+ * Items do not exist in target array or reverse match
7
+ * @param target Target array
8
+ * @param round A round for both matches
9
+ */
10
+ different(target: Array<T>, round?: boolean): Array<T>;
11
+
12
+ /**
13
+ * Make all items are unique
14
+ * @param this Input array
15
+ */
16
+ toUnique(): Array<T>;
17
+ }
18
+ }
19
+
20
+ Array.prototype.different = function <T>(
21
+ this: Array<T>,
22
+ target: Array<T>,
23
+ round?: boolean
24
+ ) {
25
+ return ArrayUtils.differences(this, target, round);
26
+ };
27
+
28
+ Array.prototype.toUnique = function <T>(this: Array<T>) {
29
+ if (this.length === 0 || typeof this[0] !== 'object')
30
+ return Array.from(new Set(this));
31
+
32
+ const newArray: T[] = [];
33
+ this.forEach((item) => {
34
+ if (newArray.some((newItem) => isEqual(item, newItem))) return;
35
+ newArray.push(item);
36
+ });
37
+ return newArray;
38
+ };
39
+
40
+ /**
41
+ * Array Utilities
42
+ */
43
+ export namespace ArrayUtils {
44
+ /**
45
+ * Array 1 items do not exist in Array 2 or reverse match
46
+ * @param a1 Array 1
47
+ * @param a2 Array 2
48
+ * @param round A round for both matches
49
+ */
50
+ export function differences<T>(a1: T[], a2: T[], round?: boolean) {
51
+ const diff = a1.filter((x) => !a2.includes(x));
52
+ if (round) return [...diff, ...a2.filter((x) => !a1.includes(x))];
53
+ return diff;
54
+ }
55
+ }
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 {
@@ -178,18 +179,6 @@ export namespace Utils {
178
179
  return options;
179
180
  }
180
181
 
181
- /**
182
- * Array 1 items do not exist in Array 2 or reverse match
183
- * @param a1 Array 1
184
- * @param a2 Array 2
185
- * @param round A round for both matches
186
- */
187
- export function arrayDifferences<T>(a1: T[], a2: T[], round?: boolean) {
188
- const diff = a1.filter((x) => !a2.includes(x));
189
- if (round) return [...diff, ...a2.filter((x) => !a1.includes(x))];
190
- return diff;
191
- }
192
-
193
182
  /**
194
183
  * Base64 chars to number
195
184
  * @param base64Chars Base64 chars
@@ -327,6 +316,19 @@ export namespace Utils {
327
316
  }
328
317
 
329
318
  if (initValue != null) {
319
+ // Date when meets string
320
+ if (value instanceof Date) {
321
+ if (
322
+ value.valueOf() ===
323
+ DateUtils.parse(initValue)?.valueOf()
324
+ ) {
325
+ Reflect.deleteProperty(input, key);
326
+ return;
327
+ }
328
+ changes.push(key);
329
+ return;
330
+ }
331
+
330
332
  const newValue = DataTypes.convert(value, initValue);
331
333
  if (Utils.equals(newValue, initValue)) {
332
334
  Reflect.deleteProperty(input, key);
package/src/index.ts CHANGED
@@ -9,6 +9,7 @@ export * from './storage/IStorage';
9
9
  export * from './storage/WindowStorage';
10
10
 
11
11
  export * from './ActionResult';
12
+ export * from './ArrayUtils';
12
13
  export * from './DataTypes';
13
14
  export * from './ColorUtils';
14
15
  export * from './DateUtils';