diff --git a/Examples/Example-TableColumnHighlight/Example-LocaleDecimals.html b/Examples/Example-TableColumnHighlight/Example-LocaleDecimals.html new file mode 100644 index 00000000..2997e8bb --- /dev/null +++ b/Examples/Example-TableColumnHighlight/Example-LocaleDecimals.html @@ -0,0 +1,573 @@ + + PSWriteHTML decimal number conditions
This example proves the same decimal comparison works for HTML-backed tables and JavaScript-backed tables.Use -Value 0.2 for a numeric literal, or quote locale-formatted text with -Value '0,2'.Do not use bare -Value 0,2 in PowerShell because it becomes an Object[] before PSWriteHTML sees it.
New-TableCondition -Name "Value" -ComparisonType number -Operator le -Value 0.2 -Color SeaGreen -FailColor FireBrick
+New-TableCondition -Name "Value" -ComparisonType number -Operator le -Value '0,2' -Color SeaGreen -FailColor FireBrick
+
+# Avoid bare -Value 0,2 in PowerShell.
+# PowerShell parses it as an Object[] containing 0 and 2.
ItemValue
Pass threshold0.17
Fail threshold0.25
Exact threshold0.20
Negative value-0.05
ItemValue
ItemValue
Pass threshold0,17
Fail threshold0,25
Exact threshold0,20
Negative value-0,05
ItemValue
ItemValue
ItemValue
ItemValue
ItemValue
diff --git a/Examples/Example-TableColumnHighlight/Example-LocaleDecimals.ps1 b/Examples/Example-TableColumnHighlight/Example-LocaleDecimals.ps1 new file mode 100644 index 00000000..32050638 --- /dev/null +++ b/Examples/Example-TableColumnHighlight/Example-LocaleDecimals.ps1 @@ -0,0 +1,120 @@ +Import-Module .\PSWriteHTML.psd1 -Force + +$htmlDot = @( + [PSCustomObject] @{ + Item = 'Pass threshold' + Value = '0.17' + } + [PSCustomObject] @{ + Item = 'Fail threshold' + Value = '0.25' + } + [PSCustomObject] @{ + Item = 'Exact threshold' + Value = '0.20' + } + [PSCustomObject] @{ + Item = 'Negative value' + Value = '-0.05' + } +) + +$htmlComma = @( + [PSCustomObject] @{ + Item = 'Pass threshold' + Value = '0,17' + } + [PSCustomObject] @{ + Item = 'Fail threshold' + Value = '0,25' + } + [PSCustomObject] @{ + Item = 'Exact threshold' + Value = '0,20' + } + [PSCustomObject] @{ + Item = 'Negative value' + Value = '-0,05' + } +) + +$jsDot = @( + [PSCustomObject] @{ + Item = 'Pass threshold' + Value = 0.17 + } + [PSCustomObject] @{ + Item = 'Fail threshold' + Value = 0.25 + } + [PSCustomObject] @{ + Item = 'Exact threshold' + Value = 0.20 + } + [PSCustomObject] @{ + Item = 'Negative value' + Value = -0.05 + } +) + +$jsComma = @( + [PSCustomObject] @{ + Item = 'Pass threshold' + Value = '0,17' + } + [PSCustomObject] @{ + Item = 'Fail threshold' + Value = '0,25' + } + [PSCustomObject] @{ + Item = 'Exact threshold' + Value = '0,20' + } + [PSCustomObject] @{ + Item = 'Negative value' + Value = '-0,05' + } +) + +$exampleCode = @' +New-TableCondition -Name "Value" -ComparisonType number -Operator le -Value 0.2 -Color SeaGreen -FailColor FireBrick +New-TableCondition -Name "Value" -ComparisonType number -Operator le -Value '0,2' -Color SeaGreen -FailColor FireBrick + +# Avoid bare -Value 0,2 in PowerShell. +# PowerShell parses it as an Object[] containing 0 and 2. +'@ + +New-HTML -TitleText 'PSWriteHTML decimal number conditions' -Online -FilePath "$PSScriptRoot\Example-LocaleDecimals.html" { + New-HTMLSection -HeaderText 'Locale decimal conditions' { + New-HTMLText -TextBlock { + 'This example proves the same decimal comparison works for HTML-backed tables and JavaScript-backed tables.' + 'Use -Value 0.2 for a numeric literal, or quote locale-formatted text with -Value ''0,2''.' + 'Do not use bare -Value 0,2 in PowerShell because it becomes an Object[] before PSWriteHTML sees it.' + } + New-HTMLCodeBlock -Code $exampleCode -Style 'PowerShell' -Theme enlighter + } + + New-HTMLSection -HeaderText 'HTML store with dot decimals' { + New-HTMLTable -DataTable $htmlDot -Buttons copyHtml5, excelHtml5, csvHtml5, pdfHtml5 -PagingLength 5 { + New-TableCondition -Name 'Value' -ComparisonType number -Operator le -Value 0.2 -Color SeaGreen -FailColor FireBrick + } + } + + New-HTMLSection -HeaderText 'HTML store with comma decimals' { + New-HTMLTable -DataTable $htmlComma -Buttons copyHtml5, excelHtml5, csvHtml5, pdfHtml5 -PagingLength 5 { + New-TableCondition -Name 'Value' -ComparisonType number -Operator le -Value '0,2' -Color SeaGreen -FailColor FireBrick + } + } + + New-HTMLSection -HeaderText 'JavaScript store with numeric values' { + New-HTMLTable -DataTable $jsDot -Buttons copyHtml5, excelHtml5, csvHtml5, pdfHtml5 -PagingLength 5 { + New-TableCondition -Name 'Value' -ComparisonType number -Operator le -Value 0.2 -Color SeaGreen -FailColor FireBrick + } -DataStore JavaScript + } + + New-HTMLSection -HeaderText 'JavaScript store with comma-formatted strings' { + New-HTMLTable -DataTable $jsComma -Buttons copyHtml5, excelHtml5, csvHtml5, pdfHtml5 -PagingLength 5 { + New-TableCondition -Name 'Value' -ComparisonType number -Operator le -Value '0,2' -Color SeaGreen -FailColor FireBrick + } -DataStore JavaScript + } +} diff --git a/Public/New-TableCondition.ps1 b/Public/New-TableCondition.ps1 index 87e02c4e..930c5619 100644 --- a/Public/New-TableCondition.ps1 +++ b/Public/New-TableCondition.ps1 @@ -19,7 +19,9 @@ function New-TableCondition { Specifies the comparison operator to be used. .PARAMETER Value - Specifies the value to compare against. + Specifies the value to compare against. In PowerShell decimal literals use a dot, for example 0.2. + If you want to pass locale-formatted text such as 0,2, quote it as a string. Bare 0,2 is parsed by + PowerShell as an array containing 0 and 2 before PSWriteHTML receives it. .PARAMETER Row Switch parameter to indicate if the styling should be applied to the entire row. @@ -103,7 +105,14 @@ function New-TableCondition { Specifies the text direction for fail conditions. .EXAMPLE - An example of how to use this function. + New-TableCondition -Name 'Policies_Go' -ComparisonType number -Operator le -Value 0.2 -Color SeaGreen -FailColor FireBrick + + Uses a PowerShell numeric literal with a dot decimal separator. + + .EXAMPLE + New-TableCondition -Name 'Policies_Go' -ComparisonType number -Operator le -Value '0,2' -Color SeaGreen -FailColor FireBrick + + Uses a quoted locale-formatted decimal string. Avoid bare -Value 0,2 because PowerShell treats it as an array. .NOTES Additional notes about the function. diff --git a/Tests/New-TableCondition.Tests.ps1 b/Tests/New-TableCondition.Tests.ps1 new file mode 100644 index 00000000..3ef27f08 --- /dev/null +++ b/Tests/New-TableCondition.Tests.ps1 @@ -0,0 +1,59 @@ +Import-Module "$PSScriptRoot\..\PSWriteHTML.psd1" -Force + +Describe 'New-TableCondition' { + It 'Serializes dot and quoted comma thresholds into columnHighlighter rules' { + $FilePath = "$PSScriptRoot\TemporaryCondition.html" + $htmlStore = @( + [PSCustomObject] @{ + Item = 'Pass threshold' + Value = '0,17' + } + ) + $jsStore = @( + [PSCustomObject] @{ + Item = 'Pass threshold' + Value = 0.17 + } + ) + + New-HTML { + New-HTMLTable -DataTable $htmlStore -HideFooter { + New-TableCondition -Name 'Value' -ComparisonType number -Operator le -Value '0,2' -Color SeaGreen -FailColor FireBrick + } + New-HTMLTable -DataTable $jsStore -HideFooter { + New-TableCondition -Name 'Value' -ComparisonType number -Operator le -Value 0.2 -Color SeaGreen -FailColor FireBrick + } -DataStore JavaScript + } -FilePath $FilePath -Online + + $Content = Get-Content -Path $FilePath -Raw + $Content | Should -Match '"columnHighlighter"' + $Content | Should -Match '"value"\s*:\s*"0,2"' + $Content | Should -Match '"value"\s*:\s*0.2' + + if (Test-Path $FilePath) { + Remove-Item -LiteralPath $FilePath + } + } + + It 'Receives bare comma decimals as an array because of PowerShell parsing' { + $FilePath = "$PSScriptRoot\TemporaryConditionArray.html" + $bareCommaValue = 0,2 + + New-HTML { + $script:conditionWithBareComma = New-TableCondition -Name 'Value' -ComparisonType number -Operator le -Value $bareCommaValue + } -FilePath $FilePath + + $bareCommaValue.GetType().FullName | Should -Be 'System.Object[]' + $bareCommaValue.Count | Should -Be 2 + $bareCommaValue[0] | Should -Be 0 + $bareCommaValue[1] | Should -Be 2 + $script:conditionWithBareComma.Output.Value.GetType().FullName | Should -Be 'System.Object[]' + $script:conditionWithBareComma.Output.Value.Count | Should -Be 2 + $script:conditionWithBareComma.Output.Value[0] | Should -Be 0 + $script:conditionWithBareComma.Output.Value[1] | Should -Be 2 + + if (Test-Path $FilePath) { + Remove-Item -LiteralPath $FilePath + } + } +}