Skip to content

Commit

Permalink
Merge pull request web#6 from harjaus/master
Browse files Browse the repository at this point in the history
Fixed inconsistency with test suite
  • Loading branch information
aa7401 committed Sep 20, 2019
2 parents ffe6e8c + 660bcf1 commit 0d5f324
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
61 changes: 61 additions & 0 deletions exercises/06_code_quality/nestedCallbacks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

'use strict'

const request = require('request')

getInput('enter base currency', (err, base) => {
if (err) {
console.log(err.message)
process.exit()
}
base = base.trim()
checkValidCurrencyCode(base, err => {
if (err) {
console.log(err.message)
process.exit()
}
getData(`http://api.fixer.io/latest?base=${base}`, (err, data) => {
if (err) {
console.log(err.message)
process.exit()
}
const obj = JSON.parse(data)
printObject(obj)
process.exit()
})
})
})

function getInput(prompt, callback) {
try {
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdout.write(`${prompt}: `)
process.stdin.on('data', text => callback(null, text))
} catch(err) {
callback(err)
}
}

function checkValidCurrencyCode(code, callback) {
code = code.trim()
request('http://api.fixer.io/latest', (err, res, body) => {
if (err) callback(new Error('invalid API call'))
const rates = JSON.parse(body).rates
if (!rates.hasOwnProperty(code)) callback(new Error(`invalid currency code ${code}`))
callback(null, true)
})
}

function getData(url, callback) {
request(url, (err, res, body) => {
if (err) callback(new Error('invalid API call'))
callback(null, body)
})
}

function printObject(data) {
const indent = 2
const str = JSON.stringify(data, null, indent)
console.log(str)
}
2 changes: 1 addition & 1 deletion exercises/07_unit_testing/todo/modules/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports.clear = () => {

module.exports.add = (item, qty) => {
qty = Number(qty)
if(isNaN(qty)) throw new Error('the quantity must be a number')
if(isNaN(qty)) throw new Error('qty must be a number')
data.push({item: item, qty: qty})
}

Expand Down

0 comments on commit 0d5f324

Please sign in to comment.