@etsoo/shared 1.0.75 → 1.0.76

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
@@ -131,6 +131,7 @@ String and other related utilities
131
131
 
132
132
  |Name|Description|
133
133
  |---:|---|
134
+ |charsToNumber|Base64 chars to number|
134
135
  |formatInitial|Format inital character to lower case or upper case|
135
136
  |formatString|Format string with parameters|
136
137
  |getDataChanges|Get data changed fields with input data updated|
@@ -139,6 +140,7 @@ String and other related utilities
139
140
  |mergeFormData|Merge form data to primary one|
140
141
  |mergeClasses|Merge class names|
141
142
  |newGUID|Create a GUID|
143
+ |numberToChars|Number to base64 chars|
142
144
  |objectEqual|Test two objects are equal or not|
143
145
  |parseString|Parse string (JSON) to specific type|
144
146
  |setLabels|Set source with new labels|
@@ -67,6 +67,13 @@ test('Tests for newGUID', () => {
67
67
  expect(id1.length).toBe(id2.length);
68
68
  });
69
69
 
70
+ test('Tests for numberToChars and charsToNumber', () => {
71
+ const num = 1638777042242;
72
+ const chars = Utils.numberToChars(num);
73
+ expect(chars).toEqual('QmpkdVgv');
74
+ expect(Utils.charsToNumber(chars)).toEqual(num);
75
+ });
76
+
70
77
  test('Tests for removeNonLetters', () => {
71
78
  const input = '1234-5678@abc.';
72
79
  const result = '12345678abc';
@@ -35,6 +35,12 @@ declare global {
35
35
  * Utilities
36
36
  */
37
37
  export declare namespace Utils {
38
+ /**
39
+ * Base64 chars to number
40
+ * @param base64Chars Base64 chars
41
+ * @returns Number
42
+ */
43
+ function charsToNumber(base64Chars: string): number;
38
44
  /**
39
45
  * Format inital character to lower case or upper case
40
46
  * @param input Input string
@@ -89,6 +95,12 @@ export declare namespace Utils {
89
95
  * Create a GUID
90
96
  */
91
97
  function newGUID(): string;
98
+ /**
99
+ * Number to base64 chars
100
+ * @param num Input number
101
+ * @returns Result
102
+ */
103
+ function numberToChars(num: number): string;
92
104
  /**
93
105
  * Test two objects are equal or not
94
106
  * @param obj1 Object 1
package/lib/cjs/Utils.js CHANGED
@@ -29,6 +29,20 @@ String.prototype.removeNonLetters = function () {
29
29
  */
30
30
  var Utils;
31
31
  (function (Utils) {
32
+ /**
33
+ * Base64 chars to number
34
+ * @param base64Chars Base64 chars
35
+ * @returns Number
36
+ */
37
+ function charsToNumber(base64Chars) {
38
+ const chars = typeof Buffer === 'undefined'
39
+ ? [...atob(base64Chars)].map((char) => char.charCodeAt(0))
40
+ : [...Buffer.from(base64Chars, 'base64')];
41
+ return chars.reduce((previousValue, currentValue, currentIndex) => {
42
+ return previousValue + currentValue * Math.pow(128, currentIndex);
43
+ }, 0);
44
+ }
45
+ Utils.charsToNumber = charsToNumber;
32
46
  /**
33
47
  * Format inital character to lower case or upper case
34
48
  * @param input Input string
@@ -149,6 +163,27 @@ var Utils;
149
163
  });
150
164
  }
151
165
  Utils.newGUID = newGUID;
166
+ /**
167
+ * Number to base64 chars
168
+ * @param num Input number
169
+ * @returns Result
170
+ */
171
+ function numberToChars(num) {
172
+ const codes = [];
173
+ while (num > 0) {
174
+ const code = num % 128;
175
+ codes.push(code);
176
+ num = (num - code) / 128;
177
+ }
178
+ if (typeof Buffer === 'undefined') {
179
+ return btoa(String.fromCharCode(...codes));
180
+ }
181
+ else {
182
+ const buffer = Buffer.from(codes);
183
+ return buffer.toString('base64');
184
+ }
185
+ }
186
+ Utils.numberToChars = numberToChars;
152
187
  /**
153
188
  * Test two objects are equal or not
154
189
  * @param obj1 Object 1
@@ -35,6 +35,12 @@ declare global {
35
35
  * Utilities
36
36
  */
37
37
  export declare namespace Utils {
38
+ /**
39
+ * Base64 chars to number
40
+ * @param base64Chars Base64 chars
41
+ * @returns Number
42
+ */
43
+ function charsToNumber(base64Chars: string): number;
38
44
  /**
39
45
  * Format inital character to lower case or upper case
40
46
  * @param input Input string
@@ -89,6 +95,12 @@ export declare namespace Utils {
89
95
  * Create a GUID
90
96
  */
91
97
  function newGUID(): string;
98
+ /**
99
+ * Number to base64 chars
100
+ * @param num Input number
101
+ * @returns Result
102
+ */
103
+ function numberToChars(num: number): string;
92
104
  /**
93
105
  * Test two objects are equal or not
94
106
  * @param obj1 Object 1
package/lib/mjs/Utils.js CHANGED
@@ -26,6 +26,20 @@ String.prototype.removeNonLetters = function () {
26
26
  */
27
27
  export var Utils;
28
28
  (function (Utils) {
29
+ /**
30
+ * Base64 chars to number
31
+ * @param base64Chars Base64 chars
32
+ * @returns Number
33
+ */
34
+ function charsToNumber(base64Chars) {
35
+ const chars = typeof Buffer === 'undefined'
36
+ ? [...atob(base64Chars)].map((char) => char.charCodeAt(0))
37
+ : [...Buffer.from(base64Chars, 'base64')];
38
+ return chars.reduce((previousValue, currentValue, currentIndex) => {
39
+ return previousValue + currentValue * Math.pow(128, currentIndex);
40
+ }, 0);
41
+ }
42
+ Utils.charsToNumber = charsToNumber;
29
43
  /**
30
44
  * Format inital character to lower case or upper case
31
45
  * @param input Input string
@@ -146,6 +160,27 @@ export var Utils;
146
160
  });
147
161
  }
148
162
  Utils.newGUID = newGUID;
163
+ /**
164
+ * Number to base64 chars
165
+ * @param num Input number
166
+ * @returns Result
167
+ */
168
+ function numberToChars(num) {
169
+ const codes = [];
170
+ while (num > 0) {
171
+ const code = num % 128;
172
+ codes.push(code);
173
+ num = (num - code) / 128;
174
+ }
175
+ if (typeof Buffer === 'undefined') {
176
+ return btoa(String.fromCharCode(...codes));
177
+ }
178
+ else {
179
+ const buffer = Buffer.from(codes);
180
+ return buffer.toString('base64');
181
+ }
182
+ }
183
+ Utils.numberToChars = numberToChars;
149
184
  /**
150
185
  * Test two objects are equal or not
151
186
  * @param obj1 Object 1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.75",
3
+ "version": "1.0.76",
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
@@ -77,6 +77,22 @@ String.prototype.removeNonLetters = function (this: string) {
77
77
  * Utilities
78
78
  */
79
79
  export namespace Utils {
80
+ /**
81
+ * Base64 chars to number
82
+ * @param base64Chars Base64 chars
83
+ * @returns Number
84
+ */
85
+ export function charsToNumber(base64Chars: string) {
86
+ const chars =
87
+ typeof Buffer === 'undefined'
88
+ ? [...atob(base64Chars)].map((char) => char.charCodeAt(0))
89
+ : [...Buffer.from(base64Chars, 'base64')];
90
+
91
+ return chars.reduce((previousValue, currentValue, currentIndex) => {
92
+ return previousValue + currentValue * Math.pow(128, currentIndex);
93
+ }, 0);
94
+ }
95
+
80
96
  /**
81
97
  * Format inital character to lower case or upper case
82
98
  * @param input Input string
@@ -218,6 +234,27 @@ export namespace Utils {
218
234
  });
219
235
  }
220
236
 
237
+ /**
238
+ * Number to base64 chars
239
+ * @param num Input number
240
+ * @returns Result
241
+ */
242
+ export function numberToChars(num: number) {
243
+ const codes = [];
244
+ while (num > 0) {
245
+ const code = num % 128;
246
+ codes.push(code);
247
+ num = (num - code) / 128;
248
+ }
249
+
250
+ if (typeof Buffer === 'undefined') {
251
+ return btoa(String.fromCharCode(...codes));
252
+ } else {
253
+ const buffer = Buffer.from(codes);
254
+ return buffer.toString('base64');
255
+ }
256
+ }
257
+
221
258
  /**
222
259
  * Test two objects are equal or not
223
260
  * @param obj1 Object 1