Skip to content

Commit

Permalink
Track base search when appending queries
Browse files Browse the repository at this point in the history
  • Loading branch information
taion committed Nov 19, 2015
1 parent 8da4252 commit 32484d0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
12 changes: 6 additions & 6 deletions modules/useBasename.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ function useBasename(createHistory) {
return location
}

function prependBasename(path) {
function prependBasename(location) {
if (!basename)
return path
return location

if (typeof path === 'string')
path = parsePath(path)
if (typeof location === 'string')
location = parsePath(location)

const pname = path.pathname
const pname = location.pathname
const normalizedBasename = basename.slice(-1) === '/' ? basename : basename + '/'
const normalizedPathname = pname.charAt(0) === '/' ? pname.slice(1) : pname
const pathname = normalizedBasename + normalizedPathname

return {
...path,
...location,
pathname
}
}
Expand Down
36 changes: 26 additions & 10 deletions modules/useQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import qs from 'qs'
import runTransitionHook from './runTransitionHook'
import parsePath from './parsePath'

const SEARCH_BASE_KEY = '$searchBase'

function defaultStringifyQuery(query) {
return qs.stringify(query, { arrayFormat: 'brackets' }).replace(/%20/g, '+')
}
Expand All @@ -26,26 +28,40 @@ function useQueries(createHistory) {
parseQueryString = defaultParseQueryString

function addQuery(location) {
if (location.query == null)
location.query = parseQueryString(location.search.substring(1))
if (location.query == null) {
const { search } = location
location.query = parseQueryString(search.substring(1))
location[SEARCH_BASE_KEY] = { search, searchBase: '' }
}

// TODO: Instead of all the book-keeping here, this should just strip the
// stringified query from the search.

return location
}

function appendQuery(path, query) {
function appendQuery(location, query) {
let queryString
if (!query || (queryString = stringifyQuery(query)) === '')
return path
return location

if (typeof path === 'string')
path = parsePath(path)
if (typeof location === 'string')
location = parsePath(location)

const searchBaseSpec = location[SEARCH_BASE_KEY]
let searchBase
if (searchBaseSpec && location.search === searchBaseSpec.search) {
searchBase = searchBaseSpec.searchBase
} else {
searchBase = location.search || ''
}

const searchBase = path.search ? path.search + '&' : '?'
const search = searchBase + queryString
const search = searchBase + (searchBase ? '&' : '?') + queryString

return {
...path,
search
...location,
search,
[SEARCH_BASE_KEY]: { search, searchBase }
}
}

Expand Down

0 comments on commit 32484d0

Please sign in to comment.