From f7aec551dafec2707152b365080ea31435be9052 Mon Sep 17 00:00:00 2001 From: Aubin Date: Wed, 4 Dec 2024 14:37:18 +0100 Subject: [PATCH 1/8] fix: handle falsey but non null values correctly --- front/lib/api/tables.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/front/lib/api/tables.ts b/front/lib/api/tables.ts index df1c38f43947..852db1a6fb10 100644 --- a/front/lib/api/tables.ts +++ b/front/lib/api/tables.ts @@ -376,7 +376,7 @@ export async function rowsFromCsv({ for (const [i, h] of header.entries()) { try { valuesByCol[h] ??= []; - (valuesByCol[h] as string[]).push(record[i] || ""); + (valuesByCol[h] as string[]).push(record[i] ?? ""); } catch (e) { logger.error( // temporary log to fix the valuesByCol[h].push is not a function error From 10961ffb690ed14e23dcda9a7626bf967b2c5dc9 Mon Sep 17 00:00:00 2001 From: Aubin Date: Wed, 4 Dec 2024 14:44:48 +0100 Subject: [PATCH 2/8] refactor: initialize the values once for all to simplify --- front/lib/api/tables.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/front/lib/api/tables.ts b/front/lib/api/tables.ts index 852db1a6fb10..6d90688b337f 100644 --- a/front/lib/api/tables.ts +++ b/front/lib/api/tables.ts @@ -370,12 +370,14 @@ export async function rowsFromCsv({ let i = 0; const parser = parse(csv, { delimiter }); const valuesByCol: Record = {}; + // init arrays for all headers + header.forEach((h) => (valuesByCol[h] = [])); + for await (const anyRecord of parser) { if (i++ >= rowIndex) { const record = anyRecord as string[]; for (const [i, h] of header.entries()) { try { - valuesByCol[h] ??= []; (valuesByCol[h] as string[]).push(record[i] ?? ""); } catch (e) { logger.error( From 129f936f90bbd08c1c1b7bf8f2cf473c8d2cc500 Mon Sep 17 00:00:00 2001 From: Aubin Date: Wed, 4 Dec 2024 14:45:58 +0100 Subject: [PATCH 3/8] add a toString --- front/lib/api/tables.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/front/lib/api/tables.ts b/front/lib/api/tables.ts index 6d90688b337f..25e30c0b2ead 100644 --- a/front/lib/api/tables.ts +++ b/front/lib/api/tables.ts @@ -378,7 +378,7 @@ export async function rowsFromCsv({ const record = anyRecord as string[]; for (const [i, h] of header.entries()) { try { - (valuesByCol[h] as string[]).push(record[i] ?? ""); + valuesByCol[h].push(record[i].toString() ?? ""); } catch (e) { logger.error( // temporary log to fix the valuesByCol[h].push is not a function error From 9419f9af7102d0c2181b53f03607b081efdc826a Mon Sep 17 00:00:00 2001 From: Aubin Date: Wed, 4 Dec 2024 14:46:50 +0100 Subject: [PATCH 4/8] Revert "add a toString" This reverts commit 129f936f90bbd08c1c1b7bf8f2cf473c8d2cc500. --- front/lib/api/tables.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/front/lib/api/tables.ts b/front/lib/api/tables.ts index 25e30c0b2ead..6d90688b337f 100644 --- a/front/lib/api/tables.ts +++ b/front/lib/api/tables.ts @@ -378,7 +378,7 @@ export async function rowsFromCsv({ const record = anyRecord as string[]; for (const [i, h] of header.entries()) { try { - valuesByCol[h].push(record[i].toString() ?? ""); + (valuesByCol[h] as string[]).push(record[i] ?? ""); } catch (e) { logger.error( // temporary log to fix the valuesByCol[h].push is not a function error From eac633505c6f557cffe5021c6fb0529bc6d616fa Mon Sep 17 00:00:00 2001 From: Aubin Date: Wed, 4 Dec 2024 14:46:51 +0100 Subject: [PATCH 5/8] Revert "refactor: initialize the values once for all to simplify" This reverts commit 10961ffb690ed14e23dcda9a7626bf967b2c5dc9. --- front/lib/api/tables.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/front/lib/api/tables.ts b/front/lib/api/tables.ts index 6d90688b337f..852db1a6fb10 100644 --- a/front/lib/api/tables.ts +++ b/front/lib/api/tables.ts @@ -370,14 +370,12 @@ export async function rowsFromCsv({ let i = 0; const parser = parse(csv, { delimiter }); const valuesByCol: Record = {}; - // init arrays for all headers - header.forEach((h) => (valuesByCol[h] = [])); - for await (const anyRecord of parser) { if (i++ >= rowIndex) { const record = anyRecord as string[]; for (const [i, h] of header.entries()) { try { + valuesByCol[h] ??= []; (valuesByCol[h] as string[]).push(record[i] ?? ""); } catch (e) { logger.error( From 60223002eb7265a69cc2fd44751c956808c07101 Mon Sep 17 00:00:00 2001 From: Aubin Date: Wed, 4 Dec 2024 14:48:22 +0100 Subject: [PATCH 6/8] fix clashes occuring when some header values are method names --- front/lib/api/tables.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/front/lib/api/tables.ts b/front/lib/api/tables.ts index 852db1a6fb10..46b8e732a4a0 100644 --- a/front/lib/api/tables.ts +++ b/front/lib/api/tables.ts @@ -369,7 +369,7 @@ export async function rowsFromCsv({ let i = 0; const parser = parse(csv, { delimiter }); - const valuesByCol: Record = {}; + const valuesByCol: Record = Object.create(null); for await (const anyRecord of parser) { if (i++ >= rowIndex) { const record = anyRecord as string[]; From 825cf7f2571268585ead31c1f55c354b033ed823 Mon Sep 17 00:00:00 2001 From: Aubin Date: Wed, 4 Dec 2024 15:22:27 +0100 Subject: [PATCH 7/8] add a comment --- front/lib/api/tables.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/front/lib/api/tables.ts b/front/lib/api/tables.ts index 46b8e732a4a0..043c687b5c4f 100644 --- a/front/lib/api/tables.ts +++ b/front/lib/api/tables.ts @@ -369,6 +369,7 @@ export async function rowsFromCsv({ let i = 0; const parser = parse(csv, { delimiter }); + // this differs with = {} in that it prevent errors when header values clash with object properties such as toString, constructor, .. const valuesByCol: Record = Object.create(null); for await (const anyRecord of parser) { if (i++ >= rowIndex) { From 83c6b89ee7800026e4df0a1ca2e92db74873446f Mon Sep 17 00:00:00 2001 From: Aubin Date: Wed, 4 Dec 2024 15:38:17 +0100 Subject: [PATCH 8/8] remove redundant type cast and add a toString --- front/lib/api/tables.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/front/lib/api/tables.ts b/front/lib/api/tables.ts index 043c687b5c4f..bb64a7c671e5 100644 --- a/front/lib/api/tables.ts +++ b/front/lib/api/tables.ts @@ -373,11 +373,10 @@ export async function rowsFromCsv({ const valuesByCol: Record = Object.create(null); for await (const anyRecord of parser) { if (i++ >= rowIndex) { - const record = anyRecord as string[]; for (const [i, h] of header.entries()) { try { valuesByCol[h] ??= []; - (valuesByCol[h] as string[]).push(record[i] ?? ""); + valuesByCol[h].push((anyRecord[i] ?? "").toString()); } catch (e) { logger.error( // temporary log to fix the valuesByCol[h].push is not a function error