when converting, return an object with a status plus the result

This commit is contained in:
Manuel Friedli 2015-03-26 18:13:18 +01:00
parent 90b28c793b
commit ac652b32a1

View file

@ -4,14 +4,20 @@
"id": "CHOOSE",
"name": "Please choose your conversion ...",
"convert": function(input) {
return "";
return {
"status": "OK",
"content": ""
};
}
},
{
"id": "base64encode",
"name": "Base64 encode",
"convert": function(input) {
return btoa(input);
return {
"status": "OK",
"content": btoa(input)
};
}
},
{
@ -19,9 +25,15 @@
"name": "Base64 decode",
"convert": function(input) {
try {
return atob(input);
return {
"status": "OK",
"content": atob(input)
};
} catch (exception) {
return "Invalid base64 input string.";
return {
"status": "ERROR",
"content": "Invalid base64 input string."
};
}
}
},
@ -29,7 +41,10 @@
"id": "encodeuri",
"name": "Encode URI",
"convert": function(input) {
return encodeURI(input).replace(/%5B/g, '[').replace(/%5D/g, ']');
return {
"status": "OK",
"content": encodeURI(input).replace(/%5B/g, '[').replace(/%5D/g, ']')
};
}
},
{
@ -37,9 +52,15 @@
"name": "Decode URI",
"convert": function(input) {
try {
return decodeURI(input)
return {
"status": "OK",
"content": decodeURI(input)
};
} catch (exception) {
return "Invalid URI string.";
return {
"status": "ERROR",
"content": "Invalid URI input string."
};
}
}
},
@ -47,9 +68,12 @@
"id": "encodeuricomponent",
"name": "Encode URI component",
"convert": function(input) {
return encodeURIComponent(input).replace(/[!'()*]/g, function(c) {
return {
"status": "OK",
"content": encodeURIComponent(input).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
})
};
}
},
{
@ -57,9 +81,15 @@
"name": "Decode URI component",
"convert": function(input) {
try {
return decodeURIComponent(input)
return {
"status": "OK",
"content": decodeURIComponent(input)
};
} catch (exception) {
return "Invalid URI component string.";
return {
"status": "ERROR",
"content": "Invalid URI component input string."
};
}
}
}
@ -103,6 +133,8 @@
var appendNewOutput = false;
var input = $input.val();
var conversion = $select.find(":selected").attr("name");
var result;
var status;
var output;
var plugin;
if ($output.length == 0) {
@ -110,17 +142,28 @@
}
plugin = getPluginById(conversion);
if (plugin !== null) {
output = plugin.convert(input);
result = plugin.convert(input);
output = result.content;
status = result.status
} else {
output = "internal error. Sorry.";
output = "Internal error. Sorry.";
status = "ERROR";
}
if (appendNewOutput) {
if (output !== "") {
$output = $(template.replace(/\{index\}/g, "" + outputIndex).replace(/\{content\}/g, output));
if (status === "ERROR") {
$output.find("textarea").addClass("error");
}
$("body").append($output);
}
} else {
$output.val(output);
if (status === "ERROR") {
$output.addClass("error");
} else {
$output.removeClass("error");
}
update($output);
}
}