Smart string modification filter.

This is a string modification filter that is intended to calculate the string char value and increment it.

filter Set-StringIncrement{

  <#
  PS> 10 | Set-StringIncrement
  11
  PS> 'aa' | Set-StringIncrement
  ab
  #>

  BEGIN{
      filter Set-CharIncrement{

          <#
          PS> 'a' | Set-CharIncrement
          b
          PS> '1' | Set-CharIncrement
          2
          #>
      
          PROCESS{
              IF($_ -is [int]){ IF($_ -eq 9){ 'A' }ELSE{ $_ + 1} }
              IF($_ -is [string]){ IF($_ -eq 'Z'){ 0 }ELSE{ $char = [int][char]$_ + 1; [char]$char }}
          }
      }
  }
 
  PROCESS{
      $Arr1 = $_.tostring()
      $Arr1 = $Arr1.ToCharArray()
      $Arr2 = $Arr1 | %{ [char]$_} | %{ $_.ToString()  } | %{ $element = $_; TRY{[int]$element}Catch{[string]$element} }                       
      [array]::Reverse($Arr2)
      $script:Out = $null        
      $Arr2 = $Arr2 | %{                      
                  $Element = $_
                  IF($script:Out -eq $null){
                              $script:Out = $Element | Set-CharIncrement                                
                              $Script:Out
                  }ELSEIF($script:Out -eq 0){ 
                              $script:Out = $Element | Set-CharIncrement                                
                              $Script:Out 
                  }ELSEIF($script:Out -ne 0){ 
                              $Element 
                  }
               }
      [array]::Reverse($Arr2)
      $Arr2 -join ''
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *