diff --git a/Tests/psharp.Tests.ps1 b/Tests/psharp.Tests.ps1 new file mode 100644 index 0000000..bd1f8c7 --- /dev/null +++ b/Tests/psharp.Tests.ps1 @@ -0,0 +1,54 @@ +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$modulepath = Split-Path $here +import-module -name (join-path $modulepath "psharp.psm1") + +$string = "get-process foreach-object blah" +$stringresult = "Get-Process Foreach-Object blah" +$nocamelstring = "should not be changed" + +$multilinestring = @" +This is a multi-line test of foreach-object { + blah |select-object |get-member +} +"@ +$multilinestringresult = @" +This is a Multi-Line test of Foreach-Object { + blah |Select-Object |Get-Member +} +"@ + +$multilinestring|ConvertTo-CamelCase +$multilinestring -split "`r`n" |ConvertTo-CamelCase +ConvertTo-CamelCase $string + +$string |ConvertTo-CamelCase + +Describe "ConvertTo-CamelCase" { + $result = ConvertTo-CamelCase $string + It "returns hyphenated CamelCase for cmdlet names on a single string" { + $result | Should BeExactly $stringresult + } + + $result = $string|ConvertTo-CamelCase + It "returns hyphenated CamelCase when used via the pipeline" { + $result | Should BeExactly $stringresult + } + + $result = ConvertTo-CamelCase $nocamelstring + It "should not change a string that does not have a hyphenated cmdlet name" { + $result |Should BeExactly $nocamelstring + } + + $result = ConvertTo-CamelCase $multilinestring + It "Should convert multiline strings" { + $result |Should BeExactly $multilinestringresult + } + + $result = ($multilinestring -split "`r`n" |ConvertTo-CamelCase) -join "`r`n" + It "Should convert multiline strings via the pipeline" { + $result |Should BeExactly $multilinestringresult + } +} + + + diff --git a/psharp.psm1 b/psharp.psm1 index 952b82d..d0af37f 100644 --- a/psharp.psm1 +++ b/psharp.psm1 @@ -224,18 +224,52 @@ function Find-DetailByType { Out-SearchView $list } -function ConvertTo-CamelCase ([string]$target) { +function ConvertTo-CamelCase { +<# + .SYNOPSIS + Appropriately converts data within strings to camel case if they are cmdlets in the form verb-noun. - function transform ([string]$t) { - $t[0].ToString().ToUpper() + $t.Substring(1) - } + .DESCRIPTION + This cmdlet capitalizes the first letter of any verb-noun found in the string. Any hyphenated word will be converted into CamelCase. + + .PARAMETER InputText + Specifies the string or string collection that should be converted to cmdlet CamelCase. + + .EXAMPLE + Convertto-CamelCase "select-object" + + .EXAMPLE + gc c:\file.ps1 |ConvertTo-CamelCase + + .INPUTS + System.String + + .OUTPUTS + System.String - if($target.IndexOf('-') -ge 0) { - $verb, $noun = $target.ToLower().split('-') - return "$(transform $verb)-$(transform $noun)" + .LINK + https://github.com/dfinke/PSharp + +#> + param( + [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)] + [Alias('Target')] + [string[]] $InputText + ) + + BEGIN { + function transform ([string]$t) { + $t[0].ToString().ToUpper() + $t.Substring(1) + } } - "$(transform $target)" + PROCESS { + $matches = $InputText |Select-String -AllMatches '\b(\w+)-(\w+)\b' + foreach ($m in $matches.Matches) { + $InputText = $InputText -replace $m.groups[0].value, ('{0}-{1}' -f (transform $m.groups[1].value), (transform $m.groups[2].value)) + } + $InputText + } } function ConvertTo-Function {