diff --git a/components/translator.js b/components/translator.js index 75f7ccb..b63b030 100644 --- a/components/translator.js +++ b/components/translator.js @@ -5,6 +5,83 @@ const britishOnly = require('./british-only.js') class Translator { + translateAmerican(string) { + + const americanOnlyKeys = Object.keys(americanOnly).sort((a, b) => b.length - a.length); + const americanToBritishSpellingKeys = Object.keys(americanToBritishSpelling).sort((a, b) => b.length - a.length); + const americanToBritishTitlesKeys = Object.keys(americanToBritishTitles).sort((a, b) => b.length - a.length); + + americanOnlyKeys.forEach(key => { + + const regex = new RegExp(key, 'i'); + + if (string.match(regex)) { + + const replace = `${americanOnly[key]}`; + + string = string.replace(regex, replace); + + } + + }); + + americanToBritishSpellingKeys.forEach(key => { + + const regex = new RegExp(key, 'i'); + + if (string.match(regex)) { + + const replace = `${americanToBritishSpelling[key]}` + + string = string.replace(regex, replace); + + } + + }); + + americanToBritishTitlesKeys.forEach(key => { + + const regex = new RegExp(key, 'i'); + + if (string.match(regex)) { + + const value = americanToBritishTitles[key]; + + const firstLetter = value.slice(0,1).toUpperCase(); + const rest = value.slice(1, value.length); + + const title = firstLetter + rest; + + const replace = `${title}`; + + string = string.replace(regex, 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; + + } + + translateBritish(string) { + + + + } + } module.exports = Translator; \ No newline at end of file diff --git a/tests/1_unit-tests.js b/tests/1_unit-tests.js index aefa529..3b19a46 100644 --- a/tests/1_unit-tests.js +++ b/tests/1_unit-tests.js @@ -2,12 +2,256 @@ const chai = require('chai'); const assert = chai.assert; const Translator = require('../components/translator.js'); +const translator = new Translator(); suite('Unit Tests', () => { test('Translate { Mangoes are my favorite fruit. } to British English', done => { - assert.fail(); + const string = 'Mangoes are my favorite fruit.'; + const result = translator.translateAmerican(string); + + assert.equal(result, 'Mangoes are my favourite fruit.'); + done(); + + }); + + test('Translate { I ate yogurt for breakfast. } to British English', done => { + + const string = 'I ate yogurt for breakfast.'; + const result = translator.translateAmerican(string); + + assert.equal(result, 'I ate yoghurt for breakfast.'); + done(); + + }); + + test('Translate { We had a party at my friend\'s condo. } to British English', done => { + + const string = 'We had a party at my friend\'s condo.'; + const result = translator.translateAmerican(string); + + assert.equal(result, 'We had a party at my friend\'s flat.'); + done(); + + }); + + test('Translate { Can you toss this in the trashcan for me? } to British English', done => { + + const string = 'Can you toss this in the trashcan for me?'; + const result = translator.translateAmerican(string); + + assert.equal(result, 'Can you toss this in the bin for me?'); + done(); + + }); + + test('Translate { The parking lot was full. } to British English', done => { + + const string = 'The parking lot was full.'; + const result = translator.translateAmerican(string); + + assert.equal(result, 'The car park was full.'); + done(); + + }); + + test('Translate { Like a high tech Rube Goldberg machine. } to British English', done => { + + const string = 'Like a high tech Rube Goldberg machine.'; + const result = translator.translateAmerican(string); + + assert.equal(result, 'Like a high tech Heath Robinson device.'); + done(); + + }); + + test('Translate { To play hooky means to skip class or work. } to British English', done => { + + const string = 'To play hooky means to skip class or work.'; + const result = translator.translateAmerican(string); + + assert.equal(result, 'To bunk off means to skip class or work.'); + done(); + + }); + + test('Translate { No Mr. Bond, I expect you to die. } to British English', done => { + + const string = 'No Mr. Bond, I expect you to die.'; + const result = translator.translateAmerican(string); + + assert.equal(result, 'No Mr Bond, I expect you to die.'); + done(); + + }); + + test('Translate { Dr. Grosh will see you now. } to British English', done => { + + const string = 'Dr. Grosh will see you now.'; + const result = translator.translateAmerican(string); + + assert.equal(result, 'Dr Grosh will see you now.'); + done(); + + }); + + test('Translate { Lunch is at 12:15 today. } to British English', done => { + + const string = 'Lunch is at 12:15 today.'; + const result = translator.translateAmerican(string); + + assert.equal(result, 'Lunch is at 12.15 today.'); + done(); + + }); + + test('Translate { We watched the footie match for a while. } to American English', done => { + + 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.'); + done(); + + }); + + test('Translate { Paracetamol takes up to an hour to work. } to American English', done => { + + const string = 'Paracetamol takes up to an hour to work.'; + const result = translator.translateBritish(string); + + assert.equal(result, 'Tylenol takes up to an hour to work.'); + done(); + + }); + + test('Translate { First, caramelise the onions. } to American English', done => { + + const string = 'First, caramelise the onions.'; + const result = translator.translateBritish(string); + + assert.equal(result, 'First, caramelize the onions.'); + done(); + + }); + + test('Translate { I spent the bank holiday at the funfair. } to American English', done => { + + const string = 'I spent the bank holiday at the funfair.'; + const result = translator.translateBritish(string); + + assert.equal(result, 'I spent the public holiday at the carnival.'); + done(); + + }); + + test('Translate { I had a bicky then went to the chippy. } to American English', done => { + + const string = 'I had a bicky then went to the chippy.'; + const result = translator.translateBritish(string); + + assert.equal(result, 'I had a cookie then went to the fish-and-chip shop.'); + done(); + + }); + + test('Translate { I\'ve just got bits and bobs in my bum bag. } to American English', done => { + + const string = 'I\'ve just got bits and bobs in my bum bag.'; + const result = translator.translateBritish(string); + + assert.equal(result, 'I\'ve just got odds and ends in my fanny pack.'); + done(); + + }); + + test('Translate { The car boot sale at Boxted Airfield was called off. } to American English', done => { + + const string = 'The car boot sale at Boxted Airfield was called off.'; + const result = translator.translateBritish(string); + + assert.equal(result, 'The swap meet at Boxted Airfield was called off.'); + done(); + + }); + + test('Translate { Have you met Mrs Kalyani? } to American English', done => { + + const string = 'Have you met Mrs Kalyani?'; + const result = translator.translateBritish(string); + + assert.equal(result, 'Have you met Mrs. Kalyani?'); + done(); + + }); + + test('Translate { Prof Joyner of King\'s College, London. } to American English', done => { + + const string = 'Prof Joyner of King\'s College, London.'; + const result = translator.translateBritish(string); + + assert.equal(result, 'Prof. Joyner of King\'s College, London.'); + done(); + + }); + + test('Translate { Tea time is usually around 4 or 4.30. } to American English', done => { + + const string = 'Tea time is usually around 4 or 4.30.'; + const result = translator.translateBritish(string); + + assert.equal(result, 'Tea time is usually around 4 or 4:30.'); + done(); + + }); + + test('Translate { Tea time is usually around 4 or 4.30. } to American English', done => { + + const string = 'Tea time is usually around 4 or 4.30.'; + const result = translator.translateBritish(string); + + assert.equal(result, 'Tea time is usually around 4 or 4:30.'); + done(); + + }); + + test('Highlight differences { Mangoes are my favorite fruit. } when translated to British English', done => { + + const string = 'Mangoes are my favorite fruit.'; + const result = translator.translateAmerican(string); + + assert.equal(result, 'Mangoes are my favourite fruit.'); + done(); + + }); + + test('Highlight differences { I ate yogurt for breakfast. } when translated to British English', done => { + + const string = 'I ate yogurt for breakfast.'; + const result = translator.translateAmerican(string); + + assert.equal(result, 'I ate yoghurt for breakfast.'); + done(); + + }); + + test('Highlight differences { We watched the footie match for a while. } when translated to American English', done => { + + 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.'); + done(); + + }); + + test('Highlight differences { Paracetamol takes up to an hour to work. } when translated to American English', done => { + + const string = 'Paracetamol takes up to an hour to work.'; + const result = translator.translateBritish(string); + + assert.equal(result, 'Tylenol takes up to an hour to work.'); done(); });