-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Intl.NumberFormat locale RangeError related to Simulation Details #24068
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { createSelector } from 'reselect'; | ||
import * as actionConstants from '../../store/actionConstants'; | ||
|
||
export default function reduceLocaleMessages(state = {}, { type, payload }) { | ||
|
@@ -13,7 +14,25 @@ export default function reduceLocaleMessages(state = {}, { type, payload }) { | |
} | ||
} | ||
|
||
export const getCurrentLocale = (state) => state.localeMessages.currentLocale; | ||
/** | ||
* @param state | ||
* @returns {string} one of the codes in file://./../../../app/_locales/index.json. | ||
* These codes are not safe to use with the Intl API. | ||
*/ | ||
export const getLocaleNotSafeForIntl = (state) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the sake of minimising refactor and the PR diff, is this really needed if Would the above warning in the JSDoc, plus the presence of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
state.localeMessages.currentLocale; | ||
|
||
/** | ||
* This selector returns a code from /app/_locales/index.json as a | ||
* [BCP 47 Language Tag](https://en.wikipedia.org/wiki/IETF_language_tag) for use with | ||
* the Intl API. | ||
* | ||
* @returns {Intl.UnicodeBCP47LocaleIdentifier} a locale code that can be used with the Intl API | ||
*/ | ||
export const getIntlLocale = createSelector( | ||
getLocaleNotSafeForIntl, | ||
(locale) => Intl.getCanonicalLocales(locale.replace(/_/gu, '-'))[0], | ||
); | ||
|
||
export const getCurrentLocaleMessages = (state) => state.localeMessages.current; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import locales from '../../../app/_locales/index.json'; | ||
import { getIntlLocale } from './locale'; | ||
|
||
const createMockStateWithLocale = (locale: string) => ({ | ||
localeMessages: { currentLocale: locale }, | ||
}); | ||
|
||
describe('getIntlLocale', () => { | ||
it.each(locales)('can convert locale "%s" to BCP 47 format', (locale) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love this test! But if this is the happy path, should we also have a unit test to verify we throw if a bad locale is in the state? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. Done. |
||
const mockState = createMockStateWithLocale(locale.code); | ||
|
||
// Intl.getCanonicalLocales will throw an error if the locale is invalid. | ||
expect(() => getIntlLocale(mockState)).not.toThrow(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be explicit in both these method's JSDoc that it's the user's selected locale that is returned?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.