-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(deps): inline decamelize dependency (#1099)
- Loading branch information
1 parent
d49217a
commit d932338
Showing
6 changed files
with
70 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Simplified copy of https://github.com/sindresorhus/decamelize/blob/5deb8c5b88c3dbc93ef0d68faa0fc45cf98cad9d/index.js | ||
const REPLACEMENT = '$1_$2'; | ||
|
||
/** | ||
* Convert a string into a decamelize lowercased one. | ||
* | ||
* @example | ||
* unicornsAndRainbows → unicorns_and_rainbows | ||
* myURLString → my_url_string | ||
* | ||
* @param text The string to decamelize. | ||
* @returns The decamelized string. | ||
*/ | ||
export function decamelize(text: string): string { | ||
// Checking the second character is done later on. Therefore process shorter strings here. | ||
if (text.length < 2) { | ||
return text.toLowerCase(); | ||
} | ||
|
||
// Split lowercase sequences followed by uppercase character. | ||
// `dataForUSACounties` → `data_For_USACounties` | ||
// `myURLstring → `my_URLstring` | ||
const decamelized = text.replace( | ||
/([\p{Lowercase_Letter}\d])(\p{Uppercase_Letter})/gu, | ||
REPLACEMENT | ||
); | ||
|
||
// Split multiple uppercase characters followed by one or more lowercase characters. | ||
// `my_URLstring` → `my_ur_lstring` | ||
return decamelized | ||
.replace( | ||
/(\p{Uppercase_Letter})(\p{Uppercase_Letter}\p{Lowercase_Letter}+)/gu, | ||
REPLACEMENT | ||
) | ||
.toLowerCase(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { decamelize } from '../../src/utils'; | ||
|
||
describe('utils', () => { | ||
describe('decamelize', () => { | ||
it.each([ | ||
['', ''], | ||
['A', 'a'], | ||
['A B', 'a b'], | ||
['a2b', 'a2b'], | ||
['A2B', 'a2_b'], | ||
['_A2B', '_a2_b'], | ||
['myURLstring', 'my_ur_lstring'], | ||
['unicornsAndRainbows', 'unicorns_and_rainbows'], | ||
['UNICORNS AND RAINBOWS', 'unicorns and rainbows'], | ||
['unicorns-and-rainbows', 'unicorns-and-rainbows'], | ||
['thisIsATest', 'this_is_a_test'], | ||
['myURLString', 'my_url_string'], | ||
['URLString', 'url_string'], | ||
['StringURL', 'string_url'], | ||
['testGUILabel', 'test_gui_label'], | ||
['CAPLOCKED1', 'caplocked1'], | ||
['my_URL_string', 'my_url_string'], | ||
])('should handle string %s', (input, expected) => { | ||
const actual = decamelize(input); | ||
|
||
expect(actual).toBe(expected); | ||
}); | ||
}); | ||
}); |