Skip to content

Commit 31ce17b

Browse files
committed
Formatting changes and comment-based help
1 parent 305afe9 commit 31ce17b

12 files changed

+312
-240
lines changed

ListFunctions.psm1

Lines changed: 155 additions & 119 deletions
Large diffs are not rendered by default.

Private/BuildPredicate.ps1

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
1-
Function BuildPredicate()
2-
{
1+
Function BuildPredicate() {
2+
33
[CmdletBinding()]
44
[OutputType([System.Predicate[object]])]
55
param
66
(
7-
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
7+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
88
[scriptblock] $ScriptBlock
99
)
10-
Begin
11-
{
10+
Begin {
11+
1212
$rebuild = New-Object -TypeName 'System.Collections.Generic.List[string]' -ArgumentList 2
1313
}
14-
Process
15-
{
14+
Process {
15+
1616
# Replace all instances of '$_' and '$PSItem' in the ScriptBlock with '$x'
1717
$sbString = $ScriptBlock.ToString().Replace('$_', '$x')
1818
$matchCol = [regex]::Matches($sbString, '\$PSItem', "IgnoreCase")
19+
1920
$matchCol | Select-Object Value -Unique | ForEach-Object {
2021
$sbString = $sbString.Replace($PSItem.Value, [string]'$_')
2122
}
2223

2324
# Split the ScriptBlock by new lines and add them to $rebuild
2425
$rebuild.AddRange(($sbString -split "`n"))
2526

26-
if ($rebuild[0] -cnotmatch '^\s*param') # If the first line is not the start of a 'param' block then...
27-
{
27+
if ($rebuild[0] -cnotmatch '^\s*param') { # If the first line is not the start of a 'param' block then...
28+
2829
$rebuild.Insert(0, 'param ($x)') # ...insert one at the beginning of the list.
2930
}
3031

Private/ComparerBuilder.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ namespace ListFunctions
5959
"@
6060

6161
$atArgs = @{
62-
TypeDefinition = $code
63-
Language = "CSharp"
62+
TypeDefinition = $code
63+
Language = "CSharp"
6464
ReferencedAssemblies = @(
6565
"System",
6666
"System.Collections",

Private/NewEqualityComparer.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ Function NewEqualityComparer() {
66
[ValidateScript( { $_ -is [type] -or $_ -is [string] })]
77
[object] $GenericType = "[object]",
88

9-
[Parameter(Mandatory=$true)]
9+
[Parameter(Mandatory = $true)]
1010
[scriptblock] $EqualityScript,
1111

12-
[Parameter(Mandatory=$true)]
12+
[Parameter(Mandatory = $true)]
1313
[scriptblock] $HashCodeScript
1414
)
1515

Public/Assert-All.ps1

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Function Assert-All()
2-
{
1+
Function Assert-All() {
32
<#
43
.SYNOPSIS
54
Asserts all objects of a collections satisfy a condition.
@@ -42,33 +41,27 @@
4241
[OutputType([bool])]
4342
param
4443
(
45-
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
44+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
4645
[AllowNull()]
4746
[AllowEmptyCollection()]
4847
[object[]] $InputObject,
4948

50-
[Parameter(Mandatory=$true, Position=0)]
49+
[Parameter(Mandatory = $true, Position = 0)]
5150
[scriptblock] $Condition
5251
)
53-
Begin
54-
{
52+
Begin {
5553
$list = New-Object -TypeName "System.Collections.Generic.List[object]"
5654
}
57-
Process
58-
{
59-
if ($null -ne $InputObject -and $InputObject.Length -gt 0)
60-
{
55+
Process {
56+
if ($null -ne $InputObject -and $InputObject.Length -gt 0) {
6157
$list.AddRange($InputObject)
6258
}
6359
}
64-
End
65-
{
66-
if ($list.Count -gt 0)
67-
{
60+
End {
61+
if ($list.Count -gt 0) {
6862
$list.Where($Condition).Count -eq $list.Count
6963
}
70-
else
71-
{
64+
else {
7265
$false
7366
}
7467
}

Public/Assert-Any.ps1

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Function Assert-Any()
2-
{
1+
Function Assert-Any() {
32
<#
43
.SYNOPSIS
54
Asserts any object of a collection exists or matches a condition.
@@ -42,40 +41,32 @@
4241
[OutputType([bool])]
4342
param
4443
(
45-
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
44+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
4645
[AllowNull()]
4746
[AllowEmptyCollection()]
4847
[object[]] $InputObject,
4948

50-
[Parameter(Mandatory=$false, Position=0)]
49+
[Parameter(Mandatory = $false, Position = 0)]
5150
[scriptblock] $Condition
5251
)
53-
Begin
54-
{
52+
Begin {
5553
$list = New-Object -TypeName "System.Collections.Generic.List[object]"
5654
}
57-
Process
58-
{
59-
if ($null -ne $InputObject -and $InputObject.Length -gt 0)
60-
{
55+
Process {
56+
if ($null -ne $InputObject -and $InputObject.Length -gt 0) {
6157
$list.AddRange($InputObject)
6258
}
6359
}
64-
End
65-
{
66-
if ($list.Count -gt 0)
67-
{
68-
if ($PSBoundParameters.ContainsKey("Condition"))
69-
{
60+
End {
61+
if ($list.Count -gt 0) {
62+
if ($PSBoundParameters.ContainsKey("Condition")) {
7063
$list.Where($Condition).Count -gt 0
7164
}
72-
else
73-
{
65+
else {
7466
$true
7567
}
7668
}
77-
else
78-
{
69+
else {
7970
$false
8071
}
8172
}

Public/Find-IndexOf.ps1

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Function Find-IndexOf()
2-
{
1+
Function Find-IndexOf() {
32
<#
43
.SYNOPSIS
54
Finds the index of the first element that matches a condition.
@@ -84,23 +83,18 @@
8483
[Parameter(Mandatory = $false)]
8584
[int] $Count
8685
)
87-
Begin
88-
{
86+
Begin {
8987
$list = New-Object -TypeName "System.Collections.Generic.List[object]"
9088
$Predicate = BuildPredicate -ScriptBlock $Condition
9189
}
92-
Process
93-
{
90+
Process {
9491
$list.AddRange($InputObject)
9592
}
96-
End
97-
{
98-
if (-not $PSBoundParameters.ContainsKey("Count"))
99-
{
93+
End {
94+
if (-not $PSBoundParameters.ContainsKey("Count")) {
10095
$list.FindIndex($StartIndex, $Predicate)
10196
}
102-
else
103-
{
97+
else {
10498
$list.FindIndex($StartIndex, $Count, $Predicate)
10599
}
106100
}

Public/Find-LastIndexOf.ps1

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Function Find-LastIndexOf()
2-
{
1+
Function Find-LastIndexOf() {
32
<#
43
.SYNOPSIS
54
Finds the index of the last element that matches a condition.
@@ -67,45 +66,39 @@
6766
6867
Look at Example #3 to see an example of this.
6968
#>
70-
[CmdletBinding(DefaultParameterSetName="None")]
69+
[CmdletBinding(DefaultParameterSetName = "None")]
7170
[Alias("Find-LastIndex", "LastIndexOf")]
7271
[OutputType([int])]
7372
param
7473
(
75-
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
74+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
7675
[object[]] $InputObject,
7776

78-
[Parameter(Mandatory=$true, Position=0)]
77+
[Parameter(Mandatory = $true, Position = 0)]
7978
[scriptblock] $Condition,
8079

81-
[Parameter(Mandatory=$true, ParameterSetName="ByStartingIndex")]
80+
[Parameter(Mandatory = $true, ParameterSetName = "ByStartingIndex")]
8281
[int] $StartIndex,
8382

84-
[Parameter(Mandatory=$false, ParameterSetName="ByStartingIndex")]
83+
[Parameter(Mandatory = $false, ParameterSetName = "ByStartingIndex")]
8584
[int] $Count
8685
)
87-
Begin
88-
{
86+
Begin {
8987
$list = New-Object -TypeName "System.Collections.Generic.List[object]"
9088
$Predicate = BuildPredicate -ScriptBlock $Condition
9189
}
92-
Process
93-
{
90+
Process {
9491
$list.AddRange($InputObject)
9592
}
96-
End
97-
{
98-
if (-not $PSBoundParameters.ContainsKey("StartIndex"))
99-
{
93+
End {
94+
if (-not $PSBoundParameters.ContainsKey("StartIndex")) {
10095
$StartIndex = $list.Count - 1
10196
}
10297

103-
if (-not $PSBoundParameters.ContainsKey("Count"))
104-
{
98+
if (-not $PSBoundParameters.ContainsKey("Count")) {
10599
$list.FindLastIndex($StartIndex, $Predicate)
106100
}
107-
else
108-
{
101+
else {
109102
$list.FindLastIndex($StartIndex, $Count, $Predicate)
110103
}
111104
}

Public/New-HashSet.ps1

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,52 @@
11
Function New-HashSet() {
2+
<#
3+
.SYNOPSIS
4+
Creates a HashSet of unique values.
5+
6+
.DESCRIPTION
7+
Creates a 'System.Collections.Generic.HashSet[T]' in order to store unique values into.
8+
9+
.PARAMETER Capacity
10+
The total number of elements the set can hold without resizing. Default -- 0.
11+
12+
.PARAMETER GenericType
13+
The constraining type that every object added into the list must be.
14+
15+
.PARAMETER EqualityScript
16+
The scripblock that will check the equality between any 2 objects in the set. It must return a boolean (True/False) value.
17+
18+
'$x' -or- '$args[0]' must represent the 1st item to be compared.
19+
'$y' -or - '$args[1]' must represent the 2nd item to be compared.
20+
21+
.PARAMETER HashCodeScript
22+
The scriptblock that retrieve an item's hash code value.
23+
24+
A "hash code" is a numeric value that is used to insert and identify an object in a "hash-based" collection.
25+
The easiest way to provide this through an object's 'GetHashCode()' method.
26+
Two objects that are equal return hash codes that are equal. However, the reverse is not true: equal hash codes
27+
do not imply object equality, becuase different (unequal) objects can have identical hash codes.
28+
29+
.INPUTS
30+
System.Object[] -- The objects that will immediately added to the returned set.
31+
32+
.OUTPUTS
33+
System.Collections.Generic.HashSet[T] -- where 'T' is the constrained generic type that all objects must be.
34+
35+
.EXAMPLE
36+
# Create a HashSet[string] that ignores case for equality.
37+
$set = New-HashSet [string] -EqualityScript { $x -eq $y } -HashCodeScript { $_.ToLower().GetHashCode() }
38+
39+
.EXAMPLE
40+
# Create a HashSet[object] that objects with the same 'Name' and 'Id' properties equal.
41+
$set = New-HashSet -EqualityScript { $x.Name -eq $y.Name -and $x.Id -eq $y.Id }
42+
43+
.NOTES
44+
The EqualityScript must use either '$x' and '$y' -or- '$args[0]' and '$args[1]' in the
45+
scriptblock to properly identify the 2 comparing values.
46+
47+
The HashCodeScript must use either '$_' -or- '$args[0]' in the scriptblock to properly identify
48+
the object whose hash code is retrieved.
49+
#>
250

351
[CmdletBinding(DefaultParameterSetName = "None")]
452
param (
@@ -18,8 +66,8 @@ Function New-HashSet() {
1866
[Parameter(Mandatory = $true, ParameterSetName = "WithCustomEqualityComparer")]
1967
[scriptblock] $EqualityScript,
2068

21-
[Parameter(Mandatory = $true, ParameterSetName = "WithCustomEqualityComparer")]
22-
[scriptblock] $HashCodeScript
69+
[Parameter(Mandatory = $false, ParameterSetName = "WithCustomEqualityComparer")]
70+
[scriptblock] $HashCodeScript = { $_.GetHashCode() }
2371
)
2472
Begin {
2573

Public/New-List.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,37 @@
11
Function New-List() {
2+
<#
3+
.SYNOPSIS
4+
Creates a strongly-typed list of objects.
5+
6+
.DESCRIPTION
7+
Generates a strongly-typed list of objects that can be accessed by index.
8+
9+
'System.Collections.Generic.List[T]'
10+
11+
This functions creates the list with the specified constrained type and the specified capacity. Objects can be immediately
12+
added to the list by explicit calling the "InputObject" parameter or by passing the objects through the pipeline.
13+
14+
.PARAMETER Capacity
15+
The total number of elements the list can hold without resizing. Default -- 0.
16+
17+
.PARAMETER GenericType
18+
The constraining type that every object added into the list must be.
19+
20+
.PARAMETER InputObject
21+
A collection of objects that will initially added into the new list.
22+
23+
.INPUTS
24+
System.Object[] -- Objects of any type of if constrained with a generic, objects must be of that type.
25+
26+
.OUTPUTS
27+
System.Collections.Generic.List[T] -- where 'T' is the constrained object type.
28+
29+
.EXAMPLE
30+
$list = New-List 780 -Type [string]
31+
32+
.EXAMPLE
33+
$list = ,@('hi', 'hello', 'goodbye') | New-List 3 -GenericType [string]
34+
#>
235
[CmdletBinding()]
336
param
437
(

0 commit comments

Comments
 (0)