Skip to content

Commit

Permalink
feat: add snake_case filename option (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Gores committed Apr 20, 2023
1 parent 22a5a93 commit 428b0c7
Show file tree
Hide file tree
Showing 10 changed files with 2,325 additions and 6,412 deletions.
2 changes: 1 addition & 1 deletion packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Options:
-d, --out-dir <dirname> output files into a directory
--ignore-existing ignore existing files when used with --out-dir
--ext <ext> specify a custom file extension (default: "js")
--filename-case <case> specify filename case ("pascal", "kebab", "camel") (default: "pascal")
--filename-case <case> specify filename case ("pascal", "kebab", "camel", "snake") (default: "pascal")
--icon use "1em" as width and height
--native add react-native support with react-native-svg
--memo add React.memo into the result component
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"chalk": "^4.1.2",
"commander": "^9.4.1",
"dashify": "^2.0.0",
"glob": "^8.0.3"
"glob": "^8.0.3",
"snake-case": "^3.0.4"
},
"devDependencies": {
"@types/glob": "^8.1.0",
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ exports[`cli should support different filename cases with directory output: --fi
]
`;

exports[`cli should support different filename cases with directory output: --filename-case=snake 1`] = `
[
"camel_case.js",
"index.js",
"kebab_case.js",
"multiple_dashes.js",
"pascal_case.js",
]
`;

exports[`cli should support stdin filepath 1`] = `
"import * as React from 'react'
const SvgFile = (props) => (
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ describe('cli', () => {
[1, '--filename-case=camel'],
[2, '--filename-case=pascal'],
[3, '--filename-case=kebab'],
[4, '--filename-case=snake'],
])(
'should support different filename cases with directory output',
async (index, args) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ program
.option('--ext <ext>', 'specify a custom file extension (default: "js")')
.option(
'--filename-case <case>',
'specify filename case ("pascal", "kebab", "camel") (default: "pascal")',
'specify filename case ("pascal", "kebab", "camel", "snake") (default: "pascal")',
)
.option(
'--icon [size]',
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ describe('util', () => {
expect(transformFilename('FooBar', 'camel')).toBe('fooBar')
expect(transformFilename('FooBar', 'kebab')).toBe('foo-bar')
expect(transformFilename('FooBar', 'pascal')).toBe('FooBar')
expect(transformFilename('FooBar', 'snake')).toBe('foo_bar')

expect(transformFilename('foo_bar', 'camel')).toBe('fooBar')
expect(transformFilename('foo_bar', 'kebab')).toBe('foo-bar')
expect(transformFilename('foo_bar', 'pascal')).toBe('FooBar')
expect(transformFilename('foo_bar', 'snake')).toBe('foo_bar')
})
})

Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import prettier from '@svgr/plugin-prettier'
import camelCase from 'camelcase'
// @ts-ignore
import dashify from 'dashify'
import { snakeCase } from 'snake-case'

export function transformFilename(
filename: string,
Expand All @@ -21,6 +22,8 @@ export function transformFilename(
return camelCase(filename)
case 'pascal':
return camelCase(filename, { pascalCase: true })
case 'snake':
return snakeCase(filename)
default:
throw new Error(`Unknown --filename-case ${filenameCase}`)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"@babel/core": "^7.21.3",
"@svgr/babel-preset": "workspace:*",
"camelcase": "^6.2.0",
"cosmiconfig": "^8.1.3"
"cosmiconfig": "^8.1.3",
"snake-case": "^3.0.4"
},
"devDependencies": {
"svgo": "^3.0.2"
Expand Down

1 comment on commit 428b0c7

@vercel
Copy link

@vercel vercel bot commented on 428b0c7 Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

svgr – ./

svgr-git-main-gregberge.vercel.app
api.react-svgr.com
svgr-gregberge.vercel.app

Please sign in to comment.