-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clampSelectedValue in date and datetime mode (#54)
* Upgrade deps * Add clamp function to core * Create cold-flies-burn.md * Replace toBeTruthy() with toBe(true) * Add clampSelectedValue in date and datetime mode * Create green-rocks-compare.md * Clamp calendar value with clamp function * Update DatePicker.tsx
- Loading branch information
Showing
17 changed files
with
473 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@tempocal/core": minor | ||
--- | ||
|
||
Add clamp function to core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@tempocal/react": minor | ||
"@tempocal/www": minor | ||
--- | ||
|
||
Add clampSelectedValue in date and datetime mode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Temporal } from "@js-temporal/polyfill"; | ||
|
||
export function clamp< | ||
Value extends Temporal.PlainDate | Temporal.PlainDateTime | ||
>(value: Value, minValue?: Value, maxValue?: Value) { | ||
if ( | ||
minValue && | ||
Temporal[ | ||
value instanceof Temporal.PlainDate ? "PlainDate" : "PlainDateTime" | ||
].compare(value, minValue) < 0 | ||
) { | ||
return minValue; | ||
} | ||
|
||
if ( | ||
maxValue && | ||
Temporal[ | ||
value instanceof Temporal.PlainDate ? "PlainDate" : "PlainDateTime" | ||
].compare(value, maxValue) > 0 | ||
) { | ||
return maxValue; | ||
} | ||
|
||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,281 @@ | ||
import { Temporal } from "@js-temporal/polyfill"; | ||
import { expect, test } from "vitest"; | ||
import { clamp } from "../src/clamp"; | ||
|
||
test("clamp (PlainDate, unbounded)", () => { | ||
const result = clamp( | ||
Temporal.PlainDate.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
}) | ||
); | ||
|
||
expect(result instanceof Temporal.PlainDate).toBe(true); | ||
|
||
expect( | ||
result.equals( | ||
Temporal.PlainDate.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
}) | ||
) | ||
).toBe(true); | ||
}); | ||
|
||
test("clamp (PlainDate, before min)", () => { | ||
const result = clamp( | ||
Temporal.PlainDate.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 1, | ||
}), | ||
Temporal.PlainDate.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
}) | ||
); | ||
|
||
expect(result instanceof Temporal.PlainDate).toBe(true); | ||
|
||
expect( | ||
result.equals( | ||
Temporal.PlainDate.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
}) | ||
) | ||
).toBe(true); | ||
}); | ||
|
||
test("clamp (PlainDate, after min)", () => { | ||
const result = clamp( | ||
Temporal.PlainDate.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 3, | ||
}), | ||
Temporal.PlainDate.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
}) | ||
); | ||
|
||
expect(result instanceof Temporal.PlainDate).toBe(true); | ||
|
||
expect( | ||
result.equals( | ||
Temporal.PlainDate.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 3, | ||
}) | ||
) | ||
).toBe(true); | ||
}); | ||
|
||
test("clamp (PlainDate, before max)", () => { | ||
const result = clamp( | ||
Temporal.PlainDate.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
}), | ||
undefined, | ||
Temporal.PlainDate.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 4, | ||
}) | ||
); | ||
|
||
expect(result instanceof Temporal.PlainDate).toBe(true); | ||
|
||
expect( | ||
result.equals( | ||
Temporal.PlainDate.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
}) | ||
) | ||
).toBe(true); | ||
}); | ||
|
||
test("clamp (PlainDate, after max)", () => { | ||
const result = clamp( | ||
Temporal.PlainDate.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 5, | ||
}), | ||
undefined, | ||
Temporal.PlainDate.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 4, | ||
}) | ||
); | ||
|
||
expect(result instanceof Temporal.PlainDate).toBe(true); | ||
|
||
expect( | ||
result.equals( | ||
Temporal.PlainDate.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 4, | ||
}) | ||
) | ||
).toBe(true); | ||
}); | ||
|
||
test("clamp (PlainDateTime, unbounded)", () => { | ||
const result = clamp( | ||
Temporal.PlainDateTime.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
hour: 2, | ||
}) | ||
); | ||
|
||
expect(result instanceof Temporal.PlainDateTime).toBe(true); | ||
|
||
expect( | ||
result.equals( | ||
Temporal.PlainDateTime.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
hour: 2, | ||
}) | ||
) | ||
).toBe(true); | ||
}); | ||
|
||
test("clamp (PlainDateTime, before min)", () => { | ||
const result = clamp( | ||
Temporal.PlainDateTime.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
hour: 1, | ||
}), | ||
Temporal.PlainDateTime.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
hour: 2, | ||
}) | ||
); | ||
|
||
expect(result instanceof Temporal.PlainDateTime).toBe(true); | ||
|
||
expect( | ||
result.equals( | ||
Temporal.PlainDateTime.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
hour: 2, | ||
}) | ||
) | ||
).toBe(true); | ||
}); | ||
|
||
test("clamp (PlainDateTime, after min)", () => { | ||
const result = clamp( | ||
Temporal.PlainDateTime.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
hour: 3, | ||
}), | ||
Temporal.PlainDateTime.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
hour: 2, | ||
}) | ||
); | ||
|
||
expect(result instanceof Temporal.PlainDateTime).toBe(true); | ||
|
||
expect( | ||
result.equals( | ||
Temporal.PlainDateTime.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
hour: 3, | ||
}) | ||
) | ||
).toBe(true); | ||
}); | ||
|
||
test("clamp (PlainDateTime, before max)", () => { | ||
const result = clamp( | ||
Temporal.PlainDateTime.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
hour: 1, | ||
}), | ||
undefined, | ||
Temporal.PlainDateTime.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
hour: 2, | ||
}) | ||
); | ||
|
||
expect(result instanceof Temporal.PlainDateTime).toBe(true); | ||
|
||
expect( | ||
result.equals( | ||
Temporal.PlainDateTime.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
hour: 1, | ||
}) | ||
) | ||
).toBe(true); | ||
}); | ||
|
||
test("clamp (PlainDateTime, after max)", () => { | ||
const result = clamp( | ||
Temporal.PlainDateTime.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
hour: 3, | ||
}), | ||
undefined, | ||
Temporal.PlainDateTime.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
hour: 2, | ||
}) | ||
); | ||
|
||
expect(result instanceof Temporal.PlainDateTime).toBe(true); | ||
|
||
expect( | ||
result.equals( | ||
Temporal.PlainDateTime.from({ | ||
year: 2022, | ||
month: 2, | ||
day: 2, | ||
hour: 2, | ||
}) | ||
) | ||
).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,5 @@ test("dateToTemporal", () => { | |
millisecond: 600, | ||
}) | ||
) | ||
).toBeTruthy(); | ||
).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,5 +19,5 @@ test("getMonthEndDate", () => { | |
day: 31, | ||
}) | ||
) | ||
).toBeTruthy(); | ||
).toBe(true); | ||
}); |
Oops, something went wrong.
ef92ccb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
tempocal – ./
tempocal.pierluc.io
tempocal-zertz.vercel.app
tempocal.vercel.app
tempocal-git-main-zertz.vercel.app