functions/string/Join-PSUString.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function Join-PSUString
{
<#
    .SYNOPSIS
        Joins input strings together.
     
    .DESCRIPTION
        Joins input strings together.
     
    .PARAMETER InputString
        The strings to join
     
    .PARAMETER With
        What to join the strings with.
     
    .PARAMETER BatchSize
        Default: 0
        How many to join together at a time.
        If 0 or lower are specfied, all strings will be joined together.
        Otherwise, it will join [BatchSize] strigns together at a time.
     
    .EXAMPLE
        1..9 | join ","
     
        Returns "1,2,3,4,5,6,7,8,9"
     
    .EXAMPLE
        1..9 | join "," -BatchSize 5
     
        Returns "1,2,3,4,5", "6,7,8,9"
#>

    [OutputType([System.String])]
    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline = $true)]
        [string[]]
        $InputString,
        
        [Parameter(Position = 0)]
        [string]
        $With = ([System.Environment]::NewLine),
        
        [int]
        $BatchSize
    )
    
    begin
    {
        $lines = @()
    }
    process
    {
        foreach ($line in $InputString)
        {
            $lines += $line
            
            if (($BatchSize -gt 0) -and ($lines.Count -ge $BatchSize))
            {
                $lines -join $With
                $lines = @()
            }
        }
    }
    end
    {
        $lines -join $With
    }
}
Import-PSUAlias -Name join -Command Join-PSUString