/
variantGroup.ts
37 lines (34 loc) · 1.15 KB
/
variantGroup.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
import type MagicString from 'magic-string'
export const regexClassGroup = /((?:[!\w+:_/-]|\[&?>?:?.*\])+?)([:-])\(((?:[~!\w\s:/\\,%#.$-]|\[.*?\])+?)\)(?!\s*?=>)/gm
export function expandVariantGroup(str: string, seperators?: string[], depth?: number): string
export function expandVariantGroup(str: MagicString, seperators?: string[], depth?: number): MagicString
export function expandVariantGroup(str: string | MagicString, seperators = ['-', ':'], depth = 5) {
regexClassGroup.lastIndex = 0
let hasChanged = false
let content = str.toString()
do {
const before = content
content = content.replace(
regexClassGroup,
(from, pre, sep, body: string) => {
if (!seperators.includes(sep))
return from
return body
.split(/\s/g)
.filter(Boolean)
.map(i => i === '~' ? pre : i.replace(/^(!?)(.*)/, `$1${pre}${sep}$2`))
.join(' ')
},
)
hasChanged = content !== before
depth -= 1
} while (hasChanged && depth)
if (typeof str === 'string') {
return content
}
else {
return str.length()
? str.overwrite(0, str.length(), content)
: str
}
}