Public/PsStrUtil.ps1

<############################################################################
 # Find line of code by regex1, and if following line matches regex2, then
 # append supplied code after that line
 ############################################################################>

Function AppendCodeBeforeMatchingLine([string]$csFile, [string]$regex, [string]$codeToAppend, [string]$unlessALineMatchesThisRegex) 
{
    $contents = (Get-Content $csFile)

    if($contents | ?{$_ -match $unlessALineMatchesThisRegex}) {
        Write-Host "### File '$csFile' already matches '$unlessALineMatchesThisRegex', no need to change"o
        # already matches, skip it
    } else {
        [bool]$foundMatch = $false
        for($index = 0; $index -lt $contents.Length; $index++) {

            if($contents[$index] -match $regex) {
                $contents[$index] =  $codeToAppend + "`r`n" + $contents[$index]
                $foundMatch = $true
                break
            }
        }
        if($foundMatch -eq $false) {
            $msg = @"
AppendCodeBeforeMatchingLine could not find pattern '$regex' in file '$csFile'
CALLSTACK:$(Get-PSCallStack | Out-String)
"@

            throw $msg
        }
        $contents | Set-Content $csFile
    }
}

<############################################################################
 # Replace all occurences of $lookFor with $replaceWith in $file save
 # results in place.
 ############################################################################>

Function ReplacePatternInFile([string]$file, [string]$lookFor, [string]$replaceWith)
{
    (Get-Content $file) -replace $lookFor,$replaceWith | Out-FileUtf8NoBom $file
}

<############################################################################
 # Find line of code by regex1, and if following line matches regex2, then
 # append supplied code after that line
 ############################################################################>

Function AppendCodeAfterTwoMatchingLines([string]$csFile, [string]$regex1, [string]$regex2, [string]$codeToAppend, [string]$unlessALineMatchesThisRegex) 
{
    $contents = (Get-Content $csFile)

    if($contents | ?{$_ -match $unlessALineMatchesThisRegex}) {
        Write-Host "### File '$csFile' already matches '$unlessALineMatchesThisRegex', no need to change"
        # already matches, skip it
    } else {
        [bool]$foundMatch = $false
        for($index = 1; $index -lt $contents.Length; $index++) {

            if(    ($contents[$index - 1] -match $regex1) -and ($contents[$index] -match $regex2) ) {
                $contents[$index] = $contents[$index] + "`r`n" + $codeToAppend
                $foundMatch = $true
                break
            }
        }
        if($foundMatch -eq $false) {
            $msg = @"
AppendCodeAfterTwoMatchingLines could not find patterns '$regex1' followed by '$regex2' in file '$csFile'
CALLSTACK:$(Get-PSCallStack | Out-String)
"@

            throw $msg
        }
        $contents | Set-Content $csFile
    }
}





<############################################################################
 # Cleanse multiline string for purposes of string comparison for unit testing
 #
 # Remove leading and trailing whitespace from each line
 # Collapse all remaining contiguous spaces/tabs into single space
 # Ensure all newlines are \r\n not just \n
 # Remove all blank lines
 ############################################################################>

Function Cleanse-String() {
    [cmdletbinding()]
    Param(
        [parameter(ValueFromPipeline)][string]$inputStr
    )

    [string[]]$lines = (($inputStr -split '[\r\n]') |? {$_} )
    [string]$result = ""
    [int] $index = -1 | Out-Null
    for($index = 0; $index -lt $lines.length; $index++) {
        $line = $lines[$index]
        # remove leading space
        $line = $line -replace '^\s+', ''
        # remove trailing space
        $line = $line -replace '\s+$', ''
        # collapse internal space
        $line = $line -replace '\s+', ' '

        if($line.length -gt 0) {
            if($result -eq "") {
                $result = $line
            } else {
                $result = $result + "`r`n$line"
            }
        }
    }
    return $result
}




<############################################################################
 # Convert strings like appleSauce or AppleSauce or APPLE_SAUCE to apple-sauce
 ############################################################################>

Function ConvertTo-KebabCase() {
    [cmdletbinding()]
    Param(
        [parameter(ValueFromPipeline)][string]$str
    )
    if([string]::IsNullOrWhitespace($str) -or ($str -match " ")) {
        throw "Cannot convert '$str' to kebab case, invalid string"
    } else {
        [string]$result = ($str -creplace "([a-z])([A-Z])","`$1_`$2" -creplace "-","_" -split "_" | % { $_.ToLower() }) -join "-"
        return $result 
    }
}

<############################################################################
 # Convert strings like appleSauce or AppleSauce or apple-sauce to APPLE_SAUCE
 ############################################################################>

Function ConvertTo-AllCapsCase() {
    [cmdletbinding()]
    Param(
        [parameter(ValueFromPipeline)][string]$str
    )
    if([string]::IsNullOrWhitespace($str) -or ($str -match " ")) {
        throw "Cannot convert '$str' to all caps case, invalid string"
    } else {
        [string]$result = ($str -creplace "([a-z])([A-Z])","`$1_`$2" -creplace "-","_" -split "_" | % { $_.ToUpper() }) -join "_"
        return $result 
    }
}

<############################################################################
 # Convert strings like AppleSauce or apple-sauce or APPLE_SAUCE to appleSauce
 ############################################################################>

Function ConvertTo-LowerCamelCase() {
    [cmdletbinding()]
    Param(
        [parameter(ValueFromPipeline)][string]$str
    )
    if([string]::IsNullOrWhitespace($str) -or ($str -match " ")) {
        throw "Cannot convert '$str' to lower camel case, invalid string"
    } else {
        [string]$result = ($str -creplace "([a-z])([A-Z])","`$1_`$2" -creplace "-","_" -split "_" | % { $_.substring(0, 1).ToUpper() + $_.substring(1).ToLower() }) -join ""
        $result = $result.substring(0,1).ToLower() + $result.substring(1)    
        return $result 
    }
}
    
<############################################################################
 # Convert strings like appleSauce or apple-sauce or APPLE_SAUCE to AppleSauce
 ############################################################################>

Function ConvertTo-CapitalCamelCase() {
    [cmdletbinding()]
    Param(
        [parameter(ValueFromPipeline)][string]$str
    )
    if([string]::IsNullOrWhitespace($str) -or ($str -match " ")) {
        throw "Cannot convert '$str' to capital camel case, invalid string"
    } else {
        [string]$result = ($str -creplace "([a-z])([A-Z])","`$1_`$2" -creplace "-","_" -split "_" | % { $_.substring(0, 1).ToUpper() + $_.substring(1).ToLower() }) -join ""
        return $result 
    }
}

<############################################################################
 # Convert strings like appleSauce or apple-sauce or APPLE_SAUCE or AppleSauce
 # to "Apple Sauce"
 ############################################################################>

Function ConvertTo-TitleCase() {
    [cmdletbinding()]
    Param(
        [parameter(ValueFromPipeline)][string]$str
    )
    if([string]::IsNullOrWhitespace($str) -or ($str -match " ")) {
        throw "Cannot convert '$str' to title case, invalid string"
    } else {
        [string]$result = ($str -creplace "([a-z])([A-Z])","`$1_`$2" -creplace "-","_" -split "_" | % { $_.substring(0, 1).ToUpper() + $_.substring(1).ToLower() }) -join " "
        return $result 
    }
}