-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUse-Ternary.ps1
61 lines (47 loc) · 1.83 KB
/
Use-Ternary.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function Use-Ternary {
<#
.SYNOPSIS
Evaluates a ternary condition and returns a value based on the result.
.DESCRIPTION
This function simulates a ternary operator like C#'s `?:` and returns either
the true or false result depending on the condition.
.PARAMETER Condition
The boolean condition to evaluate.
.PARAMETER TrueValue
The value to return if the condition is `$true`.
.PARAMETER FalseValue
The value to return if the condition is `$false`.
.EXAMPLE
PS> Use-Ternary -Condition ($x -gt 10) -TrueValue "Greater" -FalseValue "Smaller"
Returns "Greater" if $x is greater than 10, otherwise returns "Smaller".
.EXAMPLE
PS> Use-Ternary -Condition (Test-Path "C:\test.txt") -TrueValue "File Exists" -FalseValue "File Not Found"
Returns "File Exists" if C:\test.txt exists, otherwise "File Not Found".
.EXAMPLE
PS> $result = Use-Ternary -Condition ($var -eq $null) -TrueValue "Null Detected" -FalseValue "Value Exists"
Returns "Null Detected" if $var is `$null`, otherwise returns "Value Exists".
.NOTES
Version: 1.0.4
Author: David Wingerson
Optimized for PowerShell 5.0+ and 7.0+
Uses .NET direct execution for maximum speed.
.LINK
https://github.com/fluxnull
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[bool]$Condition,
[Parameter(Mandatory = $true, Position = 1)]
$TrueValue,
[Parameter(Mandatory = $true, Position = 2)]
$FalseValue
)
process {
# Use .NET Dictionary constructor directly
$Map = [System.Collections.Generic.Dictionary[bool, object]]::new(2)
$Map[$true] = $TrueValue
$Map[$false] = $FalseValue
return $Map[$Condition]
}
}