Skip to content

Commit

Permalink
Merge pull request #118 from melfore/develop
Browse files Browse the repository at this point in the history
Fix for time validation
  • Loading branch information
luciob authored Oct 17, 2023
2 parents 55140ef + 07b6fd9 commit ccf3814
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
6 changes: 2 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# [1.13.0](https://github.com/melfore/konva-timeline/compare/v1.12.0...v1.13.0) (2023-10-17)


### Bug Fixes

* 🐛 [Timeline] Allow tz in initialDateTime and task ranges ([361acae](https://github.com/melfore/konva-timeline/commit/361acaeaec1cdbda5b3f29e5f2a5547430587246))

- 🐛 [Timeline] Allow tz in initialDateTime and task ranges ([361acae](https://github.com/melfore/konva-timeline/commit/361acaeaec1cdbda5b3f29e5f2a5547430587246))

### Features

* 🎸 [Timeline] Added custom header label prop ([6482931](https://github.com/melfore/konva-timeline/commit/648293105579e8c388f732fa3d5eefa76a1c435f))
- 🎸 [Timeline] Added custom header label prop ([6482931](https://github.com/melfore/konva-timeline/commit/648293105579e8c388f732fa3d5eefa76a1c435f))

# [1.12.0](https://github.com/melfore/konva-timeline/compare/v1.11.0...v1.12.0) (2023-10-16)

Expand Down
7 changes: 7 additions & 0 deletions src/KonvaTimeline/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export const CustomColumnWidth: Story = {
},
};

export const CustomHeaderLabel: Story = {
args: {
...Primary.args,
headerLabel: "Test",
},
};

export const CustomResolution: Story = {
args: {
...Primary.args,
Expand Down
48 changes: 48 additions & 0 deletions src/utils/time.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { getValidTime } from "./time";

describe("getValidTime", () => {
it("invalid", () => {
expect(getValidTime(NaN)).not.toBe(NaN);
});

it("millis", () => {
expect(getValidTime(1697531400000)).toEqual(1697531400000);
});

it("ISO", () => {
expect(getValidTime("2023-10-28T22:00:00.000Z")).toEqual(1698530400000);
expect(getValidTime("2023-10-29T00:00:00.000+02:00")).toEqual(1698530400000);
expect(getValidTime("July 20, 69 20:17:40 GMT+00:00")).toEqual(-14182940000);
});

it("load - numbers", () => {
const now = new Date().getTime();
const dates = new Array(100000).fill(0).map((d) => {
return Math.floor(Math.random() * now);
});

const start = new Date().valueOf();
dates.forEach((d) => getValidTime(d));
const end = new Date().valueOf();
const operationLength = end - start;
console.log(`Validate dates: ${operationLength} ms`);

expect(operationLength).toBeLessThan(100);
});

it("load - strings", () => {
const now = new Date().getTime();
const dates = new Array(100000).fill(0).map((d) => {
const millis = Math.floor(Math.random() * now);
return new Date(millis).toISOString();
});

const start = new Date().valueOf();
dates.forEach((d) => getValidTime(d));
const end = new Date().valueOf();
const operationLength = end - start;
console.log(`Validate dates: ${operationLength} ms`);

expect(operationLength).toBeLessThan(100);
});
});
14 changes: 4 additions & 10 deletions src/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,12 @@ export interface InternalTimeRange {
* @param date the input date (number or string formats)
*/
export const getValidTime = (date: number | string): number => {
if (typeof date === "number" && !Number.isNaN(date)) {
return date;
const dateInMillis = typeof date === "number" ? date : new Date(date).getTime();
if (Number.isNaN(dateInMillis)) {
return new Date().getTime();
}

if (typeof date === "string") {
const dateTime = DateTime.fromISO(date);
if (dateTime.toISO() === date) {
return dateTime.toMillis();
}
}

return DateTime.now().toMillis();
return dateInMillis;
};

/**
Expand Down

0 comments on commit ccf3814

Please sign in to comment.