@etsoo/shared 1.1.92 → 1.1.94

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,8 +96,16 @@ 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
- Data type definitions and type safe functions. ListItemType and ListItemType1 are sugar types.
108
+ Data type definitions and type safe functions. ListItemType, ListItemType1 and ListItemType2 are sugar types.
101
109
 
102
110
  |Name|Description|
103
111
  |---:|---|
@@ -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',
@@ -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 = {}));
@@ -395,6 +395,16 @@ export type ListType = DataTypes.IdLabelItem<number>;
395
395
  * List item with string id type
396
396
  */
397
397
  export type ListType1 = DataTypes.IdLabelItem<string>;
398
+ /**
399
+ * List item with compatible id and name / label
400
+ */
401
+ export type ListType2 = {
402
+ id: DataTypes.IdType;
403
+ } & ({
404
+ label: string;
405
+ } | {
406
+ name: string;
407
+ });
398
408
  /**
399
409
  * Id default type
400
410
  */
@@ -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
@@ -98,19 +98,6 @@ var Utils;
98
98
  return options;
99
99
  }
100
100
  Utils.addBlankItem = addBlankItem;
101
- /**
102
- * Array 1 items do not exist in Array 2 or reverse match
103
- * @param a1 Array 1
104
- * @param a2 Array 2
105
- * @param round A round for both matches
106
- */
107
- function arrayDifferences(a1, a2, round) {
108
- const diff = a1.filter((x) => !a2.includes(x));
109
- if (round)
110
- return [...diff, ...a2.filter((x) => !a1.includes(x))];
111
- return diff;
112
- }
113
- Utils.arrayDifferences = arrayDifferences;
114
101
  /**
115
102
  * Base64 chars to number
116
103
  * @param base64Chars Base64 chars
@@ -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 = {}));
@@ -395,6 +395,16 @@ export type ListType = DataTypes.IdLabelItem<number>;
395
395
  * List item with string id type
396
396
  */
397
397
  export type ListType1 = DataTypes.IdLabelItem<string>;
398
+ /**
399
+ * List item with compatible id and name / label
400
+ */
401
+ export type ListType2 = {
402
+ id: DataTypes.IdType;
403
+ } & ({
404
+ label: string;
405
+ } | {
406
+ name: string;
407
+ });
398
408
  /**
399
409
  * Id default type
400
410
  */
@@ -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
@@ -92,19 +92,6 @@ export var Utils;
92
92
  return options;
93
93
  }
94
94
  Utils.addBlankItem = addBlankItem;
95
- /**
96
- * Array 1 items do not exist in Array 2 or reverse match
97
- * @param a1 Array 1
98
- * @param a2 Array 2
99
- * @param round A round for both matches
100
- */
101
- function arrayDifferences(a1, a2, round) {
102
- const diff = a1.filter((x) => !a2.includes(x));
103
- if (round)
104
- return [...diff, ...a2.filter((x) => !a1.includes(x))];
105
- return diff;
106
- }
107
- Utils.arrayDifferences = arrayDifferences;
108
95
  /**
109
96
  * Base64 chars to number
110
97
  * @param base64Chars Base64 chars
@@ -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.92",
3
+ "version": "1.1.94",
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/DataTypes.ts CHANGED
@@ -754,6 +754,13 @@ export type ListType = DataTypes.IdLabelItem<number>;
754
754
  */
755
755
  export type ListType1 = DataTypes.IdLabelItem<string>;
756
756
 
757
+ /**
758
+ * List item with compatible id and name / label
759
+ */
760
+ export type ListType2 = {
761
+ id: DataTypes.IdType;
762
+ } & ({ label: string } | { name: string });
763
+
757
764
  /**
758
765
  * Id default type
759
766
  */
package/src/Utils.ts CHANGED
@@ -179,18 +179,6 @@ export namespace Utils {
179
179
  return options;
180
180
  }
181
181
 
182
- /**
183
- * Array 1 items do not exist in Array 2 or reverse match
184
- * @param a1 Array 1
185
- * @param a2 Array 2
186
- * @param round A round for both matches
187
- */
188
- export function arrayDifferences<T>(a1: T[], a2: T[], round?: boolean) {
189
- const diff = a1.filter((x) => !a2.includes(x));
190
- if (round) return [...diff, ...a2.filter((x) => !a1.includes(x))];
191
- return diff;
192
- }
193
-
194
182
  /**
195
183
  * Base64 chars to number
196
184
  * @param base64Chars Base64 chars
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';