From 7c013d3158d91ce18378c6740b4ead4c35b1f0ff Mon Sep 17 00:00:00 2001 From: Florian Date: Fri, 5 Oct 2018 13:55:57 +0100 Subject: [PATCH] Format-String Added generic function to format strings --- PSTK.psm1 | 192 +++++++++++++++++++++++++++++++-- test/Compare-Hashtables.ps1 | 67 ------------ test/Copy-OrderedHashtable.ps1 | 8 +- test/Read-Properties.ps1 | 6 +- test/Read-Property.ps1 | 14 +-- 5 files changed, 199 insertions(+), 88 deletions(-) delete mode 100644 test/Compare-Hashtables.ps1 diff --git a/PSTK.psm1 b/PSTK.psm1 index 016aad6..5b89960 100644 --- a/PSTK.psm1 +++ b/PSTK.psm1 @@ -11,11 +11,15 @@ File name: PSTK.psm1 Author: Florian Carrier Creation date: 23/08/2018 - Last modified: 02/10/2018 + Last modified: 04/10/2018 Repository: https://github.com/Akaizoku/PSTK + Depndencies: Test-SQLConnection requires the SQLServer module .LINK https://github.com/Akaizoku/PSTK + + .LINK + https://docs.microsoft.com/en-us/sql/powershell/download-sql-server-ps-module #> # ------------------------------------------------------------------------------ @@ -81,7 +85,12 @@ function Write-Log { Mandatory = $true, HelpMessage = "Type of message to output" )] - [ValidateSet ("CHECK","ERROR","INFO","WARN")] + [ValidateSet ( + "CHECK", + "ERROR", + "INFO", + "WARN" + )] [String] $Type, [Parameter ( @@ -520,7 +529,7 @@ function Get-Properties { # ------------------------------------------------------------------------------ # Function to compare hashtables content # ------------------------------------------------------------------------------ -function Compare-Hashtables { +function Compare-Hashtable { <# .SYNOPSIS Compares hashtables content @@ -536,11 +545,11 @@ function Compare-Hashtables { first one. .OUTPUTS - Boolean. Compare-Hashtables returns a boolean depnding on the result of the + Boolean. Compare-Hashtable returns a boolean depnding on the result of the comparison between the two hashtables. .EXAMPLE - Compare-Hashtables -Reference $Hashtable1 -Difference $Hashtable2 + Compare-Hashtable -Reference $Hashtable1 -Difference $Hashtable2 #> [CmdletBinding ()] Param ( @@ -847,7 +856,10 @@ function ConvertTo-NaturalSort { Mandatory = $false, HelpMessage = "Specifies the order of the sort" )] - [ValidateSet ("A", "Asc", "Ascending", "D", "Dsc", "Desc", "Descending")] + [ValidateSet ( + "A", "Asc", "Ascending", + "D", "Dsc", "Desc", "Descending" + )] [String] $Order = "Ascending" ) @@ -1588,7 +1600,11 @@ function Get-Object { Mandatory = $false, HelpMessage = "Type of item" )] - [ValidateSet ("All", "File", "Folder")] + [ValidateSet ( + "All", + "File", + "Folder" + )] [String] $Type = "All", [Parameter ( @@ -1860,3 +1876,165 @@ function New-DynamicParameter { return $ParameterDictionary } } + +# ------------------------------------------------------------------------------ +# Capitalise string +# ------------------------------------------------------------------------------ +function ConvertTo-TitleCase { + <# + .SYNOPSIS + Convert string to title case + + .DESCRIPTION + Capitalise the first letter of each words in a string to form a title. + + .PARAMETER String + The string parameter corresponds to the string to format. It can be a single + word or a complete sentence. + + .PARAMETER Delimiter + The delimiter parameter corresponds to the character used to delimit dis- + tinct words in the string. + The default delimiter for words is the space character. + #> + [CmdletBinding ()] + Param ( + [Parameter ( + Position = 1, + Mandatory = $true, + HelpMessage = "String to format" + )] + [ValidateNotNullOrEmpty ()] + [String] + $String, + [Parameter ( + Position = 2, + Mandatory = $false, + HelpMessage = "Word delimiter" + )] + [String] + $Delimiter = " " + ) + Begin { + $Words = $String.Split($Delimiter) + $FormattedWords = New-Object -TypeName System.Collections.ArrayList + } + Process { + foreach ($Word in $Words) { + [Void]$FormattedWords.Add((Get-Culture).TextInfo.ToTitleCase($Word.ToLower())) + } + $FormattedString = $FormattedWords -join " " + return $FormattedString + } +} + +# ------------------------------------------------------------------------------ +# Format string +# ------------------------------------------------------------------------------ +function Format-String { + <# + .SYNOPSIS + Format a string + + .DESCRIPTION + Convert a string to a specified format + + .PARAMETER String + The string parameter corresponds to the string to format. It can be a single + word or a complete sentence. + + .PARAMETER Format + The format parameter corresponds to the case to convert the string to. + + .PARAMETER Delimiter + The delimiter parameter corresponds to the character used to delimit dis- + tinct words in the string. + The default delimiter for words is the space character. + + .NOTES + The delimiter for words is the space character. + #> + [CmdletBinding ()] + Param ( + [Parameter ( + Position = 1, + Mandatory = $true, + HelpMessage = "String to format" + )] + [ValidateNotNullOrEmpty ()] + [String] + $String, + [Parameter ( + Position = 2, + Mandatory = $true, + HelpMessage = "Format" + )] + [ValidateSet ( + "CamelCase", + "KebabCase", + "LowerCase", + "PaslcalCase", + "SentenceCase", + "SnakeCase", + "TitleCase", + "TrainCase", + "UpperCase" + )] + [String] + $Format, + [Parameter ( + Position = 3, + Mandatory = $false, + HelpMessage = "Word delimiter" + )] + [String] + $Delimiter = " " + ) + Begin { + # List cases that have to be capitalized + $Delimiters = [Ordered]@{ + "CamelCase" = "" + "KebabCase" = "-" + "LowerCase" = $Delimiter + "PaslcalCase" = "" + "SentenceCase" = " " + "SnakeCase" = "_" + "TitleCase" = " " + "TrainCase" = "_" + "UpperCase" = $Delimiter + } + $Capitalise = [Ordered]@{ + First = @("PaslcalCase","SentenceCase","TitleCase","TrainCase") + Others = @("CamelCase","PaslcalCase","SentenceCase","TitleCase","TrainCase") + } + # Create array of words + $Words = $String.Split($Delimiter) + $Counter = 0 + $FormattedWords = New-Object -TypeName System.Collections.ArrayList + } + Process { + foreach ($Word in $Words) { + if ($Format -ne "UpperCase") { + if ($Counter -gt 0) { + if ($Format -in $Capitalise.Others) { + [Void]$FormattedWords.Add((ConvertTo-TitleCase -String $Word)) + } else { + [Void]$FormattedWords.Add($Word.ToLower()) + } + } else { + if ($Format -in $Capitalise.First) { + [Void]$FormattedWords.Add((ConvertTo-TitleCase -String $Word)) + } else { + [Void]$FormattedWords.Add($Word.ToLower()) + } + } + } else { + [Void]$FormattedWords.Add($Word.ToUpper()) + } + $Counter += 1 + } + # Reconstruct string + $FormattedString = $FormattedWords -join $Delimiters.$Format + return $FormattedString + } +} diff --git a/test/Compare-Hashtables.ps1 b/test/Compare-Hashtables.ps1 deleted file mode 100644 index 6c752c7..0000000 --- a/test/Compare-Hashtables.ps1 +++ /dev/null @@ -1,67 +0,0 @@ -<# - .SYNOPSIS - Compare-Hashtables Unit Testing - - .DESCRIPTION - Unit Test for Compare-Hashtables function from PSTK module - - .NOTES - File name: Compare-Hashtables.ps1 - Author: Florian Carrier - Creation date: 31/08/2018 - Last modified: 26/09/2018 -#> - -# ------------------------------------------------------------------------------ -# Initialisation -# ------------------------------------------------------------------------------ -$Path = Split-Path $MyInvocation.MyCommand.Definition -$Repository = Split-Path $Path -Parent -# Import toolbox -Import-Module "$Repository\PSTK" -Force - -# ------------------------------------------------------------------------------ -# Test objects -# ------------------------------------------------------------------------------ -# Reference -$Reference = [ordered]@{ - Property1 = 1 - Property2 = 2 - Property3 = 3 - Property4 = 4 - Property5 = 5 -} -# Exact match -$Exact = [ordered]@{ - Property1 = 1 - Property2 = 2 - Property3 = 3 - Property4 = 4 - Property5 = 5 -} -# No match -$Inexact = [ordered]@{ - Property1 = 5 - Property2 = 4 - Property3 = 3 - Property4 = 2 - Property5 = 1 -} -# Empty hashtable -$Empty = [ordered]@{} - -# ------------------------------------------------------------------------------ -# Test -# ------------------------------------------------------------------------------ -$CheckExact = Compare-Hashtables -Reference $Reference -Difference $Exact -$CheckInexact = Compare-Hashtables -Reference $Reference -Difference $Inexact -$CheckEmpty = Compare-Hashtables -Reference $Reference -Difference $Empty - -# ------------------------------------------------------------------------------ -# Check outcome -# ------------------------------------------------------------------------------ -if ($CheckExact -And !$CheckInexact -And !$CheckEmpty) { - return $true -} else { - return $false -} diff --git a/test/Copy-OrderedHashtable.ps1 b/test/Copy-OrderedHashtable.ps1 index 32fb447..40ef29f 100644 --- a/test/Copy-OrderedHashtable.ps1 +++ b/test/Copy-OrderedHashtable.ps1 @@ -9,7 +9,7 @@ File name: Copy-OrderedHashtable.ps1 Author: Florian Carrier Creation date: 31/08/2018 - Last modified: 26/09/2018 + Last modified: 04/10/2018 #> # ------------------------------------------------------------------------------ @@ -50,13 +50,13 @@ $Empty = [ordered]@{} # Test # ------------------------------------------------------------------------------ $CloneSimple = Copy-OrderedHashtable -Hashtable $Simple -$CheckSimple = Compare-Hashtables -Reference $CloneSimple -Difference $Simple +$CheckSimple = Compare-Hashtable -Reference $CloneSimple -Difference $Simple $CloneComplex = Copy-OrderedHashtable -Hashtable $Complex -$CheckComplex = Compare-Hashtables -Reference $CloneComplex -Difference $Complex +$CheckComplex = Compare-Hashtable -Reference $CloneComplex -Difference $Complex $CloneEmpty = Copy-OrderedHashtable -Hashtable $Empty -$CheckEmpty = Compare-Hashtables -Reference $CloneEmpty -Difference $Empty +$CheckEmpty = Compare-Hashtable -Reference $CloneEmpty -Difference $Empty # ------------------------------------------------------------------------------ # Check outcome diff --git a/test/Read-Properties.ps1 b/test/Read-Properties.ps1 index dc8d721..a3ac924 100644 --- a/test/Read-Properties.ps1 +++ b/test/Read-Properties.ps1 @@ -9,7 +9,7 @@ File name: Read-Properties.ps1 Author: Florian Carrier Creation date: 31/08/2018 - Last modified: 26/09/2018 + Last modified: 04/10/2018 #> # ------------------------------------------------------------------------------ @@ -60,11 +60,11 @@ $Expected2 = [ordered]@{ # ------------------------------------------------------------------------------ # Without Sections $Properties1 = Read-Properties -File $PropertyFile -Directory $Repository\test\res -$Check1 = Compare-Hashtables -Reference $Expected1 -Difference $Properties1 +$Check1 = Compare-Hashtable -Reference $Expected1 -Difference $Properties1 # With Sections $Properties2 = Read-Properties -File $PropertyFile -Directory $Repository\test\res -Section -$Check2 = Compare-Hashtables -Reference $Expected2 -Difference $Properties2 +$Check2 = Compare-Hashtable -Reference $Expected2 -Difference $Properties2 # ------------------------------------------------------------------------------ # Check outcome diff --git a/test/Read-Property.ps1 b/test/Read-Property.ps1 index 4d54421..a2e653d 100644 --- a/test/Read-Property.ps1 +++ b/test/Read-Property.ps1 @@ -9,7 +9,7 @@ File name: Read-Property.ps1 Author: Florian Carrier Creation date: 31/08/2018 - Last modified: 26/09/2018 + Last modified: 04/10/2018 #> # ------------------------------------------------------------------------------ @@ -38,12 +38,12 @@ $Property4 = Read-Property -Content "Property Name =Property Value" $Property5 = Read-Property -Content "Property Name=Property Value" $Property6 = Read-Property -Content "Property Name= Property Value" -$Check1 = Compare-Hashtables -Reference $Expected -Difference $Property1 -$Check2 = Compare-Hashtables -Reference $Expected -Difference $Property2 -$Check3 = Compare-Hashtables -Reference $Expected -Difference $Property3 -$Check4 = Compare-Hashtables -Reference $Expected -Difference $Property4 -$Check5 = Compare-Hashtables -Reference $Expected -Difference $Property5 -$Check6 = Compare-Hashtables -Reference $Expected -Difference $Property6 +$Check1 = Compare-Hashtable -Reference $Expected -Difference $Property1 +$Check2 = Compare-Hashtable -Reference $Expected -Difference $Property2 +$Check3 = Compare-Hashtable -Reference $Expected -Difference $Property3 +$Check4 = Compare-Hashtable -Reference $Expected -Difference $Property4 +$Check5 = Compare-Hashtable -Reference $Expected -Difference $Property5 +$Check6 = Compare-Hashtable -Reference $Expected -Difference $Property6 # ------------------------------------------------------------------------------ # Check outcome