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

Function cartesian

packages/tailwindcss/src/cartesian.ts:10–45  ·  view source on GitHub ↗
(...sets: T)

Source from the content-addressed store, hash-verified

8 : []
9
10export function* cartesian<T extends CartesianInput>(...sets: T): Generator<CartesianResult<T>> {
11 let n = sets.length
12 if (n === 0) return
13
14 // If any input set is empty, the Cartesian product is empty.
15 if (sets.some((set) => set.length === 0)) {
16 return
17 }
18
19 // Index lookup
20 let idx = Array(n).fill(0)
21
22 while (true) {
23 // Compute current combination
24 let result = [] as CartesianResult<T>
25 for (let i = 0; i < n; i++) {
26 result[i] = sets[i][idx[i]]
27 }
28 yield result
29
30 // Update index vector
31 let k = n - 1
32 while (k >= 0) {
33 idx[k]++
34 if (idx[k] < sets[k].length) {
35 break
36 }
37 idx[k] = 0
38 k--
39 }
40
41 if (k < 0) {
42 return
43 }
44 }
45}

Calls

no outgoing calls

Tested by

no test coverage detected