Skip to content

Commit

Permalink
fix: transform to PascalCase only on first lower letter or name conta…
Browse files Browse the repository at this point in the history
…ins hyphens (#129)
  • Loading branch information
TheAlexLichter authored Jan 6, 2021
1 parent 79b36ed commit 2c819e6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { camelCase, kebabCase, upperFirst } from 'lodash'
import type { ScanDir, Component } from './types'

const LAZY_PREFIX = 'lazy'
const pascalCase = (str: string) => upperFirst(camelCase(str))
const pascalCase = (str: string) => {
const isFirstCharUppercase = str[0] === str[0].toUpperCase()
const containsHyphens = str.includes('-')
const shouldTransformToPascal = !isFirstCharUppercase || containsHyphens

return shouldTransformToPascal ? upperFirst(camelCase(str)) : str
}
const isWindows = process.platform.startsWith('win')

function sortDirsByPathLength ({ path: pathA }: ScanDir, { path: pathB }: ScanDir): number {
Expand All @@ -14,7 +20,7 @@ function sortDirsByPathLength ({ path: pathA }: ScanDir, { path: pathB }: ScanDi
function prefixComponent (prefix: string = '', { pascalName, kebabName, ...rest }: Component): Component {
return {
pascalName: pascalName.startsWith(prefix) ? pascalName : pascalCase(prefix) + pascalName,
kebabName: kebabName.startsWith(prefix) ? kebabName : kebabCase(prefix) + '-' + kebabName,
kebabName: kebabName.startsWith(prefix) ? kebabName : `${kebabCase(prefix)}-${kebabName}`,
...rest
}
}
Expand Down Expand Up @@ -54,6 +60,7 @@ export async function scanComponents (dirs: ScanDir[], srcDir: string): Promise<

const pascalName = pascalCase(fileName)
const kebabName = kebabCase(fileName)

const shortPath = filePath.replace(srcDir, '').replace(/\\/g, '/').replace(/^\//, '')
let chunkName = shortPath.replace(extname(shortPath), '')

Expand Down
5 changes: 5 additions & 0 deletions test/fixture/components/PAScal.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
Pascal Case
</div>
</template>
1 change: 1 addition & 0 deletions test/fixture/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<IconHome />
<MAwesome />
<Functional />
<PAScal />
</div>
</template>
1 change: 1 addition & 0 deletions test/unit/scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test('scanner', async () => {
'Bar',
'Big',
'Mouse',
'PAScal',
'Foo',
'Functional',
'FunctionalChild',
Expand Down
2 changes: 1 addition & 1 deletion test/unit/tagExtractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { extractTags } from '../../src/tagExtractor'
test('with template', async () => {
const tags = await extractTags(path.resolve('test/fixture/pages/index.vue'))

expect(tags).toEqual(['Header', 'Foo', 'LazyBar', 'BaseButton', 'IconHome', 'MAwesome', 'Functional', 'div'])
expect(tags).toEqual(['Header', 'Foo', 'LazyBar', 'BaseButton', 'IconHome', 'MAwesome', 'Functional', 'PAScal', 'div'])
})

test('without template', async () => {
Expand Down

0 comments on commit 2c819e6

Please sign in to comment.