File size: 3,846 Bytes
e706de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**

 * Exercise 2: Build a JSON Parser Runnable

 *

 * Goal: Create a Runnable that parses JSON strings safely

 *

 * Requirements:

 * - Takes a JSON string as input

 * - Parses it to an object

 * - Handles errors gracefully (return null if invalid)

 * - Optional: Add a default value option

 *

 * Example:

 *   const parser = new JsonParserRunnable();

 *   await parser.invoke('{"name":"Alice"}'); // Should return { name: "Alice" }

 *   await parser.invoke('invalid json');     // Should return null

 */

import { Runnable } from '../../../../src/index.js';

/**

 * JsonParserRunnable - Safely parses JSON strings

 *

 * TODO: Implement this class

 */
class JsonParserRunnable extends Runnable {
    constructor(options = {}) {
        // TODO: Call super()
        // TODO: Store options (like defaultValue)
    }

    async _call(input, config) {
        // TODO: Check if input is a string
        // TODO: Try to parse the JSON
        // TODO: If parsing fails, return null or defaultValue
        // TODO: Return the parsed object
    }
}

// ============================================================================
// Tests
// ============================================================================

async function runTests() {
    console.log('πŸ§ͺ Testing JsonParserRunnable...\n');

    try {
        // Test 1: Valid JSON object
        console.log('Test 1: Valid JSON object');
        const parser = new JsonParserRunnable();
        const result1 = await parser.invoke('{"name":"Alice","age":30}');
        console.assert(result1.name === 'Alice', 'Should parse name');
        console.assert(result1.age === 30, 'Should parse age');
        console.log('βœ… Parsed: {"name":"Alice","age":30}\n');

        // Test 2: Valid JSON array
        console.log('Test 2: Valid JSON array');
        const result2 = await parser.invoke('[1, 2, 3, 4, 5]');
        console.assert(Array.isArray(result2), 'Should return array');
        console.assert(result2.length === 5, 'Should have 5 elements');
        console.log('βœ… Parsed: [1, 2, 3, 4, 5]\n');

        // Test 3: Invalid JSON returns null
        console.log('Test 3: Invalid JSON returns null');
        const result3 = await parser.invoke('this is not json');
        console.assert(result3 === null, 'Should return null for invalid JSON');
        console.log('βœ… Returns null for invalid JSON\n');

        // Test 4: Empty string returns null
        console.log('Test 4: Empty string returns null');
        const result4 = await parser.invoke('');
        console.assert(result4 === null, 'Should return null for empty string');
        console.log('βœ… Returns null for empty string\n');

        // Test 5: With default value
        console.log('Test 5: With default value');
        const parserWithDefault = new JsonParserRunnable({
            defaultValue: { error: 'Invalid JSON' }
        });
        const result5 = await parserWithDefault.invoke('bad json');
        console.assert(result5.error === 'Invalid JSON', 'Should return default value');
        console.log('βœ… Returns default value for invalid JSON\n');

        // Test 6: Nested JSON
        console.log('Test 6: Nested JSON');
        const nested = '{"user":{"name":"Bob","address":{"city":"NYC"}}}';
        const result6 = await parser.invoke(nested);
        console.assert(result6.user.address.city === 'NYC', 'Should parse nested objects');
        console.log('βœ… Parsed nested JSON\n');

        console.log('πŸŽ‰ All tests passed!');
    } catch (error) {
        console.error('❌ Test failed:', error.message);
        console.error(error.stack);
    }
}

// Run tests
if (import.meta.url === `file://${process.argv[1]}`) {
    runTests();
}

export { JsonParserRunnable };