From 1b2d8602a206eb5b87a3299cf192e0704f01e087 Mon Sep 17 00:00:00 2001 From: zhabinka Date: Mon, 21 Jan 2019 01:03:39 +0300 Subject: [PATCH] Added RNA transcription --- .../rna-transcription/rna-transcription.js | 18 +++++++++ .../rna-transcription.spec.js | 40 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 javascript/rna-transcription/rna-transcription.js create mode 100644 javascript/rna-transcription/rna-transcription.spec.js diff --git a/javascript/rna-transcription/rna-transcription.js b/javascript/rna-transcription/rna-transcription.js new file mode 100644 index 0000000..e0d7e99 --- /dev/null +++ b/javascript/rna-transcription/rna-transcription.js @@ -0,0 +1,18 @@ +const changer = (char) => { + switch (char) { + case 'G': + return 'C'; + case 'C': + return 'G'; + case 'T': + return 'A'; + case 'A': + return 'U'; + default: + throw new Error('Invalid input DNA.'); + } +}; + +const toRna = dna => dna.split('').map(changer).join(''); + +export { toRna }; diff --git a/javascript/rna-transcription/rna-transcription.spec.js b/javascript/rna-transcription/rna-transcription.spec.js new file mode 100644 index 0000000..2a5b8b9 --- /dev/null +++ b/javascript/rna-transcription/rna-transcription.spec.js @@ -0,0 +1,40 @@ +import { toRna } from './rna-transcription'; + +describe('Transcriptor', () => { + test('empty rna sequence', () => { + expect(toRna('')).toEqual(''); + }); + + test('transcribes cytosine to guanine', () => { + expect(toRna('C')).toEqual('G'); + }); + + test('transcribes guanine to cytosine', () => { + expect(toRna('G')).toEqual('C'); + }); + + test('transcribes adenine to uracil', () => { + expect(toRna('A')).toEqual('U'); + }); + + test('transcribes thymine to adenine', () => { + expect(toRna('T')).toEqual('A'); + }); + + test('transcribes all dna nucleotides to their rna complements', () => { + expect(toRna('ACGTGGTCTTAA')) + .toEqual('UGCACCAGAAUU'); + }); + + test('correctly handles invalid input', () => { + expect(() => toRna('U')).toThrow(new Error('Invalid input DNA.')); + }); + + test('correctly handles completely invalid input', () => { + expect(() => toRna('XXX')).toThrow(new Error('Invalid input DNA.')); + }); + + test('correctly handles partially invalid input', () => { + expect(() => toRna('ACGTXXXCTTAA')).toThrow(new Error('Invalid input DNA.')); + }); +});