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

fix(shared): Parse string value including a leading number as string value #4946

Closed
wants to merge 1 commit into from
Closed

fix(shared): Parse string value including a leading number as string value #4946

wants to merge 1 commit into from

Conversation

TatsuyaYamamoto
Copy link

Motivation

defineCustomElement uses toNumber to parse the attribute value and check if the value is number or not.

Currently, toNumber uses parseFloat and returns number value if parseFloat does not return a NaN.
However, parseFloat does not parse strictly, values including chars other than numbers will also be parsed as Number.

According to MDN, it is better to use Number() for strict parsing, so I created this PR.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

Changes

  • parse a value using Number(value)

playground

const toNumber_current = (val: any): any => {
  const n = parseFloat(val)
  return isNaN(n) ? val : n
}

const toNumber_pr = (value: any): any => {
  const parsedValue = Number(value)
  return isNaN(parsedValue) ? value : parsedValue
}

console.log(toNumber_current(100)); // 100
console.log(toNumber_current(100.001)); // 100.001 
console.log(toNumber_current("100")); // 100
console.log(toNumber_current("100.001")); // 100.001 
console.log(toNumber_current("100-string")); // 100 <= (as-is) input value is string value, but output is number value.

console.log(toNumber_pr(100)); // 100
console.log(toNumber_pr(100.001)); // 100.001
console.log(toNumber_pr("100")); // 100
console.log(toNumber_pr("100.001")); // 100.001
console.log(toNumber_pr("100-string")); // "100-string" <= (to-be) output also is string value.

@TatsuyaYamamoto TatsuyaYamamoto changed the title fix: Parse string value including a leading number as string value by toNumber fix(shared): Parse string value including a leading number as string value by toNumber Nov 15, 2021
@TatsuyaYamamoto TatsuyaYamamoto changed the title fix(shared): Parse string value including a leading number as string value by toNumber fix(shared): Parse string value including a leading number as string value Nov 15, 2021
@posva
Copy link
Member

posva commented Nov 15, 2021

Related to #2598

@TatsuyaYamamoto
Copy link
Author

@posva Thank you for your comment.
I understood that toNumber() has a migration concern.

What do you think of the idea of defining toNumber() in apiCustomElement.ts to improve the behavior of parsing without worrying about compatibility?

@yyx990803 yyx990803 closed this in 7d0c63f Nov 14, 2022
chrislone pushed a commit to chrislone/core that referenced this pull request Feb 4, 2023
close vuejs#4946
close vuejs#2598
close vuejs#2604

This commit also refactors internal usage of previous loose
implementation of `toNumber` to the stricter version where applicable.
Use of `looseToNumber` is preserved for `v-model.number` modifier to
ensure backwards compatibility and consistency with Vue 2 behavior.
zhangzhonghe pushed a commit to zhangzhonghe/core that referenced this pull request Apr 12, 2023
close vuejs#4946
close vuejs#2598
close vuejs#2604

This commit also refactors internal usage of previous loose
implementation of `toNumber` to the stricter version where applicable.
Use of `looseToNumber` is preserved for `v-model.number` modifier to
ensure backwards compatibility and consistency with Vue 2 behavior.
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.

2 participants