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

feat: isBeforeMonth and isAfterMonth #648

Merged
merged 9 commits into from
Jul 23, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "15"
node-version: "18"
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
Expand Down
41 changes: 30 additions & 11 deletions __tests__/calculations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,23 @@ describe("DateTime calculations", () => {
});

utilsTest("startOfWeekNonISO", (date, utils, lib) => {
expect(utils.formatByString(utils.startOfWeek(utils.date("2018-10-28T00:00:00.000Z")), formats.dateTime[lib])).toBe(
lib === "Luxon" ? "2018-10-22 00:00" : "2018-10-28 00:00"
);
expect(
utils.formatByString(
utils.startOfWeek(utils.date("2018-10-28T00:00:00.000Z")),
formats.dateTime[lib]
)
).toBe(lib === "Luxon" ? "2018-10-22 00:00" : "2018-10-28 00:00");
});

utilsTest("endOfWeekNonISO", (date, utils, lib) => {
expect(utils.formatByString(utils.endOfWeek(utils.date("2018-10-28T00:00:00.000Z")), formats.dateTime[lib])).toBe(
lib === "Luxon" ? "2018-10-28 23:59" : "2018-11-03 23:59"
);
expect(
utils.formatByString(
utils.endOfWeek(utils.date("2018-10-28T00:00:00.000Z")),
formats.dateTime[lib]
)
).toBe(lib === "Luxon" ? "2018-10-28 23:59" : "2018-11-03 23:59");
});


utilsTest("getPreviousMonth", (date, utils, lib) => {
expect(
utils.formatByString(utils.getPreviousMonth(date), formats.dateTime[lib])
Expand Down Expand Up @@ -243,18 +248,32 @@ describe("DateTime calculations", () => {
expect(utils.isBefore(utils.date(), date)).toBeFalsy();
});

utilsTest("isBeforeDay", (date, utils, lib) => {
const previousDay = utils.addDays(date, -1);

expect(utils.isBeforeDay(date, previousDay)).toBeFalsy();
expect(utils.isBeforeDay(previousDay, date)).toBeTruthy();
});

utilsTest("isAfterDay", (date, utils, lib) => {
const nextDay = utils.addDays(date, 1);

expect(utils.isAfterDay(nextDay, date)).toBeTruthy();
expect(utils.isAfterDay(date, nextDay)).toBeFalsy();
});

utilsTest("isBeforeDay", (date, utils, lib) => {
const previousDay = utils.addDays(date, -1);
utilsTest("isBeforeMonth", (date, utils, lib) => {
const previousMonth = utils.addMonths(date, -1);

expect(utils.isBeforeDay(date, previousDay)).toBeFalsy();
expect(utils.isBeforeDay(previousDay, date)).toBeTruthy();
expect(utils.isBeforeMonth(date, previousMonth)).toBeFalsy();
expect(utils.isBeforeMonth(previousMonth, date)).toBeTruthy();
});

utilsTest("isAfterMonth", (date, utils, lib) => {
const next = utils.addMonths(date, 1);

expect(utils.isAfterMonth(date, next)).toBeFalsy();
expect(utils.isAfterMonth(next, date)).toBeTruthy();
});

utilsTest("isAfterYear", (date, utils, lib) => {
Expand Down
16 changes: 15 additions & 1 deletion __tests__/date-fns-jalali.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,20 @@ describe("DateFnsJalali", () => {
});

it("DateFnsJalali -- isBeforeDay", () => {
const previousMonth = utils.addMonths(date, -1);

expect(utils.isBeforeMonth(date, previousMonth)).toBeFalsy();
expect(utils.isBeforeMonth(previousMonth, date)).toBeTruthy();
});

it("DateFnsJalali -- isAfterMonth", () => {
const nextMonth = utils.addMonths(date, 1);

expect(utils.isAfterMonth(nextMonth, date)).toBeTruthy();
expect(utils.isAfterMonth(date, nextMonth)).toBeFalsy();
});

it("DateFnsJalali -- isBeforeMonth", () => {
const previousDay = utils.addDays(date, -1);

expect(utils.isBeforeDay(date, previousDay)).toBeFalsy();
Expand Down Expand Up @@ -335,7 +349,7 @@ describe("DateFnsJalali", () => {
it("DateFnsJalali -- parseISO", () => {
const parsedDate = utils.parseISO(TEST_TIMESTAMP);

expect(utils.toISO(parsedDate)).toEqual(TEST_TIMESTAMP.replace(".000Z", "Z"))
expect(utils.toISO(parsedDate)).toEqual(TEST_TIMESTAMP.replace(".000Z", "Z"));
});

it("DateFnsJalali -- isNull", () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/IUtils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ export interface IUtils<TDate extends ExtendableDateType> {

isAfter(value: TDate, comparing: TDate): boolean;
isAfterDay(value: TDate, comparing: TDate): boolean;
isAfterMonth(value: TDate, comparing: TDate): boolean;
isAfterYear(value: TDate, comparing: TDate): boolean;

isBefore(value: TDate, comparing: TDate): boolean;
isBeforeDay(value: TDate, comparing: TDate): boolean;
isBeforeMonth(value: TDate, comparing: TDate): boolean;
isBeforeYear(value: TDate, comparing: TDate): boolean;
isBefore(value: TDate, comparing: TDate): boolean;

isWithinRange(value: TDate, range: [TDate, TDate]): boolean;

Expand Down
8 changes: 8 additions & 0 deletions packages/date-fns-jalali/src/date-fns-jalali-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@ export default class DateFnsJalaliUtils implements IUtils<Date> {
return isBefore(date, startOfYear(value));
};

public isBeforeMonth(value: Date, comparing: Date): boolean {
return isBefore(value, startOfMonth(comparing));
}

public isAfterMonth(value: Date, comparing: Date): boolean {
return isAfter(value, startOfMonth(comparing));
}

public isAfterYear = (date: Date, value: Date) => {
return isAfter(date, endOfYear(value));
};
Expand Down
8 changes: 8 additions & 0 deletions packages/date-fns/src/date-fns-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@ export default class DateFnsUtils implements IUtils<Date> {
return isBefore(date, startOfYear(value));
};

public isBeforeMonth(value: Date, comparing: Date): boolean {
return isBefore(value, startOfMonth(comparing));
}

public isAfterMonth(value: Date, comparing: Date): boolean {
return isAfter(value, startOfMonth(comparing));
}

public isAfterYear = (date: Date, value: Date) => {
return isAfter(date, endOfYear(value));
};
Expand Down
8 changes: 8 additions & 0 deletions packages/dayjs/src/dayjs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ export default class DayjsUtils<TDate extends Dayjs = Dayjs> implements IUtils<T
return date.isBefore(value, "day");
};

public isAfterMonth = (date: Dayjs, value: Dayjs) => {
return date.isAfter(value, "month");
};

public isBeforeMonth = (date: Dayjs, value: Dayjs) => {
return date.isBefore(value, "month");
};

public isBeforeYear = (date: Dayjs, value: Dayjs) => {
return date.isBefore(value, "year");
};
Expand Down
8 changes: 8 additions & 0 deletions packages/js-joda/src/js-joda-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,14 @@ export default class JsJodaUtils implements IUtils<Temporal> {
return datedate.isBefore(valuedate);
}

isBeforeMonth(value: Temporal, comparing: Temporal): boolean {
return YearMonth.from(value).isBefore(YearMonth.from(comparing));
}

isAfterMonth(value: Temporal, comparing: Temporal): boolean {
return YearMonth.from(value).isAfter(YearMonth.from(comparing));
}

isBeforeYear(date: Temporal, value: Temporal): boolean {
let datedate = Year.from(date);
let valuedate = Year.from(value);
Expand Down
12 changes: 11 additions & 1 deletion packages/luxon/src/luxon-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ export default class LuxonUtils implements IUtils<DateTime> {
return diff.days! > 0;
};

public isBeforeMonth = (value: DateTime, comparing: DateTime) => {
const diff = value.diff(comparing.startOf("month"), "months").toObject();
return diff.months! < 0;
};

public isAfterMonth = (value: DateTime, comparing: DateTime) => {
const diff = value.diff(comparing.startOf("month"), "months").toObject();
return diff.months! > 0;
};

public isBeforeYear = (value: DateTime, comparing: DateTime) => {
const diff = value.diff(comparing.startOf("year"), "years").toObject();
return diff.years! < 0;
Expand Down Expand Up @@ -339,7 +349,7 @@ export default class LuxonUtils implements IUtils<DateTime> {
};

public getWeekdays = () => {
return Info.weekdaysFormat("narrow", { locale: this.locale });
return Info.weekdaysFormat("short", { locale: this.locale });
};

public getWeekArray = (date: DateTime) => {
Expand Down
8 changes: 8 additions & 0 deletions packages/moment/src/moment-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ export default class MomentUtils implements IUtils<defaultMoment.Moment> {
return date.isBefore(value, "day");
};

public isBeforeMonth = (date: Moment, value: Moment) => {
return date.isBefore(value, "month");
};

public isAfterMonth = (date: Moment, value: Moment) => {
return date.isAfter(value, "month");
};

public isBeforeYear = (date: Moment, value: Moment) => {
return date.isBefore(value, "year");
};
Expand Down
Loading