Skip to content

Commit

Permalink
[Backend] Fix createLocaleComparator
Browse files Browse the repository at this point in the history
  • Loading branch information
valtterikantanen committed Jul 18, 2024
1 parent cc692f3 commit 3bdf76e
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions services/backend/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@ type FieldComparator = (val1: Record<string, string>, val2: Record<string, strin
export function createLocaleComparator(field: string): FieldComparator
export function createLocaleComparator(): Comparator
export function createLocaleComparator(field?: string): Comparator | FieldComparator {
const comparator: Comparator = (val1, val2) => val1.localeCompare(val2, 'fi', { sensitivity: 'accent' })
if (!field) {
return comparator
type ValidArrayItem = string | Record<string, string>
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<string, string>, val2: Record<string, string>) => comparator(val1[field], val2[field])
}

export const getFullStudyProgrammeRights = programmeRights => {
Expand Down

0 comments on commit 3bdf76e

Please sign in to comment.