@etsoo/shared 1.2.70 → 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
@@ -197,6 +197,7 @@ Data type definitions and type safe functions. ListItemType, ListItemType1 and L
197
197
  | isSimpleObject | Is the target a simple object, all values are simple type (Type guard) |
198
198
  | isSimpleType | Is the input value simple type, include null and undefined |
199
199
  | jsonReplacer | JSON.stringify replacer with full path |
200
+ | jsonSerialize | JSON serialize with options |
200
201
 
201
202
  ## DateUtils
202
203
 
@@ -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;
@@ -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 = {}));
@@ -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.70",
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",
@@ -40,7 +40,7 @@
40
40
  "@vitejs/plugin-react": "^4.4.1",
41
41
  "jsdom": "^26.1.0",
42
42
  "typescript": "^5.8.3",
43
- "vitest": "^3.1.2"
43
+ "vitest": "^3.1.3"
44
44
  },
45
45
  "dependencies": {
46
46
  "lodash.isequal": "^4.5.0"
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
  /**