Repository: smooth-code/svgr
Pull request #4 (2017-10-12 16:42:02)

Add the transformation to the PascalCase

codecov[bot]: # [Codecov](https://codecov.io/gh/smooth-code/svgr/pull/4?src=pr&el=h1) Report
> Merging [#4](https://codecov.io/gh/smooth-code/svgr/pull/4?src=pr&el=desc) into [master](https://codecov.io/gh/smooth-code/svgr/commit/165cc1b6b9be197c4e542c6566701427b47b7f8d?src=pr&el=desc) will **decrease** coverage by `0.46%`.
> The diff coverage is `80%`.

[![Impacted file tree graph](https://codecov.io/gh/smooth-code/svgr/pull/4/graphs/tree.svg?src=pr&token=ESkRTvXFW3&width=650&height=150)](https://codecov.io/gh/smooth-code/svgr/pull/4?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master      #4      +/-   ##
=========================================
- Coverage   31.77%   31.3%   -0.47%     
=========================================
  Files          14      17       +3     
  Lines         941     971      +30     
  Branches      219     222       +3     
=========================================
+ Hits          299     304       +5     
- Misses        461     483      +22     
- Partials      181     184       +3
```


| [Impacted Files](https://codecov.io/gh/smooth-code/svgr/pull/4?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/index.js](https://codecov.io/gh/smooth-code/svgr/pull/4?src=pr&el=tree#diff-c3JjL2luZGV4Lmpz) | `100% <100%> (ø)` | :arrow_up: |
| [src/transforms/rename.js](https://codecov.io/gh/smooth-code/svgr/pull/4?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybXMvcmVuYW1lLmpz) | `100% <100%> (ø)` | |
| [src/cli/dirCommand.js](https://codecov.io/gh/smooth-code/svgr/pull/4?src=pr&el=tree#diff-c3JjL2NsaS9kaXJDb21tYW5kLmpz) | `25% <75%> (ø)` | |
| [src/cli/util.js](https://codecov.io/gh/smooth-code/svgr/pull/4?src=pr&el=tree#diff-c3JjL2NsaS91dGlsLmpz) | `12.5% <0%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/smooth-code/svgr/pull/4?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/smooth-code/svgr/pull/4?src=pr&el=footer). Last update [165cc1b...1750f78](https://codecov.io/gh/smooth-code/svgr/pull/4?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

alexeybondarenko: @neoziro PR is ready for a review :)

I was a little bit confused with the failed tests because they were ok locally and failed on CI. It was because I had the __fixture_build__ form the test pass on the master branch.
To avoid the similar situations in future I've added `clear` script before to the `test` script. 

Patch:
diff --git a/package.json b/package.json
index 2a04a2f..5301055 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 4c86794..2942819 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/index.js b/src/index.js
index ce4b2e4..53bc169 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 0000000..405ac55
--- /dev/null
+++ b/src/transforms/rename.js
@@ -0,0 +1,3 @@
+import { upperFirst, camelCase } from 'lodash'
+
+export const pascalCase = str => upperFirst(camelCase(str))


Test patch:
diff --git a/src/cli/dirCommand.test.js b/src/cli/dirCommand.test.js
new file mode 100644
index 0000000..b3f2c4f
--- /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 bb6291e..01b294a 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/transforms/rename.test.js b/src/transforms/rename.test.js
new file mode 100644
index 0000000..c0020ea
--- /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')
+  })
+})