Skip to content

Commit a04d113

Browse files
markekrausSeeminglyScience
authored andcommitted
Add Optional Custom Variable Name to ConvertTo-SplatExpression (#25)
* Add custom variable naame for ConvertTo-SplatExpression * Update ConvertTo-SplatExpression Documentation to include VariableName paramater * Move comment closer to relevant code * Add missing comma
1 parent d2b9cce commit a04d113

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

docs/en-US/ConvertTo-SplatExpression.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Convert a command expression to use splatting.
1313
## SYNTAX
1414

1515
```powershell
16-
ConvertTo-SplatExpression [[-Ast] <Ast>]
16+
ConvertTo-SplatExpression [[-Ast] <Ast>] [[-VariableName] <String>]
1717
```
1818

1919
## DESCRIPTION
@@ -60,6 +60,22 @@ Accept pipeline input: False
6060
Accept wildcard characters: False
6161
```
6262
63+
### -VariableName
64+
65+
Specifies the Varabile name to use for the splat hashtable variable.
66+
67+
```yaml
68+
Type: String
69+
Parameter Sets: (All)
70+
Aliases:
71+
72+
Required: False
73+
Position: 2
74+
Default value: none
75+
Accept pipeline input: False
76+
Accept wildcard characters: False
77+
```
78+
6379
## INPUTS
6480
6581
## OUTPUTS

module/Public/ConvertTo-SplatExpression.ps1

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ function ConvertTo-SplatExpression {
1010
[EditorCommand(DisplayName='Convert Command to Splat Expression')]
1111
param(
1212
[System.Management.Automation.Language.Ast]
13-
$Ast
13+
$Ast,
14+
15+
[String]
16+
$VariableName
1417
)
1518
begin {
1619
function ConvertFromExpressionAst($expression) {
@@ -63,12 +66,14 @@ function ConvertTo-SplatExpression {
6366
$splat.($parameter.Key) = $parameter.Value.Value
6467
}
6568

66-
# Remove the hypen, change to camelCase and add 'Splat'
67-
$variableName = [regex]::Replace(
68-
($commandName.Extent.Text -replace '-'),
69-
'^[A-Z]',
70-
{ $args[0].Value.ToLower() }) +
71-
'Splat'
69+
if (-not $VariableName) {
70+
# Remove the hypen, change to camelCase and add 'Splat'
71+
$variableName = [regex]::Replace(
72+
($commandName.Extent.Text -replace '-'),
73+
'^[A-Z]',
74+
{ $args[0].Value.ToLower() }) +
75+
'Splat'
76+
}
7277

7378
$sb = [System.Text.StringBuilder]::
7479
new('${0}' -f $variableName).

0 commit comments

Comments
 (0)