Skip to content

Commit

Permalink
Parse redundant query string keys (#165)
Browse files Browse the repository at this point in the history
* Added test query string duplicates

* Change parseQS

Change parseQS so that if elements of the parsed query string are the
same, they are combined into a vector.
  • Loading branch information
mhairi authored and trestletech committed Oct 13, 2017
1 parent 2b1a2d0 commit 8fce7d3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions R/query-string.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ parseQS <- function(qs){
ret <- as.list(vals)
names(ret) <- keys

# If duplicates, combine
combine_elements <- function(name){
unname(unlist(ret[names(ret)==name]))
}

unique_names <- unique(names(ret))

ret <- lapply(unique_names, combine_elements)
names(ret) <- unique_names

ret
}

Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-querystring.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ test_that("incomplete query strings are ignored", {
expect_equivalent(parseQS("a="), list()) # It's technically a named list()
expect_equal(parseQS("a=1&b=&c&d=1"), list(a="1", d="1"))
})

test_that("query strings with duplicates are made into vectors", {
expect_equal(parseQS("a=1&a=2&a=3&a=4"), list(a=c("1", "2", "3", "4")))
})

0 comments on commit 8fce7d3

Please sign in to comment.