From 34e3dcc91aa19e9e5a0fdb1c845e1d88e029e307 Mon Sep 17 00:00:00 2001 From: Julien Bouquillon Date: Fri, 15 Apr 2022 13:57:55 +0200 Subject: [PATCH] fix(csv-sample): prevent topKey exception (#28) --- packages/csv-sample/src/index.ts | 2 +- packages/csv-sample/src/utils.ts | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/csv-sample/src/index.ts b/packages/csv-sample/src/index.ts index 9e53613..9ad0cbf 100644 --- a/packages/csv-sample/src/index.ts +++ b/packages/csv-sample/src/index.ts @@ -74,7 +74,7 @@ const guessColumnsTypes = (samples: Sample[], onProgress: onProgressFunction) => const detectedType = topKey(detectedTypes, "type"); return { ...sample, - type: detectedType, + type: detectedType || "unknown", }; } return { diff --git a/packages/csv-sample/src/utils.ts b/packages/csv-sample/src/utils.ts index 61c3191..0d194ab 100644 --- a/packages/csv-sample/src/utils.ts +++ b/packages/csv-sample/src/utils.ts @@ -22,7 +22,7 @@ const getRandomItems = (arr: any[], n: number) => { }; // extract the most present value for a given list of records and a given key -export const topKey = (arr: Row[], key: string): string => { +export const topKey = (arr: Row[], key: string): string | undefined => { if (arr.length === 1) { return arr[0][key]; } @@ -41,8 +41,9 @@ export const topKey = (arr: Row[], key: string): string => { return a[1] - b[1]; }) .reverse(); - - return totals[0][0]; + if (totals.length && totals[0].length) { + return totals[0][0]; + } }; export const wait = (args: any) =>