kopia lustrzana https://github.com/badgen/badgen
Improve error handling
rodzic
cdc622ed75
commit
d371712afa
|
@ -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 = {
|
||||
|
|
|
@ -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()
|
||||
})
|
||||
|
|
Ładowanie…
Reference in New Issue