Skip to content

Commit c5c4e66

Browse files
committed
Don't emit '?' if empty even if it contains =$false or =$null
1 parent abef3ea commit c5c4e66

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed
148 Bytes
Binary file not shown.

src/UrlQueryStringParser/UrlQueryStringParser.psm1

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ function ConvertTo-UrlQueryString {
2424
)
2525
process {
2626
[string] $result = "" + $ContinuationOfString
27-
if ($Members.Keys -and $Members.Keys.Count) {
27+
$hasContent = $Members.Keys |
28+
Where-Object { Test-ValueIsWriteable $Members[$_]} |
29+
Foreach-Object { $true } |
30+
Select-Object -First 1
31+
32+
if ($hasContent) {
2833
$result += if (-not $ContinuationOfString) {"?"}
2934
}
3035
foreach($key in $Members.Keys) {
@@ -35,7 +40,7 @@ function ConvertTo-UrlQueryString {
3540
#
3641
# Note: -eq is NOT commutitive here, $false -eq '' but '' -ne $false. The only falsey object we want
3742
# is empty strings, and other forms of this code will include that.
38-
if ('' -eq $foundValue -or $foundValue) {
43+
if (Test-ValueIsWriteable $foundValue) {
3944
$valueArray = @($foundValue)
4045
if($value -is [array]) {
4146
$valueArray = $foundValue
@@ -109,4 +114,22 @@ function ConvertFrom-UrlQueryString {
109114
}
110115
}
111116

112-
Export-ModuleMember -Function * -Alias *
117+
Export-ModuleMember -Function * -Alias *
118+
119+
#region private functions
120+
121+
function Test-ValueIsWriteable {
122+
[CmdletBinding()]
123+
param(
124+
[Parameter(ValueFromPipeline)]
125+
126+
# The value to test. Can't use [string] here because that converts $null into ''
127+
[object] $Value
128+
)
129+
process {
130+
#return
131+
'' -eq $Value -or $Value
132+
}
133+
}
134+
135+
#endregion

test/Test-UrlQueryStringParser.ps1

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,33 @@
55
[CmdletBinding()]
66
param()
77

8-
if ((ConvertTo-UrlQueryString @{"foo"=$true} -ContinuationOfString '') -ne '?foo') {
8+
Import-Module "$PSScriptRoot\..\src\UrlQueryStringParser" -Force
9+
10+
if ((ConvertTo-UrlQueryString @{foo=$true} -ContinuationOfString '') -ne '?foo') {
911
throw "Continuation of empty failed."
1012
}
1113

12-
if ((ConvertTo-UrlQueryString @{"foo"=$true} -ContinuationOfString '?bar=baz') -ne '?bar=baz&foo') {
14+
if ((ConvertTo-UrlQueryString @{foo=$true} -ContinuationOfString '?bar=baz') -ne '?bar=baz&foo') {
1315
throw "Continuation of non-empty failed."
1416
}
1517

1618
if ((ConvertTo-UrlQueryString @{}) -ne "") {
1719
throw "Empty conversion to UrlQueryString failed."
1820
}
1921

22+
if ((ConvertTo-UrlQueryString @{foo=$null;bar=$null}) -ne "") {
23+
throw "Psuedo-empty conversion to UrlQueryString failed."
24+
}
25+
2026
if ((ConvertTo-UrlQueryString @{} -ContinuationOfString '?bar=baz') -ne "?bar=baz") {
2127
throw "Continuation with empty failed."
2228
}
2329

24-
if ((ConvertTo-UrlQueryString @{"foo"='bar baz quux'} -SkipEncodeSpaces) -ne "?foo=bar baz quux") {
30+
if ((ConvertTo-UrlQueryString @{foo='bar baz quux'} -SkipEncodeSpaces) -ne "?foo=bar baz quux") {
2531
throw "Skip encoding spaces failed."
2632
}
2733

28-
if ((ConvertTo-UrlQueryString @{"foo"='bar baz quux'}) -ne "?foo=bar%20baz%20quux") {
34+
if ((ConvertTo-UrlQueryString @{foo='bar baz quux'}) -ne "?foo=bar%20baz%20quux") {
2935
throw "Encoding spaces failed."
3036
}
3137

0 commit comments

Comments
 (0)