From b2ea9fa9f544b9da588d1f21b915d81b42883b64 Mon Sep 17 00:00:00 2001 From: Alexey Bondarenko Date: Sun, 15 Oct 2017 20:35:43 +0300 Subject: [PATCH] feat: transform filename into PascalCase (#4) --- package.json | 3 ++- src/cli/dirCommand.js | 13 +++++++++++-- src/cli/dirCommand.test.js | 15 +++++++++++++++ src/cli/index.test.js | 4 ++-- src/index.js | 9 ++------- src/transforms/rename.js | 3 +++ src/transforms/rename.test.js | 11 +++++++++++ 7 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 src/cli/dirCommand.test.js create mode 100644 src/transforms/rename.js create mode 100644 src/transforms/rename.test.js diff --git a/package.json b/package.json index 2a04a2f9..53010559 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,8 @@ "format": "prettier --write \"src/**/*.js\"", "lint": "eslint .", "release": "mversion `conventional-recommended-bump -p angular` -m", - "test": "jest --runInBand --coverage && codecov", + "clear": "rm -rf __fixtures_build__ lib", + "test": "npm run clear && jest --runInBand --coverage && codecov", "prepublish": "yarn run build" } } diff --git a/src/cli/dirCommand.js b/src/cli/dirCommand.js index 4c867945..29428191 100644 --- a/src/cli/dirCommand.js +++ b/src/cli/dirCommand.js @@ -3,13 +3,22 @@ import fs from 'mz/fs' import path from 'path' import outputFileSync from 'output-file-sync' import { convertFile, isCompilableExtension, readdir } from './util' +import { pascalCase } from '../transforms/rename' + +export const rename = (relative) => { + const relativePath = path.parse(relative) + relativePath.ext = '.js' + relativePath.name = pascalCase(relativePath.name) + relativePath.base = null + + return path.format(relativePath) +} async function dirCommand(program, filenames, opts) { async function write(src, relative) { if (!isCompilableExtension(relative)) return false - // remove extension and then append back on .js - relative = `${relative.replace(/\.(\w*?)$/, '')}.js` + relative = rename(relative) const dest = path.join(program.outDir, relative) diff --git a/src/cli/dirCommand.test.js b/src/cli/dirCommand.test.js new file mode 100644 index 00000000..b3f2c4f7 --- /dev/null +++ b/src/cli/dirCommand.test.js @@ -0,0 +1,15 @@ +import { rename } from './dirCommand' + +describe('rename', () => { + it('should transform fileName to the PascalCase', () => { + expect(rename('camel-case.js')).toBe('CamelCase.js') + expect(rename('camelCase.js')).toBe('CamelCase.js') + expect(rename('camel_case.js')).toBe('CamelCase.js') + expect(rename('camelcase.js')).toBe('Camelcase.js') + }) + + it('should change the extension to js', () => { + const result = rename('camel-case.svg') + expect(result).toBe('CamelCase.js') + }) +}) diff --git a/src/cli/index.test.js b/src/cli/index.test.js index bb6291e9..01b294a9 100644 --- a/src/cli/index.test.js +++ b/src/cli/index.test.js @@ -83,10 +83,10 @@ describe('cli', () => { it('should work with output directory', async () => { await exec('babel-node src/cli --out-dir __fixtures_build__ __fixtures__') expect( - await fs.readFile('__fixtures_build__/one.js', 'utf-8'), + await fs.readFile('__fixtures_build__/One.js', 'utf-8'), ).toMatchSnapshot() expect( - await fs.readFile('__fixtures_build__/nested/two.js', 'utf-8'), + await fs.readFile('__fixtures_build__/nested/Two.js', 'utf-8'), ).toMatchSnapshot() }) }) diff --git a/src/index.js b/src/index.js index ce4b2e4c..53bc1693 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ import h2x from './plugins/h2x' import prettier from './plugins/prettier' import transform from './plugins/transform' import wrapIntoComponent from './transforms/wrapIntoComponent' +import { pascalCase } from './transforms/rename' import stripAttribute from './h2x/stripAttribute' import emSize from './h2x/emSize' import expandProps from './h2x/expandProps' @@ -22,14 +23,8 @@ export { removeComments, } -const firstUpperCase = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}` -const hyphenToCamelCase = str => - str.replace(/-(.)/g, (match, chr) => chr.toUpperCase()) - function expandState(state) { - const componentName = firstUpperCase( - hyphenToCamelCase(path.parse(state.filePath).name), - ) + const componentName = pascalCase(path.parse(state.filePath).name) return { ...state, componentName } } diff --git a/src/transforms/rename.js b/src/transforms/rename.js new file mode 100644 index 00000000..405ac552 --- /dev/null +++ b/src/transforms/rename.js @@ -0,0 +1,3 @@ +import { upperFirst, camelCase } from 'lodash' + +export const pascalCase = str => upperFirst(camelCase(str)) diff --git a/src/transforms/rename.test.js b/src/transforms/rename.test.js new file mode 100644 index 00000000..c0020ea0 --- /dev/null +++ b/src/transforms/rename.test.js @@ -0,0 +1,11 @@ +import { pascalCase } from './rename' + +describe('pascalCase', () => { + it('should transform fileName to the PascalCase', () => { + expect(pascalCase('camel-case')).toBe('CamelCase') + expect(pascalCase('camel_case')).toBe('CamelCase') + expect(pascalCase('camelCase')).toBe('CamelCase') + expect(pascalCase('camel--Case')).toBe('CamelCase') + expect(pascalCase('camel_case')).toBe('CamelCase') + }) +})