aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPriyansh <[email protected]>2022-01-22 21:57:21 -0500
committerPriyansh <[email protected]>2022-01-22 21:57:21 -0500
commit4700bb0fafd7c7b208168dda7425193bfe1eb4db (patch)
treedcf053a8ba156f24ee2ea4e121f3d77be2c7294f
parent5dfd3ad1f2fb4bbc0bf7b92ac93f59ccd78a7fc4 (diff)
downloadizuku.js-4700bb0fafd7c7b208168dda7425193bfe1eb4db.tar.xz
izuku.js-4700bb0fafd7c7b208168dda7425193bfe1eb4db.zip
feat: function to readCSV
-rw-r--r--src/index.ts3
-rw-r--r--src/lib/data.ts29
-rw-r--r--tests/data.test.ts33
-rw-r--r--tests/support/users.csv11
4 files changed, 75 insertions, 1 deletions
diff --git a/src/index.ts b/src/index.ts
index 7d49ee5..6e7bfc8 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -8,7 +8,7 @@ import {
} from './lib/locate';
import { show, head, tail } from './lib/display';
import { getSize, info } from './lib/info';
-import { flatten, fromJSON } from './lib/data';
+import { flatten, fromJSON, fromCSV } from './lib/data';
import { isArrayOfType, range, flattenJSON } from './helpers/arrayFunctions';
class Izuku {
@@ -62,6 +62,7 @@ class Izuku {
return rangeIndex(this, index);
};
public fromJSON = fromJSON;
+ public fromCSV = fromCSV;
}
class Frame extends Izuku {
diff --git a/src/lib/data.ts b/src/lib/data.ts
index 9649d6d..a89434b 100644
--- a/src/lib/data.ts
+++ b/src/lib/data.ts
@@ -1,5 +1,6 @@
import { Frame } from '../index';
import { isValidJSONObject } from '../helpers/arrayFunctions';
+import { readFileSync } from 'fs';
/**
* flattenArray - flattens a 2D into a single array
@@ -56,3 +57,31 @@ export function fromJSON(this: Frame, json: any): Frame {
throw new Error('Invalid JSON');
}
}
+
+/**
+ * fromCSV - converts a CSV string into a rowdata - framedata is a 2D array
+ * @param csv: the CSV string to be converted
+ * @returns the rowdata
+ */
+export function fromCSV(this: Frame, csvpath: string): Frame {
+ // read the csv file
+ const csv = readFileSync(csvpath, 'utf8');
+ const rowdata: any[][] = [];
+ const rows: string[] = csv.split('\n');
+ for (let i = 0; i < rows.length; i++) {
+ const row: string[] = rows[i].split(',');
+ rowdata.push(row);
+ }
+ this.rowdata = rowdata;
+ if (this.columns.length === 0) {
+ this.columns = rowdata[0];
+ }
+ // remove the first row
+ this.rowdata.shift();
+
+ // if last row contains only empty values, remove it
+ if (this.rowdata[this.rowdata.length - 1].every((item) => item === '')) {
+ this.rowdata.pop();
+ }
+ return this;
+}
diff --git a/tests/data.test.ts b/tests/data.test.ts
index 33eb56f..d922a66 100644
--- a/tests/data.test.ts
+++ b/tests/data.test.ts
@@ -1,6 +1,7 @@
import { Frame } from '../src/index';
import { expect } from 'chai';
import JSONData from './support/users.json';
+import path = require('path');
describe('data.ts', () => {
describe('load frame from json file', () => {
@@ -41,4 +42,36 @@ describe('data.ts', () => {
);
});
});
+ describe('load data from a CSV file', () => {
+ it('should load data from a CSV file', () => {
+ const csvPath = path.join(__dirname, 'support', 'users.csv');
+ const firstRow = [
+ '1',
+ 'Durante',
+ 'Toma',
+ 'Female',
+ '55.96.246.188'
+ ];
+ const lastRow = [
+ '10',
+ 'Niki',
+ 'Ruos',
+ 'Female',
+ '110.74.213.22'
+ ];
+ const header = [
+ 'id',
+ 'first_name',
+ 'last_name',
+ 'email',
+ 'gender',
+ 'ip_address'
+ ];
+ expect(new Frame().fromCSV(csvPath).rowdata[0]).to.deep.equal(firstRow);
+ expect(new Frame().fromCSV(csvPath).rowdata[9]).to.deep.equal(lastRow);
+ expect(new Frame().fromCSV(csvPath).columns).to.deep.equal(header);
+ });
+ });
});
diff --git a/tests/support/users.csv b/tests/support/users.csv
new file mode 100644
index 0000000..c2a0a15
--- /dev/null
+++ b/tests/support/users.csv
@@ -0,0 +1,11 @@
+id,first_name,last_name,email,gender,ip_address
+1,Durante,Toma,[email protected],Female,55.96.246.188
+2,Pandora,Torbeck,[email protected],Male,249.68.84.147
+3,Ibby,D'Enrico,[email protected],Male,88.109.171.160
+4,Lorne,Forrester,[email protected],Female,146.139.172.125
+5,Morie,Penright,[email protected],Male,125.156.42.128
+6,Ferguson,Atkin,[email protected],Female,131.147.95.110
+7,Barb,Nother,[email protected],Male,212.37.0.129
+8,Prudi,Aspy,[email protected],Male,141.195.250.140
+9,Mora,Blackborne,[email protected],Male,194.215.208.39
+10,Niki,Ruos,[email protected],Female,110.74.213.22