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

WIP fix(toNumber): fix issue #2598 and add test #2604

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/runtime-dom/__tests__/directives/vOn.spec.ts
Expand Up @@ -41,9 +41,9 @@ describe('runtime-dom: v-on directive', () => {
})

test('it should support key modifiers and system modifiers', () => {
const keyNames = ["ctrl","shift","meta","alt"]
const keyNames = ['ctrl', 'shift', 'meta', 'alt']

keyNames.forEach(keyName=>{
keyNames.forEach(keyName => {
const el = document.createElement('div')
const fn = jest.fn()
// <div @keyup[keyName].esc="test"/>
Expand All @@ -52,28 +52,28 @@ describe('runtime-dom: v-on directive', () => {
'arrow-left'
])
patchEvent(el, 'onKeyup', null, nextValue, null)

triggerEvent(el, 'keyup', e => (e.key = 'a'))
expect(fn).not.toBeCalled()

triggerEvent(el, 'keyup', e => {
e[`${keyName}Key`] = false
e.key = 'esc'
})
expect(fn).not.toBeCalled()

triggerEvent(el, 'keyup', e => {
e[`${keyName}Key`] = true
e.key = 'Escape'
})
expect(fn).toBeCalledTimes(1)

triggerEvent(el, 'keyup', e => {
e[`${keyName}Key`] = true
e.key = 'ArrowLeft'
})
expect(fn).toBeCalledTimes(2)
});
})
})

test('it should support "exact" modifier', () => {
Expand Down
14 changes: 7 additions & 7 deletions packages/shared/__tests__/looseEqual.spec.ts
Expand Up @@ -54,27 +54,27 @@ describe('utils/looseEqual', () => {
const date2 = new Date(2019, 1, 2, 3, 4, 5, 7)
const file1 = new File([''], 'filename.txt', {
type: 'text/plain',
lastModified: date1.getTime(),
lastModified: date1.getTime()
})
const file2 = new File([''], 'filename.txt', {
type: 'text/plain',
lastModified: date1.getTime(),
lastModified: date1.getTime()
})
const file3 = new File([''], 'filename.txt', {
type: 'text/plain',
lastModified: date2.getTime(),
lastModified: date2.getTime()
})
const file4 = new File([''], 'filename.csv', {
type: 'text/csv',
lastModified: date1.getTime(),
lastModified: date1.getTime()
})
const file5 = new File(['abcdef'], 'filename.txt', {
type: 'text/plain',
lastModified: date1.getTime(),
lastModified: date1.getTime()
})
const file6 = new File(['12345'], 'filename.txt', {
type: 'text/plain',
lastModified: date1.getTime(),
lastModified: date1.getTime()
})

// Identical file object references
Expand Down Expand Up @@ -163,7 +163,7 @@ describe('utils/looseEqual', () => {
const date1 = new Date(2019, 1, 2, 3, 4, 5, 6)
const file1 = new File([''], 'filename.txt', {
type: 'text/plain',
lastModified: date1.getTime(),
lastModified: date1.getTime()
})

expect(looseEqual(123, '123')).toBe(true)
Expand Down
16 changes: 16 additions & 0 deletions packages/shared/__tests__/toNumber.spec.ts
@@ -0,0 +1,16 @@
import { toNumber } from '../src'

describe('utils/toNumber', () => {
test('number', () => {
expect(toNumber(1)).toBe(1)
expect(toNumber(1.3)).toBe(1.3)
expect(toNumber(NaN)).toBe(NaN)
})

test('string', () => {
expect(toNumber('123')).toBe(123)
expect(toNumber('3.14')).toBe(3.14)
expect(toNumber('3.14aa')).toBe('3.14aa')
expect(toNumber('aa3.14')).toBe('aa3.14')
})
})
4 changes: 4 additions & 0 deletions packages/shared/src/index.ts
Expand Up @@ -160,6 +160,10 @@ export const def = (obj: object, key: string | symbol, value: any) => {
}

export const toNumber = (val: any): any => {
if (typeof val === 'string') {
const matched = val.match(/\d+(\.\d+)?/)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const matched = val.match(/\d+(\.\d+)?/)
const matched = val.match(/^\d+(\.\d+)?$/)

return matched ? parseFloat(matched[0]) : val
}
const n = parseFloat(val)
return isNaN(n) ? val : n
}
Expand Down