@etsoo/shared 1.0.60 → 1.0.61

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
@@ -124,8 +124,8 @@ String and other related utilities
124
124
 
125
125
  |Name|Description|
126
126
  |---:|---|
127
- |formatLowerLetter|Format word's first letter to lower case|
128
- |formatUpperLetter|Format word's first letter to upper case|
127
+ |formatInitial|Format inital character to lower case or upper case|
128
+ |formatString|Format string with parameters|
129
129
  |getDataChanges|Get data changed fields with input data updated|
130
130
  |getTimeZone|Get time zone|
131
131
  |joinItems|Join items as a string|
@@ -23,6 +23,12 @@ test('Tests for getDataChanges', () => {
23
23
  expect(input.amount).toBeUndefined();
24
24
  });
25
25
 
26
+ test('Tests for formatInitial', () => {
27
+ expect(Utils.formatInitial('HelloWorld')).toBe('helloWorld');
28
+ expect('HelloWorld'.formatInitial(false)).toBe('helloWorld');
29
+ expect('hello'.formatInitial(true)).toBe('Hello');
30
+ });
31
+
26
32
  test('Tests for formatString', () => {
27
33
  const template = '{0} is first item, {1} is second item, {0} repeat';
28
34
  const result = 'aa is first item, bb is second item, aa repeat';
@@ -30,14 +36,6 @@ test('Tests for formatString', () => {
30
36
  expect(template.format('aa', 'bb')).toBe(result);
31
37
  });
32
38
 
33
- test('Tests for formatLowerLetter', () => {
34
- expect(Utils.formatLowerLetter('HelloWorld')).toBe('helloWorld');
35
- });
36
-
37
- test('Tests for formatUpperLetter', () => {
38
- expect(Utils.formatUpperLetter('hello')).toBe('Hello');
39
- });
40
-
41
39
  test('Tests for joinItems', () => {
42
40
  expect(Utils.joinItems(['a', undefined, ' b', '', 'c '], ',')).toBe(
43
41
  'a,b,c'
@@ -55,7 +53,10 @@ test('Tests for newGUID', () => {
55
53
  });
56
54
 
57
55
  test('Tests for removeNonLetters', () => {
58
- expect(Utils.removeNonLetters('1234-5678@abc.')).toBe('12345678abc');
56
+ const input = '1234-5678@abc.';
57
+ const result = '12345678abc';
58
+ expect(Utils.removeNonLetters(input)).toBe(result);
59
+ expect(input.removeNonLetters()).toBe(result);
59
60
  });
60
61
 
61
62
  test('Tests for objectEqual', () => {
@@ -5,9 +5,19 @@ declare global {
5
5
  * Format string
6
6
  * @param this Template
7
7
  * @param parameters Parameters to fill the template
8
- * @returns Result
9
8
  */
10
9
  format(this: string, ...parameters: string[]): string;
10
+ /**
11
+ * Forat inital character
12
+ * @param this Input string
13
+ * @param upperCase To upper case or lower case
14
+ */
15
+ formatInitial(this: string, upperCase: boolean): string;
16
+ /**
17
+ * Remove non letters (0-9, a-z, A-Z)
18
+ * @param this Input string
19
+ */
20
+ removeNonLetters(this: string): string;
11
21
  }
12
22
  }
13
23
  /**
@@ -15,22 +25,18 @@ declare global {
15
25
  */
16
26
  export declare namespace Utils {
17
27
  /**
18
- * Format word's first letter to lower case
19
- * @param word Word
28
+ * Format inital character to lower case or upper case
29
+ * @param input Input string
30
+ * @param upperCase To upper case or lower case
20
31
  */
21
- function formatLowerLetter(word: string): string;
32
+ function formatInitial(input: string, upperCase?: boolean): string;
22
33
  /**
23
- * Format string
34
+ * Format string with parameters
24
35
  * @param template Template with {0}, {1}, ...
25
36
  * @param parameters Parameters to fill the template
26
37
  * @returns Result
27
38
  */
28
39
  function formatString(template: string, ...parameters: string[]): string;
29
- /**
30
- * Format word's first letter to upper case
31
- * @param word Word
32
- */
33
- function formatUpperLetter(word: string): string;
34
40
  /**
35
41
  * Get data changed fields with input data updated
36
42
  * @param input Input data
package/lib/cjs/Utils.js CHANGED
@@ -9,21 +9,30 @@ String.prototype.format = function (...parameters) {
9
9
  });
10
10
  return template;
11
11
  };
12
+ String.prototype.formatInitial = function (upperCase = false) {
13
+ const initial = this.charAt(0);
14
+ return ((upperCase ? initial.toUpperCase() : initial.toLowerCase()) +
15
+ this.slice(1));
16
+ };
17
+ String.prototype.removeNonLetters = function () {
18
+ return this.replace(/[^a-zA-Z0-9]/g, '');
19
+ };
12
20
  /**
13
21
  * Utilities
14
22
  */
15
23
  var Utils;
16
24
  (function (Utils) {
17
25
  /**
18
- * Format word's first letter to lower case
19
- * @param word Word
26
+ * Format inital character to lower case or upper case
27
+ * @param input Input string
28
+ * @param upperCase To upper case or lower case
20
29
  */
21
- function formatLowerLetter(word) {
22
- return word.charAt(0).toLowerCase() + word.slice(1);
30
+ function formatInitial(input, upperCase = false) {
31
+ return input.formatInitial(upperCase);
23
32
  }
24
- Utils.formatLowerLetter = formatLowerLetter;
33
+ Utils.formatInitial = formatInitial;
25
34
  /**
26
- * Format string
35
+ * Format string with parameters
27
36
  * @param template Template with {0}, {1}, ...
28
37
  * @param parameters Parameters to fill the template
29
38
  * @returns Result
@@ -32,14 +41,6 @@ var Utils;
32
41
  return template.format(...parameters);
33
42
  }
34
43
  Utils.formatString = formatString;
35
- /**
36
- * Format word's first letter to upper case
37
- * @param word Word
38
- */
39
- function formatUpperLetter(word) {
40
- return word.charAt(0).toUpperCase() + word.slice(1);
41
- }
42
- Utils.formatUpperLetter = formatUpperLetter;
43
44
  /**
44
45
  * Get data changed fields with input data updated
45
46
  * @param input Input data
@@ -190,9 +191,7 @@ var Utils;
190
191
  * @returns Result
191
192
  */
192
193
  Utils.removeNonLetters = (input) => {
193
- if (input == null || input === '')
194
- return input;
195
- return input.replace(/[^a-zA-Z0-9]/g, '');
194
+ return input === null || input === void 0 ? void 0 : input.removeNonLetters();
196
195
  };
197
196
  /**
198
197
  * Set source with new labels
@@ -223,9 +222,9 @@ var Utils;
223
222
  Utils.snakeNameToWord = (name, firstOnly = false) => {
224
223
  const items = name.split('_');
225
224
  if (firstOnly) {
226
- items[0] = formatUpperLetter(items[0]);
225
+ items[0] = items[0].formatInitial(true);
227
226
  return items.join(' ');
228
227
  }
229
- return items.map((part) => formatUpperLetter(part)).join(' ');
228
+ return items.map((part) => part.formatInitial(true)).join(' ');
230
229
  };
231
230
  })(Utils = exports.Utils || (exports.Utils = {}));
@@ -5,9 +5,19 @@ declare global {
5
5
  * Format string
6
6
  * @param this Template
7
7
  * @param parameters Parameters to fill the template
8
- * @returns Result
9
8
  */
10
9
  format(this: string, ...parameters: string[]): string;
10
+ /**
11
+ * Forat inital character
12
+ * @param this Input string
13
+ * @param upperCase To upper case or lower case
14
+ */
15
+ formatInitial(this: string, upperCase: boolean): string;
16
+ /**
17
+ * Remove non letters (0-9, a-z, A-Z)
18
+ * @param this Input string
19
+ */
20
+ removeNonLetters(this: string): string;
11
21
  }
12
22
  }
13
23
  /**
@@ -15,22 +25,18 @@ declare global {
15
25
  */
16
26
  export declare namespace Utils {
17
27
  /**
18
- * Format word's first letter to lower case
19
- * @param word Word
28
+ * Format inital character to lower case or upper case
29
+ * @param input Input string
30
+ * @param upperCase To upper case or lower case
20
31
  */
21
- function formatLowerLetter(word: string): string;
32
+ function formatInitial(input: string, upperCase?: boolean): string;
22
33
  /**
23
- * Format string
34
+ * Format string with parameters
24
35
  * @param template Template with {0}, {1}, ...
25
36
  * @param parameters Parameters to fill the template
26
37
  * @returns Result
27
38
  */
28
39
  function formatString(template: string, ...parameters: string[]): string;
29
- /**
30
- * Format word's first letter to upper case
31
- * @param word Word
32
- */
33
- function formatUpperLetter(word: string): string;
34
40
  /**
35
41
  * Get data changed fields with input data updated
36
42
  * @param input Input data
package/lib/mjs/Utils.js CHANGED
@@ -6,21 +6,30 @@ String.prototype.format = function (...parameters) {
6
6
  });
7
7
  return template;
8
8
  };
9
+ String.prototype.formatInitial = function (upperCase = false) {
10
+ const initial = this.charAt(0);
11
+ return ((upperCase ? initial.toUpperCase() : initial.toLowerCase()) +
12
+ this.slice(1));
13
+ };
14
+ String.prototype.removeNonLetters = function () {
15
+ return this.replace(/[^a-zA-Z0-9]/g, '');
16
+ };
9
17
  /**
10
18
  * Utilities
11
19
  */
12
20
  export var Utils;
13
21
  (function (Utils) {
14
22
  /**
15
- * Format word's first letter to lower case
16
- * @param word Word
23
+ * Format inital character to lower case or upper case
24
+ * @param input Input string
25
+ * @param upperCase To upper case or lower case
17
26
  */
18
- function formatLowerLetter(word) {
19
- return word.charAt(0).toLowerCase() + word.slice(1);
27
+ function formatInitial(input, upperCase = false) {
28
+ return input.formatInitial(upperCase);
20
29
  }
21
- Utils.formatLowerLetter = formatLowerLetter;
30
+ Utils.formatInitial = formatInitial;
22
31
  /**
23
- * Format string
32
+ * Format string with parameters
24
33
  * @param template Template with {0}, {1}, ...
25
34
  * @param parameters Parameters to fill the template
26
35
  * @returns Result
@@ -29,14 +38,6 @@ export var Utils;
29
38
  return template.format(...parameters);
30
39
  }
31
40
  Utils.formatString = formatString;
32
- /**
33
- * Format word's first letter to upper case
34
- * @param word Word
35
- */
36
- function formatUpperLetter(word) {
37
- return word.charAt(0).toUpperCase() + word.slice(1);
38
- }
39
- Utils.formatUpperLetter = formatUpperLetter;
40
41
  /**
41
42
  * Get data changed fields with input data updated
42
43
  * @param input Input data
@@ -187,9 +188,7 @@ export var Utils;
187
188
  * @returns Result
188
189
  */
189
190
  Utils.removeNonLetters = (input) => {
190
- if (input == null || input === '')
191
- return input;
192
- return input.replace(/[^a-zA-Z0-9]/g, '');
191
+ return input === null || input === void 0 ? void 0 : input.removeNonLetters();
193
192
  };
194
193
  /**
195
194
  * Set source with new labels
@@ -220,9 +219,9 @@ export var Utils;
220
219
  Utils.snakeNameToWord = (name, firstOnly = false) => {
221
220
  const items = name.split('_');
222
221
  if (firstOnly) {
223
- items[0] = formatUpperLetter(items[0]);
222
+ items[0] = items[0].formatInitial(true);
224
223
  return items.join(' ');
225
224
  }
226
- return items.map((part) => formatUpperLetter(part)).join(' ');
225
+ return items.map((part) => part.formatInitial(true)).join(' ');
227
226
  };
228
227
  })(Utils || (Utils = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.60",
3
+ "version": "1.0.61",
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
@@ -6,9 +6,21 @@ declare global {
6
6
  * Format string
7
7
  * @param this Template
8
8
  * @param parameters Parameters to fill the template
9
- * @returns Result
10
9
  */
11
10
  format(this: string, ...parameters: string[]): string;
11
+
12
+ /**
13
+ * Forat inital character
14
+ * @param this Input string
15
+ * @param upperCase To upper case or lower case
16
+ */
17
+ formatInitial(this: string, upperCase: boolean): string;
18
+
19
+ /**
20
+ * Remove non letters (0-9, a-z, A-Z)
21
+ * @param this Input string
22
+ */
23
+ removeNonLetters(this: string): string;
12
24
  }
13
25
  }
14
26
 
@@ -23,20 +35,36 @@ String.prototype.format = function (
23
35
  return template;
24
36
  };
25
37
 
38
+ String.prototype.formatInitial = function (
39
+ this: string,
40
+ upperCase: boolean = false
41
+ ) {
42
+ const initial = this.charAt(0);
43
+ return (
44
+ (upperCase ? initial.toUpperCase() : initial.toLowerCase()) +
45
+ this.slice(1)
46
+ );
47
+ };
48
+
49
+ String.prototype.removeNonLetters = function (this: string) {
50
+ return this.replace(/[^a-zA-Z0-9]/g, '');
51
+ };
52
+
26
53
  /**
27
54
  * Utilities
28
55
  */
29
56
  export namespace Utils {
30
57
  /**
31
- * Format word's first letter to lower case
32
- * @param word Word
58
+ * Format inital character to lower case or upper case
59
+ * @param input Input string
60
+ * @param upperCase To upper case or lower case
33
61
  */
34
- export function formatLowerLetter(word: string) {
35
- return word.charAt(0).toLowerCase() + word.slice(1);
62
+ export function formatInitial(input: string, upperCase: boolean = false) {
63
+ return input.formatInitial(upperCase);
36
64
  }
37
65
 
38
66
  /**
39
- * Format string
67
+ * Format string with parameters
40
68
  * @param template Template with {0}, {1}, ...
41
69
  * @param parameters Parameters to fill the template
42
70
  * @returns Result
@@ -45,14 +73,6 @@ export namespace Utils {
45
73
  return template.format(...parameters);
46
74
  }
47
75
 
48
- /**
49
- * Format word's first letter to upper case
50
- * @param word Word
51
- */
52
- export function formatUpperLetter(word: string) {
53
- return word.charAt(0).toUpperCase() + word.slice(1);
54
- }
55
-
56
76
  /**
57
77
  * Get data changed fields with input data updated
58
78
  * @param input Input data
@@ -233,8 +253,7 @@ export namespace Utils {
233
253
  * @returns Result
234
254
  */
235
255
  export const removeNonLetters = (input?: string) => {
236
- if (input == null || input === '') return input;
237
- return input.replace(/[^a-zA-Z0-9]/g, '');
256
+ return input?.removeNonLetters();
238
257
  };
239
258
 
240
259
  /**
@@ -279,10 +298,10 @@ export namespace Utils {
279
298
  ) => {
280
299
  const items = name.split('_');
281
300
  if (firstOnly) {
282
- items[0] = formatUpperLetter(items[0]);
301
+ items[0] = items[0].formatInitial(true);
283
302
  return items.join(' ');
284
303
  }
285
304
 
286
- return items.map((part) => formatUpperLetter(part)).join(' ');
305
+ return items.map((part) => part.formatInitial(true)).join(' ');
287
306
  };
288
307
  }