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

fix: update jalaali-utils getDaysInMonth and add missing overrides #638

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};
}
Loading