Skip to content

Commit 5926aeb

Browse files
authored
fix(csv): accept readonly cell/header data (denoland#5734) (denoland#5735)
feat(csv): accept readonly cell/header data (denoland#5734)
1 parent 7632ab0 commit 5926aeb

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

csv/_io.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export function createQuoteErrorMessage(
226226
}
227227

228228
export function convertRowToObject(
229-
row: string[],
229+
row: readonly string[],
230230
headers: readonly string[],
231231
zeroBasedLine: number,
232232
) {

csv/stringify.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export type ColumnDetails = {
8484
export type Column = ColumnDetails | PropertyAccessor | PropertyAccessor[];
8585

8686
/** An object (plain or array) */
87-
export type DataItem = Record<string, unknown> | unknown[];
87+
export type DataItem = Readonly<Record<string, unknown> | unknown[]>;
8888

8989
/** Options for {@linkcode stringify}. */
9090
export type StringifyOptions = {
@@ -110,7 +110,7 @@ export type StringifyOptions = {
110110
*
111111
* @default {[]}
112112
*/
113-
columns?: Column[];
113+
columns?: readonly Column[];
114114
/**
115115
* Whether to add a
116116
* {@link https://en.wikipedia.org/wiki/Byte_order_mark | byte-order mark} to the
@@ -145,7 +145,7 @@ function getEscapedString(value: unknown, sep: string): string {
145145

146146
type NormalizedColumn = Omit<ColumnDetails, "header" | "prop"> & {
147147
header: string;
148-
prop: PropertyAccessor[];
148+
prop: readonly PropertyAccessor[];
149149
};
150150

151151
function normalizeColumn(column: Column): NormalizedColumn {
@@ -176,7 +176,7 @@ function normalizeColumn(column: Column): NormalizedColumn {
176176
*/
177177
function getValuesFromItem(
178178
item: DataItem,
179-
normalizedColumns: NormalizedColumn[],
179+
normalizedColumns: readonly NormalizedColumn[],
180180
): unknown[] {
181181
const values: unknown[] = [];
182182

@@ -426,7 +426,7 @@ function getValuesFromItem(
426426
* @returns A CSV string.
427427
*/
428428
export function stringify(
429-
data: DataItem[],
429+
data: readonly DataItem[],
430430
options?: StringifyOptions,
431431
): string {
432432
const { headers = true, separator: sep = ",", columns = [], bom = false } =

csv/stringify_test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,5 +579,17 @@ Deno.test({
579579
},
580580
},
581581
);
582+
await t.step({
583+
name: "`as const` row data and options pass type checking",
584+
fn() {
585+
const columns = ["col1", "col2"] as const;
586+
const data = [["foo", "bar"], ["baz", "qux"]] as const;
587+
const options = { columns } as const;
588+
589+
void (() => {
590+
stringify(data, options);
591+
});
592+
},
593+
});
582594
},
583595
});

0 commit comments

Comments
 (0)