Skip to content

Commit 661b06c

Browse files
Merge pull request #19 from PowershellFrameworkCollective/scopes
Create psfhashtables.md
2 parents 57e0d1b + f4cc310 commit 661b06c

2 files changed

Lines changed: 130 additions & 7 deletions

File tree

documentation/documents/psframework/utility.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ title: PSFramework: Utility
77

88
## Synopsis
99

10-
Utility is the catch-all category for individual commands that do not fit into any more specific category or would be a category ontu themselves.
10+
Utility is the catch-all category for individual commands and features that do not fit into any more specific category or would be a category ontu themselves.
1111

1212
## Commands
1313

1414
### Select-PSFObject
1515

1616
`Select-PSFObject` completely replaces `Select-Object`, offering all of its functionality, but offering greater input comfort and more readable code by avoiding hashtables for simple operations. It also adds the ability to extend an object, rather than building a new object, fully replacing `Add-Member`.
1717

18-
- [Select-PSFObject](utility/select-psfobject.html)
18+
+ [Select-PSFObject](utility/select-psfobject.html)
1919

2020
### ConvertFrom-PSFArray
2121

2222
`ConvertFrom-PSFArray` converts properties on objects that are collections into a single string.
2323
This helps with exporting as flat data, e.g. when exporting as csv.
2424

25-
- [ConvertFrom-PSFArray](utility/convertfrom-psfarray.html)
25+
+ [ConvertFrom-PSFArray](utility/convertfrom-psfarray.html)
2626

2727
### Argument Transformation Attributes
2828

@@ -33,10 +33,14 @@ This is where Argument Transformation Attributes can shine:
3333

3434
+ [ScriptTransformation](utility/ScriptTransformation.html): Implement your own conversion logic in script.
3535

36+
### PSFramework Hashtables
37+
38+
Hashtables are awesome!
39+
But they could be even better.
40+
Introducing the PSFramework Hashtables, extending hashtables to include default values, passing through unknown key or even dynamically calculating the result of an unexpected key!
41+
42+
+ [PSFramework Hashtables](utility/psfhashtables.html)
43+
3644
## Notes
3745

3846
[Back to PSFramework](https://psframework.org/documentation/documents/psframework.html)
39-
40-
| Version | 1.1 |
41-
| Written on: | 2018-10-23 |
42-
| Updated on: | 2019-11-07 |
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# PSFHashtables
2+
3+
[Back to utility](http://psframework.org/documentation/documents/psframework/utility.html)
4+
5+
## Synopsis
6+
7+
PSFramework Hashtables - `[PsfHashtable]` - are an extension / improvement over the default `[Hashtable]`.
8+
They can do everything a regular hashtable can do, but they can also offer alternative options on what to do, when asking for a key _not_ already included in the hashtable.
9+
10+
## Description
11+
12+
The `[PsfHashtable]` data-type was added as a means to make hashtables more powerful and flexible, in many cases replacing the need for switch-statements.
13+
This can help simplify the code layout significantly.
14+
15+
> Example: The simple hashtable
16+
17+
```powershell
18+
$organizations = @{
19+
LondonHR = 'London'
20+
LondonSales = 'London'
21+
LondonDev = 'London'
22+
NewYorkHR = 'New York'
23+
NewYorkSales = 'New York'
24+
NewYorkDev = 'New York'
25+
}
26+
27+
$users | Select-Object Name, Mail, @{
28+
Name = 'Users'
29+
Expression = {
30+
if ($organizations[$_.Organization]) {
31+
$organizations[$_.Organization]
32+
}
33+
else { $_.Organization}
34+
}
35+
}
36+
```
37+
38+
> Example: PsfHashtable
39+
40+
```powershell
41+
$organizations = [PsfHashtable]@{
42+
LondonHR = 'London'
43+
LondonSales = 'London'
44+
LondonDev = 'London'
45+
NewYorkHR = 'New York'
46+
NewYorkSales = 'New York'
47+
NewYorkDev = 'New York'
48+
}
49+
# If key not known: Return key
50+
$organizations.EnablePassthru()
51+
52+
$users | Select-Object Name, Mail, @{
53+
Name = 'Users'
54+
Expression = { $organizations[$_.Organization] }
55+
}
56+
```
57+
58+
## Special Features
59+
60+
So, after luring you in with a simple example, what all can the `[PsfHashtable]` do for you?
61+
62+
### PassThru
63+
64+
As shown in the previous example, a `[PsfHashtable]` can be configured to pass through the key, if it is not yet known:
65+
66+
```powershell
67+
$mapping = [PsfHashtable]@{
68+
Answer = 42
69+
Conspiracy = 23
70+
}
71+
$mapping.EnablePassthru()
72+
73+
$mapping["Answer"] # 42
74+
$mapping.Answer # 42
75+
$mapping["Foo"] # Foo
76+
$mapping.Foo # <nothing>
77+
```
78+
79+
> Note: One limitation with extending hashtables: The features on unknown keys _only_ work, when using the _index_ notation (e.g. `$mapping["Foo"]`), not the _property_ notation (e.g. `$mapping.Foo`).
80+
81+
### Default Value
82+
83+
Rather than passing through the key, you can also provide a default value to return:
84+
85+
```powershell
86+
$mapping = [PsfHashtable]@{
87+
Answer = 42
88+
Conspiracy = 23
89+
}
90+
$mapping.SetDefaultValue(1)
91+
92+
$mapping["Answer"] # 42
93+
$mapping.Answer # 42
94+
$mapping["Foo"] # 1
95+
$mapping.Foo # <nothing>
96+
```
97+
98+
> Note: One limitation with extending hashtables: The features on unknown keys _only_ work, when using the _index_ notation (e.g. `$mapping["Foo"]`), not the _property_ notation (e.g. `$mapping.Foo`).
99+
100+
### Dynamic Value
101+
102+
A third alternative is to provide a custom scriptblock that gets executed on unknown keys:
103+
104+
```powershell
105+
$mapping = [PsfHashtable]@{
106+
Answer = 42
107+
Conspiracy = 23
108+
}
109+
$mapping.SetCalculator({ $_ * 2 })
110+
111+
$mapping["Answer"] # 42
112+
$mapping.Answer # 42
113+
$mapping["Foo"] # FooFoo
114+
$mapping.Foo # <nothing>
115+
```
116+
117+
> Note: One limitation with extending hashtables: The features on unknown keys _only_ work, when using the _index_ notation (e.g. `$mapping["Foo"]`), not the _property_ notation (e.g. `$mapping.Foo`).
118+
119+
[Back to utility](http://psframework.org/documentation/documents/psframework/utility.html)

0 commit comments

Comments
 (0)