-
-
Save torgro/c6e5c74b4b5eb3a246cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Set-UpperAndLower | |
{ | |
<# | |
.Synopsis | |
Sets the first letter to upper case | |
.DESCRIPTION | |
This command take any word and convert the first letter to upper case and the rest to lower case | |
.EXAMPLE | |
Set-UpperAndLower -SingleWord something | |
Something | |
.EXAMPLE | |
Set-UpperAndLower -SingleWord SOMETHING | |
Something | |
.INPUTS | |
String | |
.OUTPUTS | |
String | |
.NOTES | |
General notes | |
.FUNCTIONALITY | |
String manipulation | |
#> | |
[cmdletbinding()] | |
Param( | |
[Parameter( | |
Mandatory=$true | |
) | |
] | |
[ValidateNotNullOrEmpty()] | |
[Alias("Text","Word")] | |
[string]$SingleWord | |
) | |
$firstLetter = ([string]$SingleWord[0]).ToUpper() | |
$therest = "" | |
if ($SingleWord.Length -gt 1) | |
{ | |
$therest = $SingleWord.Substring(1).ToLower() | |
} | |
return "$firstLetter$therest" | |
} | |
function Set-CapitalFirstLetter | |
{ | |
<# | |
.Synopsis | |
Sets the first letter to upper case in every word in a Sentence | |
.DESCRIPTION | |
This command take a whole sentence and convert the first letter of every word to upper case and the rest to lower case | |
.EXAMPLE | |
Set-CapitalFirstLetter -Sentence "the quick lazy brown fox jumped over the fence" | |
The Quick Lazy Brown Fox Jumped Over The Fence | |
.EXAMPLE | |
"the quick lazy brown fox jumped over the fence" | Set-CapitalFirstLetter | |
The Quick Lazy Brown Fox Jumped Over The Fence | |
.INPUTS | |
String | |
.OUTPUTS | |
String | |
.NOTES | |
General notes | |
.FUNCTIONALITY | |
String manipulation | |
#> | |
[cmdletbinding()] | |
Param( | |
[Parameter( | |
Mandatory=$true, | |
ValueFromPipeline=$true, | |
ValueFromPipelineByPropertyName=$true, | |
ValueFromRemainingArguments=$false, | |
Position=0) | |
] | |
[ValidateNotNullOrEmpty()] | |
[Alias("Text")] | |
[string]$Sentence | |
) | |
Begin | |
{ | |
$arrayList = New-Object -TypeName System.Collections.ArrayList | |
} | |
Process | |
{ | |
if(-not $Sentence.Contains(" ")) | |
{ | |
$newWord = Set-UpperAndLower -SingleWord $Sentence | |
$null = $arrayList.Add($newWord) | |
} | |
else | |
{ | |
foreach ($word in ($Sentence -split " ")) | |
{ | |
$newWord = Set-UpperAndLower -SingleWord $word | |
$null = $arraylist.Add($newWord) | |
} | |
} | |
} | |
End | |
{ | |
if ($arrayList.Count -gt 1) | |
{ | |
$arrayList -join " " | |
} | |
else | |
{ | |
$arrayList -join "" | |
} | |
} | |
} | |
#region tests | |
#copy this region to a separate file | |
$here = Split-Path -Parent $MyInvocation.MyCommand.Path | |
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") | |
. "$here\$sut" | |
Describe "Set-UpperAndLower tests" { | |
It "Takes a single word and sets the first letter to upper case" { | |
$Expected = "Word" | |
$Actual = Set-UpperAndLower -SingleWord "word" | |
$Actual -ceq $Expected | Should Be $true | |
} | |
It "Takes a single word and sets the first letter to upper case and lowers the rest" { | |
$Expected = "Word" | |
$Actual = Set-UpperAndLower -SingleWord "WORD" | |
$Actual -ceq $Expected | Should Be $true | |
} | |
It "Takes a single word and sets the first letter to upper case and handles special chars" { | |
$Expected = "Word?;:*" | |
$Actual = Set-UpperAndLower -SingleWord "word?;:*" | |
$Actual -ceq $Expected | Should Be $true | |
} | |
} | |
Describe "Set-CapitalFirstLetter tests" { | |
It "Takes an upper case sentence and sets the first letter of every word to upper" { | |
$Expected = "The Quick Lazy Brown Fox Jumped Over The Fence" | |
$Sentence = "The Quick Lazy Brown Fox Jumped Over The Fence".ToUpper() | |
$Actual = Set-CapitalFirstLetter -Sentence $Sentence | |
$Actual -ceq $Expected | Should Be $true | |
} | |
It "Takes an upper case sentence from the pipeline and sets the first letter of every word to upper" { | |
$Expected = "The Quick Lazy Brown Fox Jumped Over The Fence" | |
$Sentence = "The Quick Lazy Brown Fox Jumped Over The Fence".ToUpper() | |
$Actual = $Sentence | Set-CapitalFirstLetter | |
$Actual -ceq $Expected | Should Be $true | |
} | |
It "Takes an lower case sentence and sets the first letter of every word to upper" { | |
$Expected = "The Quick Lazy Brown Fox Jumped Over The Fence" | |
$Sentence = "The Quick Lazy Brown Fox Jumped Over The Fence".ToLower() | |
$Actual = Set-CapitalFirstLetter -Sentence $Sentence | |
$Actual -ceq $Expected | Should Be $true | |
} | |
It "Takes an lower case sentence rom the pipeline and sets the first letter of every word to upper" { | |
$Expected = "The Quick Lazy Brown Fox Jumped Over The Fence" | |
$Sentence = "The Quick Lazy Brown Fox Jumped Over The Fence".ToLower() | |
$Actual = $Sentence | Set-CapitalFirstLetter | |
$Actual -ceq $Expected | Should Be $true | |
} | |
It "Takes a single word and sets the first letter to upper case" { | |
$Expected = "Brown" | |
$Sentence = "brown" | |
$Actual = Set-CapitalFirstLetter -Sentence $Sentence | |
$Actual -ceq $Expected | Should Be $true | |
} | |
It "Throws an error if Parameter sentence is null" { | |
$msg = @" | |
Cannot validate argument on parameter 'Sentence'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. | |
"@ | |
{Set-CapitalFirstLetter -Sentence $null} | should throw $msg | |
} | |
It "Throws an error if Parameter sentence is empty" { | |
$msg = @" | |
Cannot validate argument on parameter 'Sentence'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. | |
"@ | |
{Set-CapitalFirstLetter -Sentence ""} | should throw $msg | |
} | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment