Skip to content

Commit d4eb0a6

Browse files
committed
Refactor io output functions into browser folder-getting rid of node related methods
1 parent 765cd99 commit d4eb0a6

6 files changed

Lines changed: 329 additions & 467 deletions

File tree

src/danfojs-base/io/browser/io.excel.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ import {
1919
ExcelInputOptionsBrowser
2020
} from "../../shared/types"
2121
import { DataFrame, NDframe, Series } from '../../'
22-
import XLSX from 'xlsx';
22+
23+
let XLSX: any;
24+
25+
try {
26+
XLSX = require("xlsx");
27+
} catch (err) {
28+
console.info(`xlsx not found. Please run "npm install xlsx" or "yarn add xlsx" in order to work with Excel files.`)
29+
30+
}
2331

2432
/**
2533
* Reads a JSON file from local or remote location into a DataFrame.
@@ -85,13 +93,13 @@ const $readExcel = async (file: any, options?: ExcelInputOptionsBrowser) => {
8593
* @param df DataFrame or Series to be converted to JSON.
8694
* @param options Configuration object. Supported options:
8795
* - `sheetName`: The sheet name to be written to. Defaults to `'Sheet1'`.
88-
* - `file`: The file to be written to. Defaults to `'./output.xlsx'`.
96+
* - `fileName`: The file to be written to. Defaults to `'./output.xlsx'`.
8997
* @example
9098
* ```
9199
* import { toExcel } from "danfojs-node"
92100
* const df = new DataFrame([[1, 2, 3], [4, 5, 6]])
93101
* toExcel(df, {
94-
* file: "./data/sample.xlsx",
102+
* fileName: "./data/sample.xlsx",
95103
* sheetName: "MySheet",
96104
* })
97105
* ```

src/danfojs-browser/package.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"description": "JavaScript library providing high performance, intuitive, and easy to use data structures for manipulating and processing structured data.",
55
"main": "dist/danfojs-browser/src/index.js",
66
"types": "dist/danfojs-browser/src/index.d.ts",
7-
"module": "lib/bundle-esm.js",
87
"directories": {
98
"test": "tests"
109
},
@@ -24,12 +23,11 @@
2423
"@tensorflow/tfjs": "^3.13.0",
2524
"mathjs": "9.4.4",
2625
"papaparse": "^5.3.1",
27-
"plotly.js-dist-min": "^2.8.0",
28-
"request": "^2.88.2",
29-
"stream-json": "^1.7.3",
30-
"table": "6.7.1",
31-
"xlsx": "^0.17.2",
32-
"seedrandom": "^2.4.3"
26+
"table": "6.7.1"
27+
},
28+
"peerDependencies": {
29+
"plotly.js-dist-min": "2.8.0",
30+
"xlsx": "0.17.2"
3331
},
3432
"scripts": {
3533
"test": "karma start --single-run --browsers ChromeHeadless karma.conf.js",
@@ -111,4 +109,4 @@
111109
]
112110
},
113111
"sideEffects": false
114-
}
112+
}

src/danfojs-browser/src/core/frame.ts

Lines changed: 140 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,21 @@
1212
* limitations under the License.
1313
* ==========================================================================
1414
*/
15-
import { BaseDataOptionType } from "../../../danfojs-base/shared/types";
1615
import BaseDataFrame from "../../../danfojs-base/core/frame"
16+
import { toCSVBrowser, toJSONBrowser, toExcelBrowser } from "../../../danfojs-base/io/browser";
17+
import {
18+
BaseDataOptionType,
19+
DataFrameInterface,
20+
CsvOutputOptionsBrowser,
21+
JsonOutputOptionsBrowser,
22+
ExcelOutputOptionsBrowser
23+
} from "../../../danfojs-base/shared/types";
1724

25+
type ExtendedDataFrameInterface = DataFrameInterface & {
26+
toCSV(options?: CsvOutputOptionsBrowser): string | void
27+
toJSON(options?: JsonOutputOptionsBrowser): object | void
28+
toExcel(options?: ExcelOutputOptionsBrowser): void
29+
}
1830

1931
/**
2032
* Two-dimensional ndarray with axis labels.
@@ -26,9 +38,135 @@ import BaseDataFrame from "../../../danfojs-base/core/frame"
2638
* @param options.dtypes Array of data types for each the column. If not specified, dtypes are/is inferred.
2739
* @param options.config General configuration object for extending or setting NDframe behavior.
2840
*/
29-
export default class DataFrame extends BaseDataFrame {
41+
export default class DataFrame extends BaseDataFrame implements ExtendedDataFrameInterface {
3042
[key: string]: any
3143
constructor(data?: any, options: BaseDataOptionType = {}) {
3244
super(data, options)
3345
}
46+
47+
/**
48+
* Converts a DataFrame to CSV.
49+
* @param options Configuration object. Supports the following options:
50+
* - `fileName`: Name of the CSV file. Defaults to `data.csv`. Option is only available in Browser.
51+
* - `download`: If true, the CSV will be downloaded. Defaults to false. Option is only available in Browser.
52+
* - `header`: Boolean indicating whether to include a header row in the CSV file.
53+
* - `sep`: Character to be used as a separator in the CSV file.
54+
*
55+
* @example
56+
* ```
57+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
58+
* const csv = df.toCSV()
59+
* console.log(csv)
60+
* //output
61+
* "A","B"
62+
* 1,2
63+
* 3,4
64+
* ```
65+
*
66+
* @example
67+
* ```
68+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
69+
* const csv = df.toCSV({ header: false })
70+
* console.log(csv)
71+
* //output
72+
* 1,2
73+
* 3,4
74+
* ```
75+
*
76+
* @example
77+
* ```
78+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
79+
* const csv = df.toCSV({ sep: ';' })
80+
* console.log(csv)
81+
* //output
82+
* "A";"B"
83+
* 1;2
84+
* 3;4
85+
* ```
86+
*
87+
* @example
88+
* ```
89+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
90+
* df.toCSV({ fileName: 'data.csv', download: true }) //Downloads file in Browser
91+
* ```
92+
*
93+
*/
94+
toCSV(options?: CsvOutputOptionsBrowser): string
95+
toCSV(options?: CsvOutputOptionsBrowser): string | void {
96+
return toCSVBrowser(this, options)
97+
98+
}
99+
100+
/**
101+
* Converts a DataFrame to JSON.
102+
* @param options Configuration object. Supported options:
103+
* - `fileName`: The name of the JSON file. Defaults to `data.json`. Option is only available in Browser.
104+
* - `download`: If true, the JSON will be downloaded. Defaults to false. Option is only available in Browser.
105+
* - `format`: The format of the JSON. Supported values are `'column'` and `'row'`. Defaults to `'column'`.
106+
*
107+
* @example
108+
* ```
109+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
110+
* const json = df.toJSON()
111+
* ```
112+
*
113+
* @example
114+
* ```
115+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
116+
* const json = df.toJSON({ format: 'row' })
117+
* console.log(json)
118+
* //output
119+
* [{"A":1,"B":2},{"A":3,"B":4}]
120+
* ```
121+
*
122+
* @example
123+
* ```
124+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
125+
* const json = df.toJSON({ format: "column" })
126+
* console.log(json)
127+
* //output
128+
* {"A":[1,3],"B":[2,4]}
129+
* ```
130+
*
131+
* @example
132+
* ```
133+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
134+
* df.toJSON({ fileName: 'data.json', download: true }) // downloads file browser
135+
* ```
136+
*/
137+
toJSON(options?: JsonOutputOptionsBrowser): object
138+
toJSON(options?: JsonOutputOptionsBrowser): object | void {
139+
return toJSONBrowser(this, options)
140+
}
141+
142+
143+
/**
144+
* Converts a DataFrame to Excel file format.
145+
* @param options Configuration object. Supported options:
146+
* - `sheetName`: The sheet name to be written to. Defaults to `'Sheet1'`.
147+
* - `filePath`: The filePath to be written to. Defaults to `'./output.xlsx'`. Option is only available in NodeJs
148+
* - `fileName`: The fileName to be written to. Defaults to `'output.xlsx'`. Option is only available in Browser
149+
*
150+
* @example
151+
* ```
152+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
153+
* df.toExcel({ filePath: './output.xlsx' }) // writes to local file system as output.xlsx in NodeJS
154+
* ```
155+
*
156+
* @example
157+
* ```
158+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
159+
* df.toExcel({ fileName: 'output.xlsx', download: true }) // downloads file browser
160+
* ```
161+
*
162+
* @example
163+
* ```
164+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
165+
* df.toExcel({ sheetName: 'Sheet2' }) // writes to Sheet2 in Excel
166+
* ```
167+
*
168+
*/
169+
toExcel(options?: ExcelOutputOptionsBrowser): void {
170+
return toExcelBrowser(this, options)
171+
}
34172
}

src/danfojs-browser/src/core/series.ts

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,21 @@
1212
* limitations under the License.
1313
* ==========================================================================
1414
*/
15-
import { BaseDataOptionType } from "../../../danfojs-base/shared/types";
1615
import BaseSeries from "../../../danfojs-base/core/series"
16+
import { toCSVBrowser, toJSONBrowser, toExcelBrowser } from "../../../danfojs-base/io/browser";
17+
import {
18+
BaseDataOptionType,
19+
SeriesInterface,
20+
CsvOutputOptionsBrowser,
21+
JsonOutputOptionsBrowser,
22+
ExcelOutputOptionsBrowser,
23+
} from "../../../danfojs-base/shared/types";
24+
25+
type ExtendedSeriesInterface = SeriesInterface & {
26+
toCSV(options?: CsvOutputOptionsBrowser): string | void
27+
toJSON(options?: JsonOutputOptionsBrowser): object | void
28+
toExcel(options?: ExcelOutputOptionsBrowser): void
29+
}
1730

1831

1932
/**
@@ -26,9 +39,135 @@ import BaseSeries from "../../../danfojs-base/core/series"
2639
* @param options.dtypes Data types of the Series data. If not specified, dtypes is inferred.
2740
* @param options.config General configuration object for extending or setting Series behavior.
2841
*/
29-
export default class Series extends BaseSeries {
42+
export default class Series extends BaseSeries implements ExtendedSeriesInterface {
3043
[key: string]: any
3144
constructor(data?: any, options: BaseDataOptionType = {}) {
3245
super(data, options)
3346
}
47+
48+
/**
49+
* Converts a Series to CSV.
50+
* @param options Configuration object. Supports the following options:
51+
* - `fileName`: Name of the CSV file. Defaults to `data.csv`. Option is only available in Browser.
52+
* - `download`: If true, the CSV will be downloaded. Defaults to false. Option is only available in Browser.
53+
* - `header`: Boolean indicating whether to include a header row in the CSV file.
54+
* - `sep`: Character to be used as a separator in the CSV file.
55+
*
56+
* @example
57+
* ```
58+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
59+
* const csv = df.toCSV()
60+
* console.log(csv)
61+
* //output
62+
* "A","B"
63+
* 1,2
64+
* 3,4
65+
* ```
66+
*
67+
* @example
68+
* ```
69+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
70+
* const csv = df.toCSV({ header: false })
71+
* console.log(csv)
72+
* //output
73+
* 1,2
74+
* 3,4
75+
* ```
76+
*
77+
* @example
78+
* ```
79+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
80+
* const csv = df.toCSV({ sep: ';' })
81+
* console.log(csv)
82+
* //output
83+
* "A";"B"
84+
* 1;2
85+
* 3;4
86+
* ```
87+
*
88+
* @example
89+
* ```
90+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
91+
* df.toCSV({ fileName: 'data.csv', download: true }) //Downloads file in Browser
92+
* ```
93+
*
94+
*/
95+
toCSV(options?: CsvOutputOptionsBrowser): string
96+
toCSV(options?: CsvOutputOptionsBrowser): string | void {
97+
return toCSVBrowser(this, options)
98+
99+
}
100+
101+
/**
102+
* Converts a DataFrame to JSON.
103+
* @param options Configuration object. Supported options:
104+
* - `fileName`: The name of the JSON file. Defaults to `data.json`. Option is only available in Browser.
105+
* - `download`: If true, the JSON will be downloaded. Defaults to false. Option is only available in Browser.
106+
* - `format`: The format of the JSON. Supported values are `'column'` and `'row'`. Defaults to `'column'`.
107+
*
108+
* @example
109+
* ```
110+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
111+
* const json = df.toJSON()
112+
* ```
113+
*
114+
* @example
115+
* ```
116+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
117+
* const json = df.toJSON({ format: 'row' })
118+
* console.log(json)
119+
* //output
120+
* [{"A":1,"B":2},{"A":3,"B":4}]
121+
* ```
122+
*
123+
* @example
124+
* ```
125+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
126+
* const json = df.toJSON({ format: "column" })
127+
* console.log(json)
128+
* //output
129+
* {"A":[1,3],"B":[2,4]}
130+
* ```
131+
*
132+
* @example
133+
* ```
134+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
135+
* df.toJSON({ fileName: 'data.json', download: true }) // downloads file browser
136+
* ```
137+
*/
138+
toJSON(options?: JsonOutputOptionsBrowser): object
139+
toJSON(options?: JsonOutputOptionsBrowser): object | void {
140+
return toJSONBrowser(this, options)
141+
}
142+
143+
144+
/**
145+
* Converts a DataFrame to Excel file format.
146+
* @param options Configuration object. Supported options:
147+
* - `sheetName`: The sheet name to be written to. Defaults to `'Sheet1'`.
148+
* - `filePath`: The filePath to be written to. Defaults to `'./output.xlsx'`. Option is only available in NodeJs
149+
* - `fileName`: The fileName to be written to. Defaults to `'output.xlsx'`. Option is only available in Browser
150+
*
151+
* @example
152+
* ```
153+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
154+
* df.toExcel({ filePath: './output.xlsx' }) // writes to local file system as output.xlsx in NodeJS
155+
* ```
156+
*
157+
* @example
158+
* ```
159+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
160+
* df.toExcel({ fileName: 'output.xlsx', download: true }) // downloads file browser
161+
* ```
162+
*
163+
* @example
164+
* ```
165+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
166+
* df.toExcel({ sheetName: 'Sheet2' }) // writes to Sheet2 in Excel
167+
* ```
168+
*
169+
*/
170+
toExcel(options?: ExcelOutputOptionsBrowser): void {
171+
return toExcelBrowser(this, options)
172+
}
34173
}

0 commit comments

Comments
 (0)