Skip to content

Commit

Permalink
fix(calendar-utils): getPaddingDayCount helper function (#2669)
Browse files Browse the repository at this point in the history
* fix(calendar-utils): fix getPaddingDayCount helper

chore: changeset

test: update correct retured value

test: update description

* refactor: kiss

* refactor: remove misleading comment

* chore: empty
  • Loading branch information
kark authored Dec 19, 2023
1 parent 33cccc8 commit 6bec284
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-dragons-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-uikit/calendar-utils': patch
---

Fix `getPaddingDayCount` helper function
4 changes: 4 additions & 0 deletions packages/calendar-utils/src/calendar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,8 @@ describe('getPaddingDayCount', () => {
expect(getPaddingDayCount('2019-01-01')).toEqual(2);
expect(getPaddingDayCount(NaN)).toEqual(0);
});
it('should return different values for different locale', () => {
expect(getPaddingDayCount('2023-10-23', 'en-US')).toEqual(0);
expect(getPaddingDayCount('2023-10-23', 'en-GB')).toEqual(6);
});
});
4 changes: 1 addition & 3 deletions packages/calendar-utils/src/calendar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import moment from 'moment';
import inRange from 'lodash/inRange';
import type { MomentInput, DurationInputArg1 } from 'moment';

export const getDaysInMonth = (day: MomentInput) => moment(day).daysInMonth();
Expand All @@ -20,8 +19,7 @@ export const getPaddingDayCount = (day: MomentInput, locale: string) => {
const firstDayOfMonth = moment(day).startOf('month').day();
const paddingDayCount = (firstDayOfMonth - firstDayOfWeek + 7) % 7;

// ensure number is always positive
return inRange(paddingDayCount, 0, 6) ? paddingDayCount : 0;
return Number.isNaN(paddingDayCount) ? 0 : paddingDayCount;
};

export const getWeekdayNames = (locale: string) => {
Expand Down

1 comment on commit 6bec284

@vercel
Copy link

@vercel vercel bot commented on 6bec284 Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.