-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(csv): add formatCSV(), types, tests
- Loading branch information
1 parent
4987e1d
commit a2ca1b6
Showing
7 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import type { Nullable } from "@thi.ng/api"; | ||
import { isArray } from "@thi.ng/checks/is-array"; | ||
import { isIterable } from "@thi.ng/checks/is-iterable"; | ||
import type { Stringer } from "@thi.ng/strings"; | ||
import { wrap } from "@thi.ng/strings/wrap"; | ||
import type { Reducer, Transducer } from "@thi.ng/transducers"; | ||
import { compR } from "@thi.ng/transducers/func/compr"; | ||
import { iterator } from "@thi.ng/transducers/iterator"; | ||
import { isReduced } from "@thi.ng/transducers/reduced"; | ||
import { str } from "@thi.ng/transducers/rfn/str"; | ||
import { transduce } from "@thi.ng/transducers/transduce"; | ||
import type { CSVRecord, CSVRow, CSVFormatOpts } from "./api"; | ||
|
||
export function formatCSV( | ||
opts?: Partial<CSVFormatOpts> | ||
): Transducer<CSVRow | CSVRecord, string>; | ||
export function formatCSV( | ||
opts: Partial<CSVFormatOpts>, | ||
src: Iterable<CSVRow | CSVRecord> | ||
): IterableIterator<string>; | ||
export function formatCSV( | ||
opts?: Partial<CSVFormatOpts>, | ||
src?: Iterable<CSVRow | CSVRecord> | ||
): any { | ||
return isIterable(src) | ||
? iterator(formatCSV(opts), src) | ||
: (rfn: Reducer<any, string>) => { | ||
let { header, cols, delim, quote } = { | ||
delim: ",", | ||
quote: `"`, | ||
cols: [], | ||
...opts, | ||
}; | ||
let colTx: Nullable<Stringer<any>>[]; | ||
const reQuote = new RegExp(quote, "g"); | ||
const reduce = rfn[2]; | ||
let headerDone = false; | ||
return compR(rfn, (acc, row: CSVRow | CSVRecord) => { | ||
if (!headerDone) { | ||
if (!header && !isArray(row)) { | ||
header = Object.keys(row); | ||
} | ||
colTx = isArray(cols) | ||
? cols | ||
: header | ||
? header.map( | ||
(id) => | ||
(<Record<string, Stringer<any>>>cols)[id] | ||
) | ||
: []; | ||
} | ||
const $row = isArray(row) | ||
? row | ||
: header!.map((k) => (<CSVRecord>row)[k]); | ||
const line = (header || $row) | ||
.map((_, i) => { | ||
const val = $row[i]; | ||
const cell = | ||
val != null | ||
? colTx[i] | ||
? colTx[i]!(val) | ||
: String(val) | ||
: ""; | ||
return cell.indexOf(quote) !== -1 | ||
? wrap(quote)( | ||
cell.replace(reQuote, `${quote}${quote}`) | ||
) | ||
: cell; | ||
}) | ||
.join(delim); | ||
if (!headerDone) { | ||
if (header) { | ||
acc = reduce(acc, header.join(delim)); | ||
} else { | ||
header = $row; | ||
} | ||
headerDone = true; | ||
!isReduced(acc) && (acc = reduce(acc, line)); | ||
return acc; | ||
} else { | ||
return reduce(acc, line); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
export const formatCSVString = ( | ||
opts: Partial<CSVFormatOpts & { rowDelim: string }> = {}, | ||
src: Iterable<CSVRow> | ||
) => transduce(formatCSV(opts), str(opts.rowDelim || "\n"), src); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./api"; | ||
export * from "./format"; | ||
export * from "./parse"; | ||
export * from "./transforms"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { group } from "@thi.ng/testament"; | ||
import * as assert from "assert"; | ||
import { formatCSV, formatFloat, zeroPad } from "../src"; | ||
|
||
group("format array", { | ||
header: () => | ||
assert.deepStrictEqual( | ||
[...formatCSV({ header: ["a", "b"] }, [[1, 2]])], | ||
["a,b", "1,2"] | ||
), | ||
|
||
"no header": () => | ||
assert.deepStrictEqual([...formatCSV({}, [[1, 2]])], ["1,2"]), | ||
|
||
tx: () => | ||
assert.deepStrictEqual( | ||
[...formatCSV({ cols: [null, formatFloat(2)] }, [[1, 2]])], | ||
["1,2.00"] | ||
), | ||
}); | ||
|
||
group("format obj", { | ||
header: () => | ||
assert.deepStrictEqual( | ||
[...formatCSV({ header: ["a", "b"] }, [{ a: 1, b: 2 }])], | ||
["a,b", "1,2"] | ||
), | ||
|
||
"no header": () => | ||
assert.deepStrictEqual( | ||
[...formatCSV({}, [{ a: 1, b: 2 }])], | ||
["a,b", "1,2"] | ||
), | ||
|
||
tx: () => | ||
assert.deepStrictEqual( | ||
[ | ||
...formatCSV({ cols: { a: zeroPad(4), b: formatFloat(2) } }, [ | ||
{ a: 1, b: 2 }, | ||
]), | ||
], | ||
["a,b", "0001,2.00"] | ||
), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters