implement Translator.translateBritish(string) method

This commit is contained in:
StevanFreeborn
2022-05-31 22:58:51 -05:00
parent a7985d7e4b
commit 8fcd23e684
2 changed files with 92 additions and 2 deletions
+90
View File
@@ -78,7 +78,97 @@ class Translator {
translateBritish(string) {
const britishOnlyKeys = Object.keys(britishOnly).sort((a, b) => b.length - a.length);
const britishToAmericanSpelling = {};
Object.keys(americanToBritishSpelling).forEach(key => {
const value = americanToBritishSpelling[key];
britishToAmericanSpelling[value] = key;
});
const britishToAmericanSpellingKeys = Object.keys(britishToAmericanSpelling).sort((a, b) => b.length - a.length);
const britishToAmericanTitles = {};
Object.keys(americanToBritishTitles).forEach(key => {
const value = americanToBritishTitles[key];
britishToAmericanTitles[value] = key;
});
const britishToAmericanTitlesKeys = Object.keys(britishToAmericanTitles).sort((a, b) => b.length - a.length);
britishOnlyKeys.forEach(key => {
const regex = new RegExp(`\\s${key}\\.|${key}\\s`, 'i');
const match = string.match(regex);
if (match) {
const replace = `<span class="highlight">${britishOnly[key]}</span>`;
const text = match[0].trim().replace('.','');
string = string.replace(text, replace);
}
});
britishToAmericanSpellingKeys.forEach(key => {
const regex = new RegExp(`\\s${key}|${key}\\s`, 'i');
const match = string.match(regex);
if (match) {
const replace = `<span class="highlight">${britishToAmericanSpelling[key]}</span>`
const text = match[0].trim();
string = string.replace(text, replace);
}
});
britishToAmericanTitlesKeys.forEach(key => {
const regex = new RegExp(`\\s${key}|${key}\\s`, 'i');
const match = string.match(regex);
if (match) {
const value = britishToAmericanTitles[key];
const firstLetter = value.slice(0,1).toUpperCase();
const rest = value.slice(1, value.length);
const title = firstLetter + rest;
const replace = `<span class="highlight">${title}</span>`;
const text = match[0].trim();
string = string.replace(text, replace);
}
});
const timeRegex = /\d+.\d{2}/;
if (string.match(timeRegex)) {
const time = string.match(timeRegex)[0].replace('.', ':');
const replace = `<span class="highlight">${time}</span>`;
string = string.replace(timeRegex, replace);
}
return string;
}
+2 -2
View File
@@ -111,7 +111,7 @@ suite('Unit Tests', () => {
const string = 'We watched the footie match for a while.';
const result = translator.translateBritish(string);
assert.equal(result, 'We watched <span class="highlight">soccer</span> match for a while.');
assert.equal(result, 'We watched the <span class="highlight">soccer</span> match for a while.');
done();
});
@@ -241,7 +241,7 @@ suite('Unit Tests', () => {
const string = 'We watched the footie match for a while.';
const result = translator.translateBritish(string);
assert.equal(result, 'We watched <span class="highlight">soccer</span> match for a while.');
assert.equal(result, 'We watched the <span class="highlight">soccer</span> match for a while.');
done();
});