node-qrcode-lite/lib/core/utils.js
Vincenzo Greco 77064f5e5e Refactor/core ()
* Split core lib into multiple files

* Refactor data encoding methods

* Refactor data masking process

* Improve qr code generation process

* Increase minimum required node version to 0.10

* Add linter

* Add tests and tests coverage

* Update travis config to fix compilation issues

* Add examples folder

* Add missing license tag in package.json

* Update build script and add sourcemap support

* Publish only strictly needed files on npm

* Update readme
2016-12-16 23:45:08 +01:00

46 lines
1.2 KiB
JavaScript

var CODEWORDS_COUNT = [
0, // Not used
26, 44, 70, 100, 134, 172, 196, 242, 292, 346,
404, 466, 532, 581, 655, 733, 815, 901, 991, 1085,
1156, 1258, 1364, 1474, 1588, 1706, 1828, 1921, 2051, 2185,
2323, 2465, 2611, 2761, 2876, 3034, 3196, 3362, 3532, 3706
]
/**
* Returns the QR Code size for the specified version
*
* @param {Number} version QR Code version
* @return {Number} size of QR code
*/
exports.getSymbolSize = function getSymbolSize (version) {
if (!version) throw new Error('"version" cannot be null or undefined')
if (version < 1 || version > 40) throw new Error('"version" should be in range from 1 to 40')
return version * 4 + 17
}
/**
* Returns the total number of codewords used to store data and EC information.
*
* @param {Number} version QR Code version
* @return {Number} Data length in bits
*/
exports.getSymbolTotalCodewords = function getSymbolTotalCodewords (version) {
return CODEWORDS_COUNT[version]
}
/**
* Encode data with Bose-Chaudhuri-Hocquenghem
*
* @param {Number} data Value to encode
* @return {Number} Encoded value
*/
exports.getBCHDigit = function (data) {
var digit = 0
while (data !== 0) {
digit++
data >>>= 1
}
return digit
}