Skip to content

Commit 484f5f7

Browse files
changes
1 parent e655295 commit 484f5f7

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

src/modules/outputparsers.ts

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,70 @@
33
*/
44
const cboutputparsers = {
55
/**
6-
* Initializes the output parser module.
7-
* Currently, this function does not perform any operations.
8-
* @param {any} output - The output to be initialized.
6+
* Parses JSON string and returns a result object.
7+
* @param {string} jsonString - The JSON string to parse.
8+
* @returns {Object} An object with success flag and parsed data or error.
99
*/
10-
init: (output: any) => {
11-
// Initialization code can be added here if necessary
10+
parseJSON: (jsonString: string): { success: boolean; parsed?: any; error?: Error } => {
11+
try {
12+
const parsed = JSON.parse(jsonString);
13+
return { success: true, parsed };
14+
} catch (error) {
15+
return { success: false, error: error as Error };
16+
}
1217
},
18+
19+
/**
20+
* Parses XML string and returns a result object.
21+
* @param {string} xmlString - The XML string to parse.
22+
* @returns {Object} An object with success flag and parsed data.
23+
*/
24+
parseXML: (xmlString: string): { success: boolean; parsed?: any } => {
25+
// Simple XML parsing - in a real implementation, would use a proper XML parser
26+
const hasValidRoot = xmlString.trim().startsWith('<') && xmlString.trim().endsWith('>');
27+
return {
28+
success: hasValidRoot,
29+
parsed: hasValidRoot ? { rootElement: xmlString } : undefined
30+
};
31+
},
32+
33+
/**
34+
* Parses CSV string and returns a result object.
35+
* @param {string} csvString - The CSV string to parse.
36+
* @returns {Object} An object with success flag and parsed data or error.
37+
*/
38+
parseCSV: (csvString: string): { success: boolean; parsed?: any[]; error?: Error } => {
39+
try {
40+
const lines = csvString.split('\n');
41+
const headers = lines[0].split(',');
42+
43+
const results = lines.slice(1).map(line => {
44+
const values = line.split(',');
45+
const obj: Record<string, string> = {};
46+
47+
headers.forEach((header, index) => {
48+
obj[header] = values[index];
49+
});
50+
51+
return obj;
52+
});
53+
54+
return { success: true, parsed: results };
55+
} catch (error) {
56+
return { success: false, error: error as Error };
57+
}
58+
},
59+
60+
/**
61+
* Parses text string and returns a result object with lines.
62+
* @param {string} text - The text to parse.
63+
* @returns {Object} An object with success flag and parsed lines.
64+
*/
65+
parseText: (text: string): { success: boolean; parsed: string[] } => {
66+
const lines = text ? text.split('\n') : [];
67+
return { success: true, parsed: lines };
68+
},
69+
1370
/**
1471
* Parses the given output and returns all the error messages.
1572
* @param {any} output - The output to parse for error messages.

testcases/tests/outputparsers-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ async function testOutputParsers() {
55
console.log('=========================');
66

77
try {
8-
await codebolt.activate();
8+
99
await codebolt.waitForConnection();
1010

1111
console.log('\n1. Testing JSON parsing with valid JSON...');

0 commit comments

Comments
 (0)