Skip to content

Commit 31ae23e

Browse files
committed
✨ Add Select-Items to enumerate collection elements
Fixes #29
1 parent 521905a commit 31ae23e

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ A collection of general-purpose functions for objects, properties, and more.
7676
- [Remove-ParameterDefault](https://github.com/brianary/ModernConveniences/wiki/Remove-ParameterDefault): Removes a value that would have been used for a parameter if none was specified, if one existed.
7777
- [Repair-MarkdownHeaders](https://github.com/brianary/ModernConveniences/wiki/Repair-MarkdownHeaders): Updates markdown content to replace level 1 & 2 ATX headers to Setext headers.
7878
- [Select-CapturesFromMatches](https://github.com/brianary/ModernConveniences/wiki/Select-CapturesFromMatches): Selects named capture group values as note properties from Select-String MatchInfo objects.
79+
- [Select-Each](https://github.com/brianary/ModernConveniences/wiki/Select-Each): Returns each element from a collection for the next process in the pipeline, individually.
7980
- [Select-ScriptCommands](https://github.com/brianary/ModernConveniences/wiki/Select-ScriptCommands): Returns the commands used by the specified script.
8081
- [Set-Clip](https://github.com/brianary/ModernConveniences/wiki/Set-Clip): Sets the contents of the clipboard with formatted HTML support (cross-platform, and more reliably on Linux Wayland).
8182
- [Set-ParameterDefault](https://github.com/brianary/ModernConveniences/wiki/Set-ParameterDefault): Assigns a value to use for the specified cmdlet parameter to use when one is not specified.

‎src/public/Select-Each.ps1‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<#
2+
.SYNOPSIS
3+
Returns each element from a collection for the next process in the pipeline, individually.
4+
5+
.INPUTS
6+
Any collection type.
7+
8+
.OUTPUTS
9+
Each item contained in the collection.
10+
11+
.FUNCTIONALITY
12+
Collection
13+
14+
.EXAMPLE
15+
@{A=1;B=2;C=3} |Select-Each |Measure-Object |Select-Object -ExpandProperty Count
16+
17+
3
18+
#>
19+
20+
#Requires -Version 7.3
21+
[CmdletBinding()] Param(
22+
# The collection to return each item from.
23+
[Parameter(ValueFromPipeline=$true)][psobject] $InputObject
24+
)
25+
Process
26+
{
27+
if($InputObject |Get-Member -Type Method -Name GetEnumerator -ErrorAction Ignore) {return $InputObject.GetEnumerator()}
28+
else
29+
{
30+
Write-Warning "Unable to reliably return each item from type '$($null -eq $InputObject ? '(null)' : $InputObject.GetType().FullName)'."
31+
return ($InputObject)
32+
}
33+
}

0 commit comments

Comments
 (0)