@etsoo/shared 1.2.70 → 1.2.72

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
 
@@ -275,6 +276,7 @@ Numbers related utilities
275
276
  | getCurrencySymbol | Get currency symbol or name from ISO code |
276
277
  | parse | Parse to number, with or without default value |
277
278
  | toExact | To the exact precision number avoiding precision lost |
279
+ | toStep | To the step number, like 0, 10, 20, 30 for step 10 |
278
280
 
279
281
  ## StorageUtils
280
282
 
@@ -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;
@@ -50,3 +50,12 @@ test("Tests for toFileSize", () => {
50
50
  expect(NumberUtils.formatFileSize(1125000)).toBe("1.07 MB");
51
51
  expect(NumberUtils.formatFileSize(1125000, 1)).toBe("1.1 MB");
52
52
  });
53
+
54
+ test("Tests for toStep", () => {
55
+ const nums = [9, 13, 20, 33, 99, 101, 3009];
56
+ const results = nums.map((num) => num.toStep(10));
57
+ expect(results).toStrictEqual([0, 10, 20, 30, 90, 100, 3000]);
58
+
59
+ const results2 = nums.map((num) => num.toStep(8));
60
+ expect(results2).toStrictEqual([8, 8, 16, 32, 96, 96, 3008]);
61
+ });
@@ -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 = {}));
@@ -5,6 +5,11 @@ declare global {
5
5
  * @param precision Precision
6
6
  */
7
7
  toExact(precision?: number): number;
8
+ /**
9
+ * To the step number, like 0, 10, 20, 30 for step 10
10
+ * @param step Step
11
+ */
12
+ toStep(step: number): number;
8
13
  }
9
14
  }
10
15
  export declare namespace NumberUtils {
@@ -9,6 +9,11 @@ Number.prototype.toExact = function (precision) {
9
9
  const p = Math.pow(10, precision);
10
10
  return Math.round(this * p) / p;
11
11
  };
12
+ Number.prototype.toStep = function (step) {
13
+ if (step <= 0)
14
+ return this;
15
+ return Math.floor(this / step) * step;
16
+ };
12
17
  var NumberUtils;
13
18
  (function (NumberUtils) {
14
19
  /**
@@ -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 = {}));
@@ -5,6 +5,11 @@ declare global {
5
5
  * @param precision Precision
6
6
  */
7
7
  toExact(precision?: number): number;
8
+ /**
9
+ * To the step number, like 0, 10, 20, 30 for step 10
10
+ * @param step Step
11
+ */
12
+ toStep(step: number): number;
8
13
  }
9
14
  }
10
15
  export declare namespace NumberUtils {
@@ -6,6 +6,11 @@ Number.prototype.toExact = function (precision) {
6
6
  const p = Math.pow(10, precision);
7
7
  return Math.round(this * p) / p;
8
8
  };
9
+ Number.prototype.toStep = function (step) {
10
+ if (step <= 0)
11
+ return this;
12
+ return Math.floor(this / step) * step;
13
+ };
9
14
  export var NumberUtils;
10
15
  (function (NumberUtils) {
11
16
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.2.70",
3
+ "version": "1.2.72",
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
  /**
@@ -5,6 +5,12 @@ declare global {
5
5
  * @param precision Precision
6
6
  */
7
7
  toExact(precision?: number): number;
8
+
9
+ /**
10
+ * To the step number, like 0, 10, 20, 30 for step 10
11
+ * @param step Step
12
+ */
13
+ toStep(step: number): number;
8
14
  }
9
15
  }
10
16
 
@@ -17,6 +23,12 @@ Number.prototype.toExact = function (this: number, precision?: number) {
17
23
  return Math.round(this * p) / p;
18
24
  };
19
25
 
26
+ Number.prototype.toStep = function (this: number, step: number) {
27
+ if (step <= 0) return this;
28
+
29
+ return Math.floor(this / step) * step;
30
+ };
31
+
20
32
  export namespace NumberUtils {
21
33
  /**
22
34
  * Format number