* Parses an object condition with `@slot` markers into condition blocks. * Each path from root to `@slot` becomes an independent condition block. * * @example * ```ts * parseObjectCondition({ * "@media (hover: hover)": { "&:is(:hover, [data-hover])": "@slot" }, * "@media (hover: none)": {
(obj: ConditionObjectQuery)
| 36 | * ``` |
| 37 | */ |
| 38 | function parseObjectCondition(obj: ConditionObjectQuery): MultiBlockCondition | MixedCondition | undefined { |
| 39 | const blocks: MixedCondition[] = [] |
| 40 | |
| 41 | function traverse(node: ConditionObjectQuery, path: string[]) { |
| 42 | for (const [key, value] of Object.entries(node)) { |
| 43 | if (value === '@slot') { |
| 44 | const parts = [...path, key] |
| 45 | const parsed = parseCondition(parts) |
| 46 | // parseCondition wraps array input in a MixedCondition (even single-element). |
| 47 | // Skip blocks where every part failed to parse (no usable conditions). |
| 48 | if (parsed && parsed.type === 'mixed' && parsed.value.length > 0) { |
| 49 | blocks.push(parsed) |
| 50 | } |
| 51 | } else if (typeof value === 'object' && value !== null) { |
| 52 | traverse(value, [...path, key]) |
| 53 | } |
| 54 | // Non-`@slot` string leaves are reported by validateConditions; ignore here. |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | traverse(obj, []) |
| 59 | |
| 60 | if (blocks.length === 0) return undefined |
| 61 | if (blocks.length === 1) return blocks[0] |
| 62 | |
| 63 | return { |
| 64 | type: 'multi-block', |
| 65 | value: blocks, |
| 66 | raw: obj, |
| 67 | } as MultiBlockCondition |
| 68 | } |
| 69 | |
| 70 | export function parseCondition(condition: ConditionQuery): ConditionDetails | undefined { |
| 71 | if (Array.isArray(condition)) { |
no test coverage detected