Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,20 @@ function switchPath(sourcePath, routes) {
let matchedPath = null
let matchedValue = null

traverseRoutes(routes, function matchPattern(pattern) {
traverseRoutes(routes, function matchPattern(pattern) { // eslint-disable-line complexity, max-len
Copy link

@Cmdv Cmdv May 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terrible code:

function validate({sourcePath, matchedPath, matchedValue, routes}) {
  let path = matchedPath ? validatePath(sourcePath, matchedPath) : null
  let value = matchedValue
  if (!path) {
    if (sourcePath === `/`) {
      path = routes[`*`] ? sourcePath : null
      value = path ? routes[`/$`] : null
    } else {
      path = routes[`*`] ? sourcePath : null
      value = path ? routes[`*`] : null
    }
  }
  return {path, value}
}

fixes this:

it('should match routes explicitly using `$`', () => {
    const {path, value} = switchPath('/', {
      '/$': 123,
      '/other/$': 456,
      '*': 'not found route'
    })

    expect(path).to.be.equal('/');
    expect(value).to.be.equal(123);
  })

if (pattern[pattern.length - 1] === `$`) {
const realPattern = pattern.split(`/$`).join(``)
if (sourcePath.search(realPattern) === 0 &&
betterMatch(pattern, matchedPath) ||
sourcePath.search(realPattern + `/`) &&
betterMatch(pattern, matchedPath))
{
matchedPath = realPattern
matchedValue = routes[pattern]
}
return
}

if (sourcePath.search(pattern) === 0 && betterMatch(pattern, matchedPath)) {
matchedPath = pattern
matchedValue = routes[pattern]
Expand Down
21 changes: 21 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,27 @@ describe('switchPath basic usage', () => {
expect(path).to.be.equal('/1736');
expect(value).to.be.equal('id is 1736');
});

Copy link

@Cmdv Cmdv May 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could also add bellow as that was where the user found the issue:
edit adding bellow test makes it fail returns not found route 😞

it('should match routes explicitly using `$`', () => {
    const {path, value} = switchPath('/', {
      '/$': 123,
      '/other/$': 456,
      '*': 'not found route'
    })

    expect(path).to.be.equal('/');
    expect(value).to.be.equal(123);
  })

it('should match routes explicitly using `$`', () => {
const {path, value} = switchPath('/other', {
'/': 123,
'/other/$': 456
})

expect(path).to.be.equal('/other');
expect(value).to.be.equal(456);
})

it('should allow opting out of partial matching for routes using `$`', () => {
const {path, value} = switchPath('/random', {
'/$': 123,
'/other': 456,
'*': 'not found route'
});

expect(path).to.be.equal('/random');
expect(value).to.be.equal('not found route');
});
});

describe('switchPath corner cases', () => {
Expand Down