|
7 | 7 | This is a simple module that provides functions to manipulate and search through Arrays, Collections, and Lists. |
8 | 8 |
|
9 | 9 | "ListFunctions" refers to the fact that some the functions are inspired by <code>System.Collections.Generic.List</code>'s methods. |
| 10 | + |
| 11 | +## Assert Cmdlets (Any / All) |
| 12 | + |
| 13 | +Sometimes you may want to just check if a specific collection has specific elements in it but don't need them themselves. |
| 14 | + |
| 15 | +### Assert-AnyObject |
| 16 | + |
| 17 | +_Aliases: __Any-Object__, __Any___ |
| 18 | + |
| 19 | +This cmdlet returns <code>true</code> or <code>false</code> if at least 1 element in the collection matches a condition - or - if no condition is provided, just if it contains at least 1 element. |
| 20 | + |
| 21 | +If a scriptblock condition is specified, then it will substitute any of the following variables for each element: <code>\$\_</code>, <code>\$this</code>, <code>\$psitem</code> |
| 22 | + |
| 23 | +```powershell |
| 24 | +$array = @(1, 2, 3) |
| 25 | +
|
| 26 | +if (($array | Any { $_ -gt 2})) { |
| 27 | +
|
| 28 | + # ... at least 1 element is greater than 2. |
| 29 | +} |
| 30 | +
|
| 31 | +if (($array | Any-Object)) { |
| 32 | +
|
| 33 | + # ... at least 1 element exists. |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### Assert-AllObject(s) |
| 38 | + |
| 39 | +This cmdlet returns <code>true</code> or <code>false</code> indicating whether __all__ elements in a collection match the specified condition. |
| 40 | + |
| 41 | +The scriptblock condition will substitute any of the following variables for each element: <code>\$\_</code>, <code>\$this</code>, <code>\$psitem</code> |
| 42 | + |
| 43 | +```powershell |
| 44 | +$array = @(1, 2, 'John') |
| 45 | +
|
| 46 | +if (-not ($array | All { $_ -is [int] })) { |
| 47 | +
|
| 48 | + #... at least 1 element is NOT an [int] object. |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Collection Constructors |
| 55 | + |
| 56 | +These cmdlets provide easier ways of constructing the more nuanced, generic types within the <code>System.Collections.Generic</code> API namespace. |
| 57 | + |
| 58 | +### New-List |
| 59 | + |
| 60 | +Constructs and returns a list of type `[System.Collections.Generic.List[T]]` where `T` is the generic type defined through the `-GenericType` parameter (defaults to `[object]`). |
| 61 | + |
| 62 | +```powershell |
| 63 | +# Create a list of objects with the default capacity (0). |
| 64 | +# Like [System.Collections.ArrayList], objects of *any* type can be added. |
| 65 | +$list = New-List |
| 66 | +
|
| 67 | +# Create a list of System.Guid objects with an initial capacity of 10,000. |
| 68 | +$list = New-List [guid] -Capacity 10000 |
| 69 | +
|
| 70 | +# Create a list of integers and provide it with the initial values to be added. |
| 71 | +$list = @(1, 2, '3') | New-List [int] |
| 72 | +# -or- |
| 73 | +$list = New-List [int] -InputObject @(3, '100', 56) |
| 74 | +
|
| 75 | +``` |
0 commit comments