Skip to content

Commit b30ffd6

Browse files
esezenHossam Hindawy
andauthored
[NO-CSL] Update camelToStartCase function (#113)
* Update camelToStartCase function * Update logic * Update logic to handle more cases --------- Co-authored-by: Hossam Hindawy <hossam.hindawy@hossam.hindawy-Q7M9F6QK6G>
1 parent 3504c23 commit b30ffd6

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/utils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ type CamelToStartCase = (camelCaseString: string) => string;
4848

4949
export const camelToStartCase: CamelToStartCase = (camelCaseString) =>
5050
camelCaseString
51-
// insert a space before all caps
52-
.replace(/([A-Z])/g, ' $1')
53-
// uppercase the first character
54-
.replace(/^./, (str) => str.toUpperCase());
51+
// insert a space between lower & upper
52+
.replace(/([a-z])([A-Z])/g, '$1 $2')
53+
// space before last upper in a sequence followed by lower
54+
.replace(/\b([A-Z]+)([A-Z])([a-z])/, '$1 $2$3')
55+
// uppercase the first letter
56+
.replace(/([a-zA-Z])/, (str) => str.toUpperCase());
5557

5658
export const toKebabCase = (str: string): string =>
5759
str

0 commit comments

Comments
 (0)