[SignalingServer] Optimize dependent modules
[platform/framework/web/wrtjs.git] / device_home / node_modules / qrcode / lib / core / mode.js
1 var VersionCheck = require('./version-check')
2 var Regex = require('./regex')
3
4 /**
5  * Numeric mode encodes data from the decimal digit set (0 - 9)
6  * (byte values 30HEX to 39HEX).
7  * Normally, 3 data characters are represented by 10 bits.
8  *
9  * @type {Object}
10  */
11 exports.NUMERIC = {
12   id: 'Numeric',
13   bit: 1 << 0,
14   ccBits: [10, 12, 14]
15 }
16
17 /**
18  * Alphanumeric mode encodes data from a set of 45 characters,
19  * i.e. 10 numeric digits (0 - 9),
20  *      26 alphabetic characters (A - Z),
21  *   and 9 symbols (SP, $, %, *, +, -, ., /, :).
22  * Normally, two input characters are represented by 11 bits.
23  *
24  * @type {Object}
25  */
26 exports.ALPHANUMERIC = {
27   id: 'Alphanumeric',
28   bit: 1 << 1,
29   ccBits: [9, 11, 13]
30 }
31
32 /**
33  * In byte mode, data is encoded at 8 bits per character.
34  *
35  * @type {Object}
36  */
37 exports.BYTE = {
38   id: 'Byte',
39   bit: 1 << 2,
40   ccBits: [8, 16, 16]
41 }
42
43 /**
44  * The Kanji mode efficiently encodes Kanji characters in accordance with
45  * the Shift JIS system based on JIS X 0208.
46  * The Shift JIS values are shifted from the JIS X 0208 values.
47  * JIS X 0208 gives details of the shift coded representation.
48  * Each two-byte character value is compacted to a 13-bit binary codeword.
49  *
50  * @type {Object}
51  */
52 exports.KANJI = {
53   id: 'Kanji',
54   bit: 1 << 3,
55   ccBits: [8, 10, 12]
56 }
57
58 /**
59  * Mixed mode will contain a sequences of data in a combination of any of
60  * the modes described above
61  *
62  * @type {Object}
63  */
64 exports.MIXED = {
65   bit: -1
66 }
67
68 /**
69  * Returns the number of bits needed to store the data length
70  * according to QR Code specifications.
71  *
72  * @param  {Mode}   mode    Data mode
73  * @param  {Number} version QR Code version
74  * @return {Number}         Number of bits
75  */
76 exports.getCharCountIndicator = function getCharCountIndicator (mode, version) {
77   if (!mode.ccBits) throw new Error('Invalid mode: ' + mode)
78
79   if (!VersionCheck.isValid(version)) {
80     throw new Error('Invalid version: ' + version)
81   }
82
83   if (version >= 1 && version < 10) return mode.ccBits[0]
84   else if (version < 27) return mode.ccBits[1]
85   return mode.ccBits[2]
86 }
87
88 /**
89  * Returns the most efficient mode to store the specified data
90  *
91  * @param  {String} dataStr Input data string
92  * @return {Mode}           Best mode
93  */
94 exports.getBestModeForData = function getBestModeForData (dataStr) {
95   if (Regex.testNumeric(dataStr)) return exports.NUMERIC
96   else if (Regex.testAlphanumeric(dataStr)) return exports.ALPHANUMERIC
97   else if (Regex.testKanji(dataStr)) return exports.KANJI
98   else return exports.BYTE
99 }
100
101 /**
102  * Return mode name as string
103  *
104  * @param {Mode} mode Mode object
105  * @returns {String}  Mode name
106  */
107 exports.toString = function toString (mode) {
108   if (mode && mode.id) return mode.id
109   throw new Error('Invalid mode')
110 }
111
112 /**
113  * Check if input param is a valid mode object
114  *
115  * @param   {Mode}    mode Mode object
116  * @returns {Boolean} True if valid mode, false otherwise
117  */
118 exports.isValid = function isValid (mode) {
119   return mode && mode.bit && mode.ccBits
120 }
121
122 /**
123  * Get mode object from its name
124  *
125  * @param   {String} string Mode name
126  * @returns {Mode}          Mode object
127  */
128 function fromString (string) {
129   if (typeof string !== 'string') {
130     throw new Error('Param is not a string')
131   }
132
133   var lcStr = string.toLowerCase()
134
135   switch (lcStr) {
136     case 'numeric':
137       return exports.NUMERIC
138     case 'alphanumeric':
139       return exports.ALPHANUMERIC
140     case 'kanji':
141       return exports.KANJI
142     case 'byte':
143       return exports.BYTE
144     default:
145       throw new Error('Unknown mode: ' + string)
146   }
147 }
148
149 /**
150  * Returns mode from a value.
151  * If value is not a valid mode, returns defaultValue
152  *
153  * @param  {Mode|String} value        Encoding mode
154  * @param  {Mode}        defaultValue Fallback value
155  * @return {Mode}                     Encoding mode
156  */
157 exports.from = function from (value, defaultValue) {
158   if (exports.isValid(value)) {
159     return value
160   }
161
162   try {
163     return fromString(value)
164   } catch (e) {
165     return defaultValue
166   }
167 }