Skip to content

Commit

Permalink
perf(shared): move regex creation outside of getUnit function body (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas-C authored Sep 7, 2024
1 parent 3fa4671 commit df982ad
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/shared/src/unit-conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ const UNIT_PX = 'px'
const UNIT_EM = 'em'
const UNIT_REM = 'rem'

export function getUnit(value = ''): string | undefined {
const DIGIT_REGEX = new RegExp(String.raw`-?\d+(?:\.\d+|\d*)`)
const UNIT_REGEX = new RegExp(`${UNIT_PX}|${UNIT_EM}|${UNIT_REM}`)
const DIGIT_REGEX = new RegExp(String.raw`-?\d+(?:\.\d+|\d*)`)
const UNIT_REGEX = new RegExp(`${UNIT_PX}|${UNIT_EM}|${UNIT_REM}`)
const VALUE_REGEX = new RegExp(`${DIGIT_REGEX.source}(${UNIT_REGEX.source})`)

const unit = value.match(new RegExp(`${DIGIT_REGEX.source}(${UNIT_REGEX.source})`))
export function getUnit(value = ''): string | undefined {
const unit = value.match(VALUE_REGEX)
return unit?.[1]
}

Expand Down

0 comments on commit df982ad

Please sign in to comment.