MCPcopy
hub / github.com/tailwindlabs/tailwindcss / expand

Function expand

packages/tailwindcss/src/utils/brace-expansion.ts:5–53  ·  view source on GitHub ↗
(pattern: string)

Source from the content-addressed store, hash-verified

3const NUMERICAL_RANGE = /^(-?\d+)\.\.(-?\d+)(?:\.\.(-?\d+))?$/
4
5export function expand(pattern: string): string[] {
6 let index = pattern.indexOf('{')
7 if (index === -1) return [pattern]
8
9 let result: string[] = []
10 let pre = pattern.slice(0, index)
11 let rest = pattern.slice(index)
12
13 // Find the matching closing brace
14 let depth = 0
15 let endIndex = rest.lastIndexOf('}')
16 for (let i = 0; i < rest.length; i++) {
17 let char = rest[i]
18 if (char === '{') {
19 depth++
20 } else if (char === '}') {
21 depth--
22 if (depth === 0) {
23 endIndex = i
24 break
25 }
26 }
27 }
28
29 if (endIndex === -1) {
30 throw new Error(`The pattern \`${pattern}\` is not balanced.`)
31 }
32
33 let inside = rest.slice(1, endIndex)
34 let post = rest.slice(endIndex + 1)
35 let parts: string[]
36
37 if (isSequence(inside)) {
38 parts = expandSequence(inside)
39 } else {
40 parts = segment(inside, ',')
41 }
42
43 parts = parts.flatMap((part) => expand(part))
44
45 let expandedTail = expand(post)
46
47 for (let tail of expandedTail) {
48 for (let part of parts) {
49 result.push(pre + part + tail)
50 }
51 }
52 return result
53}
54
55function isSequence(str: string): boolean {
56 return NUMERICAL_RANGE.test(str)

Callers 3

parseCssFunction · 0.90

Calls 3

segmentFunction · 0.90
isSequenceFunction · 0.85
expandSequenceFunction · 0.85

Tested by

no test coverage detected