Skip to content

Commit

Permalink
fix: improve performance of isColor detection (see #378)
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Jan 10, 2024
1 parent b1a04fc commit 514f6ca
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/lib/utils/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ export function isTimestamp(value: unknown): boolean {
* Source: https://stackoverflow.com/questions/6386090/validating-css-color-names/33184805
*/
export function getColorCSS(color: string): string | null {
const div = window.document.createElement('div')
colorTestDiv = colorTestDiv || window.document.createElement('div')

div.style.color = color
colorTestDiv.style.color = ''
colorTestDiv.style.color = color

const applied = div.style.color
const applied = colorTestDiv.style.color
return applied !== '' ? applied.replace(/\s+/g, '').toLowerCase() : null
}
let colorTestDiv: HTMLDivElement | null = null

/**
* Test if a string contains a valid color name or code.
Expand Down

0 comments on commit 514f6ca

Please sign in to comment.