Skip to content

Instantly share code, notes, and snippets.

@phatnguyenuit
Created October 21, 2021 17:06
Show Gist options
  • Save phatnguyenuit/5ada24a985010faae611357b96bc4af8 to your computer and use it in GitHub Desktop.
Save phatnguyenuit/5ada24a985010faae611357b96bc4af8 to your computer and use it in GitHub Desktop.
Prevent code duplication when writing unit tests with Jest .each - toKebabCase utils
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