Skip to content
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

feat: allow params number #206

Closed
wants to merge 6 commits into from
Closed

feat: allow params number #206

wants to merge 6 commits into from

Conversation

pikax
Copy link
Member

@pikax pikax commented Apr 30, 2020

Allow have param with number as a value

__tests__/router.spec.ts Outdated Show resolved Hide resolved
__tests__/router.spec.ts Outdated Show resolved Hide resolved
src/router.ts Outdated
? { ...rawLocation, params: encodeParams(rawLocation.params) }
? {
...rawLocation,
params: encodeParams(applyToParamsRaw(rawLocation.params)),
Copy link
Member

Choose a reason for hiding this comment

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

A few thoughts for refactoring:

  • encodeParams could accept numbers
  • we could create a copy of rawLocation before L256, and add an else at L261 to change the params property to an encoded version without numbers, save the decoded version as a local variable params to use it later

Copy link
Member Author

Choose a reason for hiding this comment

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

encodeParams is accepting string | number but the bind doesn't pick up correct overload, I will just create a const with the normalized params and use that across

Comment on lines 40 to 44
export function applyToParamsRaw(
params: RouteParamsRaw | undefined
): RouteParams | undefined {
return applyToParams(x => x.toString(), params)
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we could write this one as

Suggested change
export function applyToParamsRaw(
params: RouteParamsRaw | undefined
): RouteParams | undefined {
return applyToParams(x => x.toString(), params)
}
export let normalizeParams = applyParams.bind(null, paramValue => '' + paramValue)

Copy link
Member Author

Choose a reason for hiding this comment

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

noticed let instead of const, is that on purpose? If it is, why? :)

src/router.ts Outdated
@@ -275,7 +278,7 @@ export function createRouter(options: RouterOptions): Router {
// TODO: normalize params if we accept numbers as raw values
matchedRoute.params =
'params' in rawLocation
? rawLocation.params!
? applyToParamsRaw(rawLocation.params)!
Copy link
Member

Choose a reason for hiding this comment

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

we can remove the todo above

pikax and others added 2 commits May 1, 2020 20:47
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
@codecov-io
Copy link

Codecov Report

Merging #206 into master will increase coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #206      +/-   ##
==========================================
+ Coverage   88.61%   88.64%   +0.03%     
==========================================
  Files          25       25              
  Lines        1019     1022       +3     
  Branches      279      279              
==========================================
+ Hits          903      906       +3     
  Misses         66       66              
  Partials       50       50              
Impacted Files Coverage Δ
src/types/index.ts 100.00% <ø> (ø)
src/encoding.ts 100.00% <100.00%> (ø)
src/matcher/index.ts 97.54% <100.00%> (+0.02%) ⬆️
src/router.ts 91.75% <100.00%> (ø)
src/utils/index.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update eba7ebe...cd7d354. Read the comment docs.

@posva posva closed this in ef0920a May 3, 2020
@posva
Copy link
Member

posva commented May 3, 2020

I ended up doing something simpler by not allowing number params for the matcher at ef0920a
I still kept you as the author of the commit, I hope that's fine

@pikax pikax deleted the feat/allow_number_as_params branch May 4, 2020 07:43
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.

3 participants