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
1 changed files with 57 additions and 14 deletions

View File

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