@etsoo/shared 1.0.76 → 1.0.77

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
@@ -132,6 +132,7 @@ String and other related utilities
132
132
  |Name|Description|
133
133
  |---:|---|
134
134
  |charsToNumber|Base64 chars to number|
135
+ |equals|Two values equal|
135
136
  |formatInitial|Format inital character to lower case or upper case|
136
137
  |formatString|Format string with parameters|
137
138
  |getDataChanges|Get data changed fields with input data updated|
@@ -9,7 +9,8 @@ test('Tests for getDataChanges', () => {
9
9
  price: '6.0',
10
10
  amount: '',
11
11
  enabled: true,
12
- value: undefined
12
+ value: undefined,
13
+ ids: [1, 2]
13
14
  };
14
15
  const initData = {
15
16
  id: 1,
@@ -18,7 +19,8 @@ test('Tests for getDataChanges', () => {
18
19
  brand: 'ETSOO',
19
20
  price: 6,
20
21
  amount: 0,
21
- enabled: true
22
+ enabled: true,
23
+ ids: [1, 2]
22
24
  };
23
25
  const fields = Utils.getDataChanges(input, initData);
24
26
  expect(fields).toStrictEqual(['gender', 'brand', 'amount']);
@@ -82,8 +84,8 @@ test('Tests for removeNonLetters', () => {
82
84
  });
83
85
 
84
86
  test('Tests for objectEqual', () => {
85
- const obj1 = { a: 1, b: 'abc', c: true, d: null };
86
- const obj2 = { a: '1', b: 'abc', c: true };
87
+ const obj1 = { a: 1, b: 'abc', c: true, d: null, f: [1, 2] };
88
+ const obj2 = { a: '1', b: 'abc', c: true, f: [1, 2] };
87
89
  expect(Utils.objectEqual(obj1, obj2)).toBeFalsy();
88
90
  expect(Utils.objectEqual(obj1, obj2, [], 0)).toBeTruthy();
89
91
  expect(Utils.objectEqual(obj1, obj2, ['a'])).toBeTruthy();
@@ -41,6 +41,13 @@ export declare namespace Utils {
41
41
  * @returns Number
42
42
  */
43
43
  function charsToNumber(base64Chars: string): number;
44
+ /**
45
+ * Two values equal
46
+ * @param v1 Value 1
47
+ * @param v2 Value 2
48
+ * @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
49
+ */
50
+ function equals(v1: unknown, v2: unknown, strict?: number): boolean;
44
51
  /**
45
52
  * Format inital character to lower case or upper case
46
53
  * @param input Input string
package/lib/cjs/Utils.js CHANGED
@@ -43,6 +43,29 @@ var Utils;
43
43
  }, 0);
44
44
  }
45
45
  Utils.charsToNumber = charsToNumber;
46
+ /**
47
+ * Two values equal
48
+ * @param v1 Value 1
49
+ * @param v2 Value 2
50
+ * @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
51
+ */
52
+ function equals(v1, v2, strict = 1) {
53
+ // Null and undefined case
54
+ if (v1 == null || v2 == null) {
55
+ if (strict <= 1 && v1 == v2)
56
+ return true;
57
+ return v1 === v2;
58
+ }
59
+ // For array and object
60
+ if (typeof v1 === 'object')
61
+ return JSON.stringify(v1) === JSON.stringify(v2);
62
+ // 1 and '1' case
63
+ if (strict === 0)
64
+ return v1 == v2;
65
+ // Strict equal
66
+ return v1 === v2;
67
+ }
68
+ Utils.equals = equals;
46
69
  /**
47
70
  * Format inital character to lower case or upper case
48
71
  * @param input Input string
@@ -85,7 +108,7 @@ var Utils;
85
108
  }
86
109
  if (initValue != null) {
87
110
  const newValue = DataTypes_1.DataTypes.convert(value, initValue);
88
- if (newValue === initValue) {
111
+ if (Utils.equals(newValue, initValue)) {
89
112
  Reflect.deleteProperty(input, key);
90
113
  return;
91
114
  }
@@ -202,14 +225,7 @@ var Utils;
202
225
  // Values
203
226
  const v1 = Reflect.get(obj1, key);
204
227
  const v2 = Reflect.get(obj2, key);
205
- // Null and undefined case
206
- if (v1 == null && v2 == null && strict <= 1)
207
- continue;
208
- // 1 and '1' case
209
- if (strict === 0 && v1 == v2)
210
- continue;
211
- // Strict equal
212
- if (v1 !== v2)
228
+ if (!Utils.equals(v1, v2, strict))
213
229
  return false;
214
230
  }
215
231
  return true;
@@ -41,6 +41,13 @@ export declare namespace Utils {
41
41
  * @returns Number
42
42
  */
43
43
  function charsToNumber(base64Chars: string): number;
44
+ /**
45
+ * Two values equal
46
+ * @param v1 Value 1
47
+ * @param v2 Value 2
48
+ * @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
49
+ */
50
+ function equals(v1: unknown, v2: unknown, strict?: number): boolean;
44
51
  /**
45
52
  * Format inital character to lower case or upper case
46
53
  * @param input Input string
package/lib/mjs/Utils.js CHANGED
@@ -40,6 +40,29 @@ export var Utils;
40
40
  }, 0);
41
41
  }
42
42
  Utils.charsToNumber = charsToNumber;
43
+ /**
44
+ * Two values equal
45
+ * @param v1 Value 1
46
+ * @param v2 Value 2
47
+ * @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
48
+ */
49
+ function equals(v1, v2, strict = 1) {
50
+ // Null and undefined case
51
+ if (v1 == null || v2 == null) {
52
+ if (strict <= 1 && v1 == v2)
53
+ return true;
54
+ return v1 === v2;
55
+ }
56
+ // For array and object
57
+ if (typeof v1 === 'object')
58
+ return JSON.stringify(v1) === JSON.stringify(v2);
59
+ // 1 and '1' case
60
+ if (strict === 0)
61
+ return v1 == v2;
62
+ // Strict equal
63
+ return v1 === v2;
64
+ }
65
+ Utils.equals = equals;
43
66
  /**
44
67
  * Format inital character to lower case or upper case
45
68
  * @param input Input string
@@ -82,7 +105,7 @@ export var Utils;
82
105
  }
83
106
  if (initValue != null) {
84
107
  const newValue = DataTypes.convert(value, initValue);
85
- if (newValue === initValue) {
108
+ if (Utils.equals(newValue, initValue)) {
86
109
  Reflect.deleteProperty(input, key);
87
110
  return;
88
111
  }
@@ -199,14 +222,7 @@ export var Utils;
199
222
  // Values
200
223
  const v1 = Reflect.get(obj1, key);
201
224
  const v2 = Reflect.get(obj2, key);
202
- // Null and undefined case
203
- if (v1 == null && v2 == null && strict <= 1)
204
- continue;
205
- // 1 and '1' case
206
- if (strict === 0 && v1 == v2)
207
- continue;
208
- // Strict equal
209
- if (v1 !== v2)
225
+ if (!Utils.equals(v1, v2, strict))
210
226
  return false;
211
227
  }
212
228
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.76",
3
+ "version": "1.0.77",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
package/src/Utils.ts CHANGED
@@ -93,6 +93,30 @@ export namespace Utils {
93
93
  }, 0);
94
94
  }
95
95
 
96
+ /**
97
+ * Two values equal
98
+ * @param v1 Value 1
99
+ * @param v2 Value 2
100
+ * @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
101
+ */
102
+ export function equals(v1: unknown, v2: unknown, strict = 1) {
103
+ // Null and undefined case
104
+ if (v1 == null || v2 == null) {
105
+ if (strict <= 1 && v1 == v2) return true;
106
+ return v1 === v2;
107
+ }
108
+
109
+ // For array and object
110
+ if (typeof v1 === 'object')
111
+ return JSON.stringify(v1) === JSON.stringify(v2);
112
+
113
+ // 1 and '1' case
114
+ if (strict === 0) return v1 == v2;
115
+
116
+ // Strict equal
117
+ return v1 === v2;
118
+ }
119
+
96
120
  /**
97
121
  * Format inital character to lower case or upper case
98
122
  * @param input Input string
@@ -142,7 +166,7 @@ export namespace Utils {
142
166
 
143
167
  if (initValue != null) {
144
168
  const newValue = DataTypes.convert(value, initValue);
145
- if (newValue === initValue) {
169
+ if (Utils.equals(newValue, initValue)) {
146
170
  Reflect.deleteProperty(input, key);
147
171
  return;
148
172
  }
@@ -280,14 +304,7 @@ export namespace Utils {
280
304
  const v1 = Reflect.get(obj1, key);
281
305
  const v2 = Reflect.get(obj2, key);
282
306
 
283
- // Null and undefined case
284
- if (v1 == null && v2 == null && strict <= 1) continue;
285
-
286
- // 1 and '1' case
287
- if (strict === 0 && v1 == v2) continue;
288
-
289
- // Strict equal
290
- if (v1 !== v2) return false;
307
+ if (!Utils.equals(v1, v2, strict)) return false;
291
308
  }
292
309
 
293
310
  return true;