From 38038234d4aa389178c80fe89f88e6429aef530c Mon Sep 17 00:00:00 2001 From: Ivan Gabriele Date: Fri, 6 Sep 2024 09:39:38 +0200 Subject: [PATCH] fix(utils): sort by French natural order in getOptionsFromLabelledEnum() --- .../getOptionsFromLabelledEnum.test.ts | 56 ++++++++++++++++--- src/utils/getOptionsFromLabelledEnum.ts | 6 +- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/utils/__tests__/getOptionsFromLabelledEnum.test.ts b/src/utils/__tests__/getOptionsFromLabelledEnum.test.ts index e0c668b5c..83bc77c50 100644 --- a/src/utils/__tests__/getOptionsFromLabelledEnum.test.ts +++ b/src/utils/__tests__/getOptionsFromLabelledEnum.test.ts @@ -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' } ]) }) @@ -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([ { diff --git a/src/utils/getOptionsFromLabelledEnum.ts b/src/utils/getOptionsFromLabelledEnum.ts index 3e820aa6d..2251da28a 100644 --- a/src/utils/getOptionsFromLabelledEnum.ts +++ b/src/utils/getOptionsFromLabelledEnum.ts @@ -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 = Record>( labelledEnum: T, mustSort: boolean = false @@ -12,7 +12,7 @@ export function getOptionsFromLabelledEnum = Re })) if (mustSort) { - return sortBy(['label'], formattedOptions) + return formattedOptions.sort(byFrenchNaturalOrder) } return formattedOptions