From 3bdf76ed03988a95f748a1d582c6a1a0f1c85a49 Mon Sep 17 00:00:00 2001 From: Valtteri Kantanen Date: Thu, 18 Jul 2024 17:38:01 +0300 Subject: [PATCH] [Backend] Fix `createLocaleComparator` --- services/backend/src/util/index.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/services/backend/src/util/index.ts b/services/backend/src/util/index.ts index 646bdd7959..12694a658c 100644 --- a/services/backend/src/util/index.ts +++ b/services/backend/src/util/index.ts @@ -10,11 +10,22 @@ type FieldComparator = (val1: Record, val2: Record val1.localeCompare(val2, 'fi', { sensitivity: 'accent' }) - if (!field) { - return comparator + type ValidArrayItem = string | Record + return (val1: ValidArrayItem, val2: ValidArrayItem) => { + if (typeof val1 === 'string' && typeof val2 === 'string') { + return val1.localeCompare(val2, 'fi', { sensitivity: 'accent' }) + } + if ( + typeof val1 === 'object' && + typeof val2 === 'object' && + field && + typeof val1[field] === 'string' && + typeof val2[field] === 'string' + ) { + return val1[field].localeCompare(val2[field], 'fi', { sensitivity: 'accent' }) + } + throw new Error('Invalid arguments') } - return (val1: Record, val2: Record) => comparator(val1[field], val2[field]) } export const getFullStudyProgrammeRights = programmeRights => {