(df: NDframe | DataFrame | Series, options?: CsvOutputOptionsNode)
| 291 | * ``` |
| 292 | */ |
| 293 | const $toCSV = (df: NDframe | DataFrame | Series, options?: CsvOutputOptionsNode): string | void => { |
| 294 | let { filePath, sep, header } = { sep: ",", header: true, filePath: undefined, ...options } |
| 295 | |
| 296 | if (df.$isSeries) { |
| 297 | const csv = df.values.join(sep); |
| 298 | |
| 299 | if (filePath !== undefined) { |
| 300 | if (!(filePath.endsWith(".csv"))) { |
| 301 | filePath = filePath + ".csv" |
| 302 | } |
| 303 | fs.writeFileSync(filePath, csv, "utf8") |
| 304 | } else { |
| 305 | return csv; |
| 306 | } |
| 307 | } else { |
| 308 | const rows = df.values as ArrayType2D |
| 309 | let csvStr = header === true ? `${df.columns.join(sep)}\n` : "" |
| 310 | |
| 311 | for (let i = 0; i < rows.length; i++) { |
| 312 | const row = `${rows[i].join(sep)}\n`; |
| 313 | csvStr += row; |
| 314 | } |
| 315 | |
| 316 | if (filePath !== undefined) { |
| 317 | if (!(filePath.endsWith(".csv"))) { |
| 318 | filePath = filePath + ".csv" |
| 319 | } |
| 320 | fs.writeFileSync(filePath, csvStr, "utf8") |
| 321 | } else { |
| 322 | return csvStr; |
| 323 | } |
| 324 | } |
| 325 | }; |
| 326 | |
| 327 | |
| 328 | /** |
no test coverage detected