Skip to content
Merged
Prev Previous commit
Next Next commit
add isAlpha(…)
  • Loading branch information
RobinMalfait committed Nov 18, 2024
commit 3646f5466291d0bf09f59fccdf4bb9b95a27d93e
25 changes: 25 additions & 0 deletions packages/tailwindcss/src/utils/is-alpha.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { bench } from 'vitest'
import { isAlpha } from './is-alpha'

const ALPHA_REGEX_A = /^[a-zA-Z]+$/
const ALPHA_REGEX_B = /^[a-z]+$/i
const ALPHA_REGEX_C = /^[A-Z]+$/i

const implementations = new Map<string, (input: string) => boolean>([
['RegExp A', (input) => ALPHA_REGEX_A.test(input)],
['RegExp B', (input) => ALPHA_REGEX_B.test(input)],
['RegExp C', (input) => ALPHA_REGEX_C.test(input)],
['Manual', isAlpha],
])

for (let [name, check] of implementations) {
bench(name, () => {
for (let i = 0; i < 1e6; i++) {
check('abc')
check('ABC')
check('AbC')
check('a-b-c')
check('123')
}
})
}
14 changes: 14 additions & 0 deletions packages/tailwindcss/src/utils/is-alpha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const UPPER_A = 65
const UPPER_Z = 90
const LOWER_A = 97
const LOWER_Z = 122

export function isAlpha(input: string): boolean {
for (let i = 0; i < input.length; i++) {
let code = input.charCodeAt(i)
if (code < UPPER_A || (code > UPPER_Z && code < LOWER_A) || code > LOWER_Z) {
return false
}
}
return true
}