Skip to content

Commit a2d6e8e

Browse files
fix: update jalaali-utils getDaysInMonth and add missing overrides (#638)
* fix: update jalaali-utils getDaysInMonth and add missing overrides * rebase * rebase --------- Co-authored-by: Dmitriy Kovalenko <[email protected]>
1 parent 9001820 commit a2d6e8e

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

__tests__/jalaali.test.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe("Jalaali", () => {
4343
it("Should proper work with jalaali days in month", () => {
4444
const date = jalaaliUtils.date(TEST_TIMESTAMP);
4545

46-
expect(jalaaliUtils.getDaysInMonth(date)).toBe(31);
46+
expect(jalaaliUtils.getDaysInMonth(date)).toBe(30);
4747
});
4848

4949
it("Should properly render meridiem", () => {
@@ -58,9 +58,7 @@ describe("Jalaali", () => {
5858
it("Jalaali -- getYear", () => {
5959
const date = jalaaliUtils.date(TEST_TIMESTAMP);
6060

61-
expect(jalaaliUtils.getYear(date)).toEqual(
62-
1397
63-
);
61+
expect(jalaaliUtils.getYear(date)).toEqual(1397);
6462
});
6563

6664
it("Jalaali -- setYear", () => {
@@ -74,9 +72,7 @@ describe("Jalaali", () => {
7472
it("Jalaali -- getDate", () => {
7573
const date = jalaaliUtils.date(TEST_TIMESTAMP);
7674

77-
expect(jalaaliUtils.getDate(date)).toEqual(
78-
8
79-
);
75+
expect(jalaaliUtils.getDate(date)).toEqual(8);
8076
});
8177

8278
it("Jalaali -- setDate", () => {
@@ -90,9 +86,7 @@ describe("Jalaali", () => {
9086
it("Jalaali -- getYear", () => {
9187
const date = jalaaliUtils.date(TEST_TIMESTAMP);
9288

93-
expect(jalaaliUtils.getYear(date)).toEqual(
94-
1397
95-
);
89+
expect(jalaaliUtils.getYear(date)).toEqual(1397);
9690
});
9791

9892
it("Jalaali -- endOfYear", () => {

packages/jalaali/src/jalaali-utils.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,8 @@ export default class MomentUtils extends DefaultMomentUtils {
107107
return date.jMonth();
108108
};
109109

110-
public setMonth = (date: Moment, month: number) => {
111-
return date.clone().jMonth(month);
112-
};
113-
114110
public getDaysInMonth = (date: Moment) => {
115-
return date.daysInMonth();
111+
return this.moment.jDaysInMonth(date.jYear(), date.jMonth());
116112
};
117113

118114
public startOfYear = (date: Moment) => {
@@ -218,4 +214,39 @@ export default class MomentUtils extends DefaultMomentUtils {
218214

219215
return years;
220216
};
217+
public addMonths = (date: Moment, count: number) => {
218+
return count < 0
219+
? date.clone().subtract(Math.abs(count), "jMonth")
220+
: date.clone().add(count, "jMonth");
221+
};
222+
223+
public addYears = (date: Moment, count: number) => {
224+
return count < 0
225+
? date.clone().subtract(Math.abs(count), "jYear")
226+
: date.clone().add(count, "jYear");
227+
};
228+
229+
public isSameMonth = (date: Moment, comparing: Moment) => {
230+
return date.jYear() === comparing.jYear() && date.jMonth() === comparing.jMonth();
231+
};
232+
233+
public isSameYear = (date: Moment, comparing: Moment) => {
234+
return date.jYear() === comparing.jYear();
235+
};
236+
237+
public setMonth = (date: Moment, count: number) => {
238+
return date.clone().jMonth(count);
239+
};
240+
241+
public getMonthArray = (date: Moment) => {
242+
const firstMonth = date.clone().startOf("jYear");
243+
const monthArray = [firstMonth];
244+
245+
while (monthArray.length < 12) {
246+
const prevMonth = monthArray[monthArray.length - 1];
247+
monthArray.push(this.getNextMonth(prevMonth));
248+
}
249+
250+
return monthArray;
251+
};
221252
}

0 commit comments

Comments
 (0)