Skip to content

Commit

Permalink
Removed -Recurse, updated Regex Pattern, added help (#200)
Browse files Browse the repository at this point in the history
* Removed -Recurse, updated Regex Pattern, added help

* Update PSKoans/Koans/Constructs and Patterns/AboutSplatting.Koans.ps1

* Cleaned up incompleted comment.

* Fixed up comment block.
  • Loading branch information
Brandon Lundt authored and vexx32 committed Jun 11, 2019
1 parent 8a4e35a commit 8db3533
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions PSKoans/Koans/Constructs and Patterns/AboutSplatting.Koans.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,33 @@ Describe 'Splatting' {
# Here are a few common ways a detailed command to find files in a folder might be written:

# 1. Super long lines; hard to follow along with.
$LongLines = Get-ChildItem -Path $PSKoansFolder -Include '*.ps1' -Recurse -Depth 2
$LongLines = Get-ChildItem -Path $PSKoansFolder -Include '*.ps1' -Depth 2

# 2. Escaping linebreaks; more readable, but very fragile and prone to errors
$Escaping = Get-ChildItem `
-Path $PSKoansFolder `
-Include '*.ps1' `
-Recurse `
-Depth 2

# 3. Splatting using a hashtable. Note the similarity to #2 and fill in missing values.
$Parameters = @{
Path = $PSKoansFolder
Include = '__'
Recurse = $true # Switches can be assigned a boolean value.
Depth = __
}
$Splatted = Get-ChildItem @Parameters
<#
All above approaches are equal in effect, but splatting is a much tidier and more
maintainable approach.
#>
$Splatted | Should -Be $Escaping
$Escaping | Should -Be $LongLines
$Splatted.Count | Should -Be $Escaping.Count
$Splatted.Name | Should -Be $Escaping.Name

$LongLines | Should -Be $Splatted
$Escaping.Count | Should -Be $LongLines.Count
$Escaping.Name | Should -Be $LongLines.Name

$LongLines.Count | Should -Be $Splatted.Count
$LongLines.Name | Should -Be $Splatted.Name
}

}
Expand All @@ -65,6 +67,7 @@ Describe 'Splatting' {
$Value = __
if ($Value -eq 7) {
$Parameters.Add('File', $true)
$Parameters.Add('Recurse', $true)
}
else {
$Parameters.Add('Directory', $true)
Expand All @@ -77,13 +80,24 @@ Describe 'Splatting' {

It 'can be built from automatic hashtables' {
$String = "Folders:__"
$Parameters = if ($String -match 'Folders:(?<Filter>[a-z ])$') {
$Parameters = if ($String -match 'Folders:(?<Filter>[_\d\w ]*)$') {
<#
Matches is automaticed populated by the -match operater in the if statement
If $String = "Folders:00_TheBasics"
Then $Matches holds the following:
Name Value
---- -----
Filter 00_TheBasics
0 Folders:00_TheBasics
#>
$Matches.Remove(0) # Remove the 'whole' match and keep only the portions we asked for
# Clone() copies the remaining matches, so we can store them with the $Parameters assignment above.
$Matches.Clone()
}
$Parameters.Add('Path', $PSKoansFolder)
$Parameters.Add('Directory', $true)
(Get-ChildItem @Parameters).Count | Should -Be 2

(Get-ChildItem @Parameters).Count | Should -Be __
}
}
}

0 comments on commit 8db3533

Please sign in to comment.