Skip to content

Commit 71e1b00

Browse files
authoredSep 28, 2023
perf(utils): faster pascal case function (#951)
1 parent 79f6847 commit 71e1b00

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed
 

‎packages/core/src/utils/case.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { pascal } from './case';
3+
4+
describe('pascal case testing', () => {
5+
it('should convert to pascal case', () => {
6+
expect(pascal('PascalCase')).toBe('PascalCase');
7+
expect(pascal('camelCase')).toBe('CamelCase');
8+
expect(pascal('kebab-case')).toBe('KebabCase');
9+
expect(pascal('snake_case')).toBe('SnakeCase');
10+
expect(pascal('point.case')).toBe('PointCase');
11+
expect(pascal('UPPER_CASE')).toBe('UpperCase');
12+
expect(pascal('DrUn-k_CaSE')).toBe('DrUnKCaSE');
13+
});
14+
15+
it('should convert to pascal case with underscore', () => {
16+
expect(pascal('_camelCase')).toBe('_CamelCase');
17+
expect(pascal('_kebab-case')).toBe('_KebabCase');
18+
});
19+
20+
it('should convert to pascal case when more complex input is given', () => {
21+
expect(pascal('camelCase_')).toBe('CamelCase');
22+
expect(pascal('more complex input')).toBe('MoreComplexInput');
23+
});
24+
25+
it('should handle some casing edge cases', () => {
26+
expect(pascal('foo_bar_API')).toBe('FooBarAPI');
27+
});
28+
});

‎packages/core/src/utils/case.ts

+13-17
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ const deapostrophe = (s: string) => {
4343
const up = String.prototype.toUpperCase;
4444
const low = String.prototype.toLowerCase;
4545

46-
const fill = (s: string, fillWith?: string, isDeapostrophe = false) => {
47-
if (fillWith != null) {
48-
s = s.replace(regexps.fill, function (m, next) {
49-
return next ? fillWith + next : '';
50-
});
51-
}
46+
const fill = (s: string, fillWith: string, isDeapostrophe = false) => {
47+
s = s.replace(regexps.fill, function (m, next) {
48+
return next ? fillWith + next : '';
49+
});
50+
5251
if (isDeapostrophe) {
5352
s = deapostrophe(s);
5453
}
@@ -92,16 +91,13 @@ const lower = (s: string, fillWith: string, isDeapostrophe: boolean) => {
9291
export const pascal = (s: string) => {
9392
const isStartWithUnderscore = s?.startsWith('_');
9493

95-
const pascalString = fill(
96-
prep(s, false, true).replace(
97-
regexps.pascal,
98-
(m: string, border: string, letter: string) => {
99-
return up.call(letter);
100-
},
101-
),
102-
'',
103-
true,
104-
);
94+
if (regexps.upper.test(s)) {
95+
s = low.call(s);
96+
}
97+
98+
const pascalString = (s.match(/[a-zA-Z0-9]+/g) || [])
99+
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
100+
.join('');
105101

106102
return isStartWithUnderscore ? `_${pascalString}` : pascalString;
107103
};
@@ -122,7 +118,7 @@ export const kebab = (s: string) => {
122118

123119
export const upper = (
124120
s: string,
125-
fillWith?: string,
121+
fillWith: string,
126122
isDeapostrophe?: boolean,
127123
) => {
128124
return fill(

1 commit comments

Comments
 (1)

vercel[bot] commented on Sep 28, 2023

@vercel[bot]
Please sign in to comment.