Skip to content

Commit

Permalink
fix(cli): fix kebab case transformation with "_"
Browse files Browse the repository at this point in the history
Closes #280
  • Loading branch information
gregberge committed Apr 11, 2019
1 parent ddfae22 commit 39c24c5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
23 changes: 1 addition & 22 deletions packages/cli/src/dirCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,7 @@
import path from 'path'
import outputFileSync from 'output-file-sync'
import readdir from 'recursive-readdir'
import camelcase from 'camelcase'
import dashify from 'dashify'
import { convertFile, stat } from './util'

const CASE = {
KEBAB: 'kebab', // kebab-case
CAMEL: 'camel', // camelCase
PASCAL: 'pascal', // PascalCase
}

function transformFilename(filename, filenameCase) {
switch (filenameCase) {
case CASE.KEBAB:
return dashify(filename, { condense: true })
case CASE.CAMEL:
return camelcase(filename)
case CASE.PASCAL:
return camelcase(filename, { pascalCase: true })
default:
throw new Error(`Unknown --filename-case ${filenameCase}`)
}
}
import { convertFile, stat, transformFilename, CASE } from './util'

function rename(relative, ext, filenameCase) {
const relativePath = path.parse(relative)
Expand Down
21 changes: 21 additions & 0 deletions packages/cli/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,31 @@ import svgrConvert from '@svgr/core'
import svgo from '@svgr/plugin-svgo'
import jsx from '@svgr/plugin-jsx'
import prettier from '@svgr/plugin-prettier'
import camelcase from 'camelcase'
import dashify from 'dashify'

export const readFile = util.promisify(fs.readFile)
export const stat = util.promisify(fs.stat)

export const CASE = {
KEBAB: 'kebab', // kebab-case
CAMEL: 'camel', // camelCase
PASCAL: 'pascal', // PascalCase
}

export function transformFilename(filename, filenameCase) {
switch (filenameCase) {
case CASE.KEBAB:
return dashify(filename.replace(/_/g, '-'), { condense: true })
case CASE.CAMEL:
return camelcase(filename)
case CASE.PASCAL:
return camelcase(filename, { pascalCase: true })
default:
throw new Error(`Unknown --filename-case ${filenameCase}`)
}
}

export function convert(code, config, state) {
return svgrConvert.sync(code, config, {
...state,
Expand Down
14 changes: 13 additions & 1 deletion packages/cli/src/util.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import { convertFile } from './util'
import { convertFile, transformFilename, CASE } from './util'

const FIXTURES = path.join(__dirname, '../../../__fixtures__')

Expand All @@ -19,4 +19,16 @@ describe('util', () => {
expect(result).toMatchSnapshot()
})
})

describe('#transformFilename', () => {
it('should transform filename', () => {
expect(transformFilename('FooBar', CASE.CAMEL)).toBe('fooBar')
expect(transformFilename('FooBar', CASE.KEBAB)).toBe('foo-bar')
expect(transformFilename('FooBar', CASE.PASCAL)).toBe('FooBar')

expect(transformFilename('foo_bar', CASE.CAMEL)).toBe('fooBar')
expect(transformFilename('foo_bar', CASE.KEBAB)).toBe('foo-bar')
expect(transformFilename('foo_bar', CASE.PASCAL)).toBe('FooBar')
})
})
})

0 comments on commit 39c24c5

Please sign in to comment.