Created
October 21, 2021 17:06
-
-
Save phatnguyenuit/5ada24a985010faae611357b96bc4af8 to your computer and use it in GitHub Desktop.
Prevent code duplication when writing unit tests with Jest .each - toKebabCase utils
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
const createTransform = ( | |
pattern: string | RegExp, | |
separator: string, | |
wordTransformer: (word: string) => string, | |
) => (text: string) => text.split(pattern).map(wordTransformer).join(separator); | |
const exhaustedPattern = /[\s_-]|(?=[A-Z0-9])/; | |
export const toKebabCase = createTransform(exhaustedPattern, '-', (word) => | |
word.toLowerCase(), | |
); | |
// "my_example" => "this-is-my-example" | |
// "anotherExample123" => "another-example-1-2-3" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment