Skip to content

Commit

Permalink
Give locations a key by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Oct 5, 2015
1 parent b0f6d12 commit 8887d5a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## HEAD

- Give `location` objects a `key` by default
- Deprecate `history.setState`

## [v1.12.0]
Expand Down
14 changes: 4 additions & 10 deletions modules/__tests__/Location-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,17 @@ describe('a location', function () {
})

it('knows its pathname', function () {
let location = createLocation('/home?the=query')
let location = createLocation('/home?the=query#the-hash')
expect(location.pathname).toEqual('/home')
})

it('knows its hash', function () {
let location = createLocation('/home#the-hash')
expect(location.hash).toEqual('#the-hash')
})

it('knows its hash and search together', function () {
let location = createLocation('/home?the=query#the-hash')
expect(location.search).toEqual('?the=query')
expect(location.hash).toEqual('#the-hash')
})

it('knows its search string', function () {
let location = createLocation('/home?the=query')
let location = createLocation('/home?the=query#the-hash')
expect(location.search).toEqual('?the=query')
})

Expand All @@ -40,8 +34,8 @@ describe('a location', function () {
expect(location.action).toBe(POP)
})

it('has null key by default', function () {
it('has a key by default', function () {
let location = createLocation()
expect(location.key).toBe(null)
expect(location.key).toExist()
})
})
2 changes: 2 additions & 0 deletions modules/createHashHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ function createHashHistory(options={}) {
key = history.createKey()
replaceHashPath(addQueryStringValueToPath(path, queryKey, key))
}
} else {
key = state = null
}

return history.createLocation(path, state, undefined, key)
Expand Down
60 changes: 30 additions & 30 deletions modules/createHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,6 @@ function extractPath(string) {
return string.substring(match[0].length)
}

function createLocation(path='/', state=null, action=POP, key=null) {
let pathname = extractPath(path)
let search = ''
let hash = ''

let hashIndex = pathname.indexOf('#')
if (hashIndex !== -1) {
hash = pathname.substring(hashIndex)
pathname = pathname.substring(0, hashIndex)
}

let searchIndex = pathname.indexOf('?')
if (searchIndex !== -1) {
search = pathname.substring(searchIndex)
pathname = pathname.substring(0, searchIndex)
}

if (pathname === '')
pathname = '/'

return {
pathname,
search,
hash,
state,
action,
key
}
}

function locationsAreEqual(a, b) {
return a.pathname === b.pathname &&
a.search === b.search &&
Expand Down Expand Up @@ -203,6 +173,36 @@ function createHistory(options={}) {
return path
}

function createLocation(path='/', state=null, action=POP, key=createKey()) {
let pathname = extractPath(path)
let search = ''
let hash = ''

let hashIndex = pathname.indexOf('#')
if (hashIndex !== -1) {
hash = pathname.substring(hashIndex)
pathname = pathname.substring(0, hashIndex)
}

let searchIndex = pathname.indexOf('?')
if (searchIndex !== -1) {
search = pathname.substring(searchIndex)
pathname = pathname.substring(0, searchIndex)
}

if (pathname === '')
pathname = '/'

return {
pathname,
search,
hash,
state,
action,
key
}
}

// deprecated
function setState(state) {
if (location) {
Expand Down

0 comments on commit 8887d5a

Please sign in to comment.