Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Tests/psharp.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}



50 changes: 42 additions & 8 deletions psharp.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down