diff options
| author | Bobby <[email protected]> | 2022-01-28 16:54:18 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-01-28 16:54:18 -0500 |
| commit | 5aeab5e6a0e71c6a3e8ddfb02c481dac731148de (patch) | |
| tree | 365aba209616d46d33f73b6e4c9743d2e16e54b1 | |
| parent | fae750e3027a6377e9de8dafe278efce4d262df4 (diff) | |
| download | izuku.js-5aeab5e6a0e71c6a3e8ddfb02c481dac731148de.tar.xz izuku.js-5aeab5e6a0e71c6a3e8ddfb02c481dac731148de.zip | |
feat: function to save to CSV
| -rw-r--r-- | src/index.ts | 3 | ||||
| -rw-r--r-- | src/lib/export.ts | 42 | ||||
| -rw-r--r-- | tests/simple.test.ts | 2 |
3 files changed, 45 insertions, 2 deletions
diff --git a/src/index.ts b/src/index.ts index 8214328..d076567 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ import { import { show, head, tail } from './lib/display'; import { getSize, info } from './lib/info'; import { flatten, fromJSON, fromCSV, searchValue } from './lib/data'; -import { toJSON } from './lib/export'; +import { toJSON, toCSV } from './lib/export'; import { isArrayOfType, range, flattenJSON } from './helpers/arrayFunctions'; class Izuku { @@ -83,6 +83,7 @@ class Izuku { }; public title = title; public toJSON = toJSON; + public toCSV = toCSV; } class Frame extends Izuku { diff --git a/src/lib/export.ts b/src/lib/export.ts index 137e534..652f2e9 100644 --- a/src/lib/export.ts +++ b/src/lib/export.ts @@ -42,3 +42,45 @@ export function toJSON(this: Frame, path?: string, filename?: string): void { }.` ); } + +/** + * toCSV - converts frame to CSV file, takes a path to save the file. Path is optional. If no path is provided, the file is saved in the current directory + * @param frame: the frame to be converted + * @param path: @optional the path to save the file + * @param filename: @optional the name of the file + */ +export function toCSV(this: Frame, path?: string, filename?: string): void { + const data = this.rowdata; + const header = this.columns; + + // create a csv string, with keys as the header and values as the row data + const csv = data.map((row: any[]) => { + const csvRow = header.map((col: string) => { + if (row[header.indexOf(col)]) { + return row[header.indexOf(col)]; + } + return ''; + }); + return csvRow.join(','); + }); + + // add the header to the csv string + csv.unshift(header.join(',')); + + // save the csv string to a file + if (path) { + writeFileSync( + `${path}/${filename ? `${filename}` : 'data'}.csv`, + csv.join('\n') + ); + } else { + writeFileSync(`${filename ? `${filename}` : 'data'}.csv`, csv.join('\n')); + } + + // console.log the file path + console.log( + `${filename ? `${filename}` : 'data'}.csv saved at ${ + path ? path : 'root level' + }.` + ); +} diff --git a/tests/simple.test.ts b/tests/simple.test.ts index 8957d11..744be1c 100644 --- a/tests/simple.test.ts +++ b/tests/simple.test.ts @@ -5,6 +5,6 @@ const frame = new Frame(data, header); describe('Print a frame', () => { it('should print a frame', () => { - frame.title('Simple Frame').toJSON(undefined, 'hello'); + frame.title('Simple Frame').toCSV(undefined, 'hello'); }); }); |
