-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds redirect with parameters + tests + docs
- Loading branch information
1 parent
d196e91
commit ca77611
Showing
4 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/react-router/modules/__tests__/generatePath-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import expect from 'expect' | ||
import generatePath from '../generatePath' | ||
|
||
describe('generatePath', () => { | ||
describe('with pattern="/"', () => { | ||
it('returns correct url with no params', () => { | ||
const pattern = '/' | ||
const generated = generatePath(pattern) | ||
expect(generated).toBe('/') | ||
}) | ||
|
||
it('returns correct url with params', () => { | ||
const pattern = '/' | ||
const params = { foo: "tobi", bar: 123 } | ||
const generated = generatePath(pattern, params) | ||
expect(generated).toBe('/') | ||
}) | ||
}) | ||
|
||
describe('with pattern="/:foo/somewhere/:bar"', () => { | ||
it('throws with no params', () => { | ||
const pattern = '/:foo/somewhere/:bar' | ||
expect(() => { | ||
generatePath(pattern) | ||
}).toThrow() | ||
}) | ||
|
||
it('throws with some params', () => { | ||
const pattern = '/:foo/somewhere/:bar' | ||
const params = { foo: "tobi", quux: 999 } | ||
expect(() => { | ||
generatePath(pattern, params) | ||
}).toThrow() | ||
}) | ||
|
||
it('returns correct url with params', () => { | ||
const pattern = '/:foo/somewhere/:bar' | ||
const params = { foo: "tobi", bar: 123 } | ||
const generated = generatePath(pattern, params) | ||
expect(generated).toBe('/tobi/somewhere/123') | ||
}) | ||
|
||
it('returns correct url with additional params', () => { | ||
const pattern = '/:foo/somewhere/:bar' | ||
const params = { foo: "tobi", bar: 123, quux: 999 } | ||
const generated = generatePath(pattern, params) | ||
expect(generated).toBe('/tobi/somewhere/123') | ||
}) | ||
}) | ||
|
||
describe('with no path', () => { | ||
it('matches the root URL', () => { | ||
const generated = generatePath() | ||
expect(generated).toBe('/') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pathToRegexp from 'path-to-regexp' | ||
|
||
const patternCache = {} | ||
const cacheLimit = 10000 | ||
let cacheCount = 0 | ||
|
||
const compileGenerator = (pattern) => { | ||
const cacheKey = pattern | ||
const cache = patternCache[cacheKey] || (patternCache[cacheKey] = {}) | ||
|
||
if (cache[pattern]) | ||
return cache[pattern] | ||
|
||
const compiledGenerator = pathToRegexp.compile(pattern) | ||
|
||
if (cacheCount < cacheLimit) { | ||
cache[pattern] = compiledGenerator | ||
cacheCount++ | ||
} | ||
|
||
return compiledGenerator | ||
} | ||
|
||
/** | ||
* Public API for generating a URL pathname from a pattern and parameters. | ||
*/ | ||
const generatePath = (pattern = '/', params = {}) => { | ||
if (pattern === '/') { | ||
return pattern | ||
} | ||
const generator = compileGenerator(pattern) | ||
return generator(params) | ||
} | ||
|
||
export default generatePath |