nl.nlsw.Collections.psm1

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# __ _ ____ _ _ _ _ ____ ____ ____ ____ ____ ___ _ _ ____ ____ ____
# | \| |=== |/\| |___ | |--- |=== ==== [__] |--- | |/\| |--| |--< |===
#
# @file nl.nlsw.Collections.psm1
# @copyright Ernst van der Pols, Licensed under the EUPL-1.2-or-later
# @date 2022-10-18
#requires -version 3

class nlswCollections {

    # Recursively, converts a dictionary (e.g. Hashtable or OrderedDictionary) to an OrderedDictionary.
    # @param $object the object to convert
    # @param $recurse convert member dictionaries as well
    # @param $sort sort the keys in the resulting dictionary
    # @return the OrderedDictionary with key/values if present, $null if object is $null
    static [System.Collections.Specialized.OrderedDictionary] ConvertDictionaryToOrderedDictionary([System.Collections.IDictionary]$object, [bool]$recurse, [bool]$sort) {
        if ($object) {
            $result = [ordered]@{}
            $keys = $object.Keys
            if ($sort) {
                $keys = $keys | Sort-Object
            }
            foreach ($key in $keys) {
                $value = $object.$key;
                if ($recurse -and ($value -is [System.Collections.IDictionary])) {
                    $value = [nlswCollections]::ConvertDictionaryToOrderedDictionary($value, $recurse, $sort)
                }
                $result.Add($key, $value);
            }
            return $result;
        }
        return $null;
    }

    # Recursively, converts a PSObject as e.g. returned by ConvertFrom-Json to an OrderedDictionary.
    # @param $object the object to convert
    # @return the OrderedDictionary with key/values if present, $null if object is $null
    static [System.Collections.Specialized.OrderedDictionary] ConvertPSObjectToOrderedDictionary([PSObject]$object, [bool]$recurse, [bool]$sort) {
        if ($object) {
            $result = [ordered]@{}
            # convert PSObject to ordered hashtable
            $keys = $object.PSObject.Properties.Name
            if ($sort) {
                $keys = $keys | Sort-Object
            }
            foreach ($key in $keys) {
                $value = $object.$key;
                if ($recurse -and ($value -is [PSObject])) {
                    $value = [nlswCollections]::ConvertPSObjectToOrderedDictionary($value, $recurse, $sort)
                }
                $result.Add($key, $value);
            }
            return $result;
        }
        return $null;
    }
}

<#
.SYNOPSIS
 Convert an array of hashtables into a single hashtable.
 
.DESCRIPTION
 Convert an array of hashtables into a single hashtable. The value
 of an entry will be made an array, if multiple values are present.
 
.INPUT
 System.Collections.Hashtable[]
 
.OUTPUT
 System.Collections.Hashtable
 
.LINK
 https://devblogs.microsoft.com/scripting/dealing-with-powershell-hash-table-quirks/
 
.LINK
 ConvertFrom-StringData
#>

function ConvertFrom-HashtableArray {
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [hashtable[]]$hashtables
    )
    begin {
        $result = @{}
    }
    process {
        # convert array of hashtables into a single hashtable
        foreach ($hashtable in $hashtables) {
            foreach ($entry in $hashtable.GetEnumerator()) {
                if ($result.ContainsKey($entry.Key)) {
                    $value = $result[$entry.Key]
                    if ($value -is [array]) {
                        $result[$entry.Key] += $entry.Value
                    }
                    else {
                        $result[$entry.Key] = @($value, $entry.Value)
                    }
                }
                else {
                    $result.Add($entry.Key, $entry.Value)
                }
            }
        }
    }
    end {
        return $result
    }
}

<#
.SYNOPSIS
 Convert an Array, Hashtable (or other IDictionary), or PSObject to an OrderedDictionary.
 
.DESCRIPTION
 Convert an object to an System.Collections.Specialized.OrderedDictionary,
 a.k.a. an ordered hashtable, given its PowerShell type accelerator notation
 '[ordered]@{}'.
 
 The following input object types are supported:
 - System.Array
   The index numbers are used as keys
 
 - System.Collections.IDictionary
   Key-value pairs are copied in natural order.
   With the $Sort switch specified, the keys are sorted.
   With the $Recurse switch specified, IDictionary members are recursively converted as well.
 
 - System.Collections.Specialized.OrderedDictionary
   Is returned as-is.
 
 - System.Management.Automation.PSObject
   Property members are copied in natural order.
   With the $Sort switch specified, the keys are sorted.
   With the $Recurse switch specified, members of type PSObject
   are recursively converted as well.
 
.PARAMETER InputObject
 The object to convert
 
.PARAMETER Recurse
 Recursively convert member values of the input object as well.
 
.PARAMETER Sort
 Sort the key values of the resulting dictionary
 
.INPUT
 System.Array
 System.Collections.IDictionary
 System.Management.Automation.PSObject
 
.OUTPUT
 System.Collections.Specialized.OrderedDictionary
 
.LINK
 https://devblogs.microsoft.com/scripting/convertto-ordereddictionary/
 
.LINK
 https://www.powershellgallery.com/packages/PoshFunctions/2.2.1.2/Content/Functions%5CConvertTo-OrderedDictionary.ps1
#>

function ConvertTo-OrderedDictionary {
    [CmdletBinding()]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', 'Recurse', Justification="false positive")]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', 'Sort', Justification="false positive")]
    param (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [object]$InputObject,

        [Parameter(Mandatory=$false)]
        [switch]$Recurse,

        [Parameter(Mandatory=$false)]
        [switch]$Sort
    )
    begin {
    }
    process {
        $InputObject | where-object { $_ } | foreach-object {
            $object = $_
            if ($object -is [PSObject]) {
                # convert PSObject to ordered dictionary
                [nlswCollections]::ConvertPSObjectToOrderedDictionary($object, $Recurse, $Sort)
            }
            elseif ($object -is [System.Array]) {
                $result = [ordered]@{}
                for ($i = 0; $i -lt $object.Count; $i++) {
                    $result.Add($i, $object[$i])
                }
                $result
            }
            elseif ($object -is [System.Collections.IDictionary]) {
                # convert IDictionary to ordered dictionary
                [nlswCollections]::ConvertDictionaryToOrderedDictionary($object, $Recurse, $Sort)
            }
            else {
                throw [NotSupportedException]::new(("cannot convert '{0}' to an ordered hashtable" -f $_))
            }
        }
    }
    end {
    }
}

Export-ModuleMember -Function *