Skip to content

Commit 765cd99

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

File tree

7 files changed

+331
-313
lines changed

7 files changed

+331
-313
lines changed

src/danfojs-base/core/frame.ts

Lines changed: 0 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
* limitations under the License.
1313
* ==========================================================================
1414
*/
15-
import { toCSVBrowser, toExcelBrowser, toJSONBrowser } from "../io/browser";
16-
import { toCSVNode, toExcelNode, toJSONNode } from "../io/node";
1715
import dummyEncode from "../transformers/encoders/dummy.encoder";
1816
import { variance, std, median, mode, mean } from 'mathjs';
1917
import tensorflow from '../shared/tensorflowlib'
@@ -32,12 +30,6 @@ import {
3230
ArrayType2D,
3331
DataFrameInterface,
3432
BaseDataOptionType,
35-
CsvOutputOptionsBrowser,
36-
ExcelOutputOptionsBrowser,
37-
JsonOutputOptionsBrowser,
38-
CsvOutputOptionsNode,
39-
ExcelOutputOptionsNode,
40-
JsonOutputOptionsNode
4133
} from "../shared/types";
4234

4335
const utils = new Utils();
@@ -3383,157 +3375,6 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
33833375
}
33843376
}
33853377

3386-
/**
3387-
* Converts a DataFrame to CSV.
3388-
* @param options Configuration object. Supports the following options:
3389-
* - `filePath`: Local file path to write the CSV file. If not specified, the CSV will be returned as a string. Option is only available in NodeJS.
3390-
* - `fileName`: Name of the CSV file. Defaults to `data.csv`. Option is only available in Browser.
3391-
* - `download`: If true, the CSV will be downloaded. Defaults to false. Option is only available in Browser.
3392-
* - `header`: Boolean indicating whether to include a header row in the CSV file.
3393-
* - `sep`: Character to be used as a separator in the CSV file.
3394-
*
3395-
* @example
3396-
* ```
3397-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
3398-
* const csv = df.toCSV()
3399-
* console.log(csv)
3400-
* //output
3401-
* "A","B"
3402-
* 1,2
3403-
* 3,4
3404-
* ```
3405-
*
3406-
* @example
3407-
* ```
3408-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
3409-
* const csv = df.toCSV({ header: false })
3410-
* console.log(csv)
3411-
* //output
3412-
* 1,2
3413-
* 3,4
3414-
* ```
3415-
*
3416-
* @example
3417-
* ```
3418-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
3419-
* const csv = df.toCSV({ sep: ';' })
3420-
* console.log(csv)
3421-
* //output
3422-
* "A";"B"
3423-
* 1;2
3424-
* 3;4
3425-
* ```
3426-
*
3427-
* @example
3428-
* ```
3429-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
3430-
* df.toCSV({ filePath: './data.csv' }) //write to local file in NodeJS
3431-
* ```
3432-
*
3433-
* @example
3434-
* ```
3435-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
3436-
* df.toCSV({ fileName: 'data.csv', download: true }) //Downloads file in Browser
3437-
* ```
3438-
*
3439-
*/
3440-
toCSV(options?: CsvOutputOptionsBrowser | CsvOutputOptionsNode): string
3441-
toCSV(options?: CsvOutputOptionsBrowser | CsvOutputOptionsNode): string | void {
3442-
if (utils.isBrowserEnv()) {
3443-
return toCSVBrowser(this, options as CsvOutputOptionsBrowser)
3444-
} else {
3445-
return toCSVNode(this, options as CsvOutputOptionsNode)
3446-
}
3447-
}
3448-
3449-
/**
3450-
* Converts a DataFrame to JSON.
3451-
* @param options Configuration object. Supported options:
3452-
* - `filePath`: The file path to write the JSON to. If not specified, the JSON object is returned. Option is only available in NodeJS.
3453-
* - `fileName`: The name of the JSON file. Defaults to `data.json`. Option is only available in Browser.
3454-
* - `download`: If true, the JSON will be downloaded. Defaults to false. Option is only available in Browser.
3455-
* - `format`: The format of the JSON. Supported values are `'column'` and `'row'`. Defaults to `'column'`.
3456-
*
3457-
* @example
3458-
* ```
3459-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
3460-
* const json = df.toJSON()
3461-
* ```
3462-
*
3463-
* @example
3464-
* ```
3465-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
3466-
* const json = df.toJSON({ format: 'row' })
3467-
* console.log(json)
3468-
* //output
3469-
* [{"A":1,"B":2},{"A":3,"B":4}]
3470-
* ```
3471-
*
3472-
* @example
3473-
* ```
3474-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
3475-
* const json = df.toJSON({ format: "column" })
3476-
* console.log(json)
3477-
* //output
3478-
* {"A":[1,3],"B":[2,4]}
3479-
* ```
3480-
*
3481-
* @example
3482-
* ```
3483-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
3484-
* df.toJSON({ filePath: './data.json' }) // downloads to local file system as data.json in NodeJS
3485-
* ```
3486-
*
3487-
* @example
3488-
* ```
3489-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
3490-
* df.toJSON({ fileName: 'data.json', download: true }) // downloads file browser
3491-
* ```
3492-
*/
3493-
toJSON(options?: JsonOutputOptionsBrowser | JsonOutputOptionsNode): object
3494-
toJSON(options?: JsonOutputOptionsBrowser | JsonOutputOptionsNode): object | void {
3495-
if (utils.isBrowserEnv()) {
3496-
return toJSONBrowser(this, options as JsonOutputOptionsBrowser)
3497-
} else {
3498-
return toJSONNode(this, options as JsonOutputOptionsNode)
3499-
}
3500-
}
3501-
3502-
3503-
/**
3504-
* Converts a DataFrame to Excel file format.
3505-
* @param options Configuration object. Supported options:
3506-
* - `sheetName`: The sheet name to be written to. Defaults to `'Sheet1'`.
3507-
* - `filePath`: The filePath to be written to. Defaults to `'./output.xlsx'`. Option is only available in NodeJs
3508-
* - `fileName`: The fileName to be written to. Defaults to `'output.xlsx'`. Option is only available in Browser
3509-
*
3510-
* @example
3511-
* ```
3512-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
3513-
* df.toExcel({ filePath: './output.xlsx' }) // writes to local file system as output.xlsx in NodeJS
3514-
* ```
3515-
*
3516-
* @example
3517-
* ```
3518-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
3519-
* df.toExcel({ fileName: 'output.xlsx', download: true }) // downloads file browser
3520-
* ```
3521-
*
3522-
* @example
3523-
* ```
3524-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
3525-
* df.toExcel({ sheetName: 'Sheet2' }) // writes to Sheet2 in Excel
3526-
* ```
3527-
*
3528-
*/
3529-
toExcel(options?: ExcelOutputOptionsBrowser | ExcelOutputOptionsNode): void {
3530-
if (utils.isBrowserEnv()) {
3531-
toExcelBrowser(this, options as ExcelOutputOptionsBrowser)
3532-
} else {
3533-
return toExcelNode(this, options as ExcelOutputOptionsNode)
3534-
}
3535-
}
3536-
35373378
/**
35383379
* Access a single value for a row/column pair by integer position.
35393380
* Similar to {@link iloc}, in that both provide integer-based lookups.

src/danfojs-base/core/series.ts

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
* limitations under the License.
1313
* ==========================================================================
1414
*/
15-
import { toCSVBrowser, toExcelBrowser, toJSONBrowser } from "../io/browser";
16-
import { toCSVNode, toExcelNode, toJSONNode } from "../io/node";
1715
import dummyEncode from "../transformers/encoders/dummy.encoder";
1816
import { variance, std, median, mode } from 'mathjs';
1917
import tensorflow from '../shared/tensorflowlib'
@@ -2159,114 +2157,6 @@ export default class Series extends NDframe implements SeriesInterface {
21592157
}
21602158
}
21612159

2162-
/**
2163-
* Converts a Series to CSV.
2164-
* @param options Configuration object. Supports the following options:
2165-
* - `filePath`: Local file path to write the CSV file. If not specified, the CSV will be returned as a string. Option is only available in NodeJS.
2166-
* - `fileName`: Name of the CSV file. Defaults to `data.csv`. Option is only available in Browser.
2167-
* - `download`: If true, the CSV will be downloaded. Defaults to false. Option is only available in Browser.
2168-
*
2169-
* @example
2170-
* ```
2171-
* const df = new Series([1, 2, 3, 4])
2172-
* const csv = df.toCSV()
2173-
* console.log(csv)
2174-
* //output "1,2,3,4"
2175-
* ```
2176-
*
2177-
* @example
2178-
* ```
2179-
* const df = new Series([1, 2, 3, 4])
2180-
* df.toCSV({ filePath: './data.csv' }) //write to local file in NodeJS
2181-
* ```
2182-
*
2183-
* @example
2184-
* ```
2185-
* const df = new Series([1, 2, 3, 4])
2186-
* df.toCSV({ fileName: 'data.csv', download: true }) //Downloads file in Browser
2187-
* ```
2188-
*
2189-
*/
2190-
toCSV(options?: CsvOutputOptionsBrowser | CsvOutputOptionsNode): string
2191-
toCSV(options?: CsvOutputOptionsBrowser | CsvOutputOptionsNode): string | void {
2192-
if (utils.isBrowserEnv()) {
2193-
return toCSVBrowser(this, options as CsvOutputOptionsBrowser)
2194-
} else {
2195-
return toCSVNode(this, options as CsvOutputOptionsNode)
2196-
}
2197-
}
2198-
2199-
/**
2200-
* Converts a Series to JSON.
2201-
* @param options Configuration object. Supported options:
2202-
* - `filePath`: The file path to write the JSON to. If not specified, the JSON object is returned. Option is only available in NodeJS.
2203-
* - `fileName`: The name of the JSON file. Defaults to `data.json`. Option is only available in Browser.
2204-
* - `download`: If true, the JSON will be downloaded. Defaults to false. Option is only available in Browser.
2205-
*
2206-
* @example
2207-
* ```
2208-
* const df = new Series([[1, 2, 3, 4]], { columns: ['A']})
2209-
* const json = df.toJSON()
2210-
* console.log(json)
2211-
* //output { A: [ '1,2,3,4' ] }
2212-
* ```
2213-
*
2214-
* @example
2215-
* ```
2216-
* const df = new Series([1, 2, 3, 4])
2217-
* df.toJSON({ filePath: './data.json' }) // downloads to local file system as data.json in NodeJS
2218-
* ```
2219-
*
2220-
* @example
2221-
* ```
2222-
* const df = new Series([1, 2, 3, 4])
2223-
* df.toJSON({ fileName: 'data.json', download: true }) // downloads file browser
2224-
* ```
2225-
*/
2226-
toJSON(options?: JsonOutputOptionsBrowser | JsonOutputOptionsNode): object
2227-
toJSON(options?: JsonOutputOptionsBrowser | JsonOutputOptionsNode): object | void {
2228-
if (utils.isBrowserEnv()) {
2229-
return toJSONBrowser(this, options as JsonOutputOptionsBrowser)
2230-
} else {
2231-
return toJSONNode(this, options as JsonOutputOptionsNode)
2232-
}
2233-
}
2234-
2235-
2236-
/**
2237-
* Converts a Series to Excel file format.
2238-
* @param options Configuration object. Supported options:
2239-
* - `sheetName`: The sheet name to be written to. Defaults to `'Sheet1'`.
2240-
* - `filePath`: The filePath to be written to. Defaults to `'./output.xlsx'`. Option is only available in NodeJs
2241-
* - `fileName`: The fileName to be written to. Defaults to `'output.xlsx'`. Option is only available in Browser
2242-
*
2243-
* @example
2244-
* ```
2245-
* const df = new Series([1, 2, 3, 4])
2246-
* df.toExcel({ filePath: './output.xlsx' }) // writes to local file system as output.xlsx in NodeJS
2247-
* ```
2248-
*
2249-
* @example
2250-
* ```
2251-
* const df = new Series([1, 2, 3, 4])
2252-
* df.toExcel({ fileName: 'output.xlsx', download: true }) // downloads file browser
2253-
* ```
2254-
*
2255-
* @example
2256-
* ```
2257-
* const df = new Series([1, 2, 3, 4])
2258-
* df.toExcel({ sheetName: 'Sheet2' }) // writes to Sheet2 in Excel
2259-
* ```
2260-
*
2261-
*/
2262-
toExcel(options?: ExcelOutputOptionsBrowser | ExcelOutputOptionsNode): void {
2263-
if (utils.isBrowserEnv()) {
2264-
toExcelBrowser(this, options as ExcelOutputOptionsBrowser)
2265-
} else {
2266-
return toExcelNode(this, options as ExcelOutputOptionsNode)
2267-
}
2268-
}
2269-
22702160
/**
22712161
* Access a single value for a row index.
22722162
* Similar to iloc, in that both provide index-based lookups.

src/danfojs-base/shared/types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,6 @@ export interface SeriesInterface extends NDframeInterface {
181181
inplace?: boolean
182182
}): DataFrame
183183
plot(divId: string): IPlotlyLib
184-
toCSV(options?: CsvOutputOptionsBrowser): string | void
185-
toJSON(options?: JsonOutputOptionsBrowser): object | void
186-
toExcel(options?: ExcelOutputOptionsBrowser): void
187184
iat(index: number): number | string | boolean | undefined
188185
at(index: string | number): number | string | boolean | undefined
189186
}
@@ -329,9 +326,6 @@ export interface DataFrameInterface extends NDframeInterface {
329326
inplace?: boolean
330327
}): DataFrame | void
331328
plot(divId: string): IPlotlyLib
332-
toCSV(options?: CsvOutputOptionsBrowser): string | void
333-
toJSON(options?: JsonOutputOptionsBrowser): object | void
334-
toExcel(options?: ExcelOutputOptionsBrowser): void
335329
iat(row: number, column: number): number | string | boolean | undefined
336330
at(row: string | number, column: string): number | string | boolean | undefined
337331
}

src/danfojs-node/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@
2626
"request": "^2.88.2",
2727
"seedrandom": "^2.4.3",
2828
"stream-json": "^1.7.3",
29-
"table": "6.7.1",
30-
"xlsx": "^0.17.2",
31-
"plotly.js-dist-min": "^2.8.0"
29+
"table": "6.7.1"
30+
},
31+
"peerDependencies": {
32+
"plotly.js-dist-min": "^2.8.0",
33+
"xlsx": "0.17.2"
3234
},
3335
"scripts": {
3436
"test": "nyc mocha --require ts-node/register test/**/*.test.ts",
3537
"test:clean": "yarn build:clean && yarn test",
3638
"dev": "nodemon",
3739
"build": "tsc",
38-
"build:clean": "rimraf ./build && node ./scripts/prebuild.js && tsc",
40+
"build:clean": "rm -rf ./dist && node ./scripts/prebuild.js && tsc",
3941
"lint": "eslint ./src",
4042
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
4143
"coverage": "nyc report --reporter=text-lcov | coveralls && nyc report --reporter=lcov",

0 commit comments

Comments
 (0)