Skip to content

Commit 92d97c1

Browse files
authored
impr: optimize getFirstDayOfWeek (@fehmer) (monkeytypegame#6457)
!nuf
1 parent e1c8e4a commit 92d97c1

File tree

1 file changed

+118
-13
lines changed

1 file changed

+118
-13
lines changed

frontend/src/ts/utils/date-and-time.ts

Lines changed: 118 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { roundTo2 } from "@monkeytype/util/numbers";
22
import { Day } from "date-fns";
3-
import * as Locales from "date-fns/locale";
3+
44
/**
55
* Converts seconds to a human-readable string representation of time.
66
* @param sec The number of seconds to convert.
@@ -106,6 +106,115 @@ export function secondsToString(
106106
return ret.trim();
107107
}
108108

109+
/*
110+
generated by
111+
import * as Locales from "date-fns/locale";
112+
const result = {};
113+
for (const locale of Object.keys(Locales)) {
114+
// @ts-ignore
115+
const data = Locales[locale] as Locales.Locale;
116+
result[locale] = (data.options?.weekStartsOn as number) % 7;
117+
}
118+
*/
119+
120+
const weekStartsOnFallback: Record<string, Day> = {
121+
af: 0,
122+
ar: 6,
123+
arDZ: 0,
124+
arEG: 0,
125+
arMA: 1,
126+
arSA: 0,
127+
arTN: 1,
128+
az: 1,
129+
be: 1,
130+
beTarask: 1,
131+
bg: 1,
132+
bn: 0,
133+
bs: 1,
134+
ca: 1,
135+
ckb: 0,
136+
cs: 1,
137+
cy: 0,
138+
da: 1,
139+
de: 1,
140+
deAT: 1,
141+
el: 1,
142+
enAU: 1,
143+
enCA: 0,
144+
enGB: 1,
145+
enIE: 1,
146+
enIN: 1,
147+
enNZ: 1,
148+
enUS: 0,
149+
enZA: 0,
150+
eo: 1,
151+
es: 1,
152+
et: 1,
153+
eu: 1,
154+
faIR: 6,
155+
fi: 1,
156+
fr: 1,
157+
frCA: 0,
158+
frCH: 1,
159+
fy: 1,
160+
gd: 0,
161+
gl: 1,
162+
gu: 1,
163+
he: 0,
164+
hi: 0,
165+
hr: 1,
166+
ht: 1,
167+
hu: 1,
168+
hy: 1,
169+
id: 1,
170+
is: 1,
171+
it: 1,
172+
itCH: 1,
173+
ja: 0,
174+
jaHira: 0,
175+
ka: 1,
176+
kk: 1,
177+
km: 0,
178+
kn: 1,
179+
ko: 0,
180+
lb: 1,
181+
lt: 1,
182+
lv: 1,
183+
mk: 1,
184+
mn: 1,
185+
ms: 1,
186+
mt: 1,
187+
nb: 1,
188+
nl: 1,
189+
nlBE: 1,
190+
nn: 1,
191+
oc: 1,
192+
pl: 1,
193+
pt: 1,
194+
ptBR: 0,
195+
ro: 1,
196+
ru: 1,
197+
se: 1,
198+
sk: 1,
199+
sl: 1,
200+
sq: 1,
201+
sr: 1,
202+
srLatn: 1,
203+
sv: 1,
204+
ta: 1,
205+
te: 0,
206+
th: 0,
207+
tr: 1,
208+
ug: 0,
209+
uk: 1,
210+
uz: 1,
211+
uzCyrl: 1,
212+
vi: 1,
213+
zhCN: 1,
214+
zhHK: 0,
215+
zhTW: 1,
216+
};
217+
109218
export function getFirstDayOfTheWeek(): Day {
110219
if (navigator.language === undefined || navigator.language === null) {
111220
return 0;
@@ -128,20 +237,16 @@ export function getFirstDayOfTheWeek(): Day {
128237
return (locale.getWeekInfo().firstDay as number) % 7;
129238
}
130239

131-
//use date-fns for browsers like firefox
132-
// @ts-ignore
133-
let dateFnsLocale = Locales[
134-
navigator.language.replaceAll("-", "")
135-
] as Locales.Locale;
136-
137-
if (dateFnsLocale === undefined || dateFnsLocale === null) {
138-
//retry with language only
139-
// @ts-ignore
140-
dateFnsLocale = Locales[navigator.language.split("-")[0]] as Locales.Locale;
240+
//use fallback generated from date-fns for browsers like firefox
241+
let fallback = weekStartsOnFallback[navigator.language.replaceAll("-", "")];
242+
if (fallback !== undefined) {
243+
return fallback;
141244
}
142245

143-
if (dateFnsLocale !== undefined && dateFnsLocale !== null) {
144-
return ((dateFnsLocale.options?.weekStartsOn ?? 0) % 7) as Day;
246+
//retry with language only
247+
fallback = weekStartsOnFallback[navigator.language.split("-")[0] as string];
248+
if (fallback !== undefined) {
249+
return fallback;
145250
}
146251

147252
return 0; //start on sunday

0 commit comments

Comments
 (0)