Skip to content

Commit 6ccfd90

Browse files
authored
Merge pull request #958 from Sector6759/fix-array-order-inconsistencies
Fix array order inconsistencies
2 parents b8c9254 + 524f5b6 commit 6ccfd90

2 files changed

Lines changed: 203 additions & 51 deletions

File tree

lib/date.ts

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -54,35 +54,36 @@ export function getDayNames(): string[] {
5454
// Fallback to Intl
5555
const locale = getCanonicalLocale()
5656
return [
57-
new Date('1970-01-04T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'long' }),
58-
new Date('1970-01-05T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'long' }),
59-
new Date('1970-01-06T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'long' }),
60-
new Date('1970-01-07T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'long' }),
61-
new Date('1970-01-08T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'long' }),
62-
new Date('1970-01-09T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'long' }),
63-
new Date('1970-01-10T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'long' }),
57+
new Date(1970, 0, 4).toLocaleDateString(locale, { weekday: 'long' }),
58+
new Date(1970, 0, 5).toLocaleDateString(locale, { weekday: 'long' }),
59+
new Date(1970, 0, 6).toLocaleDateString(locale, { weekday: 'long' }),
60+
new Date(1970, 0, 7).toLocaleDateString(locale, { weekday: 'long' }),
61+
new Date(1970, 0, 8).toLocaleDateString(locale, { weekday: 'long' }),
62+
new Date(1970, 0, 9).toLocaleDateString(locale, { weekday: 'long' }),
63+
new Date(1970, 0, 10).toLocaleDateString(locale, { weekday: 'long' }),
6464
]
6565
}
6666

6767
/**
6868
* Get a list of day names (short names)
6969
*/
7070
export function getDayNamesShort(): string[] {
71+
// Server rendered
7172
if (typeof globalThis.dayNamesShort !== 'undefined') {
7273
return globalThis.dayNamesShort
7374
}
7475

7576
// Fallback to Intl
76-
// Note: narrow is shorter than server's "min", but it's the closest we can get
77+
// Note: short is shorter than server's "short", but it's the closest we can get
7778
const locale = getCanonicalLocale()
7879
return [
79-
new Date('1970-01-04T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'short' }),
80-
new Date('1970-01-05T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'short' }),
81-
new Date('1970-01-06T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'short' }),
82-
new Date('1970-01-07T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'short' }),
83-
new Date('1970-01-08T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'short' }),
84-
new Date('1970-01-09T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'short' }),
85-
new Date('1970-01-10T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'short' }),
80+
new Date(1970, 0, 4).toLocaleDateString(locale, { weekday: 'short' }),
81+
new Date(1970, 0, 5).toLocaleDateString(locale, { weekday: 'short' }),
82+
new Date(1970, 0, 6).toLocaleDateString(locale, { weekday: 'short' }),
83+
new Date(1970, 0, 7).toLocaleDateString(locale, { weekday: 'short' }),
84+
new Date(1970, 0, 8).toLocaleDateString(locale, { weekday: 'short' }),
85+
new Date(1970, 0, 9).toLocaleDateString(locale, { weekday: 'short' }),
86+
new Date(1970, 0, 10).toLocaleDateString(locale, { weekday: 'short' }),
8687
]
8788
}
8889

@@ -96,15 +97,16 @@ export function getDayNamesMin(): string[] {
9697
}
9798

9899
// Fallback to Intl
100+
// Note: narrow is shorter than server's "min", but it's the closest we can get
99101
const locale = getCanonicalLocale()
100102
return [
101-
new Date('1970-01-04T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'narrow' }),
102-
new Date('1970-01-05T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'narrow' }),
103-
new Date('1970-01-06T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'narrow' }),
104-
new Date('1970-01-07T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'narrow' }),
105-
new Date('1970-01-08T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'narrow' }),
106-
new Date('1970-01-09T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'narrow' }),
107-
new Date('1970-01-10T00:00:00.000Z').toLocaleDateString(locale, { weekday: 'narrow' }),
103+
new Date(1970, 0, 4).toLocaleDateString(locale, { weekday: 'narrow' }),
104+
new Date(1970, 0, 5).toLocaleDateString(locale, { weekday: 'narrow' }),
105+
new Date(1970, 0, 6).toLocaleDateString(locale, { weekday: 'narrow' }),
106+
new Date(1970, 0, 7).toLocaleDateString(locale, { weekday: 'narrow' }),
107+
new Date(1970, 0, 8).toLocaleDateString(locale, { weekday: 'narrow' }),
108+
new Date(1970, 0, 9).toLocaleDateString(locale, { weekday: 'narrow' }),
109+
new Date(1970, 0, 10).toLocaleDateString(locale, { weekday: 'narrow' }),
108110
]
109111
}
110112

@@ -120,18 +122,18 @@ export function getMonthNames(): string[] {
120122
// Fallback to Intl
121123
const locale = getCanonicalLocale()
122124
return [
123-
new Date('1970-01-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'long' }),
124-
new Date('1970-02-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'long' }),
125-
new Date('1970-03-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'long' }),
126-
new Date('1970-04-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'long' }),
127-
new Date('1970-05-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'long' }),
128-
new Date('1970-06-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'long' }),
129-
new Date('1970-07-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'long' }),
130-
new Date('1970-08-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'long' }),
131-
new Date('1970-09-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'long' }),
132-
new Date('1970-10-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'long' }),
133-
new Date('1970-11-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'long' }),
134-
new Date('1970-12-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'long' }),
125+
new Date(1970, 0).toLocaleDateString(locale, { month: 'long' }),
126+
new Date(1970, 1).toLocaleDateString(locale, { month: 'long' }),
127+
new Date(1970, 2).toLocaleDateString(locale, { month: 'long' }),
128+
new Date(1970, 3).toLocaleDateString(locale, { month: 'long' }),
129+
new Date(1970, 4).toLocaleDateString(locale, { month: 'long' }),
130+
new Date(1970, 5).toLocaleDateString(locale, { month: 'long' }),
131+
new Date(1970, 6).toLocaleDateString(locale, { month: 'long' }),
132+
new Date(1970, 7).toLocaleDateString(locale, { month: 'long' }),
133+
new Date(1970, 8).toLocaleDateString(locale, { month: 'long' }),
134+
new Date(1970, 9).toLocaleDateString(locale, { month: 'long' }),
135+
new Date(1970, 10).toLocaleDateString(locale, { month: 'long' }),
136+
new Date(1970, 11).toLocaleDateString(locale, { month: 'long' }),
135137
]
136138
}
137139

@@ -147,17 +149,17 @@ export function getMonthNamesShort(): string[] {
147149
// Fallback to Intl
148150
const locale = getCanonicalLocale()
149151
return [
150-
new Date('1970-01-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'short' }),
151-
new Date('1970-02-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'short' }),
152-
new Date('1970-03-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'short' }),
153-
new Date('1970-04-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'short' }),
154-
new Date('1970-05-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'short' }),
155-
new Date('1970-06-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'short' }),
156-
new Date('1970-07-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'short' }),
157-
new Date('1970-08-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'short' }),
158-
new Date('1970-09-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'short' }),
159-
new Date('1970-10-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'short' }),
160-
new Date('1970-11-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'short' }),
161-
new Date('1970-12-01T00:00:00.000Z').toLocaleDateString(locale, { month: 'short' }),
152+
new Date(1970, 0).toLocaleDateString(locale, { month: 'short' }),
153+
new Date(1970, 1).toLocaleDateString(locale, { month: 'short' }),
154+
new Date(1970, 2).toLocaleDateString(locale, { month: 'short' }),
155+
new Date(1970, 3).toLocaleDateString(locale, { month: 'short' }),
156+
new Date(1970, 4).toLocaleDateString(locale, { month: 'short' }),
157+
new Date(1970, 5).toLocaleDateString(locale, { month: 'short' }),
158+
new Date(1970, 6).toLocaleDateString(locale, { month: 'short' }),
159+
new Date(1970, 7).toLocaleDateString(locale, { month: 'short' }),
160+
new Date(1970, 8).toLocaleDateString(locale, { month: 'short' }),
161+
new Date(1970, 9).toLocaleDateString(locale, { month: 'short' }),
162+
new Date(1970, 10).toLocaleDateString(locale, { month: 'short' }),
163+
new Date(1970, 11).toLocaleDateString(locale, { month: 'short' }),
162164
]
163165
}

tests/date.test.ts

Lines changed: 155 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,38 @@ describe('date', () => {
8282
})
8383
})
8484

85-
describe('getDayNames', () => {
85+
describe('getDayNames TZ=Europe/Berlin', () => {
86+
beforeEach(() => {
87+
vi.stubEnv('TZ', 'Europe/Berlin')
88+
})
89+
afterEach(() => {
90+
vi.unstubAllEnvs()
91+
// @ts-expect-error - Mocking for tests
92+
delete globalThis.dayNames
93+
})
94+
95+
it('returns `globalThis.dayNames` when defined', () => {
96+
globalThis.dayNames = ['Day 0', 'Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5', 'Day 6']
97+
expect(getDayNames()).toEqual(globalThis.dayNames)
98+
})
99+
100+
it('returns English day names in "en-US" locale when `globalThis.dayNames` is not defined', () => {
101+
getCanonicalLocale.mockReturnValue('en-US')
102+
expect(getDayNames()).toEqual(['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'])
103+
})
104+
105+
it('returns German day names in "de-DE" locale when `globalThis.dayNames` is not defined', () => {
106+
getCanonicalLocale.mockReturnValue('de-DE')
107+
expect(getDayNames()).toEqual(['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'])
108+
})
109+
})
110+
111+
describe('getDayNames TZ=America/New_York', () => {
112+
beforeEach(() => {
113+
vi.stubEnv('TZ', 'America/New_York')
114+
})
86115
afterEach(() => {
116+
vi.unstubAllEnvs()
87117
// @ts-expect-error - Mocking for tests
88118
delete globalThis.dayNames
89119
})
@@ -104,8 +134,12 @@ describe('date', () => {
104134
})
105135
})
106136

107-
describe('getDayNamesShort', () => {
137+
describe('getDayNamesShort TZ=Europe/Berlin', () => {
138+
beforeEach(() => {
139+
vi.stubEnv('TZ', 'Europe/Berlin')
140+
})
108141
afterEach(() => {
142+
vi.unstubAllEnvs()
109143
// @ts-expect-error - Mocking for tests
110144
delete globalThis.dayNamesShort
111145
})
@@ -126,8 +160,38 @@ describe('date', () => {
126160
})
127161
})
128162

129-
describe('getDayNamesMin', () => {
163+
describe('getDayNamesShort TZ=America/New_York', () => {
164+
beforeEach(() => {
165+
vi.stubEnv('TZ', 'America/New_York')
166+
})
130167
afterEach(() => {
168+
vi.unstubAllEnvs()
169+
// @ts-expect-error - Mocking for tests
170+
delete globalThis.dayNamesShort
171+
})
172+
173+
it('returns `globalThis.dayNamesShort` when defined', () => {
174+
globalThis.dayNamesShort = ['D. 0', 'D. 1', 'D. 2', 'D. 3', 'D. 4', 'D. 5', 'D. 6']
175+
expect(getDayNamesShort()).toEqual(globalThis.dayNamesShort)
176+
})
177+
178+
it('returns English short day names from `Intl` in "en-US" locale when `globalThis.dayNamesShort` is not defined', () => {
179+
getCanonicalLocale.mockReturnValue('en-US')
180+
expect(getDayNamesShort()).toEqual(['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'])
181+
})
182+
183+
it('returns German short day names from `Intl` in "de-DE" locale when `globalThis.dayNamesShort` is not defined', () => {
184+
getCanonicalLocale.mockReturnValue('de-DE')
185+
expect(getDayNamesShort()).toEqual(['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'])
186+
})
187+
})
188+
189+
describe('getDayNamesMin TZ=Europe/Berlin', () => {
190+
beforeEach(() => {
191+
vi.stubEnv('TZ', 'Europe/Berlin')
192+
})
193+
afterEach(() => {
194+
vi.unstubAllEnvs()
131195
// @ts-expect-error - Mocking for tests
132196
delete globalThis.dayNamesMin
133197
})
@@ -148,8 +212,64 @@ describe('date', () => {
148212
})
149213
})
150214

151-
describe('getMonthNames', () => {
215+
describe('getDayNamesMin TZ=America/New_York', () => {
216+
beforeEach(() => {
217+
vi.stubEnv('TZ', 'America/New_York')
218+
})
152219
afterEach(() => {
220+
vi.unstubAllEnvs()
221+
// @ts-expect-error - Mocking for tests
222+
delete globalThis.dayNamesMin
223+
})
224+
225+
it('returns `globalThis.dayNamesMin` when defined', () => {
226+
globalThis.dayNamesMin = ['D0', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6']
227+
expect(getDayNamesMin()).toEqual(globalThis.dayNamesMin)
228+
})
229+
230+
it('returns English narrow day names from `Intl` in "en-US" locale when `globalThis.dayNamesMin` is not defined', () => {
231+
getCanonicalLocale.mockReturnValue('en-US')
232+
expect(getDayNamesMin()).toEqual(['S', 'M', 'T', 'W', 'T', 'F', 'S'])
233+
})
234+
235+
it('returns German narrow day names from `Intl` in "de-DE" locale when `globalThis.dayNamesMin` is not defined', () => {
236+
getCanonicalLocale.mockReturnValue('de-DE')
237+
expect(getDayNamesMin()).toEqual(['S', 'M', 'D', 'M', 'D', 'F', 'S'])
238+
})
239+
})
240+
241+
describe('getMonthNames TZ=Europe/Berlin', () => {
242+
beforeEach(() => {
243+
vi.stubEnv('TZ', 'Europe/Berlin')
244+
})
245+
afterEach(() => {
246+
vi.unstubAllEnvs()
247+
// @ts-expect-error - Mocking for tests
248+
delete globalThis.monthNames
249+
})
250+
251+
it('returns `globalThis.monthNames` when defined', () => {
252+
globalThis.monthNames = ['Month 0', 'Month 1', 'Month 2', 'Month 3', 'Month 4', 'Month 5', 'Month 6', 'Month 7', 'Month 8', 'Month 9', 'Month 10', 'Month 11']
253+
expect(getMonthNames()).toEqual(globalThis.monthNames)
254+
})
255+
256+
it('returns English month names from `Intl` in "en-US" locale when `globalThis.monthNames` is not defined', () => {
257+
getCanonicalLocale.mockReturnValue('en-US')
258+
expect(getMonthNames()).toEqual(['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'])
259+
})
260+
261+
it('returns German month names from `Intl` in "de-DE" locale when `globalThis.monthNames` is not defined', () => {
262+
getCanonicalLocale.mockReturnValue('de-DE')
263+
expect(getMonthNames()).toEqual(['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'])
264+
})
265+
})
266+
267+
describe('getMonthNames TZ=America/New_York', () => {
268+
beforeEach(() => {
269+
vi.stubEnv('TZ', 'America/New_York')
270+
})
271+
afterEach(() => {
272+
vi.unstubAllEnvs()
153273
// @ts-expect-error - Mocking for tests
154274
delete globalThis.monthNames
155275
})
@@ -170,8 +290,38 @@ describe('date', () => {
170290
})
171291
})
172292

173-
describe('getMonthNamesShort', () => {
293+
describe('getMonthNamesShort TZ=Europe/Berlin', () => {
294+
beforeEach(() => {
295+
vi.stubEnv('TZ', 'Europe/Berlin')
296+
})
297+
afterEach(() => {
298+
vi.unstubAllEnvs()
299+
// @ts-expect-error - Mocking for tests
300+
delete globalThis.monthNamesShort
301+
})
302+
303+
it('returns `globalThis.monthNamesShort` when defined', () => {
304+
globalThis.monthNamesShort = ['M. 0', 'M. 1', 'M. 2', 'M. 3', 'M. 4', 'M. 5', 'M. 6', 'M. 7', 'M. 8', 'M. 9', 'M. 10', 'M. 11']
305+
expect(getMonthNamesShort()).toEqual(globalThis.monthNamesShort)
306+
})
307+
308+
it('returns English short month names from `Intl` in "en-US" locale when `globalThis.monthNamesShort` is not defined', () => {
309+
getCanonicalLocale.mockReturnValue('en-US')
310+
expect(getMonthNamesShort()).toEqual(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
311+
})
312+
313+
it('returns German short month names from `Intl` in "de-DE" locale when `globalThis.monthNamesShort` is not defined', () => {
314+
getCanonicalLocale.mockReturnValue('de-DE')
315+
expect(getMonthNamesShort()).toEqual(['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'])
316+
})
317+
})
318+
319+
describe('getMonthNamesShort TZ=America/New_York', () => {
320+
beforeEach(() => {
321+
vi.stubEnv('TZ', 'America/New_York')
322+
})
174323
afterEach(() => {
324+
vi.unstubAllEnvs()
175325
// @ts-expect-error - Mocking for tests
176326
delete globalThis.monthNamesShort
177327
})

0 commit comments

Comments
 (0)