Commit abe7a9ba authored by Mike Greiling's avatar Mike Greiling

Merge branch 'nfriend-fix-camel-case-converter' into 'master'

Update `convertToCamelCase` utility function to ignore leading and trailing underscores

Closes #207853

See merge request gitlab-org/gitlab!26011
parents 6977b861 97c92cbc
...@@ -142,11 +142,25 @@ export const stripHtml = (string, replace = '') => { ...@@ -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 * Converts camelCase string to snake_case
......
...@@ -20,10 +20,10 @@ export default { ...@@ -20,10 +20,10 @@ export default {
}, },
computed: { computed: {
editLink() { editLink() {
return this.release.Links?.editUrl; return this.release._links?.editUrl;
}, },
selfLink() { selfLink() {
return this.release.Links?.self; return this.release._links?.self;
}, },
}, },
}; };
......
...@@ -94,8 +94,27 @@ describe('text_utility', () => { ...@@ -94,8 +94,27 @@ describe('text_utility', () => {
}); });
describe('convertToCamelCase', () => { describe('convertToCamelCase', () => {
it('converts snake_case string to camelCase string', () => { it.each`
expect(textUtils.convertToCamelCase('snake_case')).toBe('snakeCase'); 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', () => { ...@@ -37,13 +37,13 @@ describe('Release block header', () => {
const link = findHeaderLink(); const link = findHeaderLink();
expect(link.text()).toBe(release.name); 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', () => { describe('when _links.self is missing', () => {
beforeEach(() => { beforeEach(() => {
factory({ Links: { self: null } }); factory({ _links: { self: null } });
}); });
it('renders the title as text', () => { it('renders the title as text', () => {
......
...@@ -63,7 +63,7 @@ describe('Release block', () => { ...@@ -63,7 +63,7 @@ describe('Release block', () => {
it('renders an edit button that links to the "Edit release" page', () => { it('renders an edit button that links to the "Edit release" page', () => {
expect(editButton().exists()).toBe(true); 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', () => { it('renders release name', () => {
...@@ -150,8 +150,8 @@ describe('Release block', () => { ...@@ -150,8 +150,8 @@ describe('Release block', () => {
}); });
}); });
it("does not render an edit button if release.Links.editUrl isn't a string", () => { it("does not render an edit button if release._links.editUrl isn't a string", () => {
delete release.Links; delete release._links;
return factory(release).then(() => { return factory(release).then(() => {
expect(editButton().exists()).toBe(false); 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