1 var VersionCheck = require('./version-check')
2 var Regex = require('./regex')
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.
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.
26 exports.ALPHANUMERIC = {
33 * In byte mode, data is encoded at 8 bits per character.
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.
59 * Mixed mode will contain a sequences of data in a combination of any of
60 * the modes described above
69 * Returns the number of bits needed to store the data length
70 * according to QR Code specifications.
72 * @param {Mode} mode Data mode
73 * @param {Number} version QR Code version
74 * @return {Number} Number of bits
76 exports.getCharCountIndicator = function getCharCountIndicator (mode, version) {
77 if (!mode.ccBits) throw new Error('Invalid mode: ' + mode)
79 if (!VersionCheck.isValid(version)) {
80 throw new Error('Invalid version: ' + version)
83 if (version >= 1 && version < 10) return mode.ccBits[0]
84 else if (version < 27) return mode.ccBits[1]
89 * Returns the most efficient mode to store the specified data
91 * @param {String} dataStr Input data string
92 * @return {Mode} Best mode
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
102 * Return mode name as string
104 * @param {Mode} mode Mode object
105 * @returns {String} Mode name
107 exports.toString = function toString (mode) {
108 if (mode && mode.id) return mode.id
109 throw new Error('Invalid mode')
113 * Check if input param is a valid mode object
115 * @param {Mode} mode Mode object
116 * @returns {Boolean} True if valid mode, false otherwise
118 exports.isValid = function isValid (mode) {
119 return mode && mode.bit && mode.ccBits
123 * Get mode object from its name
125 * @param {String} string Mode name
126 * @returns {Mode} Mode object
128 function fromString (string) {
129 if (typeof string !== 'string') {
130 throw new Error('Param is not a string')
133 var lcStr = string.toLowerCase()
137 return exports.NUMERIC
139 return exports.ALPHANUMERIC
145 throw new Error('Unknown mode: ' + string)
150 * Returns mode from a value.
151 * If value is not a valid mode, returns defaultValue
153 * @param {Mode|String} value Encoding mode
154 * @param {Mode} defaultValue Fallback value
155 * @return {Mode} Encoding mode
157 exports.from = function from (value, defaultValue) {
158 if (exports.isValid(value)) {
163 return fromString(value)