Skip to content

Commit

Permalink
feat: add getWeek (#673)
Browse files Browse the repository at this point in the history
* feat: getWeek

* fix: Codecov

* chore: Get coverage back to 100%

---------

Co-authored-by: Dmitriy Kovalenko <dmtr.kovalenko@outlook.com>
  • Loading branch information
simon-tackstrand and dmtrKovalenko authored Dec 19, 2024
1 parent 4fe3848 commit 287b8dc
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 3 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ jobs:
run: yarn build
- name: Test
run: yarn test:coverage
- name: Upload codecov
run: npx codecov
- uses: codecov/codecov-action@v5
with:
fail_ci_if_error: true
flags: unittests
token: ${{ secrets.CODECOV_TOKEN }}
4 changes: 4 additions & 0 deletions __tests__/calculations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ describe("DateTime calculations", () => {
expect(utils.getYear(date)).toBe(2018);
});

utilsTest("getWeek", (date, utils) => {
expect(utils.getWeek(date)).toBe(44);
});

utilsTest("getMonth", (date, utils) => {
expect(utils.getMonth(date)).toBe(9);
});
Expand Down
4 changes: 4 additions & 0 deletions __tests__/date-fns-jalali.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ describe("DateFnsJalali", () => {
}
});

it("DateFnsJalali -- getWeek", () => {
expect(utils.getWeek(date)).toBe(33);
});

it("DateFnsJalali -- getYearRange", () => {
const yearRange = utils.getYearRange(date, utils.setYear(date, 1503));

Expand Down
6 changes: 6 additions & 0 deletions __tests__/hijri.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ describe("Hijri", () => {
);
});

it("Hijiri -- getWeek", () => {
const date = hijriiUtils.date(TEST_TIMESTAMP);

expect(hijriiUtils.getWeek(date)).toBe(44);
});

test.each`
format | expected
${"keyboardDate"} | ${"١٤٤١/٠٥/٠٦"}
Expand Down
48 changes: 48 additions & 0 deletions __tests__/jalaali.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,54 @@ describe("Jalaali", () => {
]);
});

it("Jalaali -- getWeek", () => {
const date = jalaaliUtils.date(TEST_TIMESTAMP);

expect(jalaaliUtils.getWeek(date)).toEqual(44);
});

it("Jalaali -- addMonths", () => {
const date = jalaaliUtils.date(TEST_TIMESTAMP);
const newDate = jalaaliUtils.addMonths(date, 2);

expect(jalaaliUtils.getMonth(newDate)).toBe(9);
});

it("Jalaali -- addYears", () => {
const date = jalaaliUtils.date(TEST_TIMESTAMP);
const newDate = jalaaliUtils.addYears(date, 2);

expect(jalaaliUtils.getYear(newDate)).toBe(1399);
});

it("Jalaali -- isSameMonth", () => {
const date = jalaaliUtils.date(TEST_TIMESTAMP);
const anotherDate = jalaaliUtils.date(TEST_TIMESTAMP);

expect(jalaaliUtils.isSameMonth(date, anotherDate)).toBeTruthy();
});

it("Jalaali -- isSameYear", () => {
const date = jalaaliUtils.date(TEST_TIMESTAMP);
const anotherDate = jalaaliUtils.date(TEST_TIMESTAMP);

expect(jalaaliUtils.isSameYear(date, anotherDate)).toBeTruthy();
});

it("Jalaali -- setMonth", () => {
const date = jalaaliUtils.date(TEST_TIMESTAMP);
const newDate = jalaaliUtils.setMonth(date, 0);

expect(jalaaliUtils.getMonth(newDate)).toBe(0);
});

it("Jalaali -- getMonthArray", () => {
const date = jalaaliUtils.date(TEST_TIMESTAMP);
const array = jalaaliUtils.getMonthArray(date);

expect(array.map((dt) => dt.month())).toEqual([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1]);
});

it("Jalaali -- getYearRange", () => {
const date = jalaaliUtils.date(TEST_TIMESTAMP);
const anotherYear = jalaaliUtils.setYear(date, 1400);
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"@types/jest": "^26.0.0",
"cldr-data": "^36.0.0",
"cldrjs": "^0.5.5",
"codecov": "^3.6.1",
"dayjs": "^1.8.17",
"full-icu": "^1.3.0",
"husky": "^5.0.9",
Expand Down
1 change: 1 addition & 0 deletions packages/core/IUtils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export interface IUtils<TDate, TLocale> {
getDate(value: TDate): number;
setDate(value: TDate, count: number): TDate;

getWeek(value: TDate): number;
getMonth(value: TDate): number;
getDaysInMonth(value: TDate): number;
setMonth(value: TDate, count: number): TDate;
Expand Down
5 changes: 5 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 @@ -23,6 +23,7 @@ import { formatISO } from "date-fns-jalali/formatISO";
import { getHours } from "date-fns-jalali/getHours";
import { getSeconds } from "date-fns-jalali/getSeconds";
import { getYear } from "date-fns-jalali/getYear";
import { getWeek } from "date-fns-jalali/getWeek";
import { getMonth } from "date-fns-jalali/getMonth";
import { getDate } from "date-fns-jalali/getDate";
import { getDay } from "date-fns-jalali/getDay";
Expand Down Expand Up @@ -398,6 +399,10 @@ export default class DateFnsJalaliUtils implements IUtils<Date, Locale> {
return getMinutes(date);
};

public getWeek = (date: Date) => {
return getWeek(date);
};

public getMonth = (date: Date) => {
return getMonth(date);
};
Expand Down
5 changes: 5 additions & 0 deletions packages/date-fns/src/date-fns-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { getDay } from "date-fns/getDay";
import { getDaysInMonth } from "date-fns/getDaysInMonth";
import { getHours } from "date-fns/getHours";
import { getMinutes } from "date-fns/getMinutes";
import { getWeek } from "date-fns/getWeek";
import { getMonth } from "date-fns/getMonth";
import { getSeconds } from "date-fns/getSeconds";
import { getYear } from "date-fns/getYear";
Expand Down Expand Up @@ -383,6 +384,10 @@ export default class DateFnsUtils implements IUtils<Date, Locale> {
return setDate(date, count);
};

public getWeek = (date: Date) => {
return getWeek(date);
};

public getMonth = (date: Date) => {
return getMonth(date);
};
Expand Down
6 changes: 6 additions & 0 deletions packages/dayjs/src/dayjs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import defaultDayjs, { QUnitType } from "dayjs";
import customParseFormatPlugin from "dayjs/plugin/customParseFormat";
import localizedFormatPlugin from "dayjs/plugin/localizedFormat";
import isBetweenPlugin from "dayjs/plugin/isBetween";
import weekOfYear from "dayjs/plugin/weekOfYear";
import { IUtils, DateIOFormats, Unit } from "@date-io/core/IUtils";

defaultDayjs.extend(customParseFormatPlugin);
defaultDayjs.extend(localizedFormatPlugin);
defaultDayjs.extend(isBetweenPlugin);
defaultDayjs.extend(weekOfYear);

interface Opts {
locale?: string;
Expand Down Expand Up @@ -282,6 +284,10 @@ export default class DayjsUtils<TDate extends Dayjs = Dayjs>
return date.set("second", count) as TDate;
};

public getWeek = (date: Dayjs) => {
return date.week();
};

public getMonth = (date: Dayjs) => {
return date.month();
};
Expand Down
4 changes: 4 additions & 0 deletions packages/hijri/src/hijri-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ export default class MomentUtils extends DefaultMomentUtils {
return date.iYear() > value.iYear();
};

public getWeek = (date: Moment) => {
return date.get("week");
};

public getMonth = (date: Moment) => {
return date.iMonth();
};
Expand Down
5 changes: 5 additions & 0 deletions packages/jalaali/src/jalaali-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export default class MomentUtils extends DefaultMomentUtils {
return date.jYear() > value.jYear();
};

public getWeek = (date: Moment) => {
return date.get("week");
};

public getMonth = (date: Moment) => {
return date.jMonth();
};
Expand Down Expand Up @@ -214,6 +218,7 @@ export default class MomentUtils extends DefaultMomentUtils {

return years;
};

public addMonths = (date: Moment, count: number) => {
return count < 0
? date.clone().subtract(Math.abs(count), "jMonth")
Expand Down
4 changes: 4 additions & 0 deletions packages/js-joda/src/js-joda-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ export default class JsJodaUtils implements IUtils<Temporal, Locale> {
return date.with(ChronoField.SECOND_OF_MINUTE, count);
}

getWeek(date: Temporal): number {
return date.get(ChronoField.ALIGNED_WEEK_OF_YEAR);
}

getMonth(date: Temporal): number {
return date.get(ChronoField.MONTH_OF_YEAR) - 1;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/luxon/src/luxon-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default class LuxonUtils implements IUtils<DateTime, string> {
return DateTime.fromJSDate(value, { locale: this.locale }) as TRes;
}

/* istanbul ignore next */
return DateTime.local() as TRes;
}

Expand Down Expand Up @@ -288,6 +289,10 @@ export default class LuxonUtils implements IUtils<DateTime, string> {
return value.set({ second: count });
};

public getWeek = (value: DateTime) => {
return value.get("weekNumber");
};

public getMonth = (value: DateTime) => {
// See https://github.com/moment/luxon/blob/master/docs/moment.md#major-functional-differences
return value.get("month") - 1;
Expand Down
4 changes: 4 additions & 0 deletions packages/moment/src/moment-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ export default class MomentUtils implements IUtils<defaultMoment.Moment, string>
return date.clone().seconds(count);
};

public getWeek = (date: Moment) => {
return date.get("week");
};

public getMonth = (date: Moment) => {
return date.get("month");
};
Expand Down

0 comments on commit 287b8dc

Please sign in to comment.