-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.min.js
1 lines (1 loc) · 2.94 KB
/
converter.min.js
1
const TYPES=["json2xml","xml2json"];var current_type="json2xml",x2js=new X2JS,switchParser=e=>{document.getElementById(current_type).style.color="#007bff",TYPES.includes(e)&&(current_type=e),document.getElementById(current_type).style.color="#ff5200",window.localStorage.setItem("converter_type",current_type);var t=window.localStorage.getItem(current_type);t?!0===isValidInputData(current_type,t)?document.getElementById("input").value=t:(window.localStorage.removeItem(current_type),clearInput()):clearInput();clearOutput()},validateJson=e=>{try{JSON.parse(e)}catch(e){return e}return!0},validateXml=e=>{const t=(new window.DOMParser).parseFromString(e,"text/xml");return!(t.getElementsByTagName("parsererror").length>0)||t.getElementsByTagName("parsererror")[0].getElementsByTagName("div")[0].innerHTML},isValidInputData=(e,t)=>{let r;switch(e){case"json2xml":r=validateJson(t);break;case"xml2json":r=validateXml(t);break;default:r="Invalid type"}return r},initPage=()=>{let e=window.localStorage.getItem("converter_type");e&&TYPES.includes(e)?current_type=e:window.localStorage.setItem("converter_type",current_type),document.getElementById(current_type).style.color="#ff5200";var t=window.localStorage.getItem(current_type);if(t){var r=isValidInputData(current_type,t);!0===r?document.getElementById("input").value=t:(window.localStorage.removeItem(current_type),console.log(r))}};initPage();var parseXml=e=>{return(new window.DOMParser).parseFromString(e,"text/xml")},parseInputData=(e,t)=>{let r=t;switch(e){case"json2xml":r=vkbeautify.xml(x2js.json2xml_str(JSON.parse(t.trim())));break;case"xml2json":r=vkbeautify.json(x2js.xml_str2json(t.trim()),4);break;default:r="Invalid type"}return r},parseData=()=>{var e=document.getElementById("input").value;if(e){var t=isValidInputData(current_type,e);!0===t?(document.getElementById("output").value=parseInputData(current_type,e),window.localStorage.setItem(current_type,e)):document.getElementById("output").value=current_type.toUpperCase()+" validation: "+t}else document.getElementById("output").value="Input is empty"},compressInputData=(e,t)=>{let r=t;switch(e){case"json2xml":r=vkbeautify.xmlmin(x2js.json2xml_str(JSON.parse(t.trim())),!0);break;case"xml2json":r=vkbeautify.jsonmin(JSON.stringify(x2js.xml_str2json(t.trim())));break;default:r="Invalid type"}return r},compressData=()=>{var e=document.getElementById("input").value;if(e&&""!==e.trim()){var t=isValidInputData(current_type,e);!0===t?(document.getElementById("output").value=compressInputData(current_type,e),window.localStorage.setItem(current_type,e)):document.getElementById("output").value=current_type.toUpperCase()+" validation: "+t}else document.getElementById("output").value="Input value is empty"},copyData=e=>{var t=document.getElementById(e);t.select(),t.setSelectionRange(0,99999),document.execCommand("copy")},clearInput=()=>{document.getElementById("input").value=""},clearOutput=()=>{document.getElementById("output").value=""},clearAll=()=>{clearOutput(),clearInput()};