Skip to content

Commit a2387ea

Browse files
committed
fix: handle filename with numbers
Fixes #62 Replace #64
1 parent b81d1ad commit a2387ea

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

src/__snapshots__/index.test.js.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,12 @@ const MyComponent = (props) => {/*?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?*/
171171
172172
export default MyComponent"
173173
`;
174+
175+
exports[`should handle componentName with only numbers 1`] = `
176+
"import React from 'react'
177+
178+
const Svg1BigSvg = (props) => <svg />
179+
180+
181+
export default Svg1BigSvg"
182+
`;

src/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@ export {
2727
removeStyle,
2828
}
2929

30-
function expandState(state) {
30+
function getComponentName(state) {
3131
const componentName = pascalCase(path.parse(state.filePath).name)
32-
return { ...state, componentName }
32+
if (Number.isNaN(parseInt(componentName[0], 10))) {
33+
return componentName
34+
}
35+
36+
return `Svg${componentName}`
37+
}
38+
39+
function expandState(state) {
40+
return { componentName: getComponentName(state), ...state }
3341
}
3442

3543
export async function rawConvert(code, options, state) {

src/index.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,28 @@ describe('rawConvert', () => {
3636
},
3737
template: wrapIntoComponent({ expandProps: true }),
3838
},
39-
{ filePath: 'MyComponent.js' },
39+
{ filePath: 'MyComponent.svg' },
4040
)
4141

4242
expect(result).toMatchSnapshot()
4343
})
4444
})
4545

46+
it('should handle componentName with only numbers', async () => {
47+
const result = await rawConvert(
48+
`<svg></svg>`,
49+
{
50+
h2x: {
51+
plugins: [jsx],
52+
},
53+
template: wrapIntoComponent({ expandProps: true }),
54+
},
55+
{ filePath: '1_big_svg.svg' },
56+
)
57+
58+
expect(result).toMatchSnapshot()
59+
})
60+
4661
describe('convert', () => {
4762
it('should convert using config', async () => {
4863
const result = await convert(

0 commit comments

Comments
 (0)