Public/BoxTurtleUtil.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
############################################################################## #.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 } } |