Skip to content

Commit

Permalink
Fixed a bug where query strings would not be represented correctly if…
Browse files Browse the repository at this point in the history
… the array contained only one item
  • Loading branch information
aaronsky committed May 6, 2020
1 parent 1684bd0 commit 452da6b
Showing 1 changed file with 19 additions and 14 deletions.
Expand Up @@ -18,11 +18,11 @@ extension Array where Element == URLQueryItem {
}
append(URLQueryItem(name: key, value: String(value)))
}

enum ArrayFormat {
case indices
case brackets

func format(for index: Int) -> String {
switch self {
case .indices:
Expand All @@ -32,21 +32,26 @@ extension Array where Element == URLQueryItem {
}
}
}

mutating func append(_ items: [String], forKey key: String, arrayFormat: ArrayFormat = .brackets) {
append(contentsOf: items
.enumerated()
.map {
URLQueryItem(name: "\(key)\(arrayFormat.format(for: $0.offset))",
value: $0.element)
})
if items.isEmpty {
return
} else if items.count == 1 {
appendIfNeeded(items.first, forKey: key)
} else {
append(contentsOf: items
.enumerated()
.map {
URLQueryItem(name: "\(key)\(arrayFormat.format(for: $0.offset))",
value: $0.element)
})
}
}

mutating func append(_ items: [String: String], forKey key: String) {
append(contentsOf: items
.map {
URLQueryItem(name: "\(key)[\($0.key)]",
value: $0.value)
append(contentsOf: items.map {
URLQueryItem(name: "\(key)[\($0.key)]",
value: $0.value)
})
}
}
Expand Down

0 comments on commit 452da6b

Please sign in to comment.