-
Notifications
You must be signed in to change notification settings - Fork 4
fix: clone settings locale data #96
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -5,6 +5,23 @@ import {normalizeTimeZone} from '../timeZone'; | |
| import {localeLoaders} from './locales'; | ||
| import type {Locale, Parser, PublicSettings, UpdateLocaleConfig} from './types'; | ||
|
|
||
| function cloneLocaleData<T>(value: T): T { | ||
|
Collaborator
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. Worth a one-line comment that this helper intentionally assumes locale data is function-or-JSON-shaped. Unlike
Contributor
Author
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. Fixed in 589af2a: added a one-line note that this helper is specific to Dayjs JSON-like locale data with function fields and is not a general |
||
| if (!value || typeof value !== 'object') { | ||
| return value; | ||
| } | ||
|
|
||
| if (Array.isArray(value)) { | ||
| return value.map((item) => cloneLocaleData(item)) as T; | ||
| } | ||
|
|
||
| // Dayjs locale data contains function fields such as ordinal. | ||
| // Clone array/plain-object containers so callers can safely mutate the result, | ||
| // while keeping locale functions callable by reference. | ||
| return Object.fromEntries( | ||
| Object.entries(value).map(([key, item]) => [key, cloneLocaleData(item)]), | ||
| ) as T; | ||
| } | ||
|
|
||
| class Settings implements PublicSettings { | ||
| // 'en' - preloaded locale in dayjs | ||
| private loadedLocales = new Set(['en']); | ||
|
|
@@ -50,7 +67,7 @@ class Settings implements PublicSettings { | |
| throw new Error('There is something really wrong happening. Locale data is absent.'); | ||
| } | ||
|
|
||
| return structuredClone(localeObject) as Locale; | ||
| return cloneLocaleData(localeObject) as Locale; | ||
| } | ||
|
|
||
| setLocale(locale: string) { | ||
|
|
||
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.
This test runs against whatever locale the previous test left set (
de) rather than a locale it sets itself. It passes regardless sinceenalso hasordinal+formats, but callingsettings.setLocale('en')at the start would make it self-contained and order-independent.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.
Fixed in 589af2a: added
settings.setLocale('en')at the start of this test, so it is self-contained and order-independent.