@etsoo/shared 1.2.69 → 1.2.71

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
@@ -112,16 +112,17 @@ ETSOO Extended abstract history class
112
112
 
113
113
  Array related utilities
114
114
 
115
- | Name | Description |
116
- | ----------: | ------------------------------------------------------ |
117
- | differences | Array 1 items do not exist in Array 2 or reverse match |
118
- | max | Get max number item or number item property |
119
- | maxItem | Get max field value item |
120
- | min | Get min number item or number item property |
121
- | minItem | Get min field value item |
122
- | remove | Remove items by value or condition |
123
- | sum | Sum number items or number item properties |
124
- | toUnique | Make all items are unique |
115
+ | Name | Description |
116
+ | ----------: | ------------------------------------------------------------ |
117
+ | differences | Array 1 items do not exist in Array 2 or reverse match |
118
+ | max | Get max number item or number item property |
119
+ | maxItem | Get max field value item |
120
+ | mergeArrays | Merge arrays, remove duplicates, and sort by the first array |
121
+ | min | Get min number item or number item property |
122
+ | minItem | Get min field value item |
123
+ | remove | Remove items by value or condition |
124
+ | sum | Sum number items or number item properties |
125
+ | toUnique | Make all items are unique |
125
126
 
126
127
  ## DataTypes
127
128
 
@@ -196,6 +197,7 @@ Data type definitions and type safe functions. ListItemType, ListItemType1 and L
196
197
  | isSimpleObject | Is the target a simple object, all values are simple type (Type guard) |
197
198
  | isSimpleType | Is the input value simple type, include null and undefined |
198
199
  | jsonReplacer | JSON.stringify replacer with full path |
200
+ | jsonSerialize | JSON serialize with options |
199
201
 
200
202
  ## DateUtils
201
203
 
@@ -63,6 +63,15 @@ test("Tests for maxItem / minItem", () => {
63
63
  expect(emptyItems.maxItem("amount")).toBeUndefined();
64
64
  });
65
65
 
66
+ test("Tests for mergeArrays", () => {
67
+ const a1 = ["e", "a", "c"];
68
+ const a2 = ["a", "c", "d"];
69
+ const a3 = ["b", "c", "f"];
70
+ const result = ArrayUtils.mergeArrays(a1, a2, a3);
71
+ expect(result).toEqual(["e", "a", "c", "d", "b", "f"]);
72
+ expect(result.length).toBe(6);
73
+ });
74
+
66
75
  test("Tests for remove simple", () => {
67
76
  const items = [1, 2, 3, 4, 5];
68
77
  const result = items.remove(1, 5, (item) => item % 2 === 0);
@@ -202,6 +202,26 @@ test("Tests for jsonReplacer", () => {
202
202
  expect(json2).toBe('{"c":{"c2":false}}');
203
203
  });
204
204
 
205
+ test("Tests for jsonSerialize", () => {
206
+ const obj = { a: 1, b: null, c: { c1: undefined, c2: false, c3: 128 } };
207
+ const json = DataTypes.jsonSerialize(obj);
208
+ expect(json).toBe('{"a":1,"c":{"c2":false,"c3":128}}');
209
+ });
210
+
211
+ test("Tests for jsonSerialize with removing empty values", () => {
212
+ const obj = {
213
+ a: 1,
214
+ b: null,
215
+ c: { c1: undefined, c2: false, c3: 128, c4: "" },
216
+ d: ""
217
+ };
218
+ const json = DataTypes.jsonSerialize(obj, {
219
+ ignoreEmpty: true,
220
+ ignoreNull: false
221
+ });
222
+ expect(json).toBe('{"a":1,"b":null,"c":{"c2":false,"c3":128}}');
223
+ });
224
+
205
225
  test("Tests for EditType", () => {
206
226
  type AddEntity = {
207
227
  name: string;
@@ -61,4 +61,11 @@ export declare namespace ArrayUtils {
61
61
  * @param round A round for both matches
62
62
  */
63
63
  function differences<T>(a1: T[], a2: T[], round?: boolean): T[];
64
+ /**
65
+ * Merge arrays, remove duplicates, and sort by the first array
66
+ * @param sort Array to sort
67
+ * @param param All arrays to merge
68
+ * @returns Result
69
+ */
70
+ function mergeArrays<T>(sort: T[], ...param: T[][]): T[];
64
71
  }
@@ -99,4 +99,24 @@ var ArrayUtils;
99
99
  return diff;
100
100
  }
101
101
  ArrayUtils.differences = differences;
102
+ /**
103
+ * Merge arrays, remove duplicates, and sort by the first array
104
+ * @param sort Array to sort
105
+ * @param param All arrays to merge
106
+ * @returns Result
107
+ */
108
+ function mergeArrays(sort, ...param) {
109
+ const result = [...sort];
110
+ for (let i = 0; i < param.length; i++) {
111
+ const arr = param[i];
112
+ for (let j = 0; j < arr.length; j++) {
113
+ const item = arr[j];
114
+ if (!result.includes(item)) {
115
+ result.push(item);
116
+ }
117
+ }
118
+ }
119
+ return result;
120
+ }
121
+ ArrayUtils.mergeArrays = mergeArrays;
102
122
  })(ArrayUtils || (exports.ArrayUtils = ArrayUtils = {}));
@@ -420,6 +420,16 @@ export declare namespace DataTypes {
420
420
  * @returns JSON receiver function
421
421
  */
422
422
  function jsonBigintReceiver(...args: string[]): (this: any, key: any, value: any) => any;
423
+ /**
424
+ * JSON serialize with options
425
+ * @param obj Object to serialize
426
+ * @param options Options to ignore null or empty values
427
+ * @returns Result
428
+ */
429
+ function jsonSerialize(obj: unknown, options?: {
430
+ ignoreNull?: boolean;
431
+ ignoreEmpty?: boolean;
432
+ }): string;
423
433
  }
424
434
  /**
425
435
  * Number and string combination id type
@@ -498,4 +498,21 @@ var DataTypes;
498
498
  });
499
499
  }
500
500
  DataTypes.jsonBigintReceiver = jsonBigintReceiver;
501
+ /**
502
+ * JSON serialize with options
503
+ * @param obj Object to serialize
504
+ * @param options Options to ignore null or empty values
505
+ * @returns Result
506
+ */
507
+ function jsonSerialize(obj, options) {
508
+ const { ignoreNull = true, ignoreEmpty = false } = options ?? {};
509
+ return JSON.stringify(obj, (_key, value) => {
510
+ if (ignoreNull && value == null)
511
+ return undefined;
512
+ if (ignoreEmpty && value === "")
513
+ return undefined;
514
+ return value;
515
+ });
516
+ }
517
+ DataTypes.jsonSerialize = jsonSerialize;
501
518
  })(DataTypes || (exports.DataTypes = DataTypes = {}));
@@ -61,4 +61,11 @@ export declare namespace ArrayUtils {
61
61
  * @param round A round for both matches
62
62
  */
63
63
  function differences<T>(a1: T[], a2: T[], round?: boolean): T[];
64
+ /**
65
+ * Merge arrays, remove duplicates, and sort by the first array
66
+ * @param sort Array to sort
67
+ * @param param All arrays to merge
68
+ * @returns Result
69
+ */
70
+ function mergeArrays<T>(sort: T[], ...param: T[][]): T[];
64
71
  }
@@ -93,4 +93,24 @@ export var ArrayUtils;
93
93
  return diff;
94
94
  }
95
95
  ArrayUtils.differences = differences;
96
+ /**
97
+ * Merge arrays, remove duplicates, and sort by the first array
98
+ * @param sort Array to sort
99
+ * @param param All arrays to merge
100
+ * @returns Result
101
+ */
102
+ function mergeArrays(sort, ...param) {
103
+ const result = [...sort];
104
+ for (let i = 0; i < param.length; i++) {
105
+ const arr = param[i];
106
+ for (let j = 0; j < arr.length; j++) {
107
+ const item = arr[j];
108
+ if (!result.includes(item)) {
109
+ result.push(item);
110
+ }
111
+ }
112
+ }
113
+ return result;
114
+ }
115
+ ArrayUtils.mergeArrays = mergeArrays;
96
116
  })(ArrayUtils || (ArrayUtils = {}));
@@ -420,6 +420,16 @@ export declare namespace DataTypes {
420
420
  * @returns JSON receiver function
421
421
  */
422
422
  function jsonBigintReceiver(...args: string[]): (this: any, key: any, value: any) => any;
423
+ /**
424
+ * JSON serialize with options
425
+ * @param obj Object to serialize
426
+ * @param options Options to ignore null or empty values
427
+ * @returns Result
428
+ */
429
+ function jsonSerialize(obj: unknown, options?: {
430
+ ignoreNull?: boolean;
431
+ ignoreEmpty?: boolean;
432
+ }): string;
423
433
  }
424
434
  /**
425
435
  * Number and string combination id type
@@ -495,4 +495,21 @@ export var DataTypes;
495
495
  });
496
496
  }
497
497
  DataTypes.jsonBigintReceiver = jsonBigintReceiver;
498
+ /**
499
+ * JSON serialize with options
500
+ * @param obj Object to serialize
501
+ * @param options Options to ignore null or empty values
502
+ * @returns Result
503
+ */
504
+ function jsonSerialize(obj, options) {
505
+ const { ignoreNull = true, ignoreEmpty = false } = options ?? {};
506
+ return JSON.stringify(obj, (_key, value) => {
507
+ if (ignoreNull && value == null)
508
+ return undefined;
509
+ if (ignoreEmpty && value === "")
510
+ return undefined;
511
+ return value;
512
+ });
513
+ }
514
+ DataTypes.jsonSerialize = jsonSerialize;
498
515
  })(DataTypes || (DataTypes = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.2.69",
3
+ "version": "1.2.71",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -37,10 +37,10 @@
37
37
  "homepage": "https://github.com/ETSOO/Shared#readme",
38
38
  "devDependencies": {
39
39
  "@types/lodash.isequal": "^4.5.8",
40
- "@vitejs/plugin-react": "^4.4.0",
40
+ "@vitejs/plugin-react": "^4.4.1",
41
41
  "jsdom": "^26.1.0",
42
42
  "typescript": "^5.8.3",
43
- "vitest": "^3.1.1"
43
+ "vitest": "^3.1.3"
44
44
  },
45
45
  "dependencies": {
46
46
  "lodash.isequal": "^4.5.0"
package/src/ArrayUtils.ts CHANGED
@@ -212,4 +212,26 @@ export namespace ArrayUtils {
212
212
  if (round) return [...diff, ...a2.filter((x) => !a1.includes(x))];
213
213
  return diff;
214
214
  }
215
+
216
+ /**
217
+ * Merge arrays, remove duplicates, and sort by the first array
218
+ * @param sort Array to sort
219
+ * @param param All arrays to merge
220
+ * @returns Result
221
+ */
222
+ export function mergeArrays<T>(sort: T[], ...param: T[][]): T[] {
223
+ const result = [...sort];
224
+
225
+ for (let i = 0; i < param.length; i++) {
226
+ const arr = param[i];
227
+ for (let j = 0; j < arr.length; j++) {
228
+ const item = arr[j];
229
+ if (!result.includes(item)) {
230
+ result.push(item);
231
+ }
232
+ }
233
+ }
234
+
235
+ return result;
236
+ }
215
237
  }
package/src/DataTypes.ts CHANGED
@@ -800,6 +800,25 @@ export namespace DataTypes {
800
800
  return value;
801
801
  });
802
802
  }
803
+
804
+ /**
805
+ * JSON serialize with options
806
+ * @param obj Object to serialize
807
+ * @param options Options to ignore null or empty values
808
+ * @returns Result
809
+ */
810
+ export function jsonSerialize(
811
+ obj: unknown,
812
+ options?: { ignoreNull?: boolean; ignoreEmpty?: boolean }
813
+ ) {
814
+ const { ignoreNull = true, ignoreEmpty = false } = options ?? {};
815
+
816
+ return JSON.stringify(obj, (_key, value) => {
817
+ if (ignoreNull && value == null) return undefined;
818
+ if (ignoreEmpty && value === "") return undefined;
819
+ return value;
820
+ });
821
+ }
803
822
  }
804
823
 
805
824
  /**