Skip to content

Commit

Permalink
Merge pull request #12 from momozor/patch-5.1
Browse files Browse the repository at this point in the history
Clarify imports and functions
  • Loading branch information
Max2408 authored May 24, 2021
2 parents 9f9eeb1 + 47ef123 commit 5b8ad93
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 40 deletions.
4 changes: 1 addition & 3 deletions Src/Controllers/Others.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ const Uselessweb = require('../../models/Uselessweb')
const stories = require('../../models/letsnotmeet')
const truthAndDare = require('../../models/truthordare')

let randomStr = require('../Utilities/functions').randomStr

let uuid = require('../Utilities/functions').uuid
const {randomStr, uuid} = require('../Utilities/functions')

exports.joke = async(req, res, next) => {
try{
Expand Down
11 changes: 3 additions & 8 deletions Src/Controllers/Text.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
let reverseString = require('../Utilities/functions').reverseString

let text2Binary = require('../Utilities/functions').text2Binary

let sarcasticConverter = require('../Utilities/functions').sarcasticConverter

let textToMorse = require('../Utilities/functions').textToMorse
let {reverseString, textToBinary, sarcasticConverter, textToMorse} =
require('../Utilities/functions')

exports.shuffle = async(req, res, next) => {
try{
Expand Down Expand Up @@ -74,7 +69,7 @@ exports.binary = async(req, res, next) => {
return next();
}

let binary = text2Binary(text)
let binary = textToBinary(text)


res.status(200).json({
Expand Down
49 changes: 21 additions & 28 deletions Src/Utilities/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ module.exports = {
uuid : function(){
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'

const randomNumber = function(container) {
const randomNumber = function(container){
return Math.floor(Math.random() * container.length)
}
const randomCharacter = function(length) {
const randomCharacter = function(length){
return characters.charAt(randomNumber(characters))
}

const makeKeyPart = function(length) {
const makeKeyPart = function(length){
return Array(length).fill().map(function() {
return randomCharacter(length)
}).join('')
Expand All @@ -26,7 +26,7 @@ module.exports = {
}
return result;
},
textToMorse : function(text) {
textToMorse : function(text){
const morseCodeDictionary = {
'a': '.-',
'b': '-...',
Expand Down Expand Up @@ -80,38 +80,31 @@ module.exports = {

const splittedText = text.toLowerCase().split(/(?!$)/)

return splittedText.map(function(code) {
return morseCodeDictionary[code];
return splittedText.map(function(char){
return morseCodeDictionary[char];
}).join(' ')

},
sarcasticConverter : function(text) {
let output = ""

for (var i = 0; i < text.length; i++) {
let char = text.charAt(i);

let possibleOutcomes = ['low' , 'high'];

let randomOutcomes = possibleOutcomes[Math.floor(Math.random() * possibleOutcomes.length)]

if(randomOutcomes === 'low'){
char = char.toLowerCase()
}else if(randomOutcomes === 'high'){
char = char.toUpperCase()
}

output += char
sarcasticConverter : function(text){
const possibleOutcomes = ['low', 'high']
const outcome = function() {
return possibleOutcomes[
Math.floor(Math.random() * possibleOutcomes.length)
]
}

return output

return text.split('').map(function(char) {
if (outcome() === 'low') {
return char.toLowerCase()
}
return char.toUpperCase()
}).join('')
},
text2Binary : function(string) {
textToBinary : function(string){
return string.split('').map(function (char) {
return char.charCodeAt(0).toString(2);
}).join(' ');
},
reverseString : function(string) {
reverseString : function(string){
return string.split('').reverse().join('')
}

Expand Down
12 changes: 11 additions & 1 deletion tests/Utilities/functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ test('textToMorse2', function(){
.toBe('.- -... -.-. -.. . ..-. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -.-- --.. ----- .---- ..--- ...-- ....- ..... -.... --... ---.. ----. .-.-.- --..-- ---... ..--.. .----. -....- -..-. -.--.- -.--. .-..-. .--.-. -...- /')
})

test('stringReverse', function() {
test('sarcasticConverter', function(){
expect(functions.sarcasticConverter('meow'))
.toMatch(/^\w+$/)
})

test('textToBinary', function(){
expect(functions.textToBinary('hello world 1 2 3 4 5 6 7 8 9'))
.toBe('1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100 100000 110001 100000 110010 100000 110011 100000 110100 100000 110101 100000 110110 100000 110111 100000 111000 100000 111001')
})

test('reverseString', function(){
expect(functions.reverseString('hello world'))
.toBe('dlrow olleh')
})

0 comments on commit 5b8ad93

Please sign in to comment.