Skip to content

Commit

Permalink
fix: handle filename with numbers
Browse files Browse the repository at this point in the history
Fixes #62
Replace #64
  • Loading branch information
gregberge committed Mar 25, 2018
1 parent b81d1ad commit a2387ea
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,12 @@ const MyComponent = (props) => {/*?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?*/
export default MyComponent"
`;

exports[`should handle componentName with only numbers 1`] = `
"import React from 'react'
const Svg1BigSvg = (props) => <svg />
export default Svg1BigSvg"
`;
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ export {
removeStyle,
}

function expandState(state) {
function getComponentName(state) {
const componentName = pascalCase(path.parse(state.filePath).name)
return { ...state, componentName }
if (Number.isNaN(parseInt(componentName[0], 10))) {
return componentName
}

return `Svg${componentName}`
}

function expandState(state) {
return { componentName: getComponentName(state), ...state }
}

export async function rawConvert(code, options, state) {
Expand Down
17 changes: 16 additions & 1 deletion src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,28 @@ describe('rawConvert', () => {
},
template: wrapIntoComponent({ expandProps: true }),
},
{ filePath: 'MyComponent.js' },
{ filePath: 'MyComponent.svg' },
)

expect(result).toMatchSnapshot()
})
})

it('should handle componentName with only numbers', async () => {
const result = await rawConvert(
`<svg></svg>`,
{
h2x: {
plugins: [jsx],
},
template: wrapIntoComponent({ expandProps: true }),
},
{ filePath: '1_big_svg.svg' },
)

expect(result).toMatchSnapshot()
})

describe('convert', () => {
it('should convert using config', async () => {
const result = await convert(
Expand Down

0 comments on commit a2387ea

Please sign in to comment.