mirror of
https://github.com/pheralb/svgl.git
synced 2025-12-29 08:01:36 +08:00
⚙️ Refactor SVG filename parsing and replace toComponentName function
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
interface ParseSvgFilename {
|
||||
file: string;
|
||||
log?: boolean;
|
||||
}
|
||||
|
||||
export const parseSvgFilename = (params: ParseSvgFilename): string => {
|
||||
const { file, log } = params;
|
||||
const name = file.replace(/\.svg$/i, "");
|
||||
|
||||
let component = name.replace(/(^\w|[-_]\w)/g, (m) =>
|
||||
m.replace(/[-_]/, "").toUpperCase(),
|
||||
);
|
||||
|
||||
if (/^\d/.test(component)) {
|
||||
if (log) {
|
||||
console.log(`[⚠️] Component name starts with a number: ${component}`);
|
||||
}
|
||||
component = "Icon" + component;
|
||||
}
|
||||
|
||||
const reserved = new Set([
|
||||
"default",
|
||||
"class",
|
||||
"function",
|
||||
"var",
|
||||
"export",
|
||||
"import",
|
||||
"extends",
|
||||
"new",
|
||||
"delete",
|
||||
"enum",
|
||||
"package",
|
||||
]);
|
||||
if (reserved.has(component)) {
|
||||
if (log) {
|
||||
console.log(`[⚠️] Component name is a reserved keyword: ${component}`);
|
||||
}
|
||||
component = "Icon" + component[0].toUpperCase() + component.slice(1);
|
||||
}
|
||||
|
||||
return component;
|
||||
};
|
||||
Reference in New Issue
Block a user