-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for alpha to hex-input (#91)
Co-authored-by: Toby Zerner <toby.zerner@gmail.com>
- Loading branch information
1 parent
b3e11c8
commit f6a5d4c
Showing
5 changed files
with
182 additions
and
11 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
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,4 +1,13 @@ | ||
const hex3 = /^#?[0-9A-F]{3}$/i; | ||
const hex6 = /^#?[0-9A-F]{6}$/i; | ||
const matcher = /^#?([0-9A-F]{3,8})$/i; | ||
|
||
export const validHex = (color: string): boolean => hex6.test(color) || hex3.test(color); | ||
export const validHex = (value: string, alpha?: boolean): boolean => { | ||
const match = matcher.exec(value); | ||
const length = match ? match[1].length : 0; | ||
|
||
return ( | ||
length === 3 || // '#rgb' format | ||
length === 6 || // '#rrggbb' format | ||
(!!alpha && length === 4) || // '#rgba' format | ||
(!!alpha && length === 8) // '#rrggbbaa' format | ||
); | ||
}; |
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