From 0263018f65d93951659c651dda3349a50d433f2a Mon Sep 17 00:00:00 2001 From: YuNing Chen Date: Thu, 3 Aug 2023 18:41:11 +0800 Subject: [PATCH] Optimize splitCamelCase function performance (#270) Test if contain first then split --- src/tools/utils.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/tools/utils.ts b/src/tools/utils.ts index 75c43f8..7854847 100644 --- a/src/tools/utils.ts +++ b/src/tools/utils.ts @@ -342,14 +342,15 @@ export function chunkArray(arr: T[], len: number): T[][] { * @param text */ export function splitCamelCase(text: string): string[] { - const split = text + // if no camel case found, do nothing + if (!/[a-z][A-Z]/.test(text)) { + return []; + } + const splittedText = text .replace(/([a-z](?=[A-Z]))/g, '$1 ') .split(' ') - .filter(t => t) - if (split.length > 1) { - return split - } - return [] + .filter(t => t); + return splittedText; } /**