├── .gitignore ├── src ├── index.ts ├── core │ ├── IFileExporter.ts │ └── BaseBuilder.ts └── csv-builder │ └── CsvBuilder.ts ├── .travis.yml ├── tsconfig.json ├── package.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | node_modules 3 | package-lock.json -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export { default as CsvBuilder } from './csv-builder/CsvBuilder'; -------------------------------------------------------------------------------- /src/core/IFileExporter.ts: -------------------------------------------------------------------------------- 1 | export default interface IFileExporter { 2 | exportFile(): void; 3 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | node_js: 4 | - 10 5 | install: 6 | - npm install 7 | script: 8 | - npm run build 9 | deploy: 10 | provider: npm 11 | email: "mehmetbaran@mehmetbaran.net" 12 | api_key: "$NPM_TOKEN" 13 | skip_cleanup: true 14 | on: 15 | branch: master -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "outDir": "./dist", 4 | "target": "es5", 5 | "module": "commonjs", 6 | "declaration": true, 7 | "strict": true, 8 | "esModuleInterop": true 9 | }, 10 | "include": [ 11 | "index.ts", 12 | "src/**/*" 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /src/core/BaseBuilder.ts: -------------------------------------------------------------------------------- 1 | export default class BaseBuilder { 2 | protected exportFile(dataType: string, fileName: string, data: string) { 3 | if (window.navigator.msSaveOrOpenBlob) { 4 | var blob = new Blob([data]); 5 | window.navigator.msSaveOrOpenBlob(blob, fileName); 6 | } else { 7 | var charBom = "\uFEFF"; 8 | var encodedData = encodeURIComponent(data); 9 | let content = `data:text/${dataType};charset=utf-8,${charBom}${encodedData}`; 10 | 11 | var link = document.createElement("a"); 12 | link.setAttribute("href", content); 13 | link.setAttribute("download", fileName); 14 | document.body.appendChild(link); 15 | 16 | link.click(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "filefy", 3 | "version": "0.1.11", 4 | "description": "A javascript library to produce downloadable files sucs as in CSV, PDF, XLSX, DOCX formats", 5 | "main": "./dist/index.js", 6 | "homepage": "https://github.com/mbrn/filefy#readme", 7 | "author": "Mehmet Baran", 8 | "license": "MIT", 9 | "files": [ 10 | "dist" 11 | ], 12 | "scripts": { 13 | "build": "tsc" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "git+https://github.com/mbrn/filefy.git" 18 | }, 19 | "keywords": [ 20 | "javascript", 21 | "file", 22 | "export", 23 | "csv", 24 | "pdf", 25 | "xlsx" 26 | ], 27 | "bugs": { 28 | "url": "https://github.com/mbrn/filefy/issues" 29 | }, 30 | "devDependencies": { 31 | "typescript": "3.1.3" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Mehmet Baran 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # filefy 2 | [![Build Status](https://travis-ci.org/mbrn/filefy.svg?branch=master)](https://travis-ci.org/mbrn/filefy) 3 | [![npm package](https://img.shields.io/npm/v/filefy/latest.svg)](https://www.npmjs.com/package/filefy) 4 | [![NPM Downloads](https://img.shields.io/npm/dt/filefy.svg?style=flat)](https://npmcharts.com/compare/filefy?minimal=true) 5 | [![Follow on Twitter](https://img.shields.io/twitter/follow/baranmehmet.svg?label=follow+baranmehmet)](https://twitter.com/baranmehmet) 6 | 7 | 8 | A javascript library to produce downloadable files such as in CSV, PDF, XLSX, DOCX formats 9 | 10 | > Only CSV export is available for now! 11 | 12 | ## Installation 13 | 14 | To install filefy with `npm`: 15 | 16 | npm install --save filefy 17 | 18 | To install filefy with `yarn`: 19 | 20 | yarn add filefy 21 | 22 | ## Usage 23 | 24 | ```js 25 | import { CsvBuilder } from 'filefy'; 26 | 27 | var csvBuilder = new CsvBuilder("user_list.csv") 28 | .setColumns(["name", "surname"]) 29 | .addRow(["Eve", "Holt"]) 30 | .addRows([ 31 | ["Charles", "Morris"], 32 | ["Tracey", "Ramos"] 33 | ]) 34 | .exportFile(); 35 | ``` 36 | 37 | ## Licence 38 | 39 | This project is licensed under the terms of the [MIT license](/LICENSE). 40 | -------------------------------------------------------------------------------- /src/csv-builder/CsvBuilder.ts: -------------------------------------------------------------------------------- 1 | import IFileExporter from '../core/IFileExporter'; 2 | import BaseBuilder from '../core/BaseBuilder'; 3 | 4 | export default class CsvBuilder extends BaseBuilder implements IFileExporter { 5 | private _FileName: string = ''; 6 | private _Delimeter: string = ','; 7 | private _Columns: string[] = []; 8 | private _RowData: string[][] = []; 9 | 10 | constructor(fileName: string) { 11 | super(); 12 | this._FileName = fileName; 13 | } 14 | 15 | public setColumns(columns: string[]): CsvBuilder { 16 | this._Columns = columns; 17 | return this; 18 | } 19 | 20 | public setDelimeter(delimeter: string): CsvBuilder { 21 | this._Delimeter = delimeter; 22 | return this; 23 | } 24 | 25 | public addRow(row: string[]): CsvBuilder { 26 | this._RowData.push(row); 27 | return this; 28 | } 29 | 30 | public addRows(rows: string[][]): CsvBuilder { 31 | this._RowData = [...this._RowData, ...rows]; 32 | return this; 33 | } 34 | 35 | private escapeCell(cellData: string): string { 36 | if(typeof cellData === 'string') { 37 | return '"' + cellData.replace(/\"/g, '""') + '"'; 38 | } 39 | return cellData; 40 | } 41 | 42 | private getRowData(row: string[]): string { 43 | return row.map(this.escapeCell).join(this._Delimeter); 44 | } 45 | 46 | public exportFile(): void { 47 | let dataArray:string[] = []; 48 | if(this._Columns && this._Columns.length > 0) { 49 | dataArray.push(this.getRowData(this._Columns)); 50 | } 51 | 52 | this._RowData.forEach(row => { 53 | dataArray.push(this.getRowData(row)); 54 | }); 55 | 56 | const csvContent = dataArray.join("\r\n"); 57 | super.exportFile('csv', this._FileName, csvContent); 58 | } 59 | } 60 | --------------------------------------------------------------------------------