diff --git a/packages/fighting-design/_utils/utils/__test__/utils.spec.ts b/packages/fighting-design/_utils/utils/__test__/utils.spec.ts index f4213063ec..20fdf2cb83 100644 --- a/packages/fighting-design/_utils/utils/__test__/utils.spec.ts +++ b/packages/fighting-design/_utils/utils/__test__/utils.spec.ts @@ -1,10 +1,11 @@ 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', () => { @@ -12,6 +13,7 @@ test('zeroPad', () => { expect(zeroPad(2)).toBe('02') expect(zeroPad(4)).toBe('04') expect(zeroPad(NaN)).toBe('00') + expect(zeroPad(4)).toBe('04') }) test('convertSize', () => { @@ -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([]) +}) diff --git a/packages/fighting-design/_utils/utils/index.ts b/packages/fighting-design/_utils/utils/index.ts index e66c1638b0..bde2f6160f 100644 --- a/packages/fighting-design/_utils/utils/index.ts +++ b/packages/fighting-design/_utils/utils/index.ts @@ -102,6 +102,8 @@ export const sizeToNum = (size: string | number): number => { return isNumber(parse) ? parse : 0 } +const convertFormatRegExp = /([A-Z])/g + /** * 驼峰命名转换为短横线命名 * @@ -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() + } }) }