UNPKG

angular2-csv

Version:

> Helper library for create CSV file in Angular 6

2 lines 5.31 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@angular/common")):"function"==typeof define&&define.amd?define("angular2-csv",["exports","@angular/core","@angular/common"],e):e(t["angular2-csv"]={},t.ng.core,t.ng.common)}(this,function(t,e,o){"use strict";var n=function(){function t(){}return t.decorators=[{type:e.Injectable,args:[{providedIn:"root"}]}],t.ctorParameters=function(){return[]},t.ngInjectableDef=e.defineInjectable({factory:function(){return new t},token:t,providedIn:"root"}),t}();function E(t){var e="function"==typeof Symbol&&t[Symbol.iterator],o=0;return e?e.call(t):{next:function(){return t&&o>=t.length&&(t=void 0),{value:t&&t[o++],done:!t}}}}var r=function(){function t(){this.filename=this.filename||"mycsv.csv",this.options=this.objectAssign({},i,this.options),this.label_btn="download",this.csv=""}return t.prototype.onDownload=function(){this.generateCsv()},t.prototype.ngOnInit=function(){},t.prototype.generateCsv=function(){if(this.options.useBom&&(this.csv+=d.BOM),this.options.showTitle&&(this.csv+=this.options.title+"\r\n\n"),this.getHeaders(),this.getBody(),""!==this.csv){var t=new Blob([this.csv],{type:"text/csv;charset=utf8;"});if(navigator.msSaveBlob){var e=this.options.filename.replace(/ /g,"_")+".csv";navigator.msSaveBlob(t,e)}else{encodeURI(this.csv);var o=document.createElement("a");o.href=URL.createObjectURL(t),o.setAttribute("visibility","hidden"),o.download=this.filename.replace(/ /g,"_")+".csv",document.body.appendChild(o),o.click(),document.body.removeChild(o)}this.csv=""}else console.log("Invalid data")},t.prototype.getHeaders=function(){if(0<this.options.headers.length){var t="";try{for(var e=E(this.options.headers),o=e.next();!o.done;o=e.next()){t+=o.value+this.options.fieldSeparator}}catch(i){n={error:i}}finally{try{o&&!o.done&&(r=e["return"])&&r.call(e)}finally{if(n)throw n.error}}t=t.slice(0,-1),this.csv+=t+d.EOL}var n,r},t.prototype.getBody=function(){try{for(var t=E(this.data),e=t.next();!e.done;e=t.next()){var o=e.value,n="";if(!this.isEmptyObject(o)||!this.options.removeNewLines)if("undefined"!=typeof this.options.keys&&this.options.keys.length){try{for(var r=E(this.options.keys),i=r.next();!i.done;i=r.next()){var s=i.value;n+=this.formartData(o[s])+this.options.fieldSeparator}}catch(f){c={error:f}}finally{try{i&&!i.done&&(p=r["return"])&&p.call(r)}finally{if(c)throw c.error}}n=n.slice(0,-1),this.csv+=n+d.EOL}else{for(var s in o)o[s]&&(n+=this.formartData(o[s])+this.options.fieldSeparator);this.csv+=n+d.EOL}}}catch(u){a={error:u}}finally{try{e&&!e.done&&(l=t["return"])&&l.call(t)}finally{if(a)throw a.error}}var a,l,c,p},t.prototype.formartData=function(t){return"locale"===this.options.decimalseparator&&this.isFloat(t)?t.toLocaleString():"."!==this.options.decimalseparator&&this.isFloat(t)?t.toString().replace(".",this.options.decimalseparator):"string"==typeof t?(t=t.replace(/"/g,'""'),(this.options.quoteStrings||-1<t.indexOf(",")||-1<t.indexOf("\n")||-1<t.indexOf("\r"))&&(t=this.options.quoteStrings+t+this.options.quoteStrings),t):"boolean"==typeof t?t?"TRUE":"FALSE":t},t.prototype.isEmptyObject=function(t){return t&&0===Object.keys(t).length},t.prototype.isFloat=function(t){return+t===t&&(!isFinite(t)||Boolean(t%1))},t.prototype.toObject=function(t){if(null===t||t===undefined)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(t)},t.prototype.objectAssign=function(t){for(var e,o=[],n=1;n<arguments.length;n++)o[n-1]=arguments[n];for(var r,i,s,a=this.toObject(t),l=Object.prototype.hasOwnProperty,c=Object.prototype.propertyIsEnumerable,p=1;p<arguments.length;p++){for(var f in e=Object(arguments[p]))l.call(e,f)&&(a[f]=e[f]);if(Object.getOwnPropertySymbols){r=Object.getOwnPropertySymbols(e);try{for(var u=E(r),d=u.next();!d.done;d=u.next()){var h=d.value;c.call(e,h)&&(a[h]=e[h])}}catch(y){i={error:y}}finally{try{d&&!d.done&&(s=u["return"])&&s.call(u)}finally{if(i)throw i.error}}}}return a},t.decorators=[{type:e.Component,args:[{selector:"angular2csv",template:'<button (click)="onDownload()">{{ label_btn }}</button>',styles:[]}]}],t.ctorParameters=function(){return[]},t.propDecorators={data:[{type:e.Input}],filename:[{type:e.Input}],options:[{type:e.Input}]},t}(),d=function(){function t(){}return t.EOL="\r\n",t.BOM="\ufeff",t.DEFAULT_FIELD_SEPARATOR=",",t.DEFAULT_DECIMAL_SEPARATOR=".",t.DEFAULT_QUOTE='"',t.DEFAULT_SHOW_TITLE=!1,t.DEFAULT_TITLE="My Report",t.DEFAULT_FILENAME="mycsv.csv",t.DEFAULT_SHOW_LABELS=!1,t.DEFAULT_USE_BOM=!0,t.DEFAULT_HEADER=[],t.DEFAULT_KEY=[],t.DEFAULT_REMOVE_NEW_LINES=!1,t}(),i={filename:d.DEFAULT_FILENAME,fieldSeparator:d.DEFAULT_FIELD_SEPARATOR,quoteStrings:d.DEFAULT_QUOTE,decimalseparator:d.DEFAULT_DECIMAL_SEPARATOR,showLabels:d.DEFAULT_SHOW_LABELS,showTitle:d.DEFAULT_SHOW_TITLE,title:d.DEFAULT_TITLE,useBom:d.DEFAULT_USE_BOM,headers:d.DEFAULT_HEADER,keys:d.DEFAULT_KEY,removeNewLines:d.DEFAULT_REMOVE_NEW_LINES},s=function(){function t(){}return t.decorators=[{type:e.NgModule,args:[{imports:[o.CommonModule],declarations:[r],exports:[r]}]}],t}();t.Angular2CsvService=n,t.Angular2CsvComponent=r,t.CsvConfigConsts=d,t.ConfigDefaults=i,t.Angular2CsvModule=s,Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=angular2-csv.umd.min.js.map