Repository navigation
fix: update csv-parse to 7.0.3 to clear its security alert
#5115
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /** | ||
| * @jest-environment node | ||
| */ | ||
| import { parseContentRows } from "../../app/utils/tsvParser"; | ||
|
|
||
| const FIELD = { | ||
| ACTIVE: "Active", | ||
| COUNT: "Count", | ||
| NAME: "Name", | ||
| SIZE: "Size", | ||
| }; | ||
|
|
||
| const FIELD_TYPE = { | ||
| ACTIVE: "boolean", | ||
| COUNT: "number", | ||
| NAME: "string", | ||
| SIZE: "string", | ||
| }; | ||
|
|
||
| // Tab-delimited, with a stray quote in an unquoted value and an empty "Count" cell. | ||
| const TSV = [ | ||
| "Name\tCount\tActive\tSize", | ||
| 'alpha\t1,234\tTRUE\t 12" ruler ', | ||
| "beta\t\tfalse\tplain", | ||
| ].join("\n"); | ||
|
|
||
| interface Row { | ||
| Active: boolean; | ||
| Count: number; | ||
| Name: string; | ||
| Size: string; | ||
| } | ||
|
|
||
| /** | ||
| * Parses the test TSV the way the catalog and consent code builds do. | ||
| * @returns parsed rows. | ||
| */ | ||
| async function parseRows(): Promise<Row[]> { | ||
| return parseContentRows<Row>(TSV, "\t", FIELD, FIELD_TYPE); | ||
| } | ||
|
|
||
| describe("parseContentRows", () => { | ||
| test("uses the header row as each row's keys", async () => { | ||
| const rows = await parseRows(); | ||
| expect(rows).toHaveLength(2); | ||
| for (const row of rows) { | ||
| expect(Object.keys(row)).toEqual(["Name", "Count", "Active", "Size"]); | ||
| } | ||
| }); | ||
|
|
||
| test("casts values by field type", async () => { | ||
| const [alpha, beta] = await parseRows(); | ||
| expect(alpha.Count).toBe(1234); | ||
| expect(alpha.Active).toBe(true); | ||
| expect(beta.Active).toBe(false); | ||
| expect(alpha.Name).toBe("alpha"); | ||
| }); | ||
|
|
||
| test("keeps a stray quote in an unquoted value and trims the string", async () => { | ||
| const [alpha] = await parseRows(); | ||
| expect(alpha.Size).toBe('12" ruler'); | ||
| }); | ||
|
|
||
| test("parses an empty number cell as 0", async () => { | ||
| const [, beta] = await parseRows(); | ||
| expect(beta.Count).toBe(0); | ||
| }); | ||
|
|
||
| test("returns the full rows", async () => { | ||
| expect(await parseRows()).toEqual([ | ||
| { Active: true, Count: 1234, Name: "alpha", Size: '12" ruler' }, | ||
| { Active: false, Count: 0, Name: "beta", Size: "plain" }, | ||
| ]); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,28 +11,27 @@ import fs from "fs"; | |
| type ReturnType = string | number | boolean; | ||
|
|
||
| /** | ||
| * Returns the file contents parsed into a model shaped by FIELD, or as row arrays if FIELD is omitted. | ||
| * Returns the file contents parsed into a model shaped by FIELD. | ||
|
Contributor
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. Optional: consider rewording. The keys come from the header row, and |
||
| * | ||
| * @param content - to be parsed | ||
| * @param delimiter - csv delimiter | ||
| * @param FIELD - fields | ||
| * @param FIELD_TYPE - field types | ||
| * @returns @see ReturnType[] | ||
| * @returns one object per row, keyed by header. | ||
| */ | ||
| export const parseContentRows = async function parseContentRows<T>( | ||
| content: Buffer | string, | ||
| delimiter = ",", | ||
| FIELD: { [key: string]: string }, | ||
| FIELD_TYPE: { [key: string]: string } | ||
| ): Promise<T[]> { | ||
| if (!FIELD) return parseCsv(content, { delimiter, relax_quotes: true }); | ||
| const keyTypes = Object.fromEntries( | ||
| Object.entries(FIELD_TYPE).map(([header, type]) => [FIELD[header], type]) | ||
| ); | ||
| return parseCsv(content, { | ||
| return parseCsv<T>(content, { | ||
| cast: (datum: string, info: CastingContext) => | ||
| info.header ? datum : parseDatumValue(datum, keyTypes[info.column]), | ||
| columns: (row: string[]) => row.map((header: string) => header), | ||
| columns: true, | ||
| delimiter, | ||
| relax_quotes: true, | ||
| }); | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: consider adding an
"array"column (e.g.Tagswith"a, b,,c"→["a","b","c"]). The catalog relies on it forDATA_TYPES,DISEASESandSTUDY_DESIGNS.