All files / src/modes number.mode.js

96.77% Statements 60/62
71.42% Branches 5/7
100% Functions 4/4
96.77% Lines 60/62

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 631x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 4x 4x 4x 10x 10x 4x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x   1x 1x 1x  
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))