/**
* Parses user input with verbose logging.
*
* @param {string} userInput The user's input.
* @returns {object|null} The parsed data, or null if parsing fails.
*/
function parseUserInput(userInput) {
console.log(`Parsing user input: ${userInput}`); // Log the input
try {
const parsedData = parseData(userInput); // Call the parsing function
console.log(`Successfully parsed data: ${JSON.stringify(parsedData)}`); // Log parsed data
return parsedData;
} catch (error) {
console.error(`Error parsing data: ${error.message}`); // Log the error
return null;
}
}
/**
* Parses the input string based on a defined format.
* This is a placeholder, replace with your actual parsing logic.
*
* @param {string} input The input string to parse.
* @returns {object} The parsed data.
*/
function parseData(input) {
// Example parsing logic (replace with your own)
if (input.startsWith("name=")) {
const name = input.substring(5); // Extract the name
return { name: name };
} else if (input.startsWith("age=")) {
const age = parseInt(input.substring(4), 10); // Extract age and parse as integer
return { age: age };
} else {
throw new Error("Invalid input format."); // Throw error if input format is invalid
}
}
// Example usage (for testing purposes)
const userInput1 = "name=John Doe";
const parsedData1 = parseUserInput(userInput1);
console.log("Parsed data 1:", parsedData1);
const userInput2 = "age=30";
const parsedData2 = parseUserInput(userInput2);
console.log("Parsed data 2:", parsedData2);
const userInput3 = "invalid input";
const parsedData3 = parseUserInput(userInput3);
console.log("Parsed data 3:", parsedData3);
Add your comment