[SignalingServer] Optimize dependent modules
[platform/framework/web/wrtjs.git] / device_home / node_modules / qrcode / lib / core / alphanumeric-data.js
1 var Mode = require('./mode')
2
3 /**
4  * Array of characters available in alphanumeric mode
5  *
6  * As per QR Code specification, to each character
7  * is assigned a value from 0 to 44 which in this case coincides
8  * with the array index
9  *
10  * @type {Array}
11  */
12 var ALPHA_NUM_CHARS = [
13   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
14   'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
15   'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
16   ' ', '$', '%', '*', '+', '-', '.', '/', ':'
17 ]
18
19 function AlphanumericData (data) {
20   this.mode = Mode.ALPHANUMERIC
21   this.data = data
22 }
23
24 AlphanumericData.getBitsLength = function getBitsLength (length) {
25   return 11 * Math.floor(length / 2) + 6 * (length % 2)
26 }
27
28 AlphanumericData.prototype.getLength = function getLength () {
29   return this.data.length
30 }
31
32 AlphanumericData.prototype.getBitsLength = function getBitsLength () {
33   return AlphanumericData.getBitsLength(this.data.length)
34 }
35
36 AlphanumericData.prototype.write = function write (bitBuffer) {
37   var i
38
39   // Input data characters are divided into groups of two characters
40   // and encoded as 11-bit binary codes.
41   for (i = 0; i + 2 <= this.data.length; i += 2) {
42     // The character value of the first character is multiplied by 45
43     var value = ALPHA_NUM_CHARS.indexOf(this.data[i]) * 45
44
45     // The character value of the second digit is added to the product
46     value += ALPHA_NUM_CHARS.indexOf(this.data[i + 1])
47
48     // The sum is then stored as 11-bit binary number
49     bitBuffer.put(value, 11)
50   }
51
52   // If the number of input data characters is not a multiple of two,
53   // the character value of the final character is encoded as a 6-bit binary number.
54   if (this.data.length % 2) {
55     bitBuffer.put(ALPHA_NUM_CHARS.indexOf(this.data[i]), 6)
56   }
57 }
58
59 module.exports = AlphanumericData