Skip to content

Commit

Permalink
perf(utils): caches pascal function generated strings
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Oct 9, 2023
1 parent ed1261a commit 5f3622c
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/core/src/utils/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ const lower = (s: string, fillWith: string, isDeapostrophe: boolean) => {
return fill(low.call(prep(s, !!fillWith)), fillWith, isDeapostrophe);
};

// Caches the previously converted strings to improve performance
let pascalMemory: Record<string, string> = {};

export const pascal = (s: string) => {
if (pascalMemory[s]) {
return pascalMemory[s];
}

const isStartWithUnderscore = s?.startsWith('_');

if (regexps.upper.test(s)) {
Expand All @@ -99,7 +106,13 @@ export const pascal = (s: string) => {
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
.join('');

return isStartWithUnderscore ? `_${pascalString}` : pascalString;
const pascalWithUnderscore = isStartWithUnderscore
? `_${pascalString}`
: pascalString;

pascalMemory[s] = pascalWithUnderscore;

return pascalWithUnderscore;
};

export const camel = (s: string) => {
Expand Down

0 comments on commit 5f3622c

Please sign in to comment.