UNPKG

5.48 kBJavaScriptView Raw
1!function(){"use strict";function t(t,e){this.options=e||{},this.reset(t)}var e=Array.prototype.slice,i=Object.prototype.toString;t.VERSION="0.0.6",t.LEFT=0,t.CENTER=1,t.RIGHT=2,t.factory=function(e,i){return new t(e,i)},t.align=function(e,i,n,r){return e===t.LEFT?t.alignLeft(i,n,r):e===t.RIGHT?t.alignRight(i,n,r):e===t.CENTER?t.alignCenter(i,n,r):t.alignAuto(i,n,r)},t.alignLeft=function(t,e,i){if(!e||0>e)return"";"undefined"==typeof t&&(t=""),"undefined"==typeof i&&(i=" "),"string"!=typeof t&&(t=t.toString());var n=e+1-t.length;return 0>=n?t:t+Array(e+1-t.length).join(i)},t.alignCenter=function(e,i,n){if(!i||0>i)return"";"undefined"==typeof e&&(e=""),"undefined"==typeof n&&(n=" "),"string"!=typeof e&&(e=e.toString());var r=e.length,o=Math.floor(i/2-r/2),s=Math.abs(r%2-i%2),i=e.length;return t.alignRight("",o,n)+e+t.alignLeft("",o+s,n)},t.alignRight=function(t,e,i){if(!e||0>e)return"";"undefined"==typeof t&&(t=""),"undefined"==typeof i&&(i=" "),"string"!=typeof t&&(t=t.toString());var n=e+1-t.length;return 0>=n?t:Array(e+1-t.length).join(i)+t},t.alignAuto=function(e,n,r){"undefined"==typeof e&&(e="");var o=i.call(e);if(r||(r=" "),n=+n,"[object String]"!==o&&(e=e.toString()),e.length<n)switch(o){case"[object Number]":return t.alignRight(e,n,r);default:return t.alignLeft(e,n,r)}return e},t.arrayFill=function(t,e){for(var i=new Array(t),n=0;n!==t;n++)i[n]=e;return i},t.prototype.reset=t.prototype.clear=function(e){return this.__name="",this.__nameAlign=t.CENTER,this.__rows=[],this.__maxCells=0,this.__aligns=[],this.__colMaxes=[],this.__spacing=1,this.__heading=null,this.__headingAlign=t.CENTER,this.setBorder(),"[object String]"===i.call(e)?this.__name=e:"[object Object]"===i.call(e)&&this.fromJSON(e),this},t.prototype.setBorder=function(t,e,i,n){return this.__border=!0,1===arguments.length&&(e=i=n=t),this.__edge=t||"|",this.__fill=e||"-",this.__top=i||".",this.__bottom=n||"'",this},t.prototype.removeBorder=function(){return this.__border=!1,this.__edge=" ",this.__fill=" ",this},t.prototype.setAlign=function(t,e){return this.__aligns[t]=e,this},t.prototype.setTitle=function(t){return this.__name=t,this},t.prototype.getTitle=function(){return this.__name},t.prototype.setTitleAlign=function(t){return this.__nameAlign=t,this},t.prototype.sort=function(t){return this.__rows.sort(t),this},t.prototype.sortColumn=function(t,e){return this.__rows.sort(function(i,n){return e(i[t],n[t])}),this},t.prototype.setHeading=function(t){return(arguments.length>1||"[object Array]"!==i.call(t))&&(t=e.call(arguments)),this.__heading=t,this},t.prototype.getHeading=function(){return this.__heading.slice()},t.prototype.setHeadingAlign=function(t){return this.__headingAlign=t,this},t.prototype.addRow=function(t){return(arguments.length>1||"[object Array]"!==i.call(t))&&(t=e.call(arguments)),this.__maxCells=Math.max(this.__maxCells,t.length),this.__rows.push(t),this},t.prototype.getRows=function(){return this.__rows.slice().map(function(t){return t.slice()})},t.prototype.addRowMatrix=function(t){for(var e=0;e<t.length;e++)this.addRow(t[e]);return this},t.prototype.clearRows=function(){return this.__rows=[],this.__maxCells=0,this.__colMaxes=[],this},t.prototype.setJustify=function(t){return 0===arguments.length&&(t=!0),this.__justify=!!t,this},t.prototype.toJSON=function(){return{title:this.getTitle(),heading:this.getHeading(),rows:this.getRows()}},t.prototype.parse=t.prototype.fromJSON=function(t){return this.clear().setTitle(t.title).setHeading(t.heading).addRowMatrix(t.rows)},t.prototype.render=t.prototype.valueOf=t.prototype.toString=function(){for(var e,i=this,n=[],r=this.__maxCells,o=t.arrayFill(r,0),s=3*r,h=this.__rows,a=this.__border,_=this.__heading?[this.__heading].concat(h):h,l=0;l<_.length;l++)for(var u=_[l],g=0;r>g;g++){var p=u[g];o[g]=Math.max(o[g],p?p.toString().length:0)}this.__colMaxes=o,e=this.__justify?Math.max.apply(null,o):0,o.forEach(function(t){s+=e?e:t+i.__spacing}),e&&(s+=o.length),s-=this.__spacing,a&&n.push(this._seperator(s-r+1,this.__top)),this.__name&&(n.push(this._renderTitle(s-r+1)),a&&n.push(this._seperator(s-r+1))),this.__heading&&(n.push(this._renderRow(this.__heading," ",this.__headingAlign)),n.push(this._rowSeperator(r,this.__fill)));for(var l=0;l<this.__rows.length;l++)n.push(this._renderRow(this.__rows[l]," "));a&&n.push(this._seperator(s-r+1,this.__bottom));var f=this.options.prefix||"";return f+n.join("\n"+f)},t.prototype._seperator=function(e,i){return i||(i=this.__edge),i+t.alignRight(i,e,this.__fill)},t.prototype._rowSeperator=function(){var e=t.arrayFill(this.__maxCells,this.__fill);return this._renderRow(e,this.__fill)},t.prototype._renderTitle=function(e){var i=" "+this.__name+" ",n=t.align(this.__nameAlign,i,e-1," ");return this.__edge+n+this.__edge},t.prototype._renderRow=function(e,i,n){for(var r=[""],o=this.__colMaxes,s=0;s<this.__maxCells;s++){var h=e[s],a=this.__justify?Math.max.apply(null,o):o[s],_=a,l=this.__aligns[s],u=n,g="alignAuto";"undefined"==typeof n&&(u=l),u===t.LEFT&&(g="alignLeft"),u===t.CENTER&&(g="alignCenter"),u===t.RIGHT&&(g="alignRight"),r.push(t[g](h,_,i))}var p=r.join(i+this.__edge+i);return p=p.substr(1,p.length),p+i+this.__edge},["Left","Right","Center"].forEach(function(i){var n=t[i.toUpperCase()];["setAlign","setTitleAlign","setHeadingAlign"].forEach(function(r){t.prototype[r+i]=function(){var t=e.call(arguments).concat(n);return this[r].apply(this,t)}})}),"undefined"!=typeof exports?module.exports=t:this.AsciiTable=t}.call(this);
\No newline at end of file