From 1f9edd8e30d52af701cd30c1b8610dcf1e38ad20 Mon Sep 17 00:00:00 2001 From: Emmanuel Chambon Date: Thu, 22 Dec 2022 00:32:32 +0100 Subject: [PATCH 1/2] fix(use-i18n): correct formatUnit type --- packages/use-i18n/src/__tests__/formatUnit.ts | 32 ++++++++----------- packages/use-i18n/src/formatUnit.ts | 4 ++- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/packages/use-i18n/src/__tests__/formatUnit.ts b/packages/use-i18n/src/__tests__/formatUnit.ts index 80166600a..df14519bb 100644 --- a/packages/use-i18n/src/__tests__/formatUnit.ts +++ b/packages/use-i18n/src/__tests__/formatUnit.ts @@ -3,14 +3,16 @@ import formatUnit, { supportedUnits } from '../formatUnit' const locales = ['en', 'fr', 'ro'] -const tests = [ - ...Object.keys(supportedUnits).map(unit => [ +type TestType = [string, FormatUnitOptions, string, number] + +const tests: TestType[] = [ + ...(Object.keys(supportedUnits).map(unit => [ 'should work with', { unit }, 'fr', 12.56, - ]), - ...Object.keys(supportedUnits) + ]) as TestType[]), + ...(Object.keys(supportedUnits) .map(unit => locales.map(locale => [ `should work with locale ${locale} and`, @@ -19,8 +21,8 @@ const tests = [ 12.56, ]), ) - .flat(), - ...Object.keys(supportedUnits) + .flat() as TestType[]), + ...(Object.keys(supportedUnits) .map(unit => locales.map(locale => [ `should work with long format, locale ${locale} and`, @@ -29,19 +31,19 @@ const tests = [ 12.56, ]), ) - .flat(), - ...Object.keys(supportedUnits).map(unit => [ + .flat() as TestType[]), + ...(Object.keys(supportedUnits).map(unit => [ 'should work with maximumFractionDigits', { maximumFractionDigits: 3, unit }, 'fr', 12.56, - ]), - ...Object.keys(supportedUnits).map(unit => [ + ]) as TestType[]), + ...(Object.keys(supportedUnits).map(unit => [ 'should work with minimumFractionDigits', { minimumFractionDigits: 3, unit }, 'fr', 12, - ]), + ]) as TestType[]), ] describe('formatUnit', () => { @@ -53,12 +55,6 @@ describe('formatUnit', () => { }) test.each(tests)('%s %o', (_, options, locale, amount) => { - expect( - formatUnit( - locale as string, - amount as number, - options as FormatUnitOptions, - ), - ).toMatchSnapshot() + expect(formatUnit(locale, amount, options)).toMatchSnapshot() }) }) diff --git a/packages/use-i18n/src/formatUnit.ts b/packages/use-i18n/src/formatUnit.ts index 1be297a5b..e1c172ad2 100644 --- a/packages/use-i18n/src/formatUnit.ts +++ b/packages/use-i18n/src/formatUnit.ts @@ -190,7 +190,9 @@ const format = type SimpleUnits = `${ExponentName}${Unit}${'-humanized' | ''}` type ComplexUnits = `${Unit}${'s' | ''}${'-humanized' | ''}` -type PerSecondUnit = `bit${'s' | ''}${'-per-second' | ''}${'-humanized' | ''}` +type PerSecondUnit = `${ExponentName | ''}bit${'s' | ''}${'-per-second' | ''}${ + | '-humanized' + | ''}` type SupportedUnits = SimpleUnits | ComplexUnits | PerSecondUnit export const supportedUnits: Partial< From 93fb7c6fc6524770913bdb7dd0fd37bc52029ef5 Mon Sep 17 00:00:00 2001 From: Emmanuel Chambon Date: Fri, 23 Dec 2022 15:09:08 +0100 Subject: [PATCH 2/2] fix: feedback --- packages/use-i18n/src/__tests__/formatUnit.ts | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/packages/use-i18n/src/__tests__/formatUnit.ts b/packages/use-i18n/src/__tests__/formatUnit.ts index df14519bb..fe5d6ac83 100644 --- a/packages/use-i18n/src/__tests__/formatUnit.ts +++ b/packages/use-i18n/src/__tests__/formatUnit.ts @@ -5,45 +5,45 @@ const locales = ['en', 'fr', 'ro'] type TestType = [string, FormatUnitOptions, string, number] +const SUPPORTED_UNITS = Object.keys( + supportedUnits, +) as FormatUnitOptions['unit'][] + const tests: TestType[] = [ - ...(Object.keys(supportedUnits).map(unit => [ + ...SUPPORTED_UNITS.map(unit => [ 'should work with', { unit }, 'fr', 12.56, - ]) as TestType[]), - ...(Object.keys(supportedUnits) - .map(unit => - locales.map(locale => [ - `should work with locale ${locale} and`, - { unit }, - locale, - 12.56, - ]), - ) - .flat() as TestType[]), - ...(Object.keys(supportedUnits) - .map(unit => - locales.map(locale => [ - `should work with long format, locale ${locale} and`, - { short: false, unit }, - locale, - 12.56, - ]), - ) - .flat() as TestType[]), - ...(Object.keys(supportedUnits).map(unit => [ + ]), + ...SUPPORTED_UNITS.map(unit => + locales.map(locale => [ + `should work with locale ${locale} and`, + { unit }, + locale, + 12.56, + ]), + ).flat(), + ...SUPPORTED_UNITS.map(unit => + locales.map(locale => [ + `should work with long format, locale ${locale} and`, + { short: false, unit }, + locale, + 12.56, + ]), + ).flat(), + ...SUPPORTED_UNITS.map(unit => [ 'should work with maximumFractionDigits', { maximumFractionDigits: 3, unit }, 'fr', 12.56, - ]) as TestType[]), - ...(Object.keys(supportedUnits).map(unit => [ + ]), + ...SUPPORTED_UNITS.map(unit => [ 'should work with minimumFractionDigits', { minimumFractionDigits: 3, unit }, 'fr', 12, - ]) as TestType[]), + ]), ] describe('formatUnit', () => {