Skip to content

Commit

Permalink
[fixed] Compare query by string value
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Oct 8, 2015
1 parent c43fb61 commit bdab3d8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
15 changes: 14 additions & 1 deletion modules/__tests__/isActive-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('isActive', function () {
describe('with no query', function () {
it('is active', function (done) {
React.render((
<Router history={createHistory('/home')}>
<Router history={createHistory('/home?the=query')}>
<Route path="/home" />
</Router>
), node, function () {
Expand All @@ -45,6 +45,19 @@ describe('isActive', function () {
})
})

describe('with a query that also matches by value, but not by type', function () {
it('is active', function (done) {
React.render((
<Router history={createHistory('/home?the=query&n=2&show=false')}>
<Route path="/home" />
</Router>
), node, function () {
expect(this.history.isActive('/home', { the: 'query', n: 2, show: false })).toBe(true)
done()
})
})
})

describe('with a query that does not match', function () {
it('is not active', function (done) {
React.render((
Expand Down
29 changes: 24 additions & 5 deletions modules/isActive.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import { matchPattern } from './PatternUtils'
import deepEqual from 'deep-equal'

function deepEqual(a, b) {
if (a == b)
return true

if (a == null || b == null)
return false

if (Array.isArray(a)) {
return Array.isArray(b) && a.length === b.length && a.every(function (item, index) {
return deepEqual(item, b[index])
})
}

if (typeof a === 'object') {
for (let p in a)
if (a.hasOwnProperty(p) && (!b.hasOwnProperty(p) || !deepEqual(a[p], b[p])))
return false

return true
}

return String(a) === String(b)
}

function paramsAreActive(paramNames, paramValues, activeParams) {
return paramNames.every(function (paramName, index) {
Expand All @@ -11,10 +34,6 @@ function getMatchingRoute(pathname, activeRoutes, activeParams) {
let route, pattern, basename = ''
for (let i = 0, len = activeRoutes.length; i < len; ++i) {
route = activeRoutes[i]

//if (!route.path)
// return false

pattern = route.path || ''

if (pattern.charAt(0) !== '/')
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
],
"license": "MIT",
"dependencies": {
"deep-equal": "^1.0.0",
"history": "1.12.1",
"invariant": "^2.0.0",
"warning": "^2.0.0"
Expand Down

0 comments on commit bdab3d8

Please sign in to comment.