-
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.
fix(color-validator): improve handling of non-strings
- Loading branch information
Showing
7 changed files
with
155 additions
and
102 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 |
---|---|---|
|
@@ -11,4 +11,8 @@ yarn-*.log | |
# Parcel Artifacts | ||
.parcel-cache | ||
|
||
# Build Artifacts | ||
dist | ||
|
||
# Jest Artifacts | ||
coverage |
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
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,22 +1,46 @@ | ||
import { TailwindColorGroup } from "tailwindcss/tailwind-config" | ||
import { TwColorValidatorOpts } from "./types" | ||
|
||
const VALID_COLOR_FORM = `((#[0-9A-Fa-f]{6})|(?!#)[a-z]+(-?[0-9]{2,3}|))` | ||
const VALID_HEX = `#([A-Fa-f\\d]{6})([A-Fa-f\\d]{2})?(?!\\/(?!\\S))` | ||
const VALID_COLOR_FORM = `${VALID_HEX}|(?<!#)\\b[a-z]+\\b(-?[0-9]{2,3}|)(?!-)` | ||
const VALID_ALPHA = `\\/(?=(\\b([1-9]|[1-9][0-9]|100)\\b))` | ||
|
||
export const isValidArray = (value: unknown): value is Array<string> => | ||
Array.isArray(value) && value.every((v) => typeof v === "string") | ||
|
||
/** | ||
* Checks if a given color is valid according to the | ||
* specified TailwindCSS config. | ||
* Checks if a given color/value is valid, and whether it should to be parsed. | ||
* | ||
* @returns A validator function | ||
* @returns A validator function. | ||
*/ | ||
export const twColorValidator = | ||
(colorPalette: TailwindColorGroup) => | ||
(value: string): boolean => | ||
Object.hasOwn(colorPalette, value) | ||
(value: unknown, { strict = true }: TwColorValidatorOpts = {}): boolean => { | ||
if (!value) return false | ||
|
||
// Can be any valid value, not just what's in the config | ||
if (!strict) { | ||
// Since some colors are stored in arrays, assuming the values | ||
// in the array are valid, the array itself is valid | ||
if (isValidArray(value)) return true | ||
|
||
if ( | ||
typeof value !== "string" || | ||
/\b(?:rgba?)\b|\b(?:hsla?)\b/gi.test(value) | ||
) { | ||
return false | ||
} | ||
|
||
return new RegExp(`${VALID_COLOR_FORM}(${VALID_ALPHA})?`).test(value) | ||
} | ||
|
||
// Strictly from the specified config | ||
return typeof value === "string" && Object.hasOwn(colorPalette, value) | ||
} | ||
|
||
/** | ||
* Checks first if the color is in a valid form, then | ||
* checks if it has a valid `alpha` color channel. | ||
*/ | ||
export const hasAlpha = (value: string): boolean => | ||
new RegExp(`(?<=(${VALID_COLOR_FORM}))${VALID_ALPHA}`, "g").test(value) | ||
export const hasValidAlpha = (value: string): boolean => | ||
new RegExp(`(?<=(${VALID_COLOR_FORM}))${VALID_ALPHA}`).test(value) |
Oops, something went wrong.