Improve error handling

pull/3/head
Amio 2018-07-18 13:59:40 +08:00
rodzic cdc622ed75
commit d371712afa
2 zmienionych plików z 18 dodań i 11 usunięć

Wyświetl plik

@ -4,9 +4,8 @@ function calcWidth (charWidthTable) {
const SCALE = 10
const widthTable = charWidthTable.map(w => Math.round(w * SCALE))
return function (text) {
if (typeof text !== 'string') {
return 0
} else {
typeAssert(typeof text === 'string', 'Input must be string')
let total = 0
let i = text.length
let charCode = 0
@ -17,6 +16,9 @@ function calcWidth (charWidthTable) {
return total / SCALE
}
}
const typeAssert = (assertion, message) => {
if (!assertion) throw new TypeError(message)
}
module.exports = {

Wyświetl plik

@ -7,3 +7,8 @@ tap.test('calc width for "npm"', t => {
t.is(calcWidth('npm'), 24.9, 'result is correct value')
t.end()
})
tap.test('exception handling', t => {
t.throws(() => calcWidth(0), TypeError, 'throw if feed with non-string input')
t.end()
})