Skip to content

Commit

Permalink
feat: add isTimeEqual function (#77)
Browse files Browse the repository at this point in the history
* feat: add `isTimeEqual` function

#76

* test: add tests for `isTimeEqual`

#76

* docs: add docs for `isTimeEqual`

#76
  • Loading branch information
Bikossor authored Jun 12, 2022
1 parent 54ec7e3 commit b9adc3c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/predate-website/docs/api/isTimeEqual.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# `isTimeEqual`

The `isTimeEqual` function checks if hours, minutes, seconds and milliseconds are equal. Day, month and year are ignored.

```ts
function isTimeEqual(
first: PreTime | PreDateTime,
second: PreTime | PreDateTime
): boolean;
```
44 changes: 44 additions & 0 deletions packages/predate/src/isTimeEqual.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-disable functional/functional-parameters */
/* eslint-disable functional/no-return-void */
/* eslint-disable functional/no-expression-statement */
import { createTime } from "./createTime";
import { createDateTime } from "./createDateTime";
import { isTimeEqual } from "./isTimeEqual";

describe("isTimeEqual", () => {
it("should return \"true\" when two equal \"PreTime\" are given", () => {
const firstDate = createTime.fromArray([12, 15, 30, 60]);
const secondDate = createTime.fromArray([12, 15, 30, 60]);

const actualIsEqual = isTimeEqual(firstDate, secondDate);

expect(actualIsEqual).toBe(true);
});

it("should return \"false\" when two unequal \"PreTime\" are given", () => {
const firstDate = createTime.fromArray([12, 15, 30, 60]);
const secondDate = createTime.fromArray([12, 20, 30, 60]);

const actualIsEqual = isTimeEqual(firstDate, secondDate);

expect(actualIsEqual).toBe(false);
});

it("should return \"true\" when \"PreDate\" and \"PreDateTime\" with equal times are given", () => {
const firstDate = createTime.fromArray([12, 15, 30, 60]);
const secondDate = createDateTime.fromArray([2022, 0, 1, 12, 15, 30, 60]);

const actualIsEqual = isTimeEqual(firstDate, secondDate);

expect(actualIsEqual).toBe(true);
});

it("should return \"true\" when two \"PreDateTime\" with unequal dates are given", () => {
const firstDate = createDateTime.fromArray([2022, 0, 1, 12, 15, 30, 60]);
const secondDate = createDateTime.fromArray([2022, 1, 1, 12, 15, 30, 60]);

const actualIsEqual = isTimeEqual(firstDate, secondDate);

expect(actualIsEqual).toBe(true);
});
});
19 changes: 19 additions & 0 deletions packages/predate/src/isTimeEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { PreDateTime, PreTime } from "./types";

/**
* Checks if hours, minutes, seconds and milliseconds are equal. Day, month and year are ignored.
* @param first
* @param second
* @returns
*/
export const isTimeEqual = (
first: PreTime | PreDateTime,
second: PreTime | PreDateTime
): boolean => {
return (
first.hours === second.hours &&
first.minutes === second.minutes &&
first.seconds === second.seconds &&
first.milliseconds === second.milliseconds
);
};

0 comments on commit b9adc3c

Please sign in to comment.