Skip to content

Commit

Permalink
Add TODOs to decade functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kossnocorp committed Jan 20, 2024
1 parent c0aa54c commit e0a272d
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/endOfDecade/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { toDate } from "../toDate/index.js";
export function endOfDecade<DateType extends Date>(
date: DateType | number | string,
): DateType {
// TODO: Switch to more technical definition in of decades that start with 1
// end with 0. I.e. 2001-2010 instead of current 2000-2009. It's a breaking
// change, so it can only be done in 4.0.
const _date = toDate(date);
const year = _date.getFullYear();
const decade = 9 + Math.floor(year / 10) * 10;
Expand Down
11 changes: 10 additions & 1 deletion src/endOfDecade/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */

import assert from "assert";
import { describe, it } from "vitest";
import { describe, expect, it } from "vitest";
import { endOfDecade } from "./index.js";

describe("endOfDecade", () => {
Expand Down Expand Up @@ -33,4 +33,13 @@ describe("endOfDecade", () => {
const result = endOfDecade(new Date(NaN));
assert(result instanceof Date && isNaN(result.getTime()));
});

it("properly works with negative numbers", () => {
expect(endOfDecade(new Date(2001, 0, 1))).toEqual(
new Date(2009, 11, 31, 23, 59, 59, 999),
);
expect(endOfDecade(new Date(-2009, 0, 1))).toEqual(
new Date(-2001, 11, 31, 23, 59, 59, 999),
);
});
});
3 changes: 3 additions & 0 deletions src/getDecade/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { toDate } from "../toDate/index.js";
export function getDecade<DateType extends Date>(
date: DateType | number | string,
): number {
// TODO: Switch to more technical definition in of decades that start with 1
// end with 0. I.e. 2001-2010 instead of current 2000-2009. It's a breaking
// change, so it can only be done in 4.0.
const _date = toDate(date);
const year = _date.getFullYear();
const decade = Math.floor(year / 10) * 10;
Expand Down
7 changes: 6 additions & 1 deletion src/getDecade/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */

import assert from "assert";
import { describe, it } from "vitest";
import { describe, expect, it } from "vitest";
import { getDecade } from "./index.js";

describe("getDecade", () => {
Expand All @@ -19,4 +19,9 @@ describe("getDecade", () => {
const result = getDecade(new Date(NaN));
assert(isNaN(result));
});

it("properly works with negative numbers", () => {
expect(getDecade(new Date(2009, 0, 1))).toBe(2000);
expect(getDecade(new Date(-2001, 0, 1))).toBe(-2010);
});
});
3 changes: 3 additions & 0 deletions src/lastDayOfDecade/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { toDate } from "../toDate/index.js";
export function lastDayOfDecade<DateType extends Date>(
date: DateType | number | string,
): DateType {
// TODO: Switch to more technical definition in of decades that start with 1
// end with 0. I.e. 2001-2010 instead of current 2000-2009. It's a breaking
// change, so it can only be done in 4.0.
const _date = toDate(date);
const year = _date.getFullYear();
const decade = 9 + Math.floor(year / 10) * 10;
Expand Down
11 changes: 10 additions & 1 deletion src/lastDayOfDecade/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */

import assert from "assert";
import { describe, it } from "vitest";
import { describe, expect, it } from "vitest";
import { lastDayOfDecade } from "./index.js";

describe("lastDayOfDecade", () => {
Expand All @@ -27,4 +27,13 @@ describe("lastDayOfDecade", () => {
const result = lastDayOfDecade(new Date(NaN));
assert(result instanceof Date && isNaN(result.getTime()));
});

it("properly works with negative numbers", () => {
expect(lastDayOfDecade(new Date(2001, 0, 1))).toEqual(
new Date(2009, 11, 31),
);
expect(lastDayOfDecade(new Date(-2009, 0, 1))).toEqual(
new Date(-2001, 11, 31),
);
});
});
3 changes: 3 additions & 0 deletions src/startOfDecade/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { toDate } from "../toDate/index.js";
export function startOfDecade<DateType extends Date>(
date: DateType | number | string,
): DateType {
// TODO: Switch to more technical definition in of decades that start with 1
// end with 0. I.e. 2001-2010 instead of current 2000-2009. It's a breaking
// change, so it can only be done in 4.0.
const _date = toDate(date);
const year = _date.getFullYear();
const decade = Math.floor(year / 10) * 10;
Expand Down
7 changes: 6 additions & 1 deletion src/startOfDecade/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */

import assert from "assert";
import { describe, it } from "vitest";
import { describe, expect, it } from "vitest";
import { startOfDecade } from "./index.js";

describe("startOfDecade", () => {
Expand All @@ -27,4 +27,9 @@ describe("startOfDecade", () => {
const result = startOfDecade(new Date(NaN));
assert(result instanceof Date && isNaN(result.getTime()));
});

it("properly works with negative numbers", () => {
expect(startOfDecade(new Date(2009, 0, 1))).toEqual(new Date(2000, 0, 1));
expect(startOfDecade(new Date(-2001, 0, 1))).toEqual(new Date(-2010, 0, 1));
});
});

0 comments on commit e0a272d

Please sign in to comment.