Skip to content

Commit

Permalink
fix(utils): sort by French natural order in getOptionsFromLabelledEnum()
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Sep 6, 2024
1 parent ca16c6b commit 3803823
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
56 changes: 47 additions & 9 deletions src/utils/__tests__/getOptionsFromLabelledEnum.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,62 @@
/* eslint-disable typescript-sort-keys/string-enum */

import { describe, expect, it } from '@jest/globals'

import { getOptionsFromLabelledEnum } from '../getOptionsFromLabelledEnum'

describe('utils/getOptionsFromLabelledEnum()', () => {
it('should return the expected array of options', () => {
it('should return the expected array of options naturally French-sorted', () => {
enum LabelledEnum {
TWO = 'two',
ONE = 'one'
XYLOPHONE = 'Xylophone',
SCREEN_LOWERCASE = 'écran',
SCREEN = 'Écran',
E_LOWERCASE = 'e',
E = 'E',
LAMP_SHADE = 'Abat-jour',
TODO = 'À faire',
A_LOWERCASE = 'a',
A = 'A'
}
const mustSort = true

const result = getOptionsFromLabelledEnum(LabelledEnum, true)
const result = getOptionsFromLabelledEnum(LabelledEnum, mustSort)

expect(result).toStrictEqual([
{
label: 'one',
value: 'ONE'
label: 'a',
value: 'A_LOWERCASE'
},
{
label: 'A',
value: 'A'
},
{
label: 'À faire',
value: 'TODO'
},
{
label: 'Abat-jour',
value: 'LAMP_SHADE'
},
{
label: 'e',
value: 'E_LOWERCASE'
},
{
label: 'E',
value: 'E'
},
{
label: 'écran',
value: 'SCREEN_LOWERCASE'
},
{
label: 'Écran',
value: 'SCREEN'
},
{
label: 'two',
value: 'TWO'
label: 'Xylophone',
value: 'XYLOPHONE'
}
])
})
Expand All @@ -30,8 +67,9 @@ describe('utils/getOptionsFromLabelledEnum()', () => {
APPLE = 'apple',
GRAPES = 'grapes'
}
const mustSort = false

const result = getOptionsFromLabelledEnum(LabelledEnum)
const result = getOptionsFromLabelledEnum(LabelledEnum, mustSort)

expect(result).toStrictEqual([
{
Expand Down
6 changes: 3 additions & 3 deletions src/utils/getOptionsFromLabelledEnum.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sortBy } from 'lodash/fp'

import type { Option } from '../types/definitions'

const byFrenchNaturalOrder = (a: Option, b: Option) => a.label.localeCompare(b.label, 'fr', { sensitivity: 'base' })

export function getOptionsFromLabelledEnum<T extends Record<string, string> = Record<string, string>>(
labelledEnum: T,
mustSort: boolean = false
Expand All @@ -12,7 +12,7 @@ export function getOptionsFromLabelledEnum<T extends Record<string, string> = Re
}))

if (mustSort) {
return sortBy(['label'], formattedOptions)
return formattedOptions.sort(byFrenchNaturalOrder)
}

return formattedOptions
Expand Down

0 comments on commit 3803823

Please sign in to comment.