From b14ac07568d659a988a1880bf6376becd503309a Mon Sep 17 00:00:00 2001 From: Alex Christoffer Rasmussen Date: Fri, 19 Apr 2024 19:36:06 +0100 Subject: [PATCH] excel import leading and trailing whitespace (#902) * add test for leading and trailing whitespace * trim whitespace --- .../server/src/utils/__tests__/parser.test.ts | 33 +++++++++++++++++++ apps/server/src/utils/parser.ts | 4 +-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/apps/server/src/utils/__tests__/parser.test.ts b/apps/server/src/utils/__tests__/parser.test.ts index 12e3aa035..caf58ea00 100644 --- a/apps/server/src/utils/__tests__/parser.test.ts +++ b/apps/server/src/utils/__tests__/parser.test.ts @@ -1407,4 +1407,37 @@ describe('parseExcel()', () => { expect(events.at(6).timeStart).toEqual(59400000); expect(events.at(7).timeStart).toEqual(78300000); }); + + it('handle leading and trailing whitespace', () => { + const testData = [ + ['Time Start', 'Time End', ' Title', 'End Action', 'Public', 'Skip', 'Notes', 'Colour ', 'cue'], + ['4:30:00', '4:30:00', 'A song from the hearth', 'load-next', 'x', '', 'Rainbow chase', '#F00', 102], + ]; + + const importMap = { + worksheet: 'event schedule', + timeStart: ' time start', + timeEnd: 'time end ', + duration: 'duration', + cue: 'cue', + title: 'title', + isPublic: 'public', + skip: 'skip', + note: 'notes', + colour: 'colour', + endAction: 'end action', + timerType: 'timer type', + timeWarning: 'warning time', + timeDanger: 'danger time', + custom: {}, + }; + + const result = parseExcel(testData, importMap); + const rundown = parseRundown(result); + const events = rundown.filter((e) => e.type === SupportedEvent.Event) as OntimeEvent[]; + expect(events.at(0).timeStart).toEqual(16200000); //<--leading white space in MAP + expect(events.at(0).timeEnd).toEqual(16200000); //<--trailing white space in MAP + expect(events.at(0).title).toEqual('A song from the hearth'); //<--leading white space in Excel data + expect(events.at(0).colour).toEqual('#F00'); //<--trailing white space in Excel data + }); }); diff --git a/apps/server/src/utils/parser.ts b/apps/server/src/utils/parser.ts index b58f5e6db..64e2c5f92 100644 --- a/apps/server/src/utils/parser.ts +++ b/apps/server/src/utils/parser.ts @@ -74,7 +74,7 @@ export const parseExcel = (excelData: unknown[][], options?: Partial) for (const [key, value] of Object.entries(importMap)) { if (typeof value === 'string') { - importMap[key] = value.toLocaleLowerCase(); + importMap[key] = value.toLocaleLowerCase().trim(); } } @@ -222,7 +222,7 @@ export const parseExcel = (excelData: unknown[][], options?: Partial) if (column.length === 0) { continue; } - const columnText = column.toLowerCase(); + const columnText = column.toLowerCase().trim(); // check if it is an ontime column if (handlers[columnText]) {