Commit 64963937 authored by Kev's avatar Kev

Create convertUnicodeToAscii text_utility method

parent d795ab07
...@@ -275,6 +275,76 @@ export const convertToSentenceCase = string => { ...@@ -275,6 +275,76 @@ export const convertToSentenceCase = string => {
*/ */
export const convertToTitleCase = string => string.replace(/\b[a-z]/g, s => s.toUpperCase()); export const convertToTitleCase = string => string.replace(/\b[a-z]/g, s => s.toUpperCase());
const unicodeConversion = [
[/[ÀÁÂÃÅĀĂĄ]/g, 'A'],
[/[Æ]/g, 'AE'],
[/[ÇĆĈĊČ]/g, 'C'],
[/[ÈÉÊËĒĔĖĘĚ]/g, 'E'],
[/[ÌÍÎÏĨĪĬĮİ]/g, 'I'],
[/[Ððĥħ]/g, 'h'],
[/[ÑŃŅŇʼn]/g, 'N'],
[/[ÒÓÔÕÖØŌŎŐ]/g, 'O'],
[/[ÙÚÛŨŪŬŮŰŲ]/g, 'U'],
[/[ÝŶŸ]/g, 'Y'],
[/[Þñþńņň]/g, 'n'],
[/[ߌŜŞŠ]/g, 'S'],
[/[àáâãåāăąĸ]/g, 'a'],
[/[æ]/g, 'ae'],
[/[çćĉċč]/g, 'c'],
[/[èéêëēĕėęě]/g, 'e'],
[/[ìíîïĩīĭį]/g, 'i'],
[/[òóôõöøōŏő]/g, 'o'],
[/[ùúûũūŭůűų]/g, 'u'],
[/[ýÿŷ]/g, 'y'],
[/[ĎĐ]/g, 'D'],
[/[ďđ]/g, 'd'],
[/[ĜĞĠĢ]/g, 'G'],
[/[ĝğġģŊŋſ]/g, 'g'],
[/[ĤĦ]/g, 'H'],
[/[ıśŝşš]/g, 's'],
[/[IJ]/g, 'IJ'],
[/[ij]/g, 'ij'],
[/[Ĵ]/g, 'J'],
[/[ĵ]/g, 'j'],
[/[Ķ]/g, 'K'],
[/[ķ]/g, 'k'],
[/[ĹĻĽĿŁ]/g, 'L'],
[/[ĺļľŀł]/g, 'l'],
[/[Œ]/g, 'OE'],
[/[œ]/g, 'oe'],
[/[ŔŖŘ]/g, 'R'],
[/[ŕŗř]/g, 'r'],
[/[ŢŤŦ]/g, 'T'],
[/[ţťŧ]/g, 't'],
[/[Ŵ]/g, 'W'],
[/[ŵ]/g, 'w'],
[/[ŹŻŽ]/g, 'Z'],
[/[źżž]/g, 'z'],
[/ö/g, 'oe'],
[/ü/g, 'ue'],
[/ä/g, 'ae'],
[/Ö/g, 'Oe'],
[/Ü/g, 'Ue'],
[/Ä/g, 'Ae'],
];
/**
* Converts each non-ascii character in a string to
* it's ascii equivalent (if defined).
*
* e.g. "Dĭd söméònê äšk fœŕ Ůnĭċődę?" => "Did someone aesk foer Unicode?"
*
* @param {String} string
* @returns {String}
*/
export const convertUnicodeToAscii = string => {
for (let [regex, replacer] of unicodeConversion) {
string = string.replace(regex, replacer);
}
return string;
};
/** /**
* Splits camelCase or PascalCase words * Splits camelCase or PascalCase words
* e.g. HelloWorld => Hello World * e.g. HelloWorld => Hello World
......
import $ from 'jquery'; import $ from 'jquery';
import DEFAULT_PROJECT_TEMPLATES from 'ee_else_ce/projects/default_project_templates'; import DEFAULT_PROJECT_TEMPLATES from 'ee_else_ce/projects/default_project_templates';
import { addSelectOnFocusBehaviour } from '../lib/utils/common_utils'; import { addSelectOnFocusBehaviour } from '../lib/utils/common_utils';
import { convertToTitleCase, humanize, slugify } from '../lib/utils/text_utility'; import {
convertToTitleCase,
humanize,
slugify,
convertUnicodeToAscii,
} from '../lib/utils/text_utility';
let hasUserDefinedProjectPath = false; let hasUserDefinedProjectPath = false;
let hasUserDefinedProjectName = false; let hasUserDefinedProjectName = false;
const onProjectNameChange = ($projectNameInput, $projectPathInput) => { const onProjectNameChange = ($projectNameInput, $projectPathInput) => {
const slug = slugify($projectNameInput.val()); const slug = slugify(convertUnicodeToAscii($projectNameInput.val()));
$projectPathInput.val(slug); $projectPathInput.val(slug);
}; };
......
...@@ -205,6 +205,26 @@ describe('text_utility', () => { ...@@ -205,6 +205,26 @@ describe('text_utility', () => {
}); });
}); });
describe('convertUnicodeToAscii', () => {
it('does nothing on an empty string', () => {
expect(textUtils.convertUnicodeToAscii('')).toBe('');
});
it('does nothing on an already ascii string', () => {
expect(textUtils.convertUnicodeToAscii('The quick brown fox jumps over the lazy dog.')).toBe(
'The quick brown fox jumps over the lazy dog.',
);
});
it('replaces Unicode characters', () => {
expect(textUtils.convertUnicodeToAscii('Dĭd söméònê äšk fœŕ Ůnĭċődę?')).toBe(
'Did someone aesk foer Unicode?',
);
expect(textUtils.convertUnicodeToAscii("Jürgen's Projekt")).toBe("Juergen's Projekt");
});
});
describe('splitCamelCase', () => { describe('splitCamelCase', () => {
it('separates a PascalCase word to two', () => { it('separates a PascalCase word to two', () => {
expect(textUtils.splitCamelCase('HelloWorld')).toBe('Hello World'); expect(textUtils.splitCamelCase('HelloWorld')).toBe('Hello World');
......
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