Skip to content

Commit

Permalink
fix: update jalaali-utils getDaysInMonth and add missing overrides (#638
Browse files Browse the repository at this point in the history
)

* fix: update jalaali-utils getDaysInMonth and add missing overrides

* rebase

* rebase

---------

Co-authored-by: Dmitriy Kovalenko <[email protected]>
  • Loading branch information
mrafei and dmtrKovalenko authored Mar 12, 2024
1 parent 9001820 commit a2d6e8e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
14 changes: 4 additions & 10 deletions __tests__/jalaali.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("Jalaali", () => {
it("Should proper work with jalaali days in month", () => {
const date = jalaaliUtils.date(TEST_TIMESTAMP);

expect(jalaaliUtils.getDaysInMonth(date)).toBe(31);
expect(jalaaliUtils.getDaysInMonth(date)).toBe(30);
});

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

expect(jalaaliUtils.getYear(date)).toEqual(
1397
);
expect(jalaaliUtils.getYear(date)).toEqual(1397);
});

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

expect(jalaaliUtils.getDate(date)).toEqual(
8
);
expect(jalaaliUtils.getDate(date)).toEqual(8);
});

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

expect(jalaaliUtils.getYear(date)).toEqual(
1397
);
expect(jalaaliUtils.getYear(date)).toEqual(1397);
});

it("Jalaali -- endOfYear", () => {
Expand Down
41 changes: 36 additions & 5 deletions packages/jalaali/src/jalaali-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,8 @@ export default class MomentUtils extends DefaultMomentUtils {
return date.jMonth();
};

public setMonth = (date: Moment, month: number) => {
return date.clone().jMonth(month);
};

public getDaysInMonth = (date: Moment) => {
return date.daysInMonth();
return this.moment.jDaysInMonth(date.jYear(), date.jMonth());
};

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

return years;
};
public addMonths = (date: Moment, count: number) => {
return count < 0
? date.clone().subtract(Math.abs(count), "jMonth")
: date.clone().add(count, "jMonth");
};

public addYears = (date: Moment, count: number) => {
return count < 0
? date.clone().subtract(Math.abs(count), "jYear")
: date.clone().add(count, "jYear");
};

public isSameMonth = (date: Moment, comparing: Moment) => {
return date.jYear() === comparing.jYear() && date.jMonth() === comparing.jMonth();
};

public isSameYear = (date: Moment, comparing: Moment) => {
return date.jYear() === comparing.jYear();
};

public setMonth = (date: Moment, count: number) => {
return date.clone().jMonth(count);
};

public getMonthArray = (date: Moment) => {
const firstMonth = date.clone().startOf("jYear");
const monthArray = [firstMonth];

while (monthArray.length < 12) {
const prevMonth = monthArray[monthArray.length - 1];
monthArray.push(this.getNextMonth(prevMonth));
}

return monthArray;
};
}

0 comments on commit a2d6e8e

Please sign in to comment.