-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(input-date-picker): normalize year to current century (#7622)
**Related Issue:** #7588 ## Summary Normalizes year to current century. Ex: If the user types `01/01/20` the `value` will be corrected to `01/01/2020`
- Loading branch information
1 parent
237a2c5
commit 0ca35e9
Showing
7 changed files
with
199 additions
and
3 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
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
19 changes: 19 additions & 0 deletions
19
packages/calcite-components/src/components/input-date-picker/util.spec.ts
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,19 @@ | ||
import { isTwoDigitYear, normalizeToCurrentCentury } from "./utils"; | ||
|
||
describe("utils", () => { | ||
describe("isTwoDigitYear", () => { | ||
it("returns whether a given ISO string date has two digit year", () => { | ||
expect(isTwoDigitYear("2023-08-01")).toBe(false); | ||
expect(isTwoDigitYear("00-08-01")).toBe(true); | ||
expect(isTwoDigitYear("")).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("normalizedYear", () => { | ||
it("return normalized date for a given ISO string date with two digit year", () => { | ||
expect(normalizeToCurrentCentury(20)).toEqual(2020); | ||
expect(normalizeToCurrentCentury(0)).toBe(2000); | ||
expect(normalizeToCurrentCentury(1)).toBe(2001); | ||
}); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
packages/calcite-components/src/components/input-date-picker/utils.ts
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,27 @@ | ||
import { datePartsFromISO } from "../../utils/date"; | ||
|
||
/** | ||
* Specifies if an ISO string date (YYYY-MM-DD) has two digit year. | ||
* | ||
* @param {string} value | ||
* @returns {boolean} | ||
*/ | ||
export function isTwoDigitYear(value: string): boolean { | ||
if (!value) { | ||
return false; | ||
} | ||
const { year } = datePartsFromISO(value); | ||
return Number(year) < 100; | ||
} | ||
|
||
/** | ||
* Returns a normalized year to current century from a given two digit year number. | ||
* | ||
* @param {number} twoDigitYear | ||
* @returns {string} | ||
*/ | ||
export function normalizeToCurrentCentury(twoDigitYear: number): number { | ||
const currentYear = new Date().getFullYear(); | ||
const normalizedYear = Math.floor(currentYear / 100) * 100 + twoDigitYear; | ||
return normalizedYear; | ||
} |
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