@futdevpro/fsm-dynamo 1.11.32 → 1.11.33

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