Commit 0d32d318 authored by Luke Ward's avatar Luke Ward Committed by Paul Slaughter

Replace slugifyWithHyphens with improved slugify function

parent 903227b0
import $ from 'jquery'; import $ from 'jquery';
import { slugifyWithHyphens } from './lib/utils/text_utility'; import { slugify } from './lib/utils/text_utility';
export default class Group { export default class Group {
constructor() { constructor() {
...@@ -14,7 +14,7 @@ export default class Group { ...@@ -14,7 +14,7 @@ export default class Group {
} }
update() { update() {
const slug = slugifyWithHyphens(this.groupName.val()); const slug = slugify(this.groupName.val());
this.groupPath.val(slug); this.groupPath.val(slug);
} }
......
...@@ -44,11 +44,18 @@ export const pluralize = (str, count) => str + (count > 1 || count === 0 ? 's' : ...@@ -44,11 +44,18 @@ export const pluralize = (str, count) => str + (count > 1 || count === 0 ? 's' :
export const dasherize = str => str.replace(/[_\s]+/g, '-'); export const dasherize = str => str.replace(/[_\s]+/g, '-');
/** /**
* Replaces whitespaces with hyphens and converts to lower case * Replaces whitespaces with hyphens, convert to lower case and remove non-allowed special characters
* @param {String} str * @param {String} str
* @returns {String} * @returns {String}
*/ */
export const slugifyWithHyphens = str => str.toLowerCase().replace(/\s+/g, '-'); export const slugify = str => {
const slug = str
.trim()
.toLowerCase()
.replace(/[^a-zA-Z0-9_.-]+/g, '-');
return slug === '-' ? '' : slug;
};
/** /**
* Replaces whitespaces with underscore and converts to lower case * Replaces whitespaces with underscore and converts to lower case
......
import $ from 'jquery'; import $ from 'jquery';
import { addSelectOnFocusBehaviour } from '../lib/utils/common_utils'; import { addSelectOnFocusBehaviour } from '../lib/utils/common_utils';
import { slugifyWithHyphens } from '../lib/utils/text_utility'; import { slugify } from '../lib/utils/text_utility';
import { s__ } from '~/locale'; import { s__ } from '~/locale';
let hasUserDefinedProjectPath = false; let hasUserDefinedProjectPath = false;
...@@ -34,7 +34,7 @@ const deriveProjectPathFromUrl = $projectImportUrl => { ...@@ -34,7 +34,7 @@ const deriveProjectPathFromUrl = $projectImportUrl => {
}; };
const onProjectNameChange = ($projectNameInput, $projectPathInput) => { const onProjectNameChange = ($projectNameInput, $projectPathInput) => {
const slug = slugifyWithHyphens($projectNameInput.val()); const slug = slugify($projectNameInput.val());
$projectPathInput.val(slug); $projectPathInput.val(slug);
}; };
......
---
title: Replace slugifyWithHyphens with improved slugify function
merge_request: 30172
author: Luke Ward
type: fixed
...@@ -55,9 +55,24 @@ describe('text_utility', () => { ...@@ -55,9 +55,24 @@ describe('text_utility', () => {
}); });
}); });
describe('slugifyWithHyphens', () => { describe('slugify', () => {
it('should remove accents and convert to lower case', () => {
expect(textUtils.slugify('João')).toEqual('jo-o');
});
it('should replaces whitespaces with hyphens and convert to lower case', () => { it('should replaces whitespaces with hyphens and convert to lower case', () => {
expect(textUtils.slugifyWithHyphens('My Input String')).toEqual('my-input-string'); expect(textUtils.slugify('My Input String')).toEqual('my-input-string');
});
it('should remove trailing whitespace and replace whitespaces within string with a hyphen', () => {
expect(textUtils.slugify(' a new project ')).toEqual('a-new-project');
});
it('should only remove non-allowed special characters', () => {
expect(textUtils.slugify('test!_pro-ject~')).toEqual('test-_pro-ject-');
});
it('should squash multiple hypens', () => {
expect(textUtils.slugify('test!!!!_pro-ject~')).toEqual('test-_pro-ject-');
});
it('should return empty string if only non-allowed characters', () => {
expect(textUtils.slugify('здрасти')).toEqual('');
}); });
}); });
......
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