From 8fcd23e684bb8cf30c78a6e9f388a73b210f52eb Mon Sep 17 00:00:00 2001 From: StevanFreeborn Date: Tue, 31 May 2022 22:58:51 -0500 Subject: [PATCH] implement Translator.translateBritish(string) method --- components/translator.js | 90 ++++++++++++++++++++++++++++++++++++++++ tests/1_unit-tests.js | 4 +- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/components/translator.js b/components/translator.js index b63b030..e14bfa6 100644 --- a/components/translator.js +++ b/components/translator.js @@ -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 = `${britishOnly[key]}`; + 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 = `${britishToAmericanSpelling[key]}` + 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 = `${title}`; + 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 = `${time}`; + + string = string.replace(timeRegex, replace); + + } + + return string; } diff --git a/tests/1_unit-tests.js b/tests/1_unit-tests.js index 3b19a46..36c885f 100644 --- a/tests/1_unit-tests.js +++ b/tests/1_unit-tests.js @@ -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 soccer match for a while.'); + assert.equal(result, 'We watched the soccer 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 soccer match for a while.'); + assert.equal(result, 'We watched the soccer match for a while.'); done(); });