Commit fa7d4f4e authored by Nathan Friend's avatar Nathan Friend

Make convertToSnakeCase handle edge cases

This commit updates the convertToSnakeCase utility function to handle
edge cases, in particular cases where the input only contains numbers.
parent 1c704297
......@@ -168,7 +168,7 @@ export const convertToCamelCase = string =>
* @param {*} string
*/
export const convertToSnakeCase = string =>
slugifyWithUnderscore(string.match(/([a-zA-Z][^A-Z]*)/g).join(' '));
slugifyWithUnderscore((string.match(/([a-zA-Z][^A-Z]*)/g) || [string]).join(' '));
/**
* Converts a sentence to lower case from the second word onwards
......
......@@ -126,6 +126,8 @@ describe('text_utility', () => {
${'snake case'} | ${'snake_case'}
${'snake_case'} | ${'snake_case'}
${'snakeCasesnake Case'} | ${'snake_casesnake_case'}
${'123'} | ${'123'}
${'123 456'} | ${'123_456'}
`('converts string $txt to $result string', ({ txt, result }) => {
expect(textUtils.convertToSnakeCase(txt)).toEqual(result);
});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment