Skip to content
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

🐛 Day of week labels scrambled when using startOfWeek #25

Open
nikez opened this issue Jun 3, 2024 · 1 comment
Open

🐛 Day of week labels scrambled when using startOfWeek #25

nikez opened this issue Jun 3, 2024 · 1 comment
Assignees
Labels
Type: Bug Something isn't working

Comments

@nikez
Copy link

nikez commented Jun 3, 2024

Describe the bug
If you choose that the "startOfWeek" is, e.g. 1 for Monday, the day of the week labels get scrambled.

To Reproduce

  1. Set startOfWeek="1"
  2. Open datepicker
  3. Look at day of the week labels

Expected behavior
I expect it to not be scrambled.

Screenshots
image

Desktop

  • OS: Windows 11
  • Browser: Firefox / Chrome
  • Version 126.0.1 FF, unknown Chrome

Smartphone - Didn't test

  • Device: N/A
  • OS: N/A
  • Browser N/A
  • Version N/A

Additional context

@nikez nikez added the Type: Bug Something isn't working label Jun 3, 2024
@dysfunc dysfunc self-assigned this Jun 3, 2024
@jamesscottbrown
Copy link

The problem is that you are providing a string (startOfWeek="1") rather than a number (startOfWeek={1}).

The labels for the days are constructed by the expression dowLabels[(labelIndex + startOfWeek) % 7].

If startOfWeek is a string, then the + operator is interpreted as string concatenation rather than addition: labelIndex is converted to a string, with a 1 added to the end. This string is then cast back to a number when the modulus (%) is taken.

Adding a string to a number:

[0, 1, 2, 3, 4, 5, 6].map(i => (i+ "1")) 
["01",  "11", "21", "31", "41", "51", "61"]
[0,1,2,3,4,5,6].map(i => (i+ "1") % 7)
[ 1, 4, 0, 3, 6, 2, 5 ]

The library could be made more robust against misuse by using the parseNumber function (dowLabels[(labelIndex + parseNumber(startOfWeek, 10)) % 7]) or an extra + operator (dowLabels[(labelIndex + +startOfWeek) % 7] - note the space to distinguish from a ++ increment operator) to cast startOfWeek to a number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants