Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | 7x 7x 7x 7x 7x 7x 7x 7x 38x 38x 38x 38x 38x 7x 7x 7x 7x 7x 7x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 6x 8x 8x 8x 8x 8x 7x 7x 7x 7x 7x 10x 10x 10x 16x 16x 10x 7x 7x 7x 7x 7x 7x 7x 16x 16x 16x 16x 6x 7x 7x 7x | import { MODE_NUMBER } from './mode-bits.constants.js'
/**
* Create QR code numeric mode object
* @param {string} data - data of mode object
* @returns {import('./mode-bits.constants.js').ModeObject} created mode object
*/
export const QrNumber = (data) => Object.freeze({
data,
mode: MODE_NUMBER,
length: data.length,
write: writeDataToBitBuffer.bind(null, data),
})
/**
* Writes numeric data to bit buffer that will be used to generate the QR code
* @param {string} data - QrNumber mode object data
* @param {import("./../utils/qr-bit-buffer.js").QrBitBuffer} buffer - target bit buffer
*/
function writeDataToBitBuffer (data, buffer) {
let i = 0
const { length } = data
while (i + 2 < length) {
buffer.put(strToNum(data.slice(i, i + 3)), 10)
i += 3
}
if (i < length) {
if (length - i === 1) {
buffer.put(strToNum(data.slice(i, i + 1)), 4)
} else if (length - i === 2) {
buffer.put(strToNum(data.slice(i, i + 2)), 7)
}
}
}
/**
* @param {string} s - target string
* @returns {number} `s` as number
*/
function strToNum (s) {
let num = 0
for (let i = 0, e = s.length; i < e; i++) {
num = num * 10 + charToNum(s.charAt(i))
}
return num
};
/**
*
* @param {string} c - target character
* @returns {number} `c` as number
*/
function charToNum (c) {
if (c >= '0' && c <= '9') {
return /** @type {number} */(c.codePointAt(0)) - zeroCodePoint
}
throw new Error(`illegal char: ${c}`)
};
const zeroCodePoint = /** @type {number} */('0'.codePointAt(0))
|