Skip to content

Fixed inconsistency with test suite #6

Merged
merged 2 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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