Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Windows PowerShell TFM Book Contest and Giveaway
Function ConvertTo-CapitalizedString
{
<#
.SYNOPSIS
Capitalizes the first character of a string
.PARAMETER Delimiter
Uses space as default delimiter to split input strings. Delimiters can be single characters only.
.PARAMETER NoDelimiter
Disable string splitting based on specified delimiter.
.EXAMPLE
"ThE wINDOWS Powershell TFM book CONTEST aNd GiVeAwAy" | ConvertTo-CapitalizedString
The Windows Powershell Tfm Book Contest And Giveaway
.EXAMPLE
"ThE wINDOWS Powershell TFM book CONTEST aNd GiVeAwAy" | ConvertTo-CapitalizedString -NoDelimiter
The windows powershell tfm book contest and giveaway
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0)]
[ValidateNotNullOrEmpty()]
[string[]]
$String,
[Parameter(Position=1)]
[ValidateNotNullOrEmpty()]
[char]
$Delimiter = ' ',
[switch]
$NoDelimiter
)
BEGIN
{
$Result = New-object System.Collections.ArrayList
Function New-CapitalizedString
{
Param($String)
[string]$FirstCharacter = $String[0]
[string]$Rest = $String.Remove(0,1)
$FirstCharacter.ToUpper() + $Rest.ToLower()
}
}
PROCESS
{
foreach($Element in $String)
{
Write-Verbose "Processing [$Element]"
if($NoDelimiter)
{
$tmpString = New-CapitalizedString $Element
Write-Verbose "Converted [$Element] to [$tmpString]"
}
else
{
Write-Verbose "Using delimiter [$Delimiter] to split [$Element]"
$tmpString = foreach($Word in $Element.split($Delimiter))
{
New-CapitalizedString $word
}
$tmpString = $tmpString -join $Delimiter
Write-Verbose "Converted [$Element] to [$tmpString]"
}
$Result.Add($tmpString) | Out-Null
}
}
END {$result}
}
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"
Describe "ConvertTo-CapitalizedString" {
$TestStrings = "haha, test me","ThE wINDOWS Powershell TFM book CONTEST aNd GiVeAwAy"
$TestString = "a aa b"
It "should return 2 strings with all words capitalized (pipeline input)" {
$result = $TestStrings | ConvertTo-CapitalizedString -verbose
$result.count | Should Be 2
$result[0] | Should BeExactly "Haha, Test Me"
$result[1] | Should BeExactly "The Windows Powershell Tfm Book Contest And Giveaway"
}
It "should return 2 strings with all words capitalized" {
$result = ConvertTo-CapitalizedString -String $TestStrings -verbose
$result.count | Should Be 2
$result[0] | Should BeExactly "Haha, Test Me"
$result[1] | Should BeExactly "The Windows Powershell Tfm Book Contest And Giveaway"
}
It "should return 2 strings with a capital letter and the rest in lower case (pipeline input)" {
$result = $TestStrings | ConvertTo-CapitalizedString -NoDelimiter -verbose
$result.count | Should Be 2
$result[0] | Should BeExactly "Haha, test me"
$result[1] | Should BeExactly "The windows powershell tfm book contest and giveaway"
}
It "should return 2 strings with a capital letter and the rest in lower case" {
$result = ConvertTo-CapitalizedString -String $TestStrings -NoDelimiter -verbose
$result.count | Should Be 2
$result[0] | Should BeExactly "Haha, test me"
$result[1] | Should BeExactly "The windows powershell tfm book contest and giveaway"
}
It "should handle 1 character words (pipeline input)" {
$result = $TestString| ConvertTo-CapitalizedString -verbose
$result.count | Should Be 1
$result | Should BeExactly "A Aa B"
}
It "should handle 1 character words" {
$result = ConvertTo-CapitalizedString $TestString -verbose
$result.count | Should Be 1
$result | Should BeExactly "A Aa B"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment