-
Notifications
You must be signed in to change notification settings - Fork 655
fix(csv): add columns
options type for array items
#6363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f5985b5
initial commit
timreichen 58be2c7
fix jsdoc and exports
timreichen 829759e
Merge branch 'main' into csv-columns-options-type
timreichen b0c9723
update
timreichen 1976a2d
Merge branch 'main' into csv-columns-options-type
timreichen 076735e
update
timreichen 57af2e2
Merge branch 'main' into csv-columns-options-type
timreichen 72b06e9
Merge branch 'main' into csv-columns-options-type
timreichen 0a2e529
Merge branch 'main' into csv-columns-options-type
timreichen 6249b87
Merge branch 'main' into csv-columns-options-type
timreichen e9ab541
Merge branch 'main' into csv-columns-options-type
timreichen 628b59d
update
timreichen 78e76b5
Merge branch 'main' into csv-columns-options-type
timreichen 5f89e9b
Merge branch 'main' into csv-columns-options-type
timreichen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -85,11 +85,43 @@ export type ColumnDetails = { | |
*/ | ||
export type Column = ColumnDetails | PropertyAccessor | PropertyAccessor[]; | ||
|
||
/** An object (plain or array) */ | ||
export type DataItem = Readonly<Record<string, unknown> | unknown[]>; | ||
|
||
/** Options for {@linkcode stringify}. */ | ||
export type StringifyOptions = { | ||
/** Options for {@linkcode stringify} with an array of of arrays as data. */ | ||
export type ArrayStringifyOptions = { | ||
/** Whether to include the row of headers or not. | ||
* | ||
* @default {true} | ||
*/ | ||
headers?: boolean; | ||
/** | ||
* Delimiter used to separate values. Examples: | ||
* - `","` _comma_ | ||
* - `"\t"` _tab_ | ||
* - `"|"` _pipe_ | ||
* - etc. | ||
* | ||
* @default {","} | ||
*/ | ||
separator?: string; | ||
/** | ||
* A list of instructions for how to target and transform the data for each | ||
* column of output. This is also where you can provide an explicit header | ||
* name for the column. | ||
* | ||
* @default {undefined} | ||
*/ | ||
columns?: readonly number[] | undefined; | ||
/** | ||
* Whether to add a | ||
* {@link https://en.wikipedia.org/wiki/Byte_order_mark | byte-order mark} to the | ||
* beginning of the file content. Required by software such as MS Excel to | ||
* properly display Unicode text. | ||
* | ||
* @default {false} | ||
*/ | ||
bom?: boolean; | ||
}; | ||
/** Options for {@linkcode stringify} with an array of of objects as data. */ | ||
export type ObjectStringifyOptions = { | ||
/** Whether to include the row of headers or not. | ||
* | ||
* @default {true} | ||
|
@@ -123,21 +155,23 @@ export type StringifyOptions = { | |
*/ | ||
bom?: boolean; | ||
}; | ||
/** Options for {@linkcode stringify}. */ | ||
export type StringifyOptions = ArrayStringifyOptions | ObjectStringifyOptions; | ||
|
||
/** | ||
* Converts an array of objects into a CSV string. | ||
* | ||
* @example Default options | ||
* @example Give an array of objects without specifying columns | ||
* ```ts | ||
* import { stringify } from "@std/csv/unstable-stringify"; | ||
* import { assertEquals } from "@std/assert/equals"; | ||
* | ||
* const data = [ | ||
* ["Rick", 70], | ||
* ["Morty", 14], | ||
* { name: "Rick", age: 70 }, | ||
* { name: "Morty", age: 14 }, | ||
* ]; | ||
* | ||
* assertEquals(stringify(data), `Rick,70\r\nMorty,14\r\n`); | ||
* assertEquals(stringify(data), `name,age\r\nRick,70\r\nMorty,14\r\n`); | ||
* ``` | ||
* | ||
* @example Give an array of objects and specify columns | ||
|
@@ -155,19 +189,6 @@ export type StringifyOptions = { | |
* assertEquals(stringify(data, { columns }), `name,age\r\nRick,70\r\nMorty,14\r\n`); | ||
* ``` | ||
* | ||
* @example Give an array of objects without specifying columns | ||
* ```ts | ||
* import { stringify } from "@std/csv/unstable-stringify"; | ||
* import { assertEquals } from "@std/assert/equals"; | ||
* | ||
* const data = [ | ||
* { name: "Rick", age: 70 }, | ||
* { name: "Morty", age: 14 }, | ||
* ]; | ||
* | ||
* assertEquals(stringify(data), `name,age\r\nRick,70\r\nMorty,14\r\n`); | ||
* ``` | ||
* | ||
* @example Give an array of objects and specify columns with `headers: false` | ||
* ```ts | ||
* import { stringify } from "@std/csv/unstable-stringify"; | ||
|
@@ -178,10 +199,8 @@ export type StringifyOptions = { | |
* { name: "Morty", age: 14 }, | ||
* ]; | ||
* | ||
* const columns = ["name", "age"]; | ||
* | ||
* assertEquals( | ||
* stringify(data, { columns, headers: false }), | ||
* stringify(data, { headers: false }), | ||
* `Rick,70\r\nMorty,14\r\n`, | ||
* ); | ||
* ``` | ||
|
@@ -280,9 +299,21 @@ export type StringifyOptions = { | |
* ); | ||
* ``` | ||
* | ||
* @param data The source data to stringify. It's an array of items which are | ||
* plain objects or arrays. | ||
* @param options Options for the stringification. | ||
* @returns A CSV string. | ||
*/ | ||
export function stringify( | ||
data: Record<string, unknown>[], | ||
options?: ObjectStringifyOptions, | ||
): string; | ||
/** | ||
* Converts an array of arrays into a CSV string. | ||
* | ||
* @example Give an array of string arrays and specify columns with renaming | ||
* ```ts | ||
* import { stringify } from "@std/csv/unstable-stringify"; | ||
* import { stringify } from "@std/csv/stringify"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this example doesn't pass type checking anymore. Why do you change the specifier here to the stable version? |
||
* import { assertEquals } from "@std/assert/equals"; | ||
* | ||
* const data = [ | ||
|
@@ -303,7 +334,7 @@ export type StringifyOptions = { | |
* | ||
* @example Emit TSV (tab-separated values) with `separator: "\t"` | ||
* ```ts | ||
* import { stringify } from "@std/csv/unstable-stringify"; | ||
* import { stringify } from "@std/csv/stringify"; | ||
* import { assertEquals } from "@std/assert/equals"; | ||
* | ||
* const data = [ | ||
|
@@ -316,7 +347,7 @@ export type StringifyOptions = { | |
* | ||
* @example Prepend a byte-order mark with `bom: true` | ||
* ```ts | ||
* import { stringify } from "@std/csv/unstable-stringify"; | ||
* import { stringify } from "@std/csv/stringify"; | ||
* import { assertEquals } from "@std/assert/equals"; | ||
* | ||
* const data = [["Rick", 70]]; | ||
|
@@ -330,8 +361,54 @@ export type StringifyOptions = { | |
* @returns A CSV string. | ||
*/ | ||
export function stringify( | ||
data: readonly DataItem[], | ||
options?: StringifyOptions, | ||
data: unknown[][], | ||
options?: ArrayStringifyOptions, | ||
): string; | ||
/** | ||
* Converts an array of objects or arrays into a CSV string. | ||
* | ||
* @example Give an array of objects without specifying columns | ||
* ```ts | ||
* import { stringify } from "@std/csv/unstable-stringify"; | ||
* import { assertEquals } from "@std/assert/equals"; | ||
* | ||
* const data = [ | ||
* { name: "Rick", age: 70 }, | ||
* { name: "Morty", age: 14 }, | ||
* ]; | ||
* | ||
* assertEquals(stringify(data), `name,age\r\nRick,70\r\nMorty,14\r\n`); | ||
* ``` | ||
* | ||
* @example Give an array of string arrays and specify columns with renaming | ||
* ```ts | ||
* import { stringify } from "@std/csv/stringify"; | ||
* import { assertEquals } from "@std/assert/equals"; | ||
* | ||
* const data = [ | ||
* ["Rick", 70], | ||
* ["Morty", 14], | ||
* ]; | ||
* | ||
* const columns = [ | ||
* { prop: 0, header: "name" }, | ||
* { prop: 1, header: "age" }, | ||
* ]; | ||
* | ||
* assertEquals( | ||
* stringify(data, { columns }), | ||
* `name,age\r\nRick,70\r\nMorty,14\r\n`, | ||
* ); | ||
* ``` | ||
* | ||
* @param data The source data to stringify. It's an array of items which are | ||
* plain objects or arrays. | ||
* @param options Options for the stringification. | ||
* @returns A CSV string. | ||
*/ | ||
export function stringify( | ||
data: unknown[][] | Record<string, unknown>[], | ||
options?: StringifyOptions | ArrayStringifyOptions, | ||
): string { | ||
let { columns } = options ?? {}; | ||
|
||
|
@@ -349,7 +426,9 @@ export function stringify( | |
/** | ||
* Infers the columns from the first object element of the given array. | ||
*/ | ||
function inferColumns(data: readonly DataItem[]): string[] { | ||
function inferColumns( | ||
data: unknown[][] | Record<string, unknown>[], | ||
): string[] { | ||
const firstElement = data.at(0); | ||
if ( | ||
firstElement && | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.