Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions __tests__/utils/tsvParser.test.ts
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 = {

Copy link
Copy Markdown
Contributor

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. Tags with "a, b,,c" → ["a","b","c"]). The catalog relies on it for DATA_TYPES, DISEASES and STUDY_DESIGNS.

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" },
]);
});
});
9 changes: 4 additions & 5 deletions app/utils/tsvParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: consider rewording. The keys come from the header row, and FIELD only picks which columns get cast (columns not in FIELD come through uncast). Something like "Returns the rows of a delimited file as objects keyed by header, with values cast by FIELD_TYPE."

*
* @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,
});
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@tanstack/react-virtual": "^3",
"@types/fhir": "^0.0.35",
"copy-to-clipboard": "3",
"csv-parse": "^5.0.4",
"csv-parse": "^7.0.2",
"gray-matter": "^4",
"jwt-decode": "^3.1.2",
"ky": "^1.10.0",
Expand Down
Loading