Skip to content

Commit

Permalink
fix(cli-shred): 路径转 js 变量考虑边缘情况并简化测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Otto-J authored and zhetengbiji committed Apr 30, 2024
1 parent 87e819e commit 9a52d56
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 6 additions & 3 deletions packages/uni-cli-shared/src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ describe('test: packages/uni-cli-shared/src/utils.ts', () => {
'PagesIndex10Index'
)
expect(normalizeIdentifier('pages/index2-0///index')).toBe(
'PagesIndex20__Index'
'PagesIndex20Index'
)
expect(normalizeIdentifier('pages/index3--0/index')).toBe(
'PagesIndex3_0Index'
'PagesIndex30Index'
)
expect(normalizeIdentifier('pages/index4---0/index')).toBe(
'PagesIndex4__0Index'
'PagesIndex40Index'
)

expect(normalizeIdentifier('pages/index5 0/index')).toBe(
'PagesIndex50Index'
)
expect(normalizeIdentifier('2pages/index6/index')).toBe(
'_2pagesIndex6Index'
)
})
})
6 changes: 5 additions & 1 deletion packages/uni-cli-shared/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ export function checkElementNodeTag(
* @returns
*/
export function normalizeIdentifier(str: string) {
let _str = str.replace(/[^a-zA-Z0-9]/g, '-')
let _str = str.replace(/[^a-zA-Z0-9]+/g, '-')
_str = capitalize(camelize(_str))
_str = _str.replace(/-/g, '_')
// 不允许数字开头,补充 _
if (/^\d/.test(_str)) {
_str = '_' + _str
}
return _str
}

Expand Down

0 comments on commit 9a52d56

Please sign in to comment.