|
| 1 | +function Convert-TableConditionsToHighlighterRules { |
| 2 | + [CmdletBinding()] |
| 3 | + param( |
| 4 | + [Parameter(Mandatory)][System.Collections.IEnumerable] $ConditionalFormatting, |
| 5 | + [Parameter(Mandatory)][string[]] $Header |
| 6 | + ) |
| 7 | + |
| 8 | + # Build rules with idiomatic array comprehensions for clarity and simplicity |
| 9 | + [array] $rules = @( |
| 10 | + foreach ($Entry in $ConditionalFormatting) { |
| 11 | + if (-not $Entry) { continue } |
| 12 | + |
| 13 | + $ct = $Entry.ConditionType |
| 14 | + |
| 15 | + if ($ct -eq 'Condition') { |
| 16 | + # Single condition or unconditional highlight |
| 17 | + $isUnconditional = ($Entry.PSObject.Properties.Name -notcontains 'Value') -or ($null -eq $Entry.Value) |
| 18 | + if (-not $isUnconditional) { |
| 19 | + $cond = New-TablePluginCondition -Condition $Entry |
| 20 | + } |
| 21 | + |
| 22 | + # Determine target names |
| 23 | + $targetNames = if ($Entry.Row) { |
| 24 | + $Header |
| 25 | + } elseif ($Entry.HighlightHeaders) { |
| 26 | + $Entry.HighlightHeaders |
| 27 | + } else { |
| 28 | + @($Entry.Name) |
| 29 | + } |
| 30 | + |
| 31 | + # Build targets |
| 32 | + $fill = $null |
| 33 | + if ($Entry.ChildRowFill) { |
| 34 | + if ($Entry.ChildRowFill -eq 'Parent') { $fill = 'parent' } |
| 35 | + elseif ($Entry.ChildRowFill -eq 'Both') { $fill = 'both' } |
| 36 | + } |
| 37 | + [array] $targets = @( |
| 38 | + foreach ($n in $targetNames) { |
| 39 | + $t = [ordered]@{ |
| 40 | + column = $n |
| 41 | + css = $Entry.Style |
| 42 | + } |
| 43 | + if ($fill) { $t['fill'] = $fill } |
| 44 | + $t |
| 45 | + } |
| 46 | + ) |
| 47 | + |
| 48 | + # Build failTargets, if any |
| 49 | + $failTargets = $null |
| 50 | + if ($Entry.FailStyle.Keys.Count -gt 0) { |
| 51 | + $failTargets = @( |
| 52 | + foreach ($n in $targetNames) { |
| 53 | + $ft = [ordered]@{ |
| 54 | + column = $n |
| 55 | + css = $Entry.FailStyle |
| 56 | + } |
| 57 | + # Do not propagate fill to fail targets by default |
| 58 | + $ft |
| 59 | + } |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + if ($isUnconditional) { |
| 64 | + $rule = [ordered]@{ |
| 65 | + conditionsContainer = @() # unconditional |
| 66 | + targets = $targets |
| 67 | + } |
| 68 | + } else { |
| 69 | + $rule = [ordered]@{ |
| 70 | + conditionsContainer = @( |
| 71 | + [ordered]@{ |
| 72 | + logic = 'AND' |
| 73 | + conditions = @($cond) |
| 74 | + } |
| 75 | + ) |
| 76 | + targets = $targets |
| 77 | + } |
| 78 | + } |
| 79 | + if ($failTargets) { |
| 80 | + $rule['failTargets'] = $failTargets |
| 81 | + } |
| 82 | + |
| 83 | + [pscustomobject]$rule |
| 84 | + |
| 85 | + } elseif ($ct -eq 'ConditionGroup') { |
| 86 | + # Grouped conditions |
| 87 | + [array] $conditions = @( |
| 88 | + foreach ($Nested in $Entry.Conditions) { |
| 89 | + if ($Nested.Type -eq 'TableCondition') { |
| 90 | + New-TablePluginCondition -Condition $Nested.Output |
| 91 | + } |
| 92 | + } |
| 93 | + ) |
| 94 | + $groupUnconditional = ($conditions.Count -eq 0) |
| 95 | + |
| 96 | + # Determine targets |
| 97 | + $targetNames = if ($Entry.Row) { |
| 98 | + $Header |
| 99 | + } elseif ($Entry.HighlightHeaders) { |
| 100 | + $Entry.HighlightHeaders |
| 101 | + } else { |
| 102 | + @( |
| 103 | + foreach ($Nested in $Entry.Conditions) { |
| 104 | + if ($Nested.Type -eq 'TableCondition') { $Nested.Output.Name } |
| 105 | + } |
| 106 | + ) |
| 107 | + } |
| 108 | + |
| 109 | + $fill = $null |
| 110 | + if ($Entry.ChildRowFill) { |
| 111 | + if ($Entry.ChildRowFill -eq 'Parent') { $fill = 'parent' } |
| 112 | + elseif ($Entry.ChildRowFill -eq 'Both') { $fill = 'both' } |
| 113 | + } |
| 114 | + [array] $targets = @( |
| 115 | + foreach ($n in $targetNames) { |
| 116 | + $t = [ordered]@{ |
| 117 | + column = $n |
| 118 | + css = $Entry.Style |
| 119 | + } |
| 120 | + if ($fill) { $t['fill'] = $fill } |
| 121 | + $t |
| 122 | + } |
| 123 | + ) |
| 124 | + |
| 125 | + $failTargets = $null |
| 126 | + if ($Entry.FailStyle.Keys.Count -gt 0) { |
| 127 | + $failTargets = @( |
| 128 | + foreach ($n in $targetNames) { |
| 129 | + [ordered]@{ |
| 130 | + column = $n |
| 131 | + css = $Entry.FailStyle |
| 132 | + } |
| 133 | + } |
| 134 | + ) |
| 135 | + } |
| 136 | + |
| 137 | + if ($groupUnconditional) { |
| 138 | + $rule = [ordered]@{ |
| 139 | + conditionsContainer = @() # unconditional |
| 140 | + targets = $targets |
| 141 | + } |
| 142 | + } else { |
| 143 | + $rule = [ordered]@{ |
| 144 | + conditionsContainer = @( |
| 145 | + [ordered]@{ |
| 146 | + logic = $Entry.Logic |
| 147 | + conditions = $conditions |
| 148 | + } |
| 149 | + ) |
| 150 | + targets = $targets |
| 151 | + } |
| 152 | + } |
| 153 | + if ($failTargets) { |
| 154 | + $rule['failTargets'] = $failTargets |
| 155 | + } |
| 156 | + |
| 157 | + [pscustomobject]$rule |
| 158 | + } |
| 159 | + } |
| 160 | + ) |
| 161 | + |
| 162 | + return ,$rules |
| 163 | +} |
0 commit comments