@futdevpro/fsm-dynamo 1.14.36 → 1.14.37

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.
@@ -0,0 +1,362 @@
1
+ /**
2
+ * String casing típusok enum
3
+ */
4
+ export enum DyFM_StringCase_Type {
5
+ camelCase = 'camel-case',
6
+ PascalCase = 'pascal-case',
7
+ kebabCase = 'kebab-case',
8
+ snake_case = 'snake-case',
9
+ }
10
+
11
+ /**
12
+ * String casing utility class
13
+ * Támogatja a különböző casing típusok közötti konverziót
14
+ */
15
+ export class DyFM_StringCase {
16
+
17
+ /**
18
+ * Bármilyen casing-ből szavak tömbjét generálja
19
+ * @param input - A bemeneti string bármilyen casing formátumban
20
+ * @returns Szavak tömbje kisbetűvel
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * const words = DyFM_StringCase.parseWords('stockItemId');
25
+ * // Output: ['stock', 'item', 'id']
26
+ * ```
27
+ */
28
+ static parseWords(input: string): string[] {
29
+ if (!input || input.trim().length === 0) {
30
+ return [];
31
+ }
32
+
33
+ // Normalizálás: különböző separator-eket space-re cseréljük
34
+ const normalized: string = input
35
+ .replace(/[\s_\-]+/g, ' ')
36
+ .replace(/[^a-zA-Z0-9\s]/g, ' ')
37
+ .trim();
38
+
39
+ // Split by spaces first
40
+ const spaceSegments: string[] = normalized.split(/\s+/).filter((s: string): boolean => s.length > 0);
41
+ const allSegments: string[] = [];
42
+
43
+ // Process each space-separated segment to detect camelCase and acronyms
44
+ for (const segment of spaceSegments) {
45
+ if (segment.length === 0) {
46
+ continue;
47
+ }
48
+
49
+ // Track word boundaries: start of each word
50
+ const wordStarts: number[] = [0]; // First character is always a word start
51
+
52
+ for (let i = 1; i < segment.length; i++) {
53
+ const char: string = segment[i];
54
+ const prevChar: string = segment[i - 1];
55
+ const nextChar: string = i < segment.length - 1 ? segment[i + 1] : '';
56
+
57
+ const isUppercase: boolean = /[A-Z]/.test(char);
58
+ const isLowercase: boolean = /[a-z]/.test(char);
59
+ const isLetter: boolean = /[a-zA-Z]/.test(char);
60
+ const isDigit: boolean = /[0-9]/.test(char);
61
+ const prevIsUppercase: boolean = /[A-Z]/.test(prevChar);
62
+ const prevIsLowercase: boolean = /[a-z]/.test(prevChar);
63
+ const prevIsDigit: boolean = /[0-9]/.test(prevChar);
64
+ const nextIsLowercase: boolean = /[a-z]/.test(nextChar);
65
+ const nextIsUppercase: boolean = /[A-Z]/.test(nextChar);
66
+
67
+ // Number followed by letter: new word start
68
+ // This handles: john2Doe -> john, 2, Doe
69
+ if (prevIsDigit && isLetter) {
70
+ wordStarts.push(i);
71
+ }
72
+ // camelCase boundary: lowercase followed by uppercase
73
+ else if (prevIsLowercase && isUppercase) {
74
+ wordStarts.push(i);
75
+ }
76
+ // Uppercase followed by uppercase then lowercase: new word start
77
+ // This handles:
78
+ // - XMLParser -> XML (acronym ends at L, Parser starts at P) when previous is acronym (multiple uppercase)
79
+ // - AVery -> A (single letter) + Very (starts at V) when previous is single uppercase
80
+ // Pattern: ...X Y z... where X and Y are uppercase, z is lowercase
81
+ else if (isUppercase && prevIsUppercase && nextIsLowercase) {
82
+ // Check if previous characters form an acronym (multiple consecutive uppercase) or single uppercase
83
+ let acronymStart: number = i - 1;
84
+ while (acronymStart > 0 && /[A-Z]/.test(segment[acronymStart - 1])) {
85
+ acronymStart--;
86
+ }
87
+ // If we have multiple uppercase before this (acronym) OR single uppercase, the new word starts at this uppercase character
88
+ if (i - acronymStart >= 1) {
89
+ wordStarts.push(i);
90
+ }
91
+ }
92
+ // Acronym boundary: uppercase sequence followed by lowercase
93
+ // This handles HTMLDocument -> HTML (acronym) + Document (new word starts at D, not at o)
94
+ // Pattern: ...L D o... where L is last of acronym, D is start of new word, o is lowercase
95
+ else if (prevIsUppercase && isLowercase) {
96
+ // Check if previous characters form an acronym
97
+ let acronymStart: number = i - 1;
98
+ while (acronymStart > 0 && /[A-Z]/.test(segment[acronymStart - 1])) {
99
+ acronymStart--;
100
+ }
101
+ // If we have multiple uppercase before this (acronym), the new word starts at the previous uppercase
102
+ // But only if the previous uppercase wasn't already a word start
103
+ if (i - acronymStart > 1) {
104
+ // Check if the previous uppercase (i-1) is already a word start
105
+ const prevIsWordStart: boolean = wordStarts.includes(i - 1);
106
+ if (!prevIsWordStart) {
107
+ // The new word starts at the previous uppercase character (i-1), not at this lowercase
108
+ wordStarts.push(i - 1);
109
+ }
110
+ }
111
+ }
112
+ }
113
+
114
+ // Extract words based on word starts
115
+ for (let i = 0; i < wordStarts.length; i++) {
116
+ const start: number = wordStarts[i];
117
+ const end: number = i < wordStarts.length - 1 ? wordStarts[i + 1] : segment.length;
118
+ const word: string = segment.substring(start, end);
119
+ if (word.length > 0) {
120
+ allSegments.push(word.toLowerCase());
121
+ }
122
+ }
123
+ }
124
+
125
+ return allSegments;
126
+ }
127
+
128
+ /**
129
+ * Bármilyen casing-ből camelCase-re konvertál
130
+ * @param input - A bemeneti string bármilyen casing formátumban
131
+ * @returns camelCase string
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * const result = DyFM_StringCase.toCamelCase('stock-item-id');
136
+ * // Output: 'stockItemId'
137
+ * ```
138
+ */
139
+ static toCamelCase(input: string): string {
140
+ const words: string[] = this.parseWords(input);
141
+ if (words.length === 0) {
142
+ return '';
143
+ }
144
+
145
+ return words
146
+ .map((word: string, index: number): string => {
147
+ if (index === 0) {
148
+ return word.toLowerCase();
149
+ }
150
+ return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
151
+ })
152
+ .join('');
153
+ }
154
+
155
+ /**
156
+ * Bármilyen casing-ből PascalCase-re konvertál
157
+ * @param input - A bemeneti string bármilyen casing formátumban
158
+ * @returns PascalCase string
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * const result = DyFM_StringCase.toPascalCase('stock-item-id');
163
+ * // Output: 'StockItemId'
164
+ * ```
165
+ */
166
+ static toPascalCase(input: string): string {
167
+ const words: string[] = this.parseWords(input);
168
+ if (words.length === 0) {
169
+ return '';
170
+ }
171
+
172
+ return words
173
+ .map((word: string): string => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
174
+ .join('');
175
+ }
176
+
177
+ /**
178
+ * Bármilyen casing-ből kebab-case-re konvertál
179
+ * @param input - A bemeneti string bármilyen casing formátumban
180
+ * @returns kebab-case string
181
+ *
182
+ * @example
183
+ * ```ts
184
+ * const result = DyFM_StringCase.toKebabCase('stockItemId');
185
+ * // Output: 'stock-item-id'
186
+ * ```
187
+ */
188
+ static toKebabCase(input: string): string {
189
+ const words: string[] = this.parseWords(input);
190
+ if (words.length === 0) {
191
+ return '';
192
+ }
193
+
194
+ return words.join('-');
195
+ }
196
+
197
+ /**
198
+ * Bármilyen casing-ből snake_case-re konvertál
199
+ * @param input - A bemeneti string bármilyen casing formátumban
200
+ * @returns snake_case string
201
+ *
202
+ * @example
203
+ * ```ts
204
+ * const result = DyFM_StringCase.toSnakeCase('stockItemId');
205
+ * // Output: 'stock_item_id'
206
+ * ```
207
+ */
208
+ static toSnakeCase(input: string): string {
209
+ const words: string[] = this.parseWords(input);
210
+ if (words.length === 0) {
211
+ return '';
212
+ }
213
+
214
+ return words.join('_');
215
+ }
216
+
217
+ /**
218
+ * Bármilyen casing-ből bármilyen más casing-re konvertál
219
+ * @param input - A bemeneti string bármilyen casing formátumban
220
+ * @param targetCase - A cél casing típus
221
+ * @returns A konvertált string a megadott casing formátumban
222
+ *
223
+ * @example
224
+ * ```ts
225
+ * const result = DyFM_StringCase.convertTo('stockItemId', DyFM_StringCase_Type.kebabCase);
226
+ * // Output: 'stock-item-id'
227
+ * ```
228
+ */
229
+ static convertTo(input: string, targetCase: DyFM_StringCase_Type): string {
230
+ switch (targetCase) {
231
+ case DyFM_StringCase_Type.camelCase:
232
+ return this.toCamelCase(input);
233
+ case DyFM_StringCase_Type.PascalCase:
234
+ return this.toPascalCase(input);
235
+ case DyFM_StringCase_Type.kebabCase:
236
+ return this.toKebabCase(input);
237
+ case DyFM_StringCase_Type.snake_case:
238
+ return this.toSnakeCase(input);
239
+ default:
240
+ return input;
241
+ }
242
+ }
243
+
244
+ /**
245
+ * Bármilyen casing-ből bármilyen más casing-re konvertál (típus nélküli verzió)
246
+ * @param input - A bemeneti string bármilyen casing formátumban
247
+ * @param targetCase - A cél casing típus string formátumban
248
+ * @returns A konvertált string a megadott casing formátumban
249
+ *
250
+ * @example
251
+ * ```ts
252
+ * const result = DyFM_StringCase.convertToAny('stockItemId', 'kebab-case');
253
+ * // Output: 'stock-item-id'
254
+ * ```
255
+ */
256
+ static convertToAny(input: string, targetCase: string): string {
257
+ const normalizedTarget: string = targetCase.toLowerCase().trim();
258
+
259
+ if (normalizedTarget === 'camelcase' || normalizedTarget === 'camel-case') {
260
+ return this.toCamelCase(input);
261
+ }
262
+ if (normalizedTarget === 'pascalcase' || normalizedTarget === 'pascal-case') {
263
+ return this.toPascalCase(input);
264
+ }
265
+ if (normalizedTarget === 'kebabcase' || normalizedTarget === 'kebab-case') {
266
+ return this.toKebabCase(input);
267
+ }
268
+ if (normalizedTarget === 'snakecase' || normalizedTarget === 'snake-case' || normalizedTarget === 'snake_case') {
269
+ return this.toSnakeCase(input);
270
+ }
271
+
272
+ // Ha nem ismert típus, visszaadja az eredeti stringet
273
+ return input;
274
+ }
275
+
276
+ /**
277
+ * Ellenőrzi, hogy egy string tartalmazza-e egy másik stringet bármilyen casing formátumban
278
+ * @param haystack - A string, amiben keresünk
279
+ * @param needle - A string, amit keresünk (bármilyen casing formátumban)
280
+ * @returns true, ha a haystack tartalmazza a needle-t bármilyen casing formátumban
281
+ *
282
+ * @example
283
+ * ```ts
284
+ * const result = DyFM_StringCase.includesIgnoreCase('/:userId/item/delete/:itemId', 'stockItemId');
285
+ * // Output: true (mert 'itemId' megtalálható)
286
+ * ```
287
+ */
288
+ static includesIgnoreCase(haystack: string, needle: string): boolean {
289
+ if (!haystack || !needle) {
290
+ return false;
291
+ }
292
+
293
+ // Különböző casing formátumokban keresünk
294
+ const cases: string[] = [
295
+ needle,
296
+ this.toCamelCase(needle),
297
+ this.toPascalCase(needle),
298
+ this.toKebabCase(needle),
299
+ this.toSnakeCase(needle),
300
+ ];
301
+
302
+ // Különböző variációk: pl. stockItemId -> itemId, stock-item-id -> item-id, stb.
303
+ const words: string[] = this.parseWords(needle);
304
+ if (words.length > 1) {
305
+ // Hozzáadunk rövidített verziókat is (pl. stockItemId -> itemId)
306
+ for (let i = 1; i < words.length; i++) {
307
+ const suffix: string = words.slice(i).join('');
308
+ cases.push(this.toCamelCase(suffix));
309
+ cases.push(this.toKebabCase(suffix));
310
+ cases.push(this.toSnakeCase(suffix));
311
+ }
312
+ }
313
+
314
+ // Ellenőrizzük, hogy a haystack tartalmazza-e bármelyik variációt
315
+ const lowerHaystack: string = haystack.toLowerCase();
316
+ for (const caseVariant of cases) {
317
+ if (caseVariant && lowerHaystack.includes(caseVariant.toLowerCase())) {
318
+ return true;
319
+ }
320
+ }
321
+
322
+ return false;
323
+ }
324
+
325
+ /**
326
+ * Converts any name to a short code
327
+ * @param name - The name to convert
328
+ * @returns The short code
329
+ * examples:
330
+ * John Doe -> JD
331
+ * JohnDoe -> JD
332
+ * John_Doe -> JD
333
+ * johnDoe -> JD
334
+ * john_doe -> JD
335
+ * john-doe -> JD
336
+ *
337
+ * @example
338
+ * ```ts
339
+ * const name = 'John Doe';
340
+ * const shortCode = DyFM_StringCase.anyNameToShortCode(name);
341
+ * console.log(shortCode);
342
+ * // Output: 'JD'
343
+ * ```
344
+ */
345
+ static anyNameToShortCode(name: string): string {
346
+ if (!name || name.trim().length === 0) {
347
+ return '';
348
+ }
349
+
350
+ const words: string[] = this.parseWords(name);
351
+
352
+ // Extract first letter of each segment, uppercase, and join
353
+ return words
354
+ .filter((segment: string): boolean => segment.length > 0)
355
+ .map((segment: string): string => {
356
+ // Get first letter (handles cases where segment might start with non-letter)
357
+ const firstLetter: string | undefined = segment.match(/[a-zA-Z]/)?.[0];
358
+ return firstLetter ? firstLetter.toUpperCase() : '';
359
+ })
360
+ .join('');
361
+ }
362
+ }
@@ -1,3 +1,5 @@
1
+ import { DyFM_StringCase } from './string-case.util';
2
+
1
3
  export interface DyFM_String_BracketSettings {
2
4
  opening: string;
3
5
  closing: string;
@@ -12,7 +14,7 @@ export interface DyFM_String_BracketDetailedResult {
12
14
  /**
13
15
  * String utility class
14
16
  */
15
- export class DyFM_String {
17
+ export class DyFM_String extends DyFM_StringCase {
16
18
 
17
19
  /**
18
20
  * Removes \n, \r and spaces from the start and end of the string
@@ -445,123 +447,4 @@ export class DyFM_String {
445
447
  return { modified, extracted };
446
448
  }
447
449
 
448
- /**
449
- * Converts any name to a short code
450
- * @param name - The name to convert
451
- * @returns The short code
452
- * examples:
453
- * John Doe -> JD
454
- * JohnDoe -> JD
455
- * John_Doe -> JD
456
- * johnDoe -> JD
457
- * john_doe -> JD
458
- * john-doe -> JD
459
- *
460
- * @example
461
- * ```ts
462
- * const name = 'John Doe';
463
- * const shortCode = DyFM_String.anyNameToShortCode(name);
464
- * console.log(shortCode);
465
- * // Output: 'JD'
466
- * ```
467
- */
468
- static anyNameToShortCode(name: string): string {
469
- if (!name || name.trim().length === 0) {
470
- return '';
471
- }
472
-
473
- // Normalize separators: replace spaces, underscores, hyphens, numbers, and special chars with spaces
474
- const normalized = name
475
- .replace(/[\s_\-]+/g, ' ')
476
- .replace(/[^a-zA-Z\s]/g, ' ')
477
- .trim();
478
-
479
- // Split by spaces first
480
- const spaceSegments = normalized.split(/\s+/).filter(s => s.length > 0);
481
- const allSegments: string[] = [];
482
-
483
- // Process each space-separated segment to detect camelCase and acronyms
484
- for (const segment of spaceSegments) {
485
- if (segment.length === 0) {
486
- continue;
487
- }
488
-
489
- // Track word boundaries: start of each word
490
- const wordStarts: number[] = [0]; // First character is always a word start
491
-
492
- for (let i = 1; i < segment.length; i++) {
493
- const char = segment[i];
494
- const prevChar = segment[i - 1];
495
- const nextChar = i < segment.length - 1 ? segment[i + 1] : '';
496
-
497
- const isUppercase = /[A-Z]/.test(char);
498
- const isLowercase = /[a-z]/.test(char);
499
- const prevIsUppercase = /[A-Z]/.test(prevChar);
500
- const prevIsLowercase = /[a-z]/.test(prevChar);
501
- const nextIsLowercase = /[a-z]/.test(nextChar);
502
- const nextIsUppercase = /[A-Z]/.test(nextChar);
503
-
504
- // camelCase boundary: lowercase followed by uppercase
505
- if (prevIsLowercase && isUppercase) {
506
- wordStarts.push(i);
507
- }
508
- // Uppercase followed by uppercase then lowercase: new word start
509
- // This handles:
510
- // - XMLParser -> XML (acronym ends at L, Parser starts at P) when previous is acronym (multiple uppercase)
511
- // - AVery -> A (single letter) + Very (starts at V) when previous is single uppercase
512
- // Pattern: ...X Y z... where X and Y are uppercase, z is lowercase
513
- else if (isUppercase && prevIsUppercase && nextIsLowercase) {
514
- // Check if previous characters form an acronym (multiple consecutive uppercase) or single uppercase
515
- let acronymStart = i - 1;
516
- while (acronymStart > 0 && /[A-Z]/.test(segment[acronymStart - 1])) {
517
- acronymStart--;
518
- }
519
- // If we have multiple uppercase before this (acronym) OR single uppercase, the new word starts at this uppercase character
520
- if (i - acronymStart >= 1) {
521
- wordStarts.push(i);
522
- }
523
- }
524
- // Acronym boundary: uppercase sequence followed by lowercase
525
- // This handles HTMLDocument -> HTML (acronym) + Document (new word starts at D, not at o)
526
- // Pattern: ...L D o... where L is last of acronym, D is start of new word, o is lowercase
527
- else if (prevIsUppercase && isLowercase) {
528
- // Check if previous characters form an acronym
529
- let acronymStart = i - 1;
530
- while (acronymStart > 0 && /[A-Z]/.test(segment[acronymStart - 1])) {
531
- acronymStart--;
532
- }
533
- // If we have multiple uppercase before this (acronym), the new word starts at the previous uppercase
534
- // But only if the previous uppercase wasn't already a word start
535
- if (i - acronymStart > 1) {
536
- // Check if the previous uppercase (i-1) is already a word start
537
- const prevIsWordStart = wordStarts.includes(i - 1);
538
- if (!prevIsWordStart) {
539
- // The new word starts at the previous uppercase character (i-1), not at this lowercase
540
- wordStarts.push(i - 1);
541
- }
542
- }
543
- }
544
- }
545
-
546
- // Extract words based on word starts
547
- for (let i = 0; i < wordStarts.length; i++) {
548
- const start = wordStarts[i];
549
- const end = i < wordStarts.length - 1 ? wordStarts[i + 1] : segment.length;
550
- const word = segment.substring(start, end);
551
- if (word.length > 0) {
552
- allSegments.push(word);
553
- }
554
- }
555
- }
556
-
557
- // Extract first letter of each segment, uppercase, and join
558
- return allSegments
559
- .filter(segment => segment.length > 0)
560
- .map(segment => {
561
- // Get first letter (handles cases where segment might start with non-letter)
562
- const firstLetter = segment.match(/[a-zA-Z]/)?.[0];
563
- return firstLetter ? firstLetter.toUpperCase() : '';
564
- })
565
- .join('');
566
- }
567
450
  }
package/src/index.ts CHANGED
@@ -17,6 +17,7 @@ export * from './_collections/utils/round-list.util';
17
17
  export * from './_collections/utils/object.util';
18
18
  export * from './_collections/utils/stack.util';
19
19
  export * from './_collections/utils/string.util';
20
+ export * from './_collections/utils/string-case.util';
20
21
  export * from './_collections/utils/time.util';
21
22
  export * from './_collections/utils/type-cloning-facility.util';
22
23
  export * from './_collections/utils/utilities.util';
Binary file