Skip to content

Commit

Permalink
Better splitting of state query string
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Jun 14, 2016
1 parent 5f2da95 commit 098cbc1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
10 changes: 8 additions & 2 deletions R/save-state.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,16 @@ encodeStateQueryString <- function(input, exclude = NULL, values = NULL) {

# Counterpart to encodeStateQueryString
decodeStateQueryString <- function(queryString) {
if (grepl("_values_", queryString)) {
splitStr <- strsplit(queryString, "_values_", fixed = TRUE)[[1]]
# Remove leading '?'
if (substr(queryString, 1, 1) == '?')
queryString <- substr(queryString, 2, nchar(queryString))

if (grepl("(^|&)_values_(&|$)", queryString)) {
splitStr <- strsplit(queryString, "(^|&)_values_(&|$)")[[1]]
inputValueStr <- splitStr[1]
valueStr <- splitStr[2]
if (is.na(valueStr))
valueStr <- ""

} else {
inputValueStr <- queryString
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test-save-state.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
context("save-state")

test_that("decoding state query string", {
expect_identical(
decodeStateQueryString("?a=1&b=2"),
list(input = list(a=1L, b=2L), values = list())
)
expect_identical(
decodeStateQueryString("?a=1&b=2&_values_&c=3"),
list(input = list(a=1L, b=2L), values = list(c=3L))
)
expect_identical(
decodeStateQueryString("?_values_&c=3"),
list(input = list(), values = list(c=3L))
)
expect_identical(
decodeStateQueryString("?a=1&b=2&_values_"),
list(input = list(a=1L, b=2L), values = list())
)
expect_identical(
decodeStateQueryString("?_values_"),
list(input = list(), values = list())
)
})

0 comments on commit 098cbc1

Please sign in to comment.