@etsoo/shared 1.0.57 → 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|
@@ -133,17 +133,27 @@ test('Tests for dataAs', () => {
133
133
  formData.append('price', '34.25');
134
134
  formData.append('options', '1,2,3,4');
135
135
  formData.append('memo', 'Memo');
136
+ formData.append('email', 'a@b');
137
+ formData.append('email', 'c@d');
138
+ formData.append('code', '123');
139
+ formData.append('code', '456');
136
140
 
137
141
  const data = DomUtils.dataAs(formData, {
138
142
  id: 'number',
139
143
  name: 'string',
140
144
  price: 'number',
141
- options: 'number[]'
145
+ options: 'number[]',
146
+ email: 'string[]',
147
+ code: 'number[]'
142
148
  });
143
149
 
144
150
  expect(data.id).toStrictEqual(1234);
145
151
  expect(data.options?.length).toStrictEqual(4);
146
152
  expect(data.options![0]).toStrictEqual(1);
153
+
154
+ expect(data.email).toEqual(['a@b', 'c@d']);
155
+ expect(data.code?.length).toStrictEqual(2);
156
+ expect(data.code).toEqual([123, 456]);
147
157
  expect(data).not.toHaveProperty('memo', 'Memo');
148
158
 
149
159
  const keepSourceData = DomUtils.dataAs(
@@ -23,12 +23,17 @@ test('Tests for getDataChanges', () => {
23
23
  expect(input.amount).toBeUndefined();
24
24
  });
25
25
 
26
- test('Tests for formatLowerLetter', () => {
27
- expect(Utils.formatLowerLetter('HelloWorld')).toBe('helloWorld');
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');
28
30
  });
29
31
 
30
- test('Tests for formatUpperLetter', () => {
31
- expect(Utils.formatUpperLetter('hello')).toBe('Hello');
32
+ test('Tests for formatString', () => {
33
+ const template = '{0} is first item, {1} is second item, {0} repeat';
34
+ const result = 'aa is first item, bb is second item, aa repeat';
35
+ expect(Utils.formatString(template, 'aa', 'bb')).toBe(result);
36
+ expect(template.format('aa', 'bb')).toBe(result);
32
37
  });
33
38
 
34
39
  test('Tests for joinItems', () => {
@@ -48,7 +53,10 @@ test('Tests for newGUID', () => {
48
53
  });
49
54
 
50
55
  test('Tests for removeNonLetters', () => {
51
- 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);
52
60
  });
53
61
 
54
62
  test('Tests for objectEqual', () => {
@@ -94,9 +94,7 @@ var DomUtils;
94
94
  // Properties
95
95
  const properties = Object.keys(template);
96
96
  // Entries
97
- const entries = isFormData(source)
98
- ? source.entries()
99
- : Object.entries(source);
97
+ const entries = Object.entries(isFormData(source) ? formDataToObject(source) : source);
100
98
  for (const [key, value] of entries) {
101
99
  // Is included or keepSource
102
100
  const property = (_a = properties.find((p) => p.localeCompare(key, 'en', { sensitivity: 'base' }) ===
@@ -1,18 +1,42 @@
1
1
  import { DataTypes } from './DataTypes';
2
+ declare global {
3
+ interface String {
4
+ /**
5
+ * Format string
6
+ * @param this Template
7
+ * @param parameters Parameters to fill the template
8
+ */
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;
21
+ }
22
+ }
2
23
  /**
3
24
  * Utilities
4
25
  */
5
26
  export declare namespace Utils {
6
27
  /**
7
- * Format word's first letter to lower case
8
- * @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
9
31
  */
10
- function formatLowerLetter(word: string): string;
32
+ function formatInitial(input: string, upperCase?: boolean): string;
11
33
  /**
12
- * Format word's first letter to upper case
13
- * @param word Word
34
+ * Format string with parameters
35
+ * @param template Template with {0}, {1}, ...
36
+ * @param parameters Parameters to fill the template
37
+ * @returns Result
14
38
  */
15
- function formatUpperLetter(word: string): string;
39
+ function formatString(template: string, ...parameters: string[]): string;
16
40
  /**
17
41
  * Get data changed fields with input data updated
18
42
  * @param input Input data
package/lib/cjs/Utils.js CHANGED
@@ -2,27 +2,45 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Utils = void 0;
4
4
  const DataTypes_1 = require("./DataTypes");
5
+ String.prototype.format = function (...parameters) {
6
+ let template = this;
7
+ parameters.forEach((value, index) => {
8
+ template = template.replace(new RegExp(`\\{${index}\\}`, 'g'), value);
9
+ });
10
+ return template;
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
+ };
5
20
  /**
6
21
  * Utilities
7
22
  */
8
23
  var Utils;
9
24
  (function (Utils) {
10
25
  /**
11
- * Format word's first letter to lower case
12
- * @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
13
29
  */
14
- function formatLowerLetter(word) {
15
- return word.charAt(0).toLowerCase() + word.slice(1);
30
+ function formatInitial(input, upperCase = false) {
31
+ return input.formatInitial(upperCase);
16
32
  }
17
- Utils.formatLowerLetter = formatLowerLetter;
33
+ Utils.formatInitial = formatInitial;
18
34
  /**
19
- * Format word's first letter to upper case
20
- * @param word Word
35
+ * Format string with parameters
36
+ * @param template Template with {0}, {1}, ...
37
+ * @param parameters Parameters to fill the template
38
+ * @returns Result
21
39
  */
22
- function formatUpperLetter(word) {
23
- return word.charAt(0).toUpperCase() + word.slice(1);
40
+ function formatString(template, ...parameters) {
41
+ return template.format(...parameters);
24
42
  }
25
- Utils.formatUpperLetter = formatUpperLetter;
43
+ Utils.formatString = formatString;
26
44
  /**
27
45
  * Get data changed fields with input data updated
28
46
  * @param input Input data
@@ -173,9 +191,7 @@ var Utils;
173
191
  * @returns Result
174
192
  */
175
193
  Utils.removeNonLetters = (input) => {
176
- if (input == null || input === '')
177
- return input;
178
- return input.replace(/[^a-zA-Z0-9]/g, '');
194
+ return input === null || input === void 0 ? void 0 : input.removeNonLetters();
179
195
  };
180
196
  /**
181
197
  * Set source with new labels
@@ -206,9 +222,9 @@ var Utils;
206
222
  Utils.snakeNameToWord = (name, firstOnly = false) => {
207
223
  const items = name.split('_');
208
224
  if (firstOnly) {
209
- items[0] = formatUpperLetter(items[0]);
225
+ items[0] = items[0].formatInitial(true);
210
226
  return items.join(' ');
211
227
  }
212
- return items.map((part) => formatUpperLetter(part)).join(' ');
228
+ return items.map((part) => part.formatInitial(true)).join(' ');
213
229
  };
214
230
  })(Utils = exports.Utils || (exports.Utils = {}));
@@ -91,9 +91,7 @@ export var DomUtils;
91
91
  // Properties
92
92
  const properties = Object.keys(template);
93
93
  // Entries
94
- const entries = isFormData(source)
95
- ? source.entries()
96
- : Object.entries(source);
94
+ const entries = Object.entries(isFormData(source) ? formDataToObject(source) : source);
97
95
  for (const [key, value] of entries) {
98
96
  // Is included or keepSource
99
97
  const property = (_a = properties.find((p) => p.localeCompare(key, 'en', { sensitivity: 'base' }) ===
@@ -1,18 +1,42 @@
1
1
  import { DataTypes } from './DataTypes';
2
+ declare global {
3
+ interface String {
4
+ /**
5
+ * Format string
6
+ * @param this Template
7
+ * @param parameters Parameters to fill the template
8
+ */
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;
21
+ }
22
+ }
2
23
  /**
3
24
  * Utilities
4
25
  */
5
26
  export declare namespace Utils {
6
27
  /**
7
- * Format word's first letter to lower case
8
- * @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
9
31
  */
10
- function formatLowerLetter(word: string): string;
32
+ function formatInitial(input: string, upperCase?: boolean): string;
11
33
  /**
12
- * Format word's first letter to upper case
13
- * @param word Word
34
+ * Format string with parameters
35
+ * @param template Template with {0}, {1}, ...
36
+ * @param parameters Parameters to fill the template
37
+ * @returns Result
14
38
  */
15
- function formatUpperLetter(word: string): string;
39
+ function formatString(template: string, ...parameters: string[]): string;
16
40
  /**
17
41
  * Get data changed fields with input data updated
18
42
  * @param input Input data
package/lib/mjs/Utils.js CHANGED
@@ -1,25 +1,43 @@
1
1
  import { DataTypes } from './DataTypes';
2
+ String.prototype.format = function (...parameters) {
3
+ let template = this;
4
+ parameters.forEach((value, index) => {
5
+ template = template.replace(new RegExp(`\\{${index}\\}`, 'g'), value);
6
+ });
7
+ return template;
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
+ };
2
17
  /**
3
18
  * Utilities
4
19
  */
5
20
  export var Utils;
6
21
  (function (Utils) {
7
22
  /**
8
- * Format word's first letter to lower case
9
- * @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
10
26
  */
11
- function formatLowerLetter(word) {
12
- return word.charAt(0).toLowerCase() + word.slice(1);
27
+ function formatInitial(input, upperCase = false) {
28
+ return input.formatInitial(upperCase);
13
29
  }
14
- Utils.formatLowerLetter = formatLowerLetter;
30
+ Utils.formatInitial = formatInitial;
15
31
  /**
16
- * Format word's first letter to upper case
17
- * @param word Word
32
+ * Format string with parameters
33
+ * @param template Template with {0}, {1}, ...
34
+ * @param parameters Parameters to fill the template
35
+ * @returns Result
18
36
  */
19
- function formatUpperLetter(word) {
20
- return word.charAt(0).toUpperCase() + word.slice(1);
37
+ function formatString(template, ...parameters) {
38
+ return template.format(...parameters);
21
39
  }
22
- Utils.formatUpperLetter = formatUpperLetter;
40
+ Utils.formatString = formatString;
23
41
  /**
24
42
  * Get data changed fields with input data updated
25
43
  * @param input Input data
@@ -170,9 +188,7 @@ export var Utils;
170
188
  * @returns Result
171
189
  */
172
190
  Utils.removeNonLetters = (input) => {
173
- if (input == null || input === '')
174
- return input;
175
- return input.replace(/[^a-zA-Z0-9]/g, '');
191
+ return input === null || input === void 0 ? void 0 : input.removeNonLetters();
176
192
  };
177
193
  /**
178
194
  * Set source with new labels
@@ -203,9 +219,9 @@ export var Utils;
203
219
  Utils.snakeNameToWord = (name, firstOnly = false) => {
204
220
  const items = name.split('_');
205
221
  if (firstOnly) {
206
- items[0] = formatUpperLetter(items[0]);
222
+ items[0] = items[0].formatInitial(true);
207
223
  return items.join(' ');
208
224
  }
209
- return items.map((part) => formatUpperLetter(part)).join(' ');
225
+ return items.map((part) => part.formatInitial(true)).join(' ');
210
226
  };
211
227
  })(Utils || (Utils = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.57",
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",
@@ -24,6 +24,11 @@
24
24
  "<rootDir>/__tests__/**/*.ts"
25
25
  ],
26
26
  "testEnvironment": "jsdom",
27
+ "moduleFileExtensions": [
28
+ "js",
29
+ "ts",
30
+ "d.ts"
31
+ ],
27
32
  "transform": {
28
33
  ".+\\.ts$": "ts-jest"
29
34
  }
package/src/DomUtils.ts CHANGED
@@ -108,9 +108,9 @@ export namespace DomUtils {
108
108
  const properties = Object.keys(template);
109
109
 
110
110
  // Entries
111
- const entries = isFormData(source)
112
- ? source.entries()
113
- : Object.entries(source);
111
+ const entries = Object.entries(
112
+ isFormData(source) ? formDataToObject(source) : source
113
+ );
114
114
 
115
115
  for (const [key, value] of entries) {
116
116
  // Is included or keepSource
package/src/Utils.ts CHANGED
@@ -1,23 +1,76 @@
1
1
  import { DataTypes } from './DataTypes';
2
2
 
3
+ declare global {
4
+ interface String {
5
+ /**
6
+ * Format string
7
+ * @param this Template
8
+ * @param parameters Parameters to fill the template
9
+ */
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;
24
+ }
25
+ }
26
+
27
+ String.prototype.format = function (
28
+ this: string,
29
+ ...parameters: string[]
30
+ ): string {
31
+ let template = this;
32
+ parameters.forEach((value, index) => {
33
+ template = template.replace(new RegExp(`\\{${index}\\}`, 'g'), value);
34
+ });
35
+ return template;
36
+ };
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
+
3
53
  /**
4
54
  * Utilities
5
55
  */
6
56
  export namespace Utils {
7
57
  /**
8
- * Format word's first letter to lower case
9
- * @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
10
61
  */
11
- export function formatLowerLetter(word: string) {
12
- return word.charAt(0).toLowerCase() + word.slice(1);
62
+ export function formatInitial(input: string, upperCase: boolean = false) {
63
+ return input.formatInitial(upperCase);
13
64
  }
14
65
 
15
66
  /**
16
- * Format word's first letter to upper case
17
- * @param word Word
67
+ * Format string with parameters
68
+ * @param template Template with {0}, {1}, ...
69
+ * @param parameters Parameters to fill the template
70
+ * @returns Result
18
71
  */
19
- export function formatUpperLetter(word: string) {
20
- return word.charAt(0).toUpperCase() + word.slice(1);
72
+ export function formatString(template: string, ...parameters: string[]) {
73
+ return template.format(...parameters);
21
74
  }
22
75
 
23
76
  /**
@@ -200,8 +253,7 @@ export namespace Utils {
200
253
  * @returns Result
201
254
  */
202
255
  export const removeNonLetters = (input?: string) => {
203
- if (input == null || input === '') return input;
204
- return input.replace(/[^a-zA-Z0-9]/g, '');
256
+ return input?.removeNonLetters();
205
257
  };
206
258
 
207
259
  /**
@@ -246,10 +298,10 @@ export namespace Utils {
246
298
  ) => {
247
299
  const items = name.split('_');
248
300
  if (firstOnly) {
249
- items[0] = formatUpperLetter(items[0]);
301
+ items[0] = items[0].formatInitial(true);
250
302
  return items.join(' ');
251
303
  }
252
304
 
253
- return items.map((part) => formatUpperLetter(part)).join(' ');
305
+ return items.map((part) => part.formatInitial(true)).join(' ');
254
306
  };
255
307
  }