-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtsvert.min.js
6 lines (6 loc) · 7 KB
/
tsvert.min.js
1
2
3
4
5
6
/**
* tsvert - a tsv converter
* Copyright (c) high5. (MIT Licensed)
* https://github.com/high5/tsvert
*/
(function(){function e(e){for(var t,n,r=1;r<arguments.length;r++){t=arguments[r];for(n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}function t(e){var t={"&":"&","<":"<",">":">",'"':"""};return e.replace(/[&<>"]/g,function(e){return t[e]})}function n(e){for(var t=0,n=0;n<e.length;n++){var r=e.charCodeAt(n);t+=r>=0&&129>r||63728==r||r>=65377&&65440>r||r>=63729&&63732>r?1:2}return t}function r(n,i){var a=e({},r.defaults,a||{});""!=a.header&&(n=a.header+"\n"+n);for(var l=n.replace(/\r/g,"").split("\n"),s=[],v="",d=l[0].split(" "),c=1;c<l.length;c++){var p={},m=l[c].split(" ");if(""!=m){for(var N=0;N<d.length;N++)0==a.html?p[d[N]]=m[N]:p[t(d[N])]=t(m[N]);s.push(p)}}switch(i){case"sql":v=o(s,a);break;case"json":v=u(s,a);break;case"php":v=h(s,a);break;case"python":v=f(s,a);break;case"ruby":v=g(s,a)}return v}function i(e,t){var n=isNaN(t)?"'":"",r="";return"string"==typeof t&&0==t.length?n="'":"const"==e||"constant"==e?n="":"str"==e||"string"==e?n="'":("flex"==e||"flexible"==e)&&(n=isNaN(t)?"'":"",0==t.length&&(n="'"),""!=n&&(r=t.toLowerCase(),("null"==r||"true"==r||"false"==r)&&(n=""))),n}function a(e){var t=e[0],r=0;for(var i in t){var a=i,l=i.split(":");2==l.length&&2!=i.split("\\:").length&&(a=l[0]),2==i.split("\\:").length&&(a=a.replace("\\","")),n(a)>r&&(r=n(a))}return r}function l(e){for(var t={},r=0;r<e.length;r++){var a=e[r];for(var l in a){var s="",o=l,u=l.split(":");2==u.length&&2!=l.split("\\:").length&&(o=u[0],s=u[1].toLowerCase()),2==l.split("\\:").length&&(o=o.replace("\\","")),quote=i(s,a[l]);var h=quote+a[l]+quote;t[o]?n(h)>t[o]&&(t[o]=n(h)):t[o]=n(h)}}return t}function s(e,t){for(var n="";;){if(1&t&&(n+=e),t>>=1,!t)break;e+=e}return n}function o(e,t){for(var n="",r=t.html===!0?"<br>":"\n",a=""===t.sqlTableName?"table":t.sqlTableName,l=0;l<e.length;l++){var s=e[l],o="",u="",h=0;n+=t.sqlBulkInsert===!0&&0!==l?",":"INSERT INTO "+a;for(var f in s){var g=f,v=s[f],d="",c="",p=f.split(":");2==p.length&&2!=f.split("\\:").length&&(g=p[0],d=p[1].toLowerCase(),("num"==d||"number"==d)&&(v=isNaN(v)?0:parseFloat(v),isNaN(v)&&(v=0)),"const"!=d&&"constant"!=d||0!=v.length||(v="UNDEFINED")),c=i(d,v),2==f.split("\\:").length&&(g=g.replace("\\","")),h++,o+=g,u+=c+v+c,h!=Object.keys(s).length&&(o+=", ",u+=", ")}t.sqlBulkInsert===!0?(n+=l>0?"("+u+")":"("+o+") VALUES("+u+")",l==e.length-1&&(n+=";")):(n+="("+o+") VALUES("+u+")",n+=";"),l!=e.length-1&&(n+=r)}return n}function u(e,t){var r="",o=t.html===!0?"<br>":"\n",u=a(e),h=l(e);r+="["+o;for(var f=0;f<e.length;f++){var g=e[f],v="",d=0;if(t.useRowNumberKey===!0){var c=f+1,p="";t.sameWidth===!0&&n(String(e.length))>1&&(p=s(" ",n(String(e.length))-n(String(c)))),r+=' "'+c+'"'+p+": {"}else r+=" {";t.indent===!0&&(v+=o);for(var m in g){var N=m,b=g[m],y="",w="",S=m.split(":");if(2==S.length&&2!=m.split("\\:").length&&(N=S[0],y=S[1].toLowerCase(),("num"==y||"number"==y)&&(b=isNaN(b)?0:parseFloat(b),isNaN(b)&&(b=0)),"const"!=y&&"constant"!=y||0!=b.length||(b="UNDEFINED")),w=i(y,b),""!=w&&(w='"'),2==m.split("\\:").length&&(N=N.replace("\\","")),d++,t.indent===!0&&(v+=" "),v+='"'+N+'"',t.indent===!0&&t.sameWidth===!0){var k=u-n(N);k>0&&(v+=s(" ",k))}var E=w+b+w;if(v+=": "+E,d!=Object.keys(g).length)if(1!=t.indent){if(v+=", ",t.sameWidth===!0){var W=h[N]-n(E);W>0&&(v+=s(" ",W))}}else v+=",";t.indent===!0&&(v+=o)}r+=t.indent===!0?v+" }":v+"}",f!=e.length-1&&(r+=","),r+=o}return r+="];"}function h(e,t){var r="",o=t.html===!0?"<br>":"\n",u=a(e),h=l(e);r+="array("+o;for(var f=0;f<e.length;f++){var g=e[f],v="",d=0;if(t.useRowNumberKey===!0){var c=f+1,p="";t.sameWidth===!0&&n(String(e.length))>1&&(p=s(" ",n(String(e.length))-n(String(c)))),r+=" "+c+" "+p+"=> array("}else r+=" array(";t.indent===!0&&(v+=o);for(var m in g){var N=m,b=g[m],y="",w="",S=m.split(":");if(2==S.length&&2!=m.split("\\:").length&&(N=S[0],y=S[1].toLowerCase(),("num"==y||"number"==y)&&(b=isNaN(b)?0:parseFloat(b),isNaN(b)&&(b=0)),"const"!=y&&"constant"!=y||0!=b.length||(b="UNDEFINED")),w=i(y,b),2==m.split("\\:").length&&(N=N.replace("\\","")),d++,t.indent===!0&&(v+=" "),v+="'"+N+"'",t.indent===!0&&t.sameWidth===!0){var k=u-n(N);k>0&&(v+=s(" ",k))}var E=w+b+w;if(v+=" => "+E,d!=Object.keys(g).length)if(1!=t.indent){if(v+=", ",t.sameWidth===!0){var W=h[N]-n(E);W>0&&(v+=s(" ",W))}}else v+=",";t.indent===!0&&(v+=o)}r+=t.indent===!0?v+" )":v+")",f!=e.length-1&&(r+=","),r+=o}return r+=");"}function f(e,t){var r="",o=t.html===!0?"<br>":"\n",u=a(e),h=l(e);r+=t.useRowNumberKey===!0?"{"+o:"["+o;for(var f=0;f<e.length;f++){var g=e[f],v="",d=0;if(t.useRowNumberKey===!0){var c=f+1,p="";t.sameWidth===!0&&n(String(e.length))>1&&(p=s(" ",n(String(e.length))-n(String(c)))),r+=" "+c+p+": {"}else r+=" {";t.indent===!0&&(v+=o);for(var m in g){var N=m,b=g[m],y="",w="",S=m.split(":");2==S.length&&2!=m.split("\\:").length&&(N=S[0],y=S[1].toLowerCase(),("num"==y||"number"==y)&&(b=isNaN(b)?0:parseFloat(b),isNaN(b)&&(b=0)),"const"!=y&&"constant"!=y||0!=b.length||(b="UNDEFINED")),w=i(y,b),""!=w&&(w='"'),2==m.split("\\:").length&&(N=N.replace("\\","")),d++,t.indent===!0&&(v+=" ");var k='"'+N+'"';if(v+=k,t.indent===!0&&t.sameWidth===!0){var E=u-n(N);E>0&&(v+=s(" ",E))}var W=w+b+w;if(v+=": "+W,d!=Object.keys(g).length)if(1!=t.indent){if(v+=", ",t.sameWidth===!0){var q=h[N]-n(W);q>0&&(v+=s(" ",q))}}else v+=",";t.indent===!0&&(v+=o)}r+=t.indent===!0?v+" }":v+"}",f!=e.length-1&&(r+=","),r+=o}return r+=t.useRowNumberKey===!0?"}":"]"}function g(e,t){var r="",o=t.html===!0?"<br>":"\n",u=a(e),h=l(e);r+=t.useRowNumberKey===!0?"{"+o:"["+o;for(var f=0;f<e.length;f++){var g=e[f],v="",d=0;if(t.useRowNumberKey===!0){var c=f+1,p="";t.sameWidth===!1&&n(String(e.length))>1&&(p=s(" ",n(String(e.length))-n(String(c)))),r+=" "+c+p+" => {"}else r+=" {";t.indent===!0&&(v+=o);for(var m in g){var N=m,b=g[m],y="",w="",S=m.split(":");2==S.length&&2!=m.split("\\:").length&&(N=S[0],y=S[1].toLowerCase(),("num"==y||"number"==y)&&(b=isNaN(b)?0:parseFloat(b),isNaN(b)&&(b=0)),"const"!=y&&"constant"!=y||0!=b.length||(b="UNDEFINED")),w=i(y,b),""!=w&&(w='"'),2==m.split("\\:").length&&(N=N.replace("\\","")),d++,t.indent===!0&&(v+=" ");var k=t.rubySymbolKey?":"+N:'"'+N+'"';if(v+=k,t.indent===!0&&t.sameWidth===!0){var E=u-n(N);E>0&&(v+=s(" ",E))}var W=w+b+w;if(v+=" => "+W,d!=Object.keys(g).length)if(1!=t.indent){if(v+=", ",t.sameWidth===!0){var q=h[N]-n(W);q>0&&(v+=s(" ",q))}}else v+=",";t.indent===!0&&(v+=o)}r+=t.indent===!0?v+" }":v+"}",f!=e.length-1&&(r+=","),r+=o}return r+=t.useRowNumberKey===!0?"}":"]"}r.options=r.setOptions=function(t){return 0==Object.keys(r.initials)&&e(r.initials,r.defaults),e(r.defaults,t),r},r.initializeOptions=function(){return e(r.defaults,r.initials),r},r.initials={},r.defaults={html:!1,indent:!1,sqlBulkInsert:!1,sqlTableName:"",rubySymbolKey:!0,useRowNumberKey:!1,sameWidth:!1,header:""},"undefined"!=typeof module&&"object"==typeof exports?module.exports=r:"function"==typeof define&&define.amd?define(function(){return r}):this.tsvert=r}).call(function(){return this||("undefined"!=typeof window?window:global)}());