pf-prefix_suffix.ps1
function Add-Prefix($prefix) { process { [string]$value = "$_" if ( -not $value.StartsWith( $prefix) ) { $value = $prefix + $value } return $value } } function Add-Prefix:::Test { 'bcde' | Add-Prefix 'a' | Should -Be 'abcde' $null | Add-Prefix 'a' | Should -Be 'a' } function Add-Suffix ($Suffix) { process { [string]$value = "$_" if ( -not $value.EndsWith($Suffix) ) { $value = $value + $Suffix } return $value } } function Add-Suffix:::Test { 'abcd' | Add-Suffix 'e' | Should -Be 'abcde' $null | Add-Suffix 'e' | Should -Be 'e' } function Update-Prefix ($prefix, $replace = '', [Switch]$Required) { begin { if ( $null -eq $prefix ) { $prefix = '' } } process { $value = "$_" if ( $value -and $value.StartsWith($prefix, [System.StringComparison]::InvariantCultureIgnoreCase ) ) { return $replace + $value.SubString( $prefix.Length ) } if ( $Required ) { throw "'$prefix' is not a prefix of '$value' " } return $value } } function Update-Prefix:::Test { 'abc', 'aab' | Update-Prefix -replace 'Z_' | Assert 'Z_abc', 'Z_aab' 'abc', 'aab' | Update-Prefix 'a' | Assert 'bc', 'ab' 'Abc', 'aab' | Update-Prefix 'a' | Assert 'bc', 'ab' { 'abc', 'aab' | Update-Prefix 'z' -Required } | assert -throw 'is not a prefix' } function Update-Suffix ($Suffix, $replace = '', [Switch]$Required) { begin { if ( $null -eq $Suffix ) { $Suffix = '' } } process { $value = "$_" if ( $value -and $value.EndsWith($Suffix, [System.StringComparison]::InvariantCultureIgnoreCase ) ) { return $value.SubString( 0, $value.length - $Suffix.Length ) + $replace } if ( $Required ) { throw "'$Suffix' is not a Suffix of '$value' " } return $value } } function Update-Suffix:::Test { 'abc', 'aab' | Update-Suffix -replace '_Z' | Assert 'abc_Z', 'aab_Z' 'abc', 'aab' | Update-Suffix 'b' | assert 'abc', 'aa' 'abc', 'aaB' | Update-Suffix 'b' | assert 'abc', 'aa' { 'abc', 'aab' | Update-Suffix 'z' -Required } | assert -throw 'is not a Suffix' } |