-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Redirect with parameters #5209
Merged
mjackson
merged 1 commit into
remix-run:master
from
dlindenkreuz:feature/redirect-with-params
Sep 29, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Obviously, this pattern does not contain any parameters. For an additional performance shortcut, we could assume that all strings that do not contain
*
,(
,)
or:
are eligible to pass through before creating and caching a compile function.