I decided to write my own function. If there is a standard algorithm to parse such a string leave a comment.
/*
* Transform a string like this:
*
* "Field1=123
* Field2=1234
* Field3=5"
*
* into an object like this:
*
* {
* "Field1": "123",
* "Field2": "1234",
* "Field3": "5",
* }
*
* */
function parseResponse (str) {
// validate the provided value
if (typeof str !== "string") {
throw new Error("Please provide a string as argument: " +
JSON.stringify(str));
}
// split it into lines
var lines = str.trim().split("\n");
// create the object that will be returned
var parsedObject = {};
// for every line
for (var i = 0; i < lines.length; ++i) {
// split the line
var splits = lines[i].split("=")
// and get the field
, field = splits[0]
// and the value
, value = splits[1];
// finally set them in the parsed object
parsedObject[field] = value;
}
// return the parsed object
return parsedObject;
};