functions/string/Format-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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
function Format-PSUString
{
<#
    .SYNOPSIS
        Allows formatting objects into strings.
     
    .DESCRIPTION
        Allows formatting objects into strings.
        This is equivalent to the '-f' operator, but supports input from pipeline.
     
    .PARAMETER InputObject
        The object to format
     
    .PARAMETER Format
        The format to apply to the object
     
    .PARAMETER LotSize
        Default: 1
        How many inputo bjects should be packed into the same format string.
     
    .PARAMETER Property
        Property name(s) from the input object to use for formatting.
        If omitted, the base object will be used.
     
    .EXAMPLE
        1..5 | format "foo {0:D2}"
     
        returns "foo 01" through "foo 05"
     
    .EXAMPLE
        1..6 | format "foo {0:D3}-{1:D3}" -LotSize 2
     
        returns "foo 001-002","foo 003-004","foo 005-006"
     
    .EXAMPLE
        Get-ChildItem | Format-PSUString '{0} : {1}' -Property Name, Length
     
        Returns a list of strings, using the Name and Length property of the items in the current folder.
#>

    [OutputType([System.String])]
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true)]
        $InputObject,
        
        [Parameter(Position = 0, Mandatory = $true)]
        [string]
        $Format,
        
        [int]
        $LotSize = 1,
        
        [string[]]
        $Property
    )
    
    begin
    {
        $values = @()
    }
    process
    {
        foreach ($item in $InputObject)
        {
            $values += $item
            if ($values.Count -ge $LotSize)
            {
                if ($Property)
                {
                    $propertyValues = @()
                    foreach ($value in $values)
                    {
                        foreach ($propertyName in $Property)
                        {
                            $propertyValues += $value.$propertyName
                        }
                    }
                    $Format -f $propertyValues
                }
                else
                {
                    $Format -f $values
                }
                $values = @()
            }
        }
    }
    end
    {
        if ($values.Count -gt 0)
        {
            if ($Property)
            {
                $propertyValues = @()
                foreach ($value in $values)
                {
                    foreach ($propertyName in $Property)
                    {
                        $propertyValues += $value.$propertyName
                    }
                }
                $Format -f $propertyValues
            }
            else
            {
                $Format -f $values
            }
        }
    }
}
Import-PSUAlias -Name format -Command Format-PSUString