Skip to content

Commit

Permalink
feat(string): Make camelCase and other functions to support emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
raon0211 committed Sep 25, 2024
1 parent 1e04c48 commit 75c41ef
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/ja/reference/string/camelCase.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ camelCase('camelCase'); // 'camelCase' を返します
camelCase('some whitespace'); // 'someWhitespace' を返します
camelCase('hyphen-text'); // 'hyphenText' を返します
camelCase('HTTPRequest'); // 'httpRequest' を返します
camelCase('Keep unicode 😅') // 'keepUnicode😅' を返します
```
1 change: 1 addition & 0 deletions docs/ko/reference/string/camelCase.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ camelCase('camelCase'); // returns 'camelCase'
camelCase('some whitespace'); // returns 'someWhitespace'
camelCase('hyphen-text'); // returns 'hyphenText'
camelCase('HTTPRequest'); // returns 'httpRequest'
camelCase('Keep unicode 😅') // returns 'keepUnicode😅'
```
1 change: 1 addition & 0 deletions docs/reference/string/camelCase.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ camelCase('camelCase'); // returns 'camelCase'
camelCase('some whitespace'); // returns 'someWhitespace'
camelCase('hyphen-text'); // returns 'hyphenText'
camelCase('HTTPRequest'); // returns 'httpRequest'
camelCase('Keep unicode 😅') // returns 'keepUnicode😅'
```
1 change: 1 addition & 0 deletions docs/zh_hans/reference/string/camelCase.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ camelCase('camelCase'); // 返回 'camelCase'
camelCase('some whitespace'); // 返回 'someWhitespace'
camelCase('hyphen-text'); // 返回 'hyphenText'
camelCase('HTTPRequest'); // 返回 'httpRequest'
camelCase('Keep unicode 😅') // 返回 'keepUnicode😅'
```
4 changes: 4 additions & 0 deletions src/string/_internal/getWords.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ describe('caseSplitPattern', () => {
it('should work with numbers', () => {
expect(getWords('foo2bar')).toEqual(['foo', '2', 'bar']);
});

it('should match emojis', () => {
expect(getWords('camelCaseHTTPRequest🚀')).toEqual(['camel', 'Case', 'HTTP', 'Request', '🚀']);
});
});
14 changes: 8 additions & 6 deletions src/string/_internal/getWords.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
/**
* Regular expression pattern to split strings into words for various case conversions
*
* This pattern matches sequences of characters in a string, considering the following case:
* This pattern matches sequences of characters in a string, considering the following cases:
* - Sequences of two or more uppercase letters followed by an uppercase letter and lowercase letters or digits (for acronyms)
* - Sequences of one uppercase letter optionally followed by lowercase letters and digits
* - Single uppercase letters
* - Sequences of digis
* - Sequences of digits
* - Emojis and other Unicode characters
*
* The resulting match can be used to convert camelCase, snake_case, kebab-case, and other mixed formats into
* a consistent format like snake case.
* a consistent format like snake case. It also supports emojis and other Unicode characters.
*
* @example
* const matches = 'camelCaseHTTPRequest'.match(CASE_SPLIT_PATTERN);
* // matches: ['camel', 'Case', 'HTTP', 'Request']
* const matches = 'camelCaseHTTPRequest🚀'.match(CASE_SPLIT_PATTERN);
* // matches: ['camel', 'Case', 'HTTP', 'Request', '🚀']
*/
const CASE_SPLIT_PATTERN = /[A-Z]?[a-z]+|[0-9]+|[A-Z]+(?![a-z])/g;
const CASE_SPLIT_PATTERN =
/[A-Z]?[a-z]+|[0-9]+|[A-Z]+(?![a-z])|\p{Emoji_Presentation}|\p{Extended_Pictographic}|\p{L}+/gu;

export function getWords(str: string): string[] {
return Array.from(str.match(CASE_SPLIT_PATTERN) ?? []);
Expand Down
4 changes: 4 additions & 0 deletions src/string/camelCase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ describe('camelCase', () => {
it('should work with screaming camel case', () => {
expect(camelCase('FOO_BAR')).toEqual('fooBar');
});

it('should support emojis', () => {
expect(camelCase('Keep unicode 😅')).toEqual('keepUnicode😅');
});
});
2 changes: 1 addition & 1 deletion src/string/camelCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { getWords } from './_internal/getWords.ts';
* const convertedStr2 = camelCase('some whitespace') // returns 'someWhitespace'
* const convertedStr3 = camelCase('hyphen-text') // returns 'hyphenText'
* const convertedStr4 = camelCase('HTTPRequest') // returns 'httpRequest'
* const convertedStr5 = camelCase('Keep unicode 😅') // returns 'keepUnicode😅'
*/

export function camelCase(str: string): string {
const words = getWords(str);

Expand Down

0 comments on commit 75c41ef

Please sign in to comment.