@futdevpro/fsm-dynamo 1.11.32 → 1.11.34

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.
Files changed (38) hide show
  1. package/.github/workflows/main.yml +21 -19
  2. package/build/_collections/utils/json-error-helper.util.d.ts +8 -0
  3. package/build/_collections/utils/json-error-helper.util.d.ts.map +1 -1
  4. package/build/_collections/utils/json-error-helper.util.js +118 -39
  5. package/build/_collections/utils/json-error-helper.util.js.map +1 -1
  6. package/build/_collections/utils/object.util.d.ts +6 -0
  7. package/build/_collections/utils/object.util.d.ts.map +1 -1
  8. package/build/_collections/utils/object.util.js +37 -1
  9. package/build/_collections/utils/object.util.js.map +1 -1
  10. package/build/_collections/utils/object.util.spec.js +393 -0
  11. package/build/_collections/utils/object.util.spec.js.map +1 -1
  12. package/build/_collections/utils/string.util.d.ts +119 -0
  13. package/build/_collections/utils/string.util.d.ts.map +1 -1
  14. package/build/_collections/utils/string.util.js +335 -0
  15. package/build/_collections/utils/string.util.js.map +1 -1
  16. package/build/_collections/utils/string.util.spec.js +694 -0
  17. package/build/_collections/utils/string.util.spec.js.map +1 -1
  18. package/build/_modules/crypto/_collections/crypto.util.d.ts.map +1 -1
  19. package/build/_modules/crypto/_collections/crypto.util.js +22 -9
  20. package/build/_modules/crypto/_collections/crypto.util.js.map +1 -1
  21. package/build/_modules/crypto/_collections/crypto.util.spec.js +3 -3
  22. package/build/_modules/crypto/_collections/crypto.util.spec.js.map +1 -1
  23. package/build/_modules/crypto/index.js +7 -6
  24. package/build/_modules/crypto/index.js.map +1 -1
  25. package/build/_modules/open-ai/index.js +7 -6
  26. package/build/_modules/open-ai/index.js.map +1 -1
  27. package/futdevpro-fsm-dynamo-01.11.34.tgz +0 -0
  28. package/package.json +1 -1
  29. package/src/_collections/utils/json-error-helper.util.ts +135 -45
  30. package/src/_collections/utils/object.util.spec.ts +503 -0
  31. package/src/_collections/utils/object.util.ts +40 -10
  32. package/src/_collections/utils/string.util.spec.ts +773 -1
  33. package/src/_collections/utils/string.util.ts +415 -1
  34. package/src/_modules/crypto/_collections/crypto.util.spec.ts +3 -3
  35. package/src/_modules/crypto/_collections/crypto.util.ts +19 -8
  36. package/src/_modules/crypto/index.ts +2 -2
  37. package/src/_modules/open-ai/index.ts +2 -2
  38. package/futdevpro-fsm-dynamo-01.11.32.tgz +0 -0
@@ -1,5 +1,13 @@
1
+ export interface DyFM_String_BracketSettings {
2
+ opening: string;
3
+ closing: string;
4
+ }
1
5
 
2
-
6
+ export interface DyFM_String_BracketDetailedResult {
7
+ content: string;
8
+ level: number;
9
+ brackets: DyFM_String_BracketSettings | null;
10
+ }
3
11
 
4
12
  /**
5
13
  * String utility class
@@ -14,4 +22,410 @@ export class DyFM_String {
14
22
  static breakTrim(value: string): string {
15
23
  return value.trim().replace(/(\r\n|\n|\r)/gm, '');
16
24
  }
25
+
26
+ /**
27
+ * Splits strings into an array of strings, extracting contents between brackets with support for nesting.
28
+ * Returns an array where the first element is the input string with bracket contents replaced,
29
+ * followed by all extracted bracket contents at each depth level.
30
+ *
31
+ * @param input - The input string to split
32
+ * @param openingBracket - The opening bracket to use
33
+ * @param closingBracket - The closing bracket to use
34
+ * @param maxDepth - The maximum depth to process nested brackets
35
+ * @param replaceContents - The string to replace bracket contents with in the main string
36
+ * @returns Array where first element is the modified string, followed by extracted contents
37
+ *
38
+ * @example
39
+ * const input = 'asdasd (and other things) around (and layered things (and more layers) around) around';
40
+ * const result = DyFM_String.nestedBracketSplit(input, '(', ')', 2, '...');
41
+ * console.log(result);
42
+ * // Output: [
43
+ * // 'asdasd ... around ... around',
44
+ * // 'and other things',
45
+ * // 'and layered things ... around',
46
+ * // 'and more layers'
47
+ * // ]
48
+ */
49
+ static nestedBracketSplit(
50
+ input: string,
51
+ openingBracket: string,
52
+ closingBracket: string,
53
+ maxDepth: number = 10,
54
+ replaceContents?: string,
55
+ ): string[] {
56
+ const detailed = this.nestedBracketSplitDetailed(input, openingBracket, closingBracket, maxDepth, replaceContents);
57
+ return detailed.map(item => item.content);
58
+ }
59
+
60
+ /**
61
+ * Detailed version of nestedBracketSplit that returns comprehensive information about each extracted bracket content.
62
+ * Returns an array where the first element contains the modified string info,
63
+ * followed by detailed information about each extracted bracket content.
64
+ *
65
+ * @param input - The input string to split
66
+ * @param openingBracket - The opening bracket to use
67
+ * @param closingBracket - The closing bracket to use
68
+ * @param maxDepth - The maximum depth to process nested brackets
69
+ * @param replaceContents - The string to replace bracket contents with in the main string
70
+ * @returns Array of objects with content, level, and bracket information
71
+ *
72
+ * @example
73
+ * const input = 'asdasd (and other things) around (and layered things (and more layers) around) around';
74
+ * const result = DyFM_String.nestedBracketSplitDetailed(input, '(', ')', 2, '...');
75
+ * console.log(result);
76
+ * // Output: [
77
+ * // { content: 'asdasd ... around ... around', level: 0, brackets: null },
78
+ * // { content: 'and other things', level: 1, brackets: { opening: '(', closing: ')' } },
79
+ * // { content: 'and layered things ... around', level: 1, brackets: { opening: '(', closing: ')' } },
80
+ * // { content: 'and more layers', level: 2, brackets: { opening: '(', closing: ')' } }
81
+ * // ]
82
+ */
83
+ static nestedBracketSplitDetailed(
84
+ input: string,
85
+ openingBracket: string,
86
+ closingBracket: string,
87
+ maxDepth: number = 10,
88
+ replaceContents?: string,
89
+ ): DyFM_String_BracketDetailedResult[] {
90
+ const brackets = [{ opening: openingBracket, closing: closingBracket }];
91
+ return this.multiBracketSplitDetailed(input, brackets, maxDepth, replaceContents);
92
+ }
93
+
94
+ /**
95
+ * Splits strings into an array of strings, extracting contents between multiple types of brackets with support for nesting.
96
+ * Returns an array where the first element is the input string with bracket contents replaced,
97
+ * followed by all extracted bracket contents at each depth level.
98
+ *
99
+ * @param input - The input string to split
100
+ * @param brackets - Array of bracket pairs with opening and closing strings
101
+ * @param maxDepth - The maximum depth to process nested brackets
102
+ * @param replaceContents - The string to replace bracket contents with in the main string
103
+ * @returns Array where first element is the modified string, followed by extracted contents
104
+ *
105
+ * @example
106
+ * const input = 'text (round) and [square] with {curly (nested)} brackets';
107
+ * const brackets = [
108
+ * { opening: '(', closing: ')' },
109
+ * { opening: '[', closing: ']' },
110
+ * { opening: '{', closing: '}' }
111
+ * ];
112
+ * const result = DyFM_String.multiBracketSplit(input, brackets, 2, '...');
113
+ * console.log(result);
114
+ * // Output: [
115
+ * // 'text ... and ... with ... brackets',
116
+ * // 'round',
117
+ * // 'square',
118
+ * // 'curly ... ',
119
+ * // 'nested'
120
+ * // ]
121
+ */
122
+ static multiBracketSplit(
123
+ input: string,
124
+ brackets: DyFM_String_BracketSettings[],
125
+ maxDepth: number = 10,
126
+ replaceContents?: string,
127
+ ): string[] {
128
+ const detailed = this.multiBracketSplitDetailed(input, brackets, maxDepth, replaceContents);
129
+ return detailed.map(item => item.content);
130
+ }
131
+
132
+ /**
133
+ * Splits strings and returns detailed information about extracted bracket contents.
134
+ * Returns an array where the first element contains the modified string info,
135
+ * followed by detailed information about each extracted bracket content.
136
+ *
137
+ * @param input - The input string to split
138
+ * @param brackets - Array of bracket pairs with opening and closing strings
139
+ * @param maxDepth - The maximum depth to process nested brackets
140
+ * @param replaceContents - The string to replace bracket contents with in the main string
141
+ * @returns Array of objects with content, level, and bracket information
142
+ *
143
+ * @example
144
+ * const input = 'text (round) and [square] with {curly (nested)} brackets';
145
+ * const brackets = [
146
+ * { opening: '(', closing: ')' },
147
+ * { opening: '[', closing: ']' },
148
+ * { opening: '{', closing: '}' }
149
+ * ];
150
+ * const result = DyFM_String.multiBracketSplitDetailed(input, brackets, 2, '...');
151
+ * console.log(result);
152
+ * // Output: [
153
+ * // { content: 'text ... and ... with ... brackets', level: 0, brackets: null },
154
+ * // { content: 'round', level: 1, brackets: { opening: '(', closing: ')' } },
155
+ * // { content: 'square', level: 1, brackets: { opening: '[', closing: ']' } },
156
+ * // { content: 'curly ...', level: 1, brackets: { opening: '{', closing: '}' } },
157
+ * // { content: 'nested', level: 2, brackets: { opening: '(', closing: ')' } }
158
+ * // ]
159
+ */
160
+ static multiBracketSplitDetailed(
161
+ input: string,
162
+ brackets: DyFM_String_BracketSettings[],
163
+ maxDepth: number = 10,
164
+ replaceContents?: string,
165
+ ): DyFM_String_BracketDetailedResult[] {
166
+ if (brackets.length === 0) {
167
+ return [{ content: input, level: 0, brackets: null }];
168
+ }
169
+
170
+ const result: DyFM_String_BracketDetailedResult[] = [];
171
+ let workingPieces: DyFM_String_BracketDetailedResult[] = [
172
+ { content: input, level: 0, brackets: null }
173
+ ];
174
+
175
+ for (let depth = 0; depth < maxDepth; depth++) {
176
+ const nextWorkingPieces: DyFM_String_BracketDetailedResult[] = [];
177
+ let foundAnyBrackets = false;
178
+
179
+ for (const piece of workingPieces) {
180
+ const extractedFromPiece = this.extractMultipleBracketsFromStringDetailed(
181
+ piece.content,
182
+ brackets,
183
+ replaceContents
184
+ );
185
+
186
+ if (extractedFromPiece.extracted.length > 0) {
187
+ foundAnyBrackets = true;
188
+
189
+ // On first depth, store the modified string
190
+ if (depth === 0) {
191
+ result.push({
192
+ content: extractedFromPiece.modified,
193
+ level: 0,
194
+ brackets: null
195
+ });
196
+ } else {
197
+ // On subsequent depths, update the piece in result
198
+ const pieceIndex = result.findIndex(r => r.content === piece.content && r.brackets === piece.brackets);
199
+ if (pieceIndex !== -1) {
200
+ result[pieceIndex].content = extractedFromPiece.modified;
201
+ }
202
+ }
203
+
204
+ // Add extracted contents to result with their bracket information
205
+ for (const extracted of extractedFromPiece.extracted) {
206
+ result.push({
207
+ content: extracted.content,
208
+ level: depth + 1,
209
+ brackets: extracted.brackets
210
+ });
211
+ }
212
+
213
+ // Add extracted contents for next iteration
214
+ for (const extracted of extractedFromPiece.extracted) {
215
+ nextWorkingPieces.push({
216
+ content: extracted.content,
217
+ level: depth + 1,
218
+ brackets: extracted.brackets ?? null
219
+ });
220
+ }
221
+ } else if (depth === 0) {
222
+ // No brackets found in input
223
+ result.push({
224
+ content: piece.content,
225
+ level: 0,
226
+ brackets: null
227
+ });
228
+ }
229
+ }
230
+
231
+ if (!foundAnyBrackets) {
232
+ break;
233
+ }
234
+
235
+ workingPieces = nextWorkingPieces;
236
+ }
237
+
238
+ return result;
239
+ }
240
+
241
+ /**
242
+ * Helper method to extract multiple bracket types from a single string with detailed information
243
+ */
244
+ private static extractMultipleBracketsFromStringDetailed(
245
+ input: string,
246
+ brackets: DyFM_String_BracketSettings[],
247
+ replaceContents?: string
248
+ ): {
249
+ modified: string;
250
+ extracted: DyFM_String_BracketDetailedResult[]
251
+ } {
252
+ const extracted: DyFM_String_BracketDetailedResult[] = [];
253
+
254
+ // Unified scanning logic. If replaceContents is undefined, we preserve input in modified.
255
+ const preserveOriginal = replaceContents === undefined;
256
+ let modified = preserveOriginal ? input : '';
257
+
258
+ // We'll scan once with a combined stack to correctly respect top-level across all types.
259
+ type StackItem = {
260
+ id: number;
261
+ openingIndex: number;
262
+ // We store the originally assumed opening type index (may be reinterpreted on close if multiple closers share same opening)
263
+ type: number;
264
+ bracket: DyFM_String_BracketSettings;
265
+ };
266
+
267
+ let i = 0;
268
+ let idSeq = 1;
269
+ const stack: StackItem[] = [];
270
+
271
+ // Track current top-level segment start and the id of the bottom-of-stack item for the current top-level bracket
272
+ let topLevelStart = 0;
273
+ let activeTopId: number | null = null;
274
+
275
+ const appendTextUntil = (endIndex: number) => {
276
+ if (!preserveOriginal) {
277
+ modified += input.substring(topLevelStart, endIndex);
278
+ }
279
+ };
280
+
281
+ const closeTopLevel = (closeIndex: number, closedItem: StackItem, closingBracket: { opening: string; closing: string }, closeTokenLength: number) => {
282
+ // Extract content between the top-level opening and this close position
283
+ const content = input.substring(topLevelStart, closeIndex);
284
+ extracted.push({ content, level: 0, brackets: closingBracket });
285
+
286
+ if (!preserveOriginal) {
287
+ // Add replacement in place of the removed top-level content
288
+ modified += (replaceContents ?? '');
289
+ }
290
+
291
+ // Advance topLevelStart after the closing token
292
+ topLevelStart = closeIndex + closeTokenLength;
293
+
294
+ // Clear entire stack; any inner unclosed brackets are discarded as unmatched
295
+ stack.length = 0;
296
+ activeTopId = null;
297
+ };
298
+
299
+ while (i < input.length) {
300
+ let found = false;
301
+ let isOpening = false;
302
+ let matchLen = 0;
303
+ let matchType = -1;
304
+ // Track whether we've already selected an opening for a same-character bracket at this position
305
+ let selectedSameCharOpenType = -1;
306
+
307
+ // Determine the longest match among all brackets at this position for opening or closing.
308
+ for (let j = 0; j < brackets.length; j++) {
309
+ const b = brackets[j];
310
+
311
+ // Opening check
312
+ if (input.substring(i, i + b.opening.length) === b.opening) {
313
+ const len = b.opening.length;
314
+ // Decide opening vs closing when opening === closing (same character) using a heuristic
315
+ if (b.opening === b.closing) {
316
+ // Decide using immediate previous character: open at start/after whitespace or after an opening punctuation
317
+ const prevCharImmediate = i > 0 ? input[i - 1] : null;
318
+ const isOpenContext = (prevCharImmediate === null) || /[\s\(\[\{<]/.test(prevCharImmediate);
319
+ // If we can't possibly close (no such type open), we must open
320
+ const canCloseSameType = stack.some(s => s.bracket.opening === b.opening && s.bracket.closing === b.closing);
321
+
322
+ // Decide if we treat this token as opening
323
+ const treatAsOpening = isOpenContext || !canCloseSameType;
324
+ if (treatAsOpening && len > matchLen) {
325
+ found = true;
326
+ isOpening = true;
327
+ matchLen = len;
328
+ matchType = j;
329
+ selectedSameCharOpenType = j;
330
+ }
331
+ } else {
332
+ if (len > matchLen) {
333
+ found = true;
334
+ isOpening = true;
335
+ matchLen = len;
336
+ matchType = j;
337
+ }
338
+ }
339
+ }
340
+
341
+ // Closing check
342
+ if (input.substring(i, i + b.closing.length) === b.closing) {
343
+ const len = b.closing.length;
344
+ // For same-character brackets, prefer closing if there is an open of this type and we didn't already pick a longer opening
345
+ if (b.opening === b.closing) {
346
+ const canCloseSameType = stack.some(s => s.bracket.opening === b.opening && s.bracket.closing === b.closing);
347
+ // For same-character, allow closing to override only if it's strictly longer OR no same-char opening was chosen at this pos
348
+ if (canCloseSameType && (len > matchLen || (len === matchLen && selectedSameCharOpenType === -1))) {
349
+ found = true;
350
+ isOpening = false;
351
+ matchLen = len;
352
+ matchType = j;
353
+ }
354
+ } else {
355
+ // For normal brackets, only consider as closing if there's a matching opener in the stack (by type)
356
+ const hasExactType = stack.some(s => s.type === j);
357
+ const hasSameOpening = !hasExactType && stack.some(s => s.bracket.opening === b.opening);
358
+ if ((hasExactType || hasSameOpening) && len > matchLen) {
359
+ found = true;
360
+ isOpening = false;
361
+ matchLen = len;
362
+ matchType = j;
363
+ }
364
+ }
365
+ }
366
+ }
367
+
368
+ if (!found) {
369
+ i++;
370
+ continue;
371
+ }
372
+
373
+ const matchedBracket = brackets[matchType];
374
+
375
+ if (isOpening) {
376
+ // Start of a bracket segment
377
+ if (stack.length === 0) {
378
+ // Before the first top-level opening, append preceding text when replacing
379
+ appendTextUntil(i);
380
+ // Set the start of content (after opening token)
381
+ topLevelStart = i + matchLen;
382
+ activeTopId = idSeq;
383
+ }
384
+
385
+ stack.push({ id: idSeq++, openingIndex: i, type: matchType, bracket: matchedBracket });
386
+ i += matchLen;
387
+ } else {
388
+ // Determine which opener this closer should match.
389
+ // First, try to find an opener of the exact type (from the top of the stack downward)
390
+ let idx = -1;
391
+ for (let k = stack.length - 1; k >= 0; k--) {
392
+ if (stack[k].type === matchType) { idx = k; break; }
393
+ }
394
+ // If not found, allow matching by same opening string (handles cases like <...> and </>)
395
+ if (idx === -1) {
396
+ for (let k = stack.length - 1; k >= 0; k--) {
397
+ if (stack[k].bracket.opening === matchedBracket.opening) { idx = k; break; }
398
+ }
399
+ }
400
+
401
+ if (idx === -1) {
402
+ // No matching opener; treat as literal
403
+ i += matchLen;
404
+ continue;
405
+ }
406
+
407
+ const removed = stack.splice(idx, 1)[0];
408
+
409
+ // If we closed the bottom-most (top-level) opener, emit extraction and replacement
410
+ if (activeTopId !== null && removed.id === activeTopId) {
411
+ closeTopLevel(i, removed, matchedBracket, matchLen);
412
+ }
413
+
414
+ i += matchLen;
415
+ }
416
+ }
417
+
418
+ // Append any trailing text when replacing
419
+ if (!preserveOriginal && topLevelStart < input.length) {
420
+ modified += input.substring(topLevelStart);
421
+ }
422
+
423
+ // If preserving, modified must be original input
424
+ if (preserveOriginal) {
425
+ return { modified: input, extracted };
426
+ }
427
+
428
+ return { modified, extracted };
429
+ }
430
+
17
431
  }
@@ -466,9 +466,9 @@ describe('| DyFM_Crypto', () => {
466
466
  const encrypted = DyFM_Crypto.encrypt(doubleStringified, testKey);
467
467
  const decrypted = DyFM_Crypto.decrypt(encrypted, testKey);
468
468
 
469
- // Should be able to parse the decrypted data back to the original object
470
- const parsed = JSON.parse(decrypted as string);
471
- expect(parsed).toEqual(testData);
469
+ // The crypto utility should automatically handle double-stringified JSON
470
+ // and return the original object directly
471
+ expect(decrypted).toEqual(testData);
472
472
  });
473
473
 
474
474
  it('| should handle character-by-character JSON data correctly', () => {
@@ -113,6 +113,7 @@ export class DyFM_Crypto {
113
113
  */
114
114
  private static safeSerialize<T>(data: T): string {
115
115
  try {
116
+ // Always use JSON.stringify to ensure proper serialization/deserialization
116
117
  return JSON.stringify(data);
117
118
  } catch (error) {
118
119
  throw new DyFM_Error({
@@ -128,22 +129,32 @@ export class DyFM_Crypto {
128
129
  */
129
130
  private static safeDeserialize<T>(data: string): T {
130
131
  try {
131
- let parsed = JSON.parse(data);
132
+ //let parsed = JSON.parse(data);
133
+ let parsed = DyFM_Object.failableSafeParseJSON(data);
132
134
 
133
- // Handle double-stringified JSON
134
- if (typeof parsed === 'string') {
135
+ // Handle double-stringified JSON (or more levels of stringification)
136
+ let maxAttempts = 3; // Prevent infinite loops
137
+ while (typeof parsed === 'string' && maxAttempts > 0) {
135
138
  try {
136
- parsed = JSON.parse(parsed);
139
+ //const nextParsed = JSON.parse(parsed);
140
+ const nextParsed = DyFM_Object.failableSafeParseJSON(parsed);
141
+ // Only continue if parsing actually changed the result
142
+ if (nextParsed !== parsed) {
143
+ parsed = nextParsed;
144
+ maxAttempts--;
145
+ } else {
146
+ break;
147
+ }
137
148
  } catch {
138
- // If second parse fails, return the string as is
139
- return parsed as T;
149
+ // If parse fails, return current state
150
+ break;
140
151
  }
141
152
  }
142
153
 
143
154
  // Handle primitive values
144
- if (typeof parsed === 'string' || typeof parsed === 'number' || typeof parsed === 'boolean') {
155
+ /* if (typeof parsed === 'string' || typeof parsed === 'number' || typeof parsed === 'boolean') {
145
156
  return parsed as T;
146
- }
157
+ } */
147
158
 
148
159
  return parsed as T;
149
160
  } catch (error) {
@@ -1,13 +1,13 @@
1
1
 
2
2
 
3
3
 
4
- try {
4
+ /* try {
5
5
  require.resolve('crypto-js');
6
6
  } catch {
7
7
  console.log(
8
8
  '❌ crypto-js package not found, please install it with: pnpm add crypto-js'
9
9
  );
10
- }
10
+ } */
11
11
 
12
12
 
13
13
  export * from './_collections/crypto.util';
@@ -1,13 +1,13 @@
1
1
 
2
2
 
3
3
 
4
- try {
4
+ /* try {
5
5
  require.resolve('open-ai');
6
6
  } catch {
7
7
  console.log(
8
8
  '❌ OpenAI package not found, please install it with: pnpm add open-ai'
9
9
  );
10
- }
10
+ } */
11
11
 
12
12
 
13
13
  // ENUMS
Binary file