@etsoo/shared 1.1.6 → 1.1.7

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
@@ -152,6 +152,7 @@ String and other related utilities
152
152
  |Name|Description|
153
153
  |---:|---|
154
154
  |charsToNumber|Base64 chars to number|
155
+ |correctTypes|Correct object's property value type|
155
156
  |equals|Two values equal|
156
157
  |formatInitial|Format inital character to lower case or upper case|
157
158
  |formatString|Format string with parameters|
@@ -1,5 +1,30 @@
1
1
  import { Utils } from '../src/Utils';
2
2
 
3
+ test('Tests for correctTypes', () => {
4
+ const input = {
5
+ id: '1',
6
+ price: '6.0',
7
+ amount: '',
8
+ date: '2022/01/28',
9
+ enabled: 'true',
10
+ ids: ['1', '2']
11
+ };
12
+ Utils.correctTypes(input, {
13
+ id: 'number',
14
+ price: 'number',
15
+ amount: 'number',
16
+ date: 'date',
17
+ enabled: 'boolean',
18
+ ids: 'number[]'
19
+ });
20
+ expect(typeof input.id).toBe('number');
21
+ expect(typeof input.price).toBe('number');
22
+ expect(input.amount).toBeUndefined();
23
+ expect((input.date as any) instanceof Date ? true : false).toBeTruthy();
24
+ expect(input.enabled).toBeTruthy();
25
+ expect(input.ids).toStrictEqual([1, 2]);
26
+ });
27
+
3
28
  test('Tests for getDataChanges', () => {
4
29
  const input = {
5
30
  id: 1,
@@ -41,6 +41,14 @@ export declare namespace Utils {
41
41
  * @returns Number
42
42
  */
43
43
  function charsToNumber(base64Chars: string): number;
44
+ /**
45
+ * Correct object's property value type
46
+ * @param input Input object
47
+ * @param fields Fields to correct
48
+ */
49
+ function correctTypes<T extends {}, F extends {
50
+ [P in keyof T]: DataTypes.BasicNames;
51
+ }>(input: T, fields: F): void;
44
52
  /**
45
53
  * Two values equal
46
54
  * @param v1 Value 1
package/lib/cjs/Utils.js CHANGED
@@ -43,6 +43,21 @@ var Utils;
43
43
  }, 0);
44
44
  }
45
45
  Utils.charsToNumber = charsToNumber;
46
+ /**
47
+ * Correct object's property value type
48
+ * @param input Input object
49
+ * @param fields Fields to correct
50
+ */
51
+ function correctTypes(input, fields) {
52
+ for (const field in fields) {
53
+ const value = Reflect.get(input, field);
54
+ const newValue = DataTypes_1.DataTypes.convertByType(value, fields[field]);
55
+ if (newValue !== value) {
56
+ Reflect.set(input, field, newValue);
57
+ }
58
+ }
59
+ }
60
+ Utils.correctTypes = correctTypes;
46
61
  /**
47
62
  * Two values equal
48
63
  * @param v1 Value 1
@@ -41,6 +41,14 @@ export declare namespace Utils {
41
41
  * @returns Number
42
42
  */
43
43
  function charsToNumber(base64Chars: string): number;
44
+ /**
45
+ * Correct object's property value type
46
+ * @param input Input object
47
+ * @param fields Fields to correct
48
+ */
49
+ function correctTypes<T extends {}, F extends {
50
+ [P in keyof T]: DataTypes.BasicNames;
51
+ }>(input: T, fields: F): void;
44
52
  /**
45
53
  * Two values equal
46
54
  * @param v1 Value 1
package/lib/mjs/Utils.js CHANGED
@@ -40,6 +40,21 @@ export var Utils;
40
40
  }, 0);
41
41
  }
42
42
  Utils.charsToNumber = charsToNumber;
43
+ /**
44
+ * Correct object's property value type
45
+ * @param input Input object
46
+ * @param fields Fields to correct
47
+ */
48
+ function correctTypes(input, fields) {
49
+ for (const field in fields) {
50
+ const value = Reflect.get(input, field);
51
+ const newValue = DataTypes.convertByType(value, fields[field]);
52
+ if (newValue !== value) {
53
+ Reflect.set(input, field, newValue);
54
+ }
55
+ }
56
+ }
57
+ Utils.correctTypes = correctTypes;
43
58
  /**
44
59
  * Two values equal
45
60
  * @param v1 Value 1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -55,13 +55,13 @@
55
55
  "dependencies": {},
56
56
  "devDependencies": {
57
57
  "@types/jest": "^27.4.0",
58
- "@typescript-eslint/eslint-plugin": "^5.10.0",
59
- "@typescript-eslint/parser": "^5.10.0",
58
+ "@typescript-eslint/eslint-plugin": "^5.10.1",
59
+ "@typescript-eslint/parser": "^5.10.1",
60
60
  "eslint": "^8.7.0",
61
61
  "eslint-config-airbnb-base": "^15.0.0",
62
62
  "eslint-plugin-import": "^2.25.4",
63
63
  "jest": "^27.4.7",
64
64
  "ts-jest": "^27.1.3",
65
- "typescript": "^4.5.4"
65
+ "typescript": "^4.5.5"
66
66
  }
67
67
  }
package/src/Utils.ts CHANGED
@@ -93,6 +93,24 @@ export namespace Utils {
93
93
  }, 0);
94
94
  }
95
95
 
96
+ /**
97
+ * Correct object's property value type
98
+ * @param input Input object
99
+ * @param fields Fields to correct
100
+ */
101
+ export function correctTypes<
102
+ T extends {},
103
+ F extends { [P in keyof T]: DataTypes.BasicNames }
104
+ >(input: T, fields: F) {
105
+ for (const field in fields) {
106
+ const value = Reflect.get(input, field);
107
+ const newValue = DataTypes.convertByType(value, fields[field]);
108
+ if (newValue !== value) {
109
+ Reflect.set(input, field, newValue);
110
+ }
111
+ }
112
+ }
113
+
96
114
  /**
97
115
  * Two values equal
98
116
  * @param v1 Value 1