Skip to content

Remove extras from old params in named routes #910

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

Merged
merged 2 commits into from
Nov 18, 2016
Merged

Conversation

znck
Copy link
Member

@znck znck commented Nov 17, 2016

Fixes #906

@znck znck changed the title 🐛 Remove extras from old params in named routes Remove extras from old params in named routes Nov 17, 2016
@fnlctrl
Copy link
Member

fnlctrl commented Nov 17, 2016

Thanks for the PR! I'll review it asap.

Copy link
Member

@fnlctrl fnlctrl left a comment

Choose a reason for hiding this comment

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

@znck Also, can we add accompanying e2e tests for it (that addresses #906)?
The test provided in the original PR (https://github.com/vuejs/vue-router/pull/851/files) didn't actually cover the case of using extra params, we should probably fix it too so that this PR can cover both cases (bugfix and using extra params)

const keys = []
const paramNames: Array<string> = []

Regexp(path, keys)
Copy link
Member

@fnlctrl fnlctrl Nov 17, 2016

Choose a reason for hiding this comment

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

We don't have to call Regexp(path) here again (without cache, actually), there's already code for getting cached keys and regexp here (https://github.com/znck/vue-router/blob/ab0a779ba6f09f5b28138c9445ce84d7263daef6/src/create-matcher.js#L163-L172).

We should probably create a separate function that uses the code above, take a path as only parameter, returns a {keys, regexp} object, maybe name it getKeysAndParams.

Then we can get keys with ES6 destructuring

const {keys} = getKeysAndParams(path)

for (let i = 0, len = keys.length; i < len; ++i) {
const key = keys[i]
if (key && 'name' in key) paramNames.push(key.name)
}
Copy link
Member

Choose a reason for hiding this comment

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

This whole for-loop can be simplified to keys.map(k => k.name) for brevity. Since we are already using cache here, the performance penalty of using a Array.prototype.map is negligible.

if (key && 'name' in key) paramNames.push(key.name)
}

return (regexpParamsCache[path] = paramNames)
Copy link
Member

@fnlctrl fnlctrl Nov 17, 2016

Choose a reason for hiding this comment

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

This can then be simplified to

return regexpParamsCache[path] || 
   (regexpParamsCache[path] = keys.map(k => k.name))

like https://github.com/znck/vue-router/blob/ab0a779ba6f09f5b28138c9445ce84d7263daef6/src/create-matcher.js#L196-L198

Copy link
Member

@fnlctrl fnlctrl left a comment

Choose a reason for hiding this comment

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

/ping @yyx990803

@jeerbl
Copy link

jeerbl commented Nov 18, 2016

+1

@yyx990803 yyx990803 merged commit 6a18729 into vuejs:dev Nov 18, 2016
@yyx990803
Copy link
Member

👍 great work @znck and @fnlctrl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants