Public/BoxTurtleUtil.ps1
############################################################################## #.SYNOPSIS # Convert string to kebab-case capitalization, e.g. # appleSauce or AppleSauce or APPLE_SAUCE all get converted to apple-sauce # #.DESCRIPTION # Convert string to kebab-case capitalization, e.g. # appleSauce or AppleSauce or APPLE_SAUCE all get converted to apple-sauce # #.PARAMETER str # String to be converted # #.EXAMPLE # ConvertTo-KebabCase "AppleSauce" -----> "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 } } |