From c214c3d7941ae114755b0cefede7ba0990f8bca9 Mon Sep 17 00:00:00 2001 From: Sungyu Kang Date: Sat, 14 Sep 2024 18:59:38 +0900 Subject: [PATCH] fix: vsc icons --- src/utils/generateIcon.ts | 5 ++--- src/utils/groupIconsByPrefix.ts | 12 ++++++++---- src/utils/separateCamelCase.spec.ts | 27 +++++++++++++++++++++++++++ src/utils/separateCamelCase.ts | 16 ++++++++++++++++ 4 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 src/utils/separateCamelCase.spec.ts create mode 100644 src/utils/separateCamelCase.ts diff --git a/src/utils/generateIcon.ts b/src/utils/generateIcon.ts index 15e18ec..5d4fccf 100644 --- a/src/utils/generateIcon.ts +++ b/src/utils/generateIcon.ts @@ -12,9 +12,8 @@ export const generateIconCode = async ( typescript: boolean ) => { const path = import.meta - .resolve("react-icons") - .replace("file://", "") - .replace("/index.mjs", `/${prefix}/index.mjs`); + .resolve(`react-icons/${prefix}`) + .replace("file://", ""); const reactIconsCode = await fs.readFile(path, "utf8"); diff --git a/src/utils/groupIconsByPrefix.ts b/src/utils/groupIconsByPrefix.ts index 17e38e1..3dcc481 100644 --- a/src/utils/groupIconsByPrefix.ts +++ b/src/utils/groupIconsByPrefix.ts @@ -1,10 +1,14 @@ +import { separateCamelCase } from "./separateCamelCase"; + export const groupIconsByPrefix = (icons: string[]) => { const groupedIcons = icons.reduce((acc, icon) => { - const prefix = icon.slice(0, 2).toLowerCase(); - if (!acc[prefix]) { - acc[prefix] = []; + const [prefix] = separateCamelCase(icon); + const lowerPrefix = prefix.toLowerCase(); + if (!acc[lowerPrefix]) { + acc[lowerPrefix] = []; } - acc[prefix].push(icon); + + acc[lowerPrefix].push(icon); return acc; }, {} as Record); diff --git a/src/utils/separateCamelCase.spec.ts b/src/utils/separateCamelCase.spec.ts new file mode 100644 index 0000000..e642fa0 --- /dev/null +++ b/src/utils/separateCamelCase.spec.ts @@ -0,0 +1,27 @@ +import { separateCamelCase } from "./separateCamelCase"; +import { expect, describe, it } from "vitest"; + +describe("separateCamelCase", () => { + it("should separate camel case", () => { + expect(separateCamelCase("helloWorld")).toStrictEqual(["hello", "World"]); + expect(separateCamelCase("Lia500Px")).toStrictEqual([ + "Lia", + "5", + "0", + "0", + "Px", + ]); + expect(separateCamelCase("HiAcademicCap")).toStrictEqual([ + "Hi", + "Academic", + "Cap", + ]); + expect(separateCamelCase("Bs0CircleFill")).toStrictEqual([ + "Bs", + "0", + "Circle", + "Fill", + ]); + }); + expect(separateCamelCase("VscAccount")).toStrictEqual(["Vsc", "Account"]); +}); diff --git a/src/utils/separateCamelCase.ts b/src/utils/separateCamelCase.ts new file mode 100644 index 0000000..c9cc52f --- /dev/null +++ b/src/utils/separateCamelCase.ts @@ -0,0 +1,16 @@ +export const separateCamelCase = (name: string) => { + const result = []; + let currentWord = ""; + + for (const char of name) { + if (char === char.toUpperCase() && currentWord) { + result.push(currentWord); + currentWord = char; + } else { + currentWord += char; + } + } + + result.push(currentWord); + return result; +};