Commit 97c92cbc authored by Nathan Friend's avatar Nathan Friend Committed by Mike Greiling

Change convertToCamelCase behavior

Updates the convertToCamelCase function to ignore leading and trailing
underscores.

Also updates code that was relying on the old behavior.
parent 420beeee
......@@ -142,11 +142,25 @@ export const stripHtml = (string, replace = '') => {
};
/**
* Converts snake_case string to camelCase
* Converts a snake_cased string to camelCase.
* Leading and trailing underscores are ignored.
*
* @param {*} string
* @param {String} string The snake_cased string to convert
* @returns {String} A camelCased version of the string
*
* @example
*
* // returns "aSnakeCasedString"
* convertToCamelCase('a_snake_cased_string')
*
* // returns "_leadingUnderscore"
* convertToCamelCase('_leading_underscore')
*
* // returns "trailingUnderscore_"
* convertToCamelCase('trailing_underscore_')
*/
export const convertToCamelCase = string => string.replace(/(_\w)/g, s => s[1].toUpperCase());
export const convertToCamelCase = string =>
string.replace(/([a-z0-9])_([a-z0-9])/gi, (match, p1, p2) => `${p1}${p2.toUpperCase()}`);
/**
* Converts camelCase string to snake_case
......
......@@ -20,10 +20,10 @@ export default {
},
computed: {
editLink() {
return this.release.Links?.editUrl;
return this.release._links?.editUrl;
},
selfLink() {
return this.release.Links?.self;
return this.release._links?.self;
},
},
};
......
......@@ -94,8 +94,27 @@ describe('text_utility', () => {
});
describe('convertToCamelCase', () => {
it('converts snake_case string to camelCase string', () => {
expect(textUtils.convertToCamelCase('snake_case')).toBe('snakeCase');
it.each`
txt | result
${'a_snake_cased_string'} | ${'aSnakeCasedString'}
${'_leading_underscore'} | ${'_leadingUnderscore'}
${'__leading_underscores'} | ${'__leadingUnderscores'}
${'trailing_underscore_'} | ${'trailingUnderscore_'}
${'trailing_underscores__'} | ${'trailingUnderscores__'}
`('converts string "$txt" to "$result"', ({ txt, result }) => {
expect(textUtils.convertToCamelCase(txt)).toBe(result);
});
it.each`
txt
${'__withoutMiddleUnderscores__'}
${''}
${'with spaces'}
${'with\nnew\r\nlines'}
${'_'}
${'___'}
`('does not modify string "$txt"', ({ txt }) => {
expect(textUtils.convertToCamelCase(txt)).toBe(txt);
});
});
......
......@@ -37,13 +37,13 @@ describe('Release block header', () => {
const link = findHeaderLink();
expect(link.text()).toBe(release.name);
expect(link.attributes('href')).toBe(release.Links.self);
expect(link.attributes('href')).toBe(release._links.self);
});
});
describe('when _links.self is missing', () => {
beforeEach(() => {
factory({ Links: { self: null } });
factory({ _links: { self: null } });
});
it('renders the title as text', () => {
......
......@@ -63,7 +63,7 @@ describe('Release block', () => {
it('renders an edit button that links to the "Edit release" page', () => {
expect(editButton().exists()).toBe(true);
expect(editButton().attributes('href')).toBe(release.Links.editUrl);
expect(editButton().attributes('href')).toBe(release._links.editUrl);
});
it('renders release name', () => {
......@@ -150,8 +150,8 @@ describe('Release block', () => {
});
});
it("does not render an edit button if release.Links.editUrl isn't a string", () => {
delete release.Links;
it("does not render an edit button if release._links.editUrl isn't a string", () => {
delete release._links;
return factory(release).then(() => {
expect(editButton().exists()).toBe(false);
......
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