-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
validators.ts
104 lines (81 loc) · 3.12 KB
/
validators.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const arbitraryValueRegex = /^\[(?:([a-z-]+):)?(.+)\]$/i
const fractionRegex = /^\d+\/\d+$/
const stringLengths = new Set(['px', 'full', 'screen'])
const tshirtUnitRegex = /^(\d+(\.\d+)?)?(xs|sm|md|lg|xl)$/
const lengthUnitRegex =
/\d+(%|px|r?em|[sdl]?v([hwib]|min|max)|pt|pc|in|cm|mm|cap|ch|ex|r?lh|cq(w|h|i|b|min|max))|\b(calc|min|max|clamp)\(.+\)|^0$/
const colorFunctionRegex = /^(rgba?|hsla?|hwb|(ok)?(lab|lch))\(.+\)$/
// Shadow always begins with x and y offset separated by underscore
const shadowRegex = /^-?((\d+)?\.?(\d+)[a-z]+|0)_-?((\d+)?\.?(\d+)[a-z]+|0)/
const imageRegex =
/^(url|image|image-set|cross-fade|element|(repeating-)?(linear|radial|conic)-gradient)\(.+\)$/
export function isLength(value: string) {
return isNumber(value) || stringLengths.has(value) || fractionRegex.test(value)
}
export function isArbitraryLength(value: string) {
return getIsArbitraryValue(value, 'length', isLengthOnly)
}
export function isNumber(value: string) {
return Boolean(value) && !Number.isNaN(Number(value))
}
export function isArbitraryNumber(value: string) {
return getIsArbitraryValue(value, 'number', isNumber)
}
export function isInteger(value: string) {
return Boolean(value) && Number.isInteger(Number(value))
}
export function isPercent(value: string) {
return value.endsWith('%') && isNumber(value.slice(0, -1))
}
export function isArbitraryValue(value: string) {
return arbitraryValueRegex.test(value)
}
export function isTshirtSize(value: string) {
return tshirtUnitRegex.test(value)
}
const sizeLabels = new Set(['length', 'size', 'percentage'])
export function isArbitrarySize(value: string) {
return getIsArbitraryValue(value, sizeLabels, isNever)
}
export function isArbitraryPosition(value: string) {
return getIsArbitraryValue(value, 'position', isNever)
}
const imageLabels = new Set(['image', 'url'])
export function isArbitraryImage(value: string) {
return getIsArbitraryValue(value, imageLabels, isImage)
}
export function isArbitraryShadow(value: string) {
return getIsArbitraryValue(value, '', isShadow)
}
export function isAny() {
return true
}
function getIsArbitraryValue(
value: string,
label: string | Set<string>,
testValue: (value: string) => boolean,
) {
const result = arbitraryValueRegex.exec(value)
if (result) {
if (result[1]) {
return typeof label === 'string' ? result[1] === label : label.has(result[1])
}
return testValue(result[2]!)
}
return false
}
function isLengthOnly(value: string) {
// `colorFunctionRegex` check is necessary because color functions can have percentages in them which which would be incorrectly classified as lengths.
// For example, `hsl(0 0% 0%)` would be classified as a length without this check.
// I could also use lookbehind assertion in `lengthUnitRegex` but that isn't supported widely enough.
return lengthUnitRegex.test(value) && !colorFunctionRegex.test(value)
}
function isNever() {
return false
}
function isShadow(value: string) {
return shadowRegex.test(value)
}
function isImage(value: string) {
return imageRegex.test(value)
}