All files / src/modes alphanum.mode.js

98.41% Statements 62/63
50% Branches 9/18
100% Functions 3/3
98.41% Lines 62/63

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 641x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 54x 54x 54x 54x 54x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 111x 111x 81x 111x 9x 30x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x   21x 21x 111x  
import { MODE_ALPHA_NUM } from './mode-bits.constants.js'
 
/**
 * Create QR code alphanumeric mode object
 * @param {string} data - data of mode object
 * @returns {import('./mode-bits.constants.js').ModeObject} created mode object
 */
export const QrAlphaNum = (data) => Object.freeze({
  data,
  mode: MODE_ALPHA_NUM,
  length: data.length,
  write: writeDataToBitBuffer.bind(null, data),
})
 
/**
 * Writes alphanumeric data to bit buffer that will be used to generate the QR code
 * @param {string} data - QrAlphaNum mode object data
 * @param {import("./../utils/qr-bit-buffer.js").QrBitBuffer} buffer - target bit buffer
 */
function writeDataToBitBuffer (data, buffer) {
  let i = 0
 
  while (i + 1 < data.length) {
    buffer.put(
      getCode(data.charAt(i)) * 45 +
          getCode(data.charAt(i + 1)), 11)
    i += 2
  }
 
  if (i < data.length) {
    buffer.put(getCode(data.charAt(i)), 6)
  }
}
 
const zeroCodePoint = /** @type {number} */('0'.codePointAt(0))
const capitalACodePoint = /** @type {number} */('A'.codePointAt(0))
 
/**
 * Get value for character `c`
 * @param {string} c - target character
 * @returns {number} character code point
 */
function getCode (c) {
  if (c >= '0' && c <= '9') {
    return /** @type {number} */(c.codePointAt(0)) - zeroCodePoint
  } else if (c >= 'A' && c <= 'Z') {
    return /** @type {number} */(c.codePointAt(0)) - capitalACodePoint + 10
  } else {
    switch (c) {
      case ' ' : return 36
      case '$' : return 37
      case '%' : return 38
      case '*' : return 39
      case '+' : return 40
      case '-' : return 41
      case '.' : return 42
      case '/' : return 43
      case ':' : return 44
      default :
        throw Error(`illegal char: ${c}`)
    }
  }
}