-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
355 additions
and
92 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
node_modules | ||
dist |
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 @@ | ||
index.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
This file was deleted.
Oops, something went wrong.
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,38 @@ | ||
const pattern = /^\d{6}(\d{2})?[+-]?\d{4}$/; | ||
|
||
const hasCorrectChecksum = (input: string) => { | ||
const sum = input | ||
.split('') | ||
.reverse() | ||
.map(Number) | ||
.map((x, i) => i % 2 ? x * 2 : x) | ||
.map((x) => x > 9 ? x - 9 : x) | ||
.reduce((x, y) => x + y); | ||
|
||
return sum % 10 === 0; | ||
}; | ||
|
||
const hasValidDate = (input: string) => { | ||
let [_, yearStr, monthStr, dayStr] = /^(\d{2})(\d{2})(\d{2})/.exec(input); | ||
|
||
const year = Number(yearStr); | ||
const month = Number(monthStr) - 1; | ||
const day = Number(dayStr); | ||
const date = new Date(year, month, day); | ||
|
||
const yearIsValid = String(date.getFullYear()).substr(-2) === yearStr; | ||
const monthIsValid = date.getMonth() === month; | ||
const dayIsValid = date.getDate() === day; | ||
|
||
return yearIsValid && monthIsValid && dayIsValid; | ||
}; | ||
|
||
export const isValid = (input: string) => { | ||
if (!pattern.test(input)) { | ||
return false; | ||
} | ||
|
||
const cleaned = input.replace(/[+-]/, '').slice(-10); | ||
|
||
return hasCorrectChecksum(cleaned) && hasValidDate(cleaned); | ||
}; |
Oops, something went wrong.