Skip to content

Commit

Permalink
Add FromString methods
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez committed Jan 30, 2024
1 parent 0c769b0 commit 51ef614
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"homepage": "https://github.com/unosquare/uno-js#readme",
"devDependencies": {
"@types/jest": "^29.5.11",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"eslint": "^8.56.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-config-prettier": "^9.1.0",
Expand Down
60 changes: 30 additions & 30 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/dateRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ export class YearQuarter extends DateRange implements IYearQuarterDateRange {
return new YearQuarter(date.getFullYear(), Math.floor(date.getMonth() / 3 + 1));
}

static FromString(date: string): YearQuarter {
const [year, quarter] = date.split('-Q');
return new YearQuarter(parseInt(year), parseInt(quarter));
}

static get CurrentQuarter(): number {
return Math.floor(new Date().getMonth() / 3 + 1);
}
Expand Down Expand Up @@ -158,6 +163,11 @@ export class YearMonth extends DateRange implements IYearMonthDateRange {
return new YearMonth(date.getFullYear(), date.getMonth() + 1);
}

static FromString(date: string): YearMonth {
const [year, month] = date.split('-');
return new YearMonth(parseInt(year), parseInt(month));
}

get IsCurrent(): boolean {
return this.Year === new Date().getFullYear() && this.Month === new Date().getMonth() + 1;
}
Expand Down Expand Up @@ -211,6 +221,11 @@ export class YearWeek extends DateRange implements IYearWeekDateRange {
return new YearWeek(date.getFullYear(), getWeekNumber(date));
}

static FromString(date: string): YearWeek {
const [year, week] = date.split('-W');
return new YearWeek(parseInt(year), parseInt(week));
}

get Next(): YearWeek {
const nextWeek = this.Week + 1;
const nextYear = this.Year + (nextWeek > 52 ? 1 : 0);
Expand Down Expand Up @@ -256,6 +271,11 @@ export class YearWeekIso extends DateRange implements IYearWeekDateRange {
return new YearWeekIso(date.getFullYear(), getWeekIsoNumber(date));
}

static FromString(date: string): YearWeekIso {
const [year, week] = date.split('-W');
return new YearWeekIso(parseInt(year), parseInt(week));
}

get Next(): YearWeekIso {
const nextWeek = this.Week + 1;
const nextYear = this.Year + (nextWeek > 52 ? 1 : 0);
Expand Down
36 changes: 36 additions & 0 deletions test/dateRange.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ describe('YearQuarter', () => {
expect(yearQuarter.Quarter).toBe(Math.floor(new Date().getMonth() / 3 + 1));
});

it('should contains current year and quarter from string', () => {
const yearQuarter = YearQuarter.FromString('2019-Q1');
expect(yearQuarter.Year).toBe(2019);
expect(yearQuarter.Quarter).toBe(1);
});

it('should contains months', () => {
const yearQuarter = new YearQuarter(2019, 1);
expect(yearQuarter.YearMonths.length).toBe(3);
Expand Down Expand Up @@ -129,6 +135,12 @@ describe('YearMonth', () => {
expect(yearMonth.Month).toBe(new Date().getMonth() + 1);
});

it('should contains current year and month from string', () => {
const yearMonth = YearMonth.FromString('2019-01');
expect(yearMonth.Year).toBe(2019);
expect(yearMonth.Month).toBe(1);
});

it('should contains a YearQuarter', () => {
const yearMonth = new YearMonth(2019, 1);
expect(yearMonth.YearQuarter.Year).toBe(2019);
Expand Down Expand Up @@ -216,6 +228,18 @@ describe('YearWeek', () => {
const yearWeek = new YearWeek(2019, 1);
expect(yearWeek.toString()).toBe('2019-W01');
});

it('should contains current year and week from date', () => {
const yearWeek = YearWeek.FromDate(new Date());
expect(yearWeek.Year).toBe(new Date().getFullYear());
expect(yearWeek.Week).toBe(getWeekNumber(new Date()));
});

it('should contains current year and week from string', () => {
const yearWeek = YearWeek.FromString('2019-W01');
expect(yearWeek.Year).toBe(2019);
expect(yearWeek.Week).toBe(1);
});
});

describe('YearWeekIso', () => {
Expand Down Expand Up @@ -265,4 +289,16 @@ describe('YearWeekIso', () => {
const yearWeekIso = new YearWeek(2019, 1);
expect(yearWeekIso.toString()).toBe('2019-W01');
});

it('should contains current year and week from date', () => {
const yearWeekIso = YearWeekIso.FromDate(new Date());
expect(yearWeekIso.Year).toBe(new Date().getFullYear());
expect(yearWeekIso.Week).toBe(getWeekIsoNumber(new Date()));
});

it('should contains current year and week from string', () => {
const yearWeekIso = YearWeekIso.FromString('2019-W01');
expect(yearWeekIso.Year).toBe(2019);
expect(yearWeekIso.Week).toBe(1);
});
});

0 comments on commit 51ef614

Please sign in to comment.