Skip to content

Commit

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

test('financial', () => {
expect(financial(1.2222)).toBe(1.22)
expect(financial(1.2222, 1)).toBe(1.2)
expect(financial(1.2222, 10)).toBe(1.2222)
expect(financial(1.2222, 0.1)).toBe(1)
})

test('zeroPad', () => {
expect(zeroPad(12)).toBe('12')
expect(zeroPad(2)).toBe('02')
expect(zeroPad(4)).toBe('04')
expect(zeroPad(NaN)).toBe('00')
expect(zeroPad(4)).toBe('04')
})

test('convertSize', () => {
Expand All @@ -34,3 +36,15 @@ test('sizeToNum', () => {
expect(sizeToNum('12px12')).toBe(12)
expect(sizeToNum('abc')).toBe(0)
})

test('convertFormat', () => {
expect(convertFormat('HelloAreYouOk')).toBe('hello-are-you-ok')
expect(convertFormat('')).toBe('')
expect(convertFormat('123')).toBe('123')
expect(convertFormat('helloAreYouOk')).toBe('hello-are-you-ok')
})

test('splitString', () => {
expect(splitString('1,2,3', ',')).toStrictEqual(['1', '2', '3'])
expect(splitString('', ',')).toStrictEqual([])
})
11 changes: 9 additions & 2 deletions packages/fighting-design/_utils/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ export const sizeToNum = (size: string | number): number => {
return isNumber(parse) ? parse : 0
}

const convertFormatRegExp = /([A-Z])/g

/**
* 驼峰命名转换为短横线命名
*
Expand All @@ -111,8 +113,13 @@ export const sizeToNum = (size: string | number): number => {
* @returns { string } 短横线命名
*/
export const convertFormat = (str: string): string => {
return str.replace(/([A-Z])/g, (match: string, p1: string): string => {
return '-' + p1.toLowerCase()
return str.replace(convertFormatRegExp, (_: string, p1: string, offset: number): string => {
// 判断是否为首字母
if (offset === 0) {
return p1.toLowerCase()
} else {
return '-' + p1.toLowerCase()
}
})
}

Expand Down

0 comments on commit 52290f8

Please sign in to comment.