node-qrcode-lite/lib/core/error-correction-level.js
2020-04-16 22:50:11 +01:00

50 lines
893 B
JavaScript

exports.L = { bit: 1 }
exports.M = { bit: 0 }
exports.Q = { bit: 3 }
exports.H = { bit: 2 }
function fromString (string) {
if (typeof string !== 'string') {
throw new Error('Param is not a string')
}
const lcStr = string.toLowerCase()
switch (lcStr) {
case 'l':
case 'low':
return exports.L
case 'm':
case 'medium':
return exports.M
case 'q':
case 'quartile':
return exports.Q
case 'h':
case 'high':
return exports.H
default:
throw new Error('Unknown EC Level: ' + string)
}
}
exports.isValid = function isValid (level) {
return level && typeof level.bit !== 'undefined' &&
level.bit >= 0 && level.bit < 4
}
exports.from = function from (value, defaultValue) {
if (exports.isValid(value)) {
return value
}
try {
return fromString(value)
} catch (e) {
return defaultValue
}
}