Skip to content

Commit

Permalink
fix(toNumber): fix issue vuejs#2598 and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
stmtk1 committed Nov 13, 2020
1 parent 15baaf1 commit be128bb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
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.14')).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+)?/)
return matched ? parseFloat(matched[0]) : val
}
const n = parseFloat(val)
return isNaN(n) ? val : n
}
Expand Down

0 comments on commit be128bb

Please sign in to comment.