|
Set-Alias 'Write-Color' Ninmonkey.Console\Write-ConsoleColorZd |
|
filter Test-StringIsNullOrWhitespace { |
|
[OutputType('System.Boolean')] |
|
[Alias('?NotBlank')] |
|
param($InputText) |
|
[String]::IsNullOrWhiteSpace( $InputText ) |
|
} |
|
|
|
filter Assert-NotBlank { |
|
[Alias('!NotBlank')] |
|
[OutputType('Object')] |
|
|
|
param($InputText) |
|
if (Test-StringIsNullOrWhitespace $InputText) { |
|
return $InputText |
|
} else { |
|
throw 'Not allowed' |
|
} |
|
} |
|
|
|
function Csv2 { |
|
$Input | Microsoft.PowerShell.Utility\Join-String -sep ', ' |
|
} |
|
|
|
$sample = 'foo', 'bar', ' ', $null, ' cat' |
|
|
|
$filesFromSomething |
|
| ?NotBlank # prevents errors on null values |
|
| Get-Item |
|
| Out-VSCode |
|
|
|
'foo', ' ', 'bar' |
|
$names |
|
| Len # prints 3 |
|
|
|
|
|
'foo', ' ', 'bar' |
|
$names |
|
| ?NotBlank |
|
| Len # prints 2 |
|
|
|
$GoodUsers = $users |
|
| SomeTransform |
|
| ?NotBlank -Prop 'Name', 'Status' |
|
|
|
$GoodUsers | Set-RegistrationCompletionEmail |
|
|
|
$userList |
|
| !NotBlank 'LastName' |
|
| UL |
|
|
|
|
|
|
|
function Format-UnorderedList { |
|
<# |
|
.EXAMPLE |
|
PS> @(gi . ; get-date; 'hi', 'world') | ul |
|
|
|
- C:\test\Format-UnorderedList |
|
- 05/23/2022 20:28:17 |
|
- hi |
|
- world |
|
|
|
.notes |
|
render-* implies ansi colors |
|
|
|
.link |
|
Ninmonkey.Console\Format-ShortTypeName |
|
.link |
|
Ninmonkey.Console\Render-ShortTypeName |
|
|
|
#> |
|
[Alias('UL')] |
|
param( |
|
[Parameter(Mandatory, ValueFromPipeline)] |
|
[string[]]$InputObject |
|
) |
|
begin { |
|
$Items = [list[object]]::new() |
|
} |
|
process { |
|
$Items.AddRange( $InputObject ) |
|
} |
|
end { |
|
$Items | Join-String -sep "`n - " -op "`n - " -os "`n" |
|
} |
|
} |
|
function Find-NotBlankProperty { |
|
# Enumerate properties, except the blank ones |
|
[Alias('?NotBlankProp')] |
|
[OutputType('System.Management.Automation.PSNoteProperty[]')] |
|
|
|
param( |
|
[Parameter(Mandatory, ValueFromPipeline)] |
|
$InputObject, |
|
|
|
#Invert the logic |
|
[Alias('Not')] |
|
[Parameter(ParameterSetName = 'InvertTest')] |
|
[switch]$OnlyBlank |
|
) |
|
begin { |
|
$ConfigWantBlank = -not $Not |
|
} |
|
process { |
|
|
|
# is a? System.Management.Automation.PSMemberInfoIntegratingCollection`1[System.Management.Automation.PSPropertyInfo] |
|
# [PSMemberInfoIntegratingCollection<PSPropertyInfo>] |
|
$InputObject.psobject.properties | ForEach-Object { |
|
Write-Debug "PropIsBlank? Testing '$($_.Name)' of '$($_.Value)' " |
|
$isPropNull = Test-StringIsNullOrWhitespace $_.Value |
|
|
|
|
|
switch ($PSCmdlet.ParameterSetName) { |
|
'InvertTest' { |
|
if ($IsPropNull) { |
|
return $_ |
|
} else { |
|
return |
|
} |
|
} |
|
|
|
default { |
|
if ( -not $IsPropNull) { |
|
return $_ |
|
} else { |
|
return |
|
} |
|
|
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
<# |
|
|
|
prints: |
|
|
|
- user1 |
|
- user2 |
|
|
|
or throws exception: |
|
Item Property 'LastName' must not be blank |
|
|
|
#> |
|
|
|
$sample |
|
| ?NotBlank |
|
| UL |
|
|
|
Hr |
|
|
|
|
|
$sample |
|
| Join-String -Separator ', ' -DoubleQuote |
|
| write-color -bg 'gray30' -fg 'gray80' |
|
|
|
|
|
|
|
Write-Warning 'pseudo code' |
|
|
|
<# |
|
filter Format-DoubleQuoteItem { |
|
# quote selft, but don't join all elements |
|
[alias('Quote')] |
|
param($InputText) |
|
Join-String -DoubleQuote -InputObject $InputText |
|
|
|
} |
|
#> |
|
|
|
Get-ChildItem .. -Depth 2 |
|
| Select-Object -First 6 | ForEach-Object Name |
|
| ForEach-Object { $_ | write-color -fg (Get-ChildItem fg: | Get-Random -Count 1) } |
|
|
|
|
|
function Assert-NotBlankWithProperty_sketch { |
|
|
|
# future: mode that removes all properties that are null automatically |
|
[OutputType('Object')] |
|
[Alias('!NotBlank')] |
|
[cmdletbinding()] |
|
param( |
|
[Parameter(ValueFromPipeline, Mandatory)] |
|
$InputObject, |
|
|
|
# property names to test |
|
[ValidateNotNullOrEmpty()] |
|
[Alias('Name')] |
|
[Parameter(Position = 0, ParameterSetName = 'WithProperty')] |
|
[string[]]$Property |
|
) |
|
|
|
process { |
|
|
|
if ( [String]::IsNullOrWhiteSpace( $Property ) ) { |
|
|
|
} |
|
# $InputObject.Psobject.Properties |
|
switch ($PSCmdlet.ParameterSetName) { |
|
'WithProperty' { |
|
$err = $Null |
|
foreach ($prop in $Property ) { |
|
$target = $InputObject.$Prop.value |
|
Test-StringIsNullOrWhitespace $target -ev err |
|
if ($Err) { |
|
Write-Error 'object Property Named $Prop was blank' |
|
return |
|
} |
|
} |
|
} |
|
|
|
default { |
|
if (Test-StringIsNullOrWhitespace $InputText ) { |
|
return $InputText |
|
} else { |
|
throw 'Not allowed' |
|
} |
|
|
|
} |
|
} |
|
} |
|
} |