Skip to content

Commit 4f60067

Browse files
committedSep 29, 2024·
Add Magic-method-Where
1 parent 00e6882 commit 4f60067

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed
 

‎Basic/Magic-method-Where/.test.ps1

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
task Test-1.magic-Where.ps1 {
3+
($1, $2, $3, $4, $5, $6 = ./Test-1.magic-Where.ps1)
4+
equals $1 'Collection`1'
5+
equals $2 0
6+
equals $3 'Collection`1'
7+
equals $4 1
8+
equals $5 'Collection`1'
9+
equals $6 2
10+
}
11+
12+
task Test-2.Where-Object.ps1 {
13+
($1, $2, $3 = ./Test-2.Where-Object.ps1)
14+
equals $1 $true
15+
equals $2 String
16+
equals $3 Object[]
17+
}

‎Basic/Magic-method-Where/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Magic method `Where`
2+
3+
The magic method `Where` is similar to the cmdlet `Where-Object` but not
4+
exactly the same. The differences may lead to subtle mistakes, e.g.
5+
on carelessly changing some code from using one to another.
6+
7+
The magic method `Where` returns a collection of zero, one, or more items.
8+
The result type is always ``[System.Collections.ObjectModel.Collection`1[PSObject]]``.
9+
10+
In same cases, i.e. with same input and script blocks, the cmdlet `Where-Object`
11+
returns either nothing (kind of null) or one item (the type depends on it)
12+
or an array of items. The result type is none, some, or `System.Object[]`.
13+
14+
**Scripts**
15+
16+
- [Test-1.magic-Where.ps1](Test-1.magic-Where.ps1)
17+
- [Test-2.Where-Object.ps1](Test-2.Where-Object.ps1)
18+
19+
---
20+
21+
- [ForEach and Where magic methods](https://powershellmagazine.com/2014/10/22/foreach-and-where-magic-methods/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
$items = 'apple', 'banana', 'orange'
2+
3+
# Empty collection
4+
$r = $items.Where({$_ -like 'foo*'})
5+
$r.GetType().Name
6+
$r.Count
7+
8+
# Collection of 1 item
9+
$r = $items.Where({$_ -like 'ban*'})
10+
$r.GetType().Name
11+
$r.Count
12+
13+
# Collection of 2 items
14+
$r = $items.Where({$_ -like '*an*'})
15+
$r.GetType().Name
16+
$r.Count
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
$items = 'apple', 'banana', 'orange'
2+
3+
# Nothing, kind of null
4+
$r = $items | Where-Object {$_ -like 'foo*'}
5+
$null -eq $r
6+
7+
# String
8+
$r = $items | Where-Object {$_ -like 'ban*'}
9+
$r.GetType().Name
10+
11+
# Array
12+
$r = $items | Where-Object {$_ -like '*an*'}
13+
$r.GetType().Name

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ to their directory. See also [TESTS]. Some scripts require
4646
- [Invocation with odd paths](Basic/Invocation-with-odd-paths)
4747
- [LastExitCode](Basic/LastExitCode)
4848
- [Local action preference variables](Basic/Local-ActionPreference)
49+
- [Magic method `Where`](Basic/Magic-method-Where)
4950
- [Misleading error location](Basic/Misleading-error-location)
5051
- [Missing ternary operator](Basic/Missing-ternary-operator)
5152
- [Negative number literal argument](Basic/Negative-number-literal-argument)

0 commit comments

Comments
 (0)
Please sign in to comment.