Skip to content

Commit

Permalink
test: 更新单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyh2001 committed Apr 15, 2024
1 parent 432bebf commit 11f67d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 12 additions & 2 deletions packages/fighting-design/_utils/utils/__test__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest'
import { financial, zeroPad, convertSize } from '..'
import { financial, zeroPad, convertSize, sizeToNum } from '..'

test('financial', () => {
expect(financial(1.2222)).toBe(1.22)
Expand All @@ -22,5 +22,15 @@ test('convertSize', () => {
expect(convertSize('', 'px')).toBe('')
expect(convertSize(12, 'px')).toBe('12px')
expect(convertSize(12)).toBe('12px')
expect(convertSize(NaN)).toBe('NaNpx')
expect(convertSize(NaN)).toBe('')
})

test('sizeToNum', () => {
expect(sizeToNum('12')).toBe(12)
expect(sizeToNum('')).toBe(0)
expect(sizeToNum(NaN)).toBe(0)
expect(sizeToNum(12)).toBe(12)
expect(sizeToNum('undefined')).toBe(0)
expect(sizeToNum('12px12')).toBe(12)
expect(sizeToNum('abc')).toBe(0)
})
6 changes: 5 additions & 1 deletion packages/fighting-design/_utils/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ export const sizeToNum = (size: string | number): number => {
if (!size) {
return 0
}

if (isNumber(size)) {
return size
}

return Number.parseFloat(size) || 0
const parse = Number.parseFloat(size)

// 避免转换结果为 NaN
return isNumber(parse) ? parse : 0
}

/**
Expand Down

0 comments on commit 11f67d4

Please sign in to comment.