mirror of
https://github.com/pheralb/svgl.git
synced 2025-12-29 08:01:36 +08:00
🛠️ Improve parseSvgFilename to support firstUpperCase option for component naming
This commit is contained in:
@@ -191,6 +191,7 @@ async function convertSvgToReact(svgPath: string): Promise<string> {
|
|||||||
const componentName = parseSvgFilename({
|
const componentName = parseSvgFilename({
|
||||||
file: path.basename(svgPath, ".svg"),
|
file: path.basename(svgPath, ".svg"),
|
||||||
log: true,
|
log: true,
|
||||||
|
firstUpperCase: true,
|
||||||
});
|
});
|
||||||
const code = await parseReactSvgContent({
|
const code = await parseReactSvgContent({
|
||||||
componentName,
|
componentName,
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
interface ParseSvgFilename {
|
interface ParseSvgFilename {
|
||||||
file: string;
|
file: string;
|
||||||
log?: boolean;
|
log?: boolean;
|
||||||
|
firstUpperCase?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const parseSvgFilename = (params: ParseSvgFilename): string => {
|
export const parseSvgFilename = (params: ParseSvgFilename): string => {
|
||||||
const { file, log } = params;
|
const { file, log, firstUpperCase = false } = params;
|
||||||
const name = file.replace(/\.svg$/i, "");
|
const name = file.replace(/\.svg$/i, "");
|
||||||
|
|
||||||
let component = name
|
let component = name
|
||||||
.replace(/[-_\s]+(.)?/g, (_, char) => (char ? char.toUpperCase() : ""))
|
.replace(/[-_\s]+(.)?/g, (_, char) => (char ? char.toUpperCase() : ""))
|
||||||
.replace(/^(.)/, (char) => char.toLowerCase());
|
.replace(/^(.)/, (char) =>
|
||||||
|
firstUpperCase ? char.toUpperCase() : char.toLowerCase(),
|
||||||
|
);
|
||||||
|
|
||||||
if (/^\d/.test(component)) {
|
if (/^\d/.test(component)) {
|
||||||
if (log) {
|
if (log) {
|
||||||
|
|||||||
Reference in New Issue
Block a user