From 39c24c54958841592d150e3e13e21fe64e4806d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Thu, 11 Apr 2019 08:38:28 +0200 Subject: [PATCH] fix(cli): fix kebab case transformation with "_" Closes #280 --- packages/cli/src/dirCommand.js | 23 +---------------------- packages/cli/src/util.js | 21 +++++++++++++++++++++ packages/cli/src/util.test.js | 14 +++++++++++++- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/packages/cli/src/dirCommand.js b/packages/cli/src/dirCommand.js index a5483df2..cdcd83b5 100644 --- a/packages/cli/src/dirCommand.js +++ b/packages/cli/src/dirCommand.js @@ -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) diff --git a/packages/cli/src/util.js b/packages/cli/src/util.js index 4091642f..6df80bba 100644 --- a/packages/cli/src/util.js +++ b/packages/cli/src/util.js @@ -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, diff --git a/packages/cli/src/util.test.js b/packages/cli/src/util.test.js index 0936f2b9..665d6357 100644 --- a/packages/cli/src/util.test.js +++ b/packages/cli/src/util.test.js @@ -1,5 +1,5 @@ import path from 'path' -import { convertFile } from './util' +import { convertFile, transformFilename, CASE } from './util' const FIXTURES = path.join(__dirname, '../../../__fixtures__') @@ -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') + }) + }) })