@futdevpro/fsm-dynamo 1.11.31 → 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.
- package/.github/workflows/main.yml +20 -21
- package/build/_collections/utils/json-error-helper.util.d.ts +8 -0
- package/build/_collections/utils/json-error-helper.util.d.ts.map +1 -1
- package/build/_collections/utils/json-error-helper.util.js +118 -39
- package/build/_collections/utils/json-error-helper.util.js.map +1 -1
- package/build/_collections/utils/object.util.d.ts +6 -0
- package/build/_collections/utils/object.util.d.ts.map +1 -1
- package/build/_collections/utils/object.util.js +37 -1
- package/build/_collections/utils/object.util.js.map +1 -1
- package/build/_collections/utils/object.util.spec.js +393 -0
- package/build/_collections/utils/object.util.spec.js.map +1 -1
- package/build/_collections/utils/string.util.d.ts +130 -0
- package/build/_collections/utils/string.util.d.ts.map +1 -1
- package/build/_collections/utils/string.util.js +354 -0
- package/build/_collections/utils/string.util.js.map +1 -1
- package/build/_collections/utils/string.util.spec.js +694 -0
- package/build/_collections/utils/string.util.spec.js.map +1 -1
- package/build/_modules/crypto/_collections/crypto-2-non-stable.util.d.ts.map +1 -1
- package/build/_modules/crypto/_collections/crypto-2-non-stable.util.js +3 -2
- package/build/_modules/crypto/_collections/crypto-2-non-stable.util.js.map +1 -1
- package/build/_modules/crypto/_collections/crypto.util.d.ts.map +1 -1
- package/build/_modules/crypto/_collections/crypto.util.js +22 -9
- package/build/_modules/crypto/_collections/crypto.util.js.map +1 -1
- package/build/_modules/crypto/_collections/crypto.util.spec.js +3 -3
- package/build/_modules/crypto/_collections/crypto.util.spec.js.map +1 -1
- package/build/_modules/crypto/index.js +7 -6
- package/build/_modules/crypto/index.js.map +1 -1
- package/build/_modules/open-ai/index.js +7 -6
- package/build/_modules/open-ai/index.js.map +1 -1
- package/futdevpro-fsm-dynamo-01.11.33.tgz +0 -0
- package/package.json +1 -1
- package/src/_collections/utils/json-error-helper.util.ts +135 -45
- package/src/_collections/utils/object.util.spec.ts +503 -0
- package/src/_collections/utils/object.util.ts +40 -10
- package/src/_collections/utils/string.util.spec.ts +773 -1
- package/src/_collections/utils/string.util.ts +426 -0
- package/src/_modules/crypto/_collections/crypto-2-non-stable.util.ts +3 -2
- package/src/_modules/crypto/_collections/crypto.util.spec.ts +3 -3
- package/src/_modules/crypto/_collections/crypto.util.ts +20 -8
- package/src/_modules/crypto/index.ts +2 -2
- package/src/_modules/open-ai/index.ts +2 -2
- package/futdevpro-fsm-dynamo-01.11.31.tgz +0 -0
|
@@ -140,4 +140,507 @@ describe('| DyFM_Object', () => {
|
|
|
140
140
|
});
|
|
141
141
|
});
|
|
142
142
|
|
|
143
|
+
describe('| failableSafeParseJSON', () => {
|
|
144
|
+
|
|
145
|
+
describe('| Basic JSON parsing', () => {
|
|
146
|
+
it('| should parse valid JSON object', () => {
|
|
147
|
+
const input = '{"name": "test", "value": 123}';
|
|
148
|
+
const expected = { name: 'test', value: 123 };
|
|
149
|
+
|
|
150
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
151
|
+
|
|
152
|
+
expect(result).toEqual(expected);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('| should parse valid JSON array', () => {
|
|
156
|
+
const input = '[1, 2, 3, "test"]';
|
|
157
|
+
const expected = [1, 2, 3, 'test'];
|
|
158
|
+
|
|
159
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
160
|
+
|
|
161
|
+
expect(result).toEqual(expected);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('| should parse valid JSON string', () => {
|
|
165
|
+
const input = '"simple string"';
|
|
166
|
+
const expected = 'simple string';
|
|
167
|
+
|
|
168
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
169
|
+
|
|
170
|
+
expect(result).toBe(expected);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('| should parse valid JSON number', () => {
|
|
174
|
+
const input = '42.5';
|
|
175
|
+
const expected = 42.5;
|
|
176
|
+
|
|
177
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
178
|
+
|
|
179
|
+
expect(result).toBe(expected);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('| should parse valid JSON boolean', () => {
|
|
183
|
+
const input = 'true';
|
|
184
|
+
const expected = true;
|
|
185
|
+
|
|
186
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
187
|
+
|
|
188
|
+
expect(result).toBe(expected);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('| should parse valid JSON null', () => {
|
|
192
|
+
const input = 'null';
|
|
193
|
+
const expected = null;
|
|
194
|
+
|
|
195
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
196
|
+
|
|
197
|
+
expect(result).toBe(expected);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe('| JSON extraction from code blocks', () => {
|
|
202
|
+
it('| should extract JSON from ```json code block', () => {
|
|
203
|
+
const input = '```json\n{"name": "test", "value": 123}\n```';
|
|
204
|
+
const expected = { name: 'test', value: 123 };
|
|
205
|
+
|
|
206
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
207
|
+
|
|
208
|
+
expect(result).toEqual(expected);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('| should extract JSON from ```json code block with extra text', () => {
|
|
212
|
+
const input = 'Here is the JSON:\n```json\n{"name": "test", "value": 123}\n```\nEnd of JSON';
|
|
213
|
+
const expected = { name: 'test', value: 123 };
|
|
214
|
+
|
|
215
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
216
|
+
|
|
217
|
+
expect(result).toEqual(expected);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('| should extract complex JSON from ```json code block', () => {
|
|
221
|
+
const input = '```json\n{\n "users": [\n {"id": 1, "name": "John"},\n {"id": 2, "name": "Jane"}\n ],\n "total": 2\n}\n```';
|
|
222
|
+
const expected = {
|
|
223
|
+
users: [
|
|
224
|
+
{ id: 1, name: 'John' },
|
|
225
|
+
{ id: 2, name: 'Jane' }
|
|
226
|
+
],
|
|
227
|
+
total: 2
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
231
|
+
|
|
232
|
+
expect(result).toEqual(expected);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('| should handle JSON with newlines in ```json code block', () => {
|
|
236
|
+
const input = '```json\n{\n "message": "Line 1\\nLine 2\\nLine 3",\n "type": "multiline"\n}\n```';
|
|
237
|
+
const expected = {
|
|
238
|
+
message: 'Line 1\nLine 2\nLine 3',
|
|
239
|
+
type: 'multiline'
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
243
|
+
|
|
244
|
+
expect(result).toEqual(expected);
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
describe('| JSON extraction without code blocks', () => {
|
|
249
|
+
it('| should parse JSON when no code block markers present', () => {
|
|
250
|
+
const input = '{"name": "test", "value": 123}';
|
|
251
|
+
const expected = { name: 'test', value: 123 };
|
|
252
|
+
|
|
253
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
254
|
+
|
|
255
|
+
expect(result).toEqual(expected);
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
describe('| Complex and stringified content', () => {
|
|
260
|
+
it('| should parse JSON containing escaped quotes', () => {
|
|
261
|
+
const input = '{"message": "He said \\"Hello\\" to her"}';
|
|
262
|
+
const expected = { message: 'He said "Hello" to her' };
|
|
263
|
+
|
|
264
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
265
|
+
|
|
266
|
+
expect(result).toEqual(expected);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it('| should parse JSON containing stringified JSON', () => {
|
|
270
|
+
const input = '{"data": "{\\"nested\\": \\"value\\", \\"number\\": 42}"}';
|
|
271
|
+
const expected = { data: '{"nested": "value", "number": 42}' };
|
|
272
|
+
|
|
273
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
274
|
+
|
|
275
|
+
expect(result).toEqual(expected);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('| should parse JSON with complex nested structures', () => {
|
|
279
|
+
const input = '{"config": {"database": {"host": "localhost", "port": 5432}, "cache": {"ttl": 3600}}, "features": ["auth", "cache"]}';
|
|
280
|
+
const expected = {
|
|
281
|
+
config: {
|
|
282
|
+
database: { host: 'localhost', port: 5432 },
|
|
283
|
+
cache: { ttl: 3600 }
|
|
284
|
+
},
|
|
285
|
+
features: ['auth', 'cache']
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
289
|
+
|
|
290
|
+
expect(result).toEqual(expected);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('| should parse JSON with special characters and unicode', () => {
|
|
294
|
+
const input = '{"unicode": "🚀 Test ñáéíóú", "special": "Line1\\nLine2\\tTabbed"}';
|
|
295
|
+
const expected = {
|
|
296
|
+
unicode: '🚀 Test ñáéíóú',
|
|
297
|
+
special: 'Line1\nLine2\tTabbed'
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
301
|
+
|
|
302
|
+
expect(result).toEqual(expected);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('| should parse JSON with very long strings', () => {
|
|
306
|
+
const longString = 'a'.repeat(1000);
|
|
307
|
+
const input = `{"longString": "${longString}", "length": ${longString.length}}`;
|
|
308
|
+
const expected = { longString: longString, length: 1000 };
|
|
309
|
+
|
|
310
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
311
|
+
|
|
312
|
+
expect(result).toEqual(expected);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('| should parse JSON with deep nesting', () => {
|
|
316
|
+
const input = '{"level1": {"level2": {"level3": {"level4": {"level5": "deep value"}}}}}';
|
|
317
|
+
const expected = {
|
|
318
|
+
level1: {
|
|
319
|
+
level2: {
|
|
320
|
+
level3: {
|
|
321
|
+
level4: {
|
|
322
|
+
level5: 'deep value'
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
330
|
+
|
|
331
|
+
expect(result).toEqual(expected);
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
describe('| Error handling and validation', () => {
|
|
336
|
+
it('| should throw error for null input', () => {
|
|
337
|
+
expect(() => {
|
|
338
|
+
DyFM_Object.failableSafeParseJSON(null as any);
|
|
339
|
+
}).toThrow();
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it('| should throw error for undefined input', () => {
|
|
343
|
+
expect(() => {
|
|
344
|
+
DyFM_Object.failableSafeParseJSON(undefined as any);
|
|
345
|
+
}).toThrow();
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it('| should throw error for empty string', () => {
|
|
349
|
+
expect(() => {
|
|
350
|
+
DyFM_Object.failableSafeParseJSON('');
|
|
351
|
+
}).toThrow();
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it('| should throw enhanced error for malformed JSON with line context', () => {
|
|
355
|
+
const input = '{\n "name": "test",\n "value": 123,\n "invalid": \n}';
|
|
356
|
+
|
|
357
|
+
try {
|
|
358
|
+
DyFM_Object.failableSafeParseJSON(input);
|
|
359
|
+
fail('Expected error to be thrown');
|
|
360
|
+
} catch (error) {
|
|
361
|
+
expect(error).toBeInstanceOf(Error);
|
|
362
|
+
const errorMessage = (error as Error).message;
|
|
363
|
+
expect(errorMessage).toContain('JSON parsing failed');
|
|
364
|
+
// The error should mention the parsing issue
|
|
365
|
+
expect(errorMessage.length).toBeGreaterThan(10);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it('| should throw enhanced error for missing comma with detailed context', () => {
|
|
370
|
+
const input = '{\n "name": "test",\n "value": 123\n "missing_comma": true\n}';
|
|
371
|
+
|
|
372
|
+
try {
|
|
373
|
+
DyFM_Object.failableSafeParseJSON(input);
|
|
374
|
+
fail('Expected error to be thrown');
|
|
375
|
+
} catch (error) {
|
|
376
|
+
expect(error).toBeInstanceOf(Error);
|
|
377
|
+
const errorMessage = (error as Error).message;
|
|
378
|
+
expect(errorMessage).toContain('JSON parsing failed');
|
|
379
|
+
|
|
380
|
+
// Should show enhanced error format with line info when position is available
|
|
381
|
+
if (errorMessage.includes('at position')) {
|
|
382
|
+
expect(errorMessage).toContain('line');
|
|
383
|
+
expect(errorMessage).toContain('Error at line');
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
it('| should throw enhanced error for unterminated string with context', () => {
|
|
389
|
+
const input = '{\n "name": "unterminated string\n "value": 123\n}';
|
|
390
|
+
|
|
391
|
+
try {
|
|
392
|
+
DyFM_Object.failableSafeParseJSON(input);
|
|
393
|
+
fail('Expected error to be thrown');
|
|
394
|
+
} catch (error) {
|
|
395
|
+
expect(error).toBeInstanceOf(Error);
|
|
396
|
+
const errorMessage = (error as Error).message;
|
|
397
|
+
expect(errorMessage).toContain('JSON parsing failed');
|
|
398
|
+
expect(errorMessage.length).toBeGreaterThan(10);
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
it('| should throw enhanced error for unexpected end of input', () => {
|
|
403
|
+
const input = '{\n "name": "test",\n "value": 123';
|
|
404
|
+
|
|
405
|
+
try {
|
|
406
|
+
DyFM_Object.failableSafeParseJSON(input);
|
|
407
|
+
fail('Expected error to be thrown');
|
|
408
|
+
} catch (error) {
|
|
409
|
+
expect(error).toBeInstanceOf(Error);
|
|
410
|
+
const errorMessage = (error as Error).message;
|
|
411
|
+
expect(errorMessage).toContain('JSON parsing failed');
|
|
412
|
+
expect(errorMessage.length).toBeGreaterThan(10);
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it('| should provide enhanced error context for code block JSON', () => {
|
|
417
|
+
const input = '```json\n{\n "valid": true,\n "invalid": [\n 1,\n 2,\n 3\n 4\n ]\n}\n```';
|
|
418
|
+
|
|
419
|
+
try {
|
|
420
|
+
DyFM_Object.failableSafeParseJSON(input);
|
|
421
|
+
fail('Expected error to be thrown');
|
|
422
|
+
} catch (error) {
|
|
423
|
+
const errorMessage = (error as Error).message;
|
|
424
|
+
|
|
425
|
+
// Should contain JSON parsing error info
|
|
426
|
+
expect(errorMessage).toContain('JSON parsing failed');
|
|
427
|
+
|
|
428
|
+
// If enhanced error reporting works, should show context
|
|
429
|
+
if (errorMessage.includes('Error at line')) {
|
|
430
|
+
expect(errorMessage).toContain('>>>'); // Error line marker
|
|
431
|
+
expect(errorMessage).toMatch(/\d+:/); // Line numbers
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
it('| should show context lines when error position is available', () => {
|
|
437
|
+
// This JSON has a clear syntax error that should trigger enhanced reporting
|
|
438
|
+
const input = '```json\n{\n "name": "test",\n "value": 123,\n "missing_comma": true\n "error": "here"\n}\n```';
|
|
439
|
+
|
|
440
|
+
try {
|
|
441
|
+
DyFM_Object.failableSafeParseJSON(input);
|
|
442
|
+
fail('Expected error to be thrown');
|
|
443
|
+
} catch (error) {
|
|
444
|
+
const errorMessage = (error as Error).message;
|
|
445
|
+
|
|
446
|
+
expect(errorMessage).toContain('JSON parsing failed');
|
|
447
|
+
|
|
448
|
+
// Should show enhanced error format when position info is available
|
|
449
|
+
if (errorMessage.includes('at position')) {
|
|
450
|
+
// Should show line context
|
|
451
|
+
expect(errorMessage).toContain('Error at line');
|
|
452
|
+
expect(errorMessage).toContain('>>>'); // Error line marker
|
|
453
|
+
|
|
454
|
+
// Should show multiple context lines
|
|
455
|
+
const contextLines = errorMessage.split('\n').filter(line =>
|
|
456
|
+
line.match(/^\s*(\d+|>>>)/)
|
|
457
|
+
);
|
|
458
|
+
expect(contextLines.length).toBeGreaterThan(0);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
describe('| Edge cases and robustness', () => {
|
|
465
|
+
it('| should handle JSON with only whitespace', () => {
|
|
466
|
+
expect(() => {
|
|
467
|
+
DyFM_Object.failableSafeParseJSON(' \n\t ');
|
|
468
|
+
}).toThrow();
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
it('| should handle very large JSON objects', () => {
|
|
472
|
+
const largeObj: any = {};
|
|
473
|
+
for (let i = 0; i < 100; i++) {
|
|
474
|
+
largeObj[`key${i}`] = `value${i}`;
|
|
475
|
+
}
|
|
476
|
+
const input = JSON.stringify(largeObj);
|
|
477
|
+
|
|
478
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
479
|
+
|
|
480
|
+
expect(result).toEqual(largeObj);
|
|
481
|
+
expect(Object.keys(result)).toHaveSize(100);
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
it('| should handle JSON with mixed types in arrays', () => {
|
|
485
|
+
const input = '[1, "string", true, null, {"nested": "object"}, [1, 2, 3]]';
|
|
486
|
+
const expected = [1, 'string', true, null, { nested: 'object' }, [1, 2, 3]];
|
|
487
|
+
|
|
488
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
489
|
+
|
|
490
|
+
expect(result).toEqual(expected);
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
it('| should handle JSON with scientific notation', () => {
|
|
494
|
+
const input = '{"small": 1e-10, "large": 1e10, "normal": 123.45}';
|
|
495
|
+
const expected = { small: 1e-10, large: 1e10, normal: 123.45 };
|
|
496
|
+
|
|
497
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
498
|
+
|
|
499
|
+
expect(result).toEqual(expected);
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
it('| should handle JSON with negative numbers', () => {
|
|
503
|
+
const input = '{"negative": -42, "positiveZero": 0, "negativeZero": -0}';
|
|
504
|
+
const expected = { negative: -42, positiveZero: 0, negativeZero: -0 };
|
|
505
|
+
|
|
506
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
507
|
+
|
|
508
|
+
expect(result).toEqual(expected);
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
it('| should preserve object key order', () => {
|
|
512
|
+
const input = '{"z": 1, "a": 2, "m": 3}';
|
|
513
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
514
|
+
|
|
515
|
+
const keys = Object.keys(result);
|
|
516
|
+
expect(keys).toEqual(['z', 'a', 'm']);
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it('| should handle empty objects and arrays', () => {
|
|
520
|
+
const input = '{"empty_obj": {}, "empty_arr": [], "null_val": null}';
|
|
521
|
+
const expected = { empty_obj: {}, empty_arr: [], null_val: null };
|
|
522
|
+
|
|
523
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
524
|
+
|
|
525
|
+
expect(result).toEqual(expected);
|
|
526
|
+
});
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
describe('| Real-world scenarios', () => {
|
|
530
|
+
it('| should parse API response format', () => {
|
|
531
|
+
const input = '{"status": "success", "data": {"users": [{"id": 1, "name": "John"}]}, "pagination": {"page": 1, "total": 100}}';
|
|
532
|
+
const expected = {
|
|
533
|
+
status: 'success',
|
|
534
|
+
data: { users: [{ id: 1, name: 'John' }] },
|
|
535
|
+
pagination: { page: 1, total: 100 }
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
539
|
+
|
|
540
|
+
expect(result).toEqual(expected);
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
it('| should parse configuration format', () => {
|
|
544
|
+
const input = '{"database": {"host": "localhost", "port": 5432, "ssl": true}, "logging": {"level": "info", "file": "/var/log/app.log"}}';
|
|
545
|
+
const expected = {
|
|
546
|
+
database: { host: 'localhost', port: 5432, ssl: true },
|
|
547
|
+
logging: { level: 'info', file: '/var/log/app.log' }
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
551
|
+
|
|
552
|
+
expect(result).toEqual(expected);
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
it('| should parse JSON from AI model response with markdown', () => {
|
|
556
|
+
const input = 'Here is the requested JSON:\n\n```json\n{\n "response": "success",\n "data": {\n "generated": true,\n "model": "gpt-4"\n }\n}\n```\n\nThis should work correctly.';
|
|
557
|
+
const expected = {
|
|
558
|
+
response: 'success',
|
|
559
|
+
data: {
|
|
560
|
+
generated: true,
|
|
561
|
+
model: 'gpt-4'
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
566
|
+
|
|
567
|
+
expect(result).toEqual(expected);
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
it('| should parse JSON with sub-inserted JSON', () => {
|
|
571
|
+
const sub = JSON.stringify({
|
|
572
|
+
name: "sub",
|
|
573
|
+
value: 456
|
|
574
|
+
});
|
|
575
|
+
const input = JSON.stringify({
|
|
576
|
+
name: "test",
|
|
577
|
+
value: 123,
|
|
578
|
+
sub: sub
|
|
579
|
+
});
|
|
580
|
+
const expected = {
|
|
581
|
+
name: 'test',
|
|
582
|
+
value: 123,
|
|
583
|
+
sub: sub
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
587
|
+
|
|
588
|
+
expect(result).toEqual(expected);
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
it('| should parse JSON with sub-inserted JSON in a code block', () => {
|
|
592
|
+
const input = 'things around ```json\n' + JSON.stringify({
|
|
593
|
+
name: "test",
|
|
594
|
+
value: 123,
|
|
595
|
+
sub: '```json\n{"name": "sub", "value": 456}\n```'
|
|
596
|
+
}) + '\n``` things around';
|
|
597
|
+
const expected = {
|
|
598
|
+
name: 'test',
|
|
599
|
+
value: 123,
|
|
600
|
+
sub: '```json\n{"name": "sub", "value": 456}\n```'
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
604
|
+
|
|
605
|
+
expect(result).toEqual(expected);
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
it('| should parse JSON with sub-inserted JSON in a code block with only ```json within', () => {
|
|
609
|
+
const input = JSON.stringify({
|
|
610
|
+
name: "test",
|
|
611
|
+
value: 123,
|
|
612
|
+
sub: "```json\n{\"name\": \"sub\", \"value\": 456}\n```"
|
|
613
|
+
});
|
|
614
|
+
const expected = {
|
|
615
|
+
name: 'test',
|
|
616
|
+
value: 123,
|
|
617
|
+
sub: "```json\n{\"name\": \"sub\", \"value\": 456}\n```"
|
|
618
|
+
};
|
|
619
|
+
|
|
620
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
621
|
+
|
|
622
|
+
expect(result).toEqual(expected);
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
it('| should parse JSON with sub-inserted commas', () => {
|
|
626
|
+
const input = JSON.stringify({
|
|
627
|
+
name: "test",
|
|
628
|
+
value: 123,
|
|
629
|
+
sub: 'content wit \"commas\" and "commas"'
|
|
630
|
+
});
|
|
631
|
+
const expected = {
|
|
632
|
+
name: 'test',
|
|
633
|
+
value: 123,
|
|
634
|
+
sub: 'content wit \"commas\" and "commas"'
|
|
635
|
+
};
|
|
636
|
+
const result = DyFM_Object.failableSafeParseJSON(input);
|
|
637
|
+
expect(result).toEqual(expected);
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
});
|
|
644
|
+
});
|
|
645
|
+
|
|
143
646
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
import { DyFM_Log } from './log.util';
|
|
3
3
|
import { DyFM_JsonErrorHelper } from './json-error-helper.util';
|
|
4
|
+
import { DyFM_String } from './string.util';
|
|
4
5
|
|
|
5
6
|
export class DyFM_Object {
|
|
6
7
|
|
|
@@ -383,12 +384,13 @@ export class DyFM_Object {
|
|
|
383
384
|
* (also will extract the JSON from "```json ... ```" wrapper)
|
|
384
385
|
* Uses enhanced error reporting with line context
|
|
385
386
|
*/
|
|
386
|
-
static
|
|
387
|
+
static failableSafeParseJSON_old<T = any>(textedJSON: string): T {
|
|
387
388
|
if (!textedJSON) {
|
|
388
389
|
throw new Error(`No content provided to JSON parse ("${textedJSON}")`);
|
|
389
390
|
}
|
|
390
391
|
|
|
391
392
|
const match = textedJSON.match(/```json(.*)```/s);
|
|
393
|
+
// TODO; ez nem lesz az igazi, mert kiszedi a tartalmakból is a sortöréseket!!! (ugyan ez pár sorral lejjebb)
|
|
392
394
|
const cleanedTextedJSON = textedJSON.replaceAll('\n', '');
|
|
393
395
|
|
|
394
396
|
if (match) {
|
|
@@ -402,15 +404,43 @@ export class DyFM_Object {
|
|
|
402
404
|
|
|
403
405
|
|
|
404
406
|
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
407
|
+
/**
|
|
408
|
+
* parses the JSON, if it fails, it throws an error
|
|
409
|
+
* (also will extract the JSON from "```json ... ```" wrapper)
|
|
410
|
+
* Uses enhanced error reporting with line context
|
|
411
|
+
*/
|
|
412
|
+
static failableSafeParseJSON<T = any>(textedJSON: string): T {
|
|
413
|
+
if (!textedJSON) {
|
|
414
|
+
throw new Error(`No content provided to JSON parse ("${textedJSON}")`);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (textedJSON.includes('```json')) {
|
|
418
|
+
// Extract content from code block, preserving newlines for error context
|
|
419
|
+
/* const extractedJson = match[1].trim(); */
|
|
420
|
+
const extractedJson = DyFM_String.multiBracketSplitDetailed(
|
|
421
|
+
textedJSON,
|
|
422
|
+
[
|
|
423
|
+
{ opening: '```json', closing: '```' },
|
|
424
|
+
{ opening: '{', closing: '}' },
|
|
425
|
+
{ opening: '[', closing: ']' },
|
|
426
|
+
],
|
|
427
|
+
1,
|
|
428
|
+
)
|
|
429
|
+
|
|
430
|
+
// Check if we have extracted content and handle undefined cases
|
|
431
|
+
if (extractedJson.length > 1 && extractedJson[1] && extractedJson[1].brackets?.opening === '```json') {
|
|
432
|
+
return DyFM_JsonErrorHelper.parseJsonWithEnhancedError(extractedJson[1].content);
|
|
433
|
+
} else if (extractedJson.length > 0 && extractedJson[0]) {
|
|
434
|
+
return DyFM_JsonErrorHelper.parseJsonWithEnhancedError(extractedJson[0].content);
|
|
435
|
+
} else {
|
|
436
|
+
// Fallback if extraction fails - use original text minus markdown wrapper
|
|
437
|
+
const cleanedText = textedJSON.replace(/```json\s*/, '').replace(/\s*```$/, '').trim();
|
|
438
|
+
return DyFM_JsonErrorHelper.parseJsonWithEnhancedError(cleanedText);
|
|
439
|
+
}
|
|
440
|
+
} else {
|
|
441
|
+
return DyFM_JsonErrorHelper.parseJsonWithEnhancedError(textedJSON.trim());
|
|
442
|
+
}
|
|
443
|
+
}
|
|
414
444
|
|
|
415
445
|
|
|
416
446
|
/**
|