All files / src/error-correction ec-polynomial.js

100% Statements 26/26
100% Branches 3/3
100% Functions 1/1
100% Lines 26/26

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 271x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 98x 69x 69x 29x 29x 29x 29x 29x 29x 29x 1x  
import { QrPolynomial } from '../utils/qr-polynomial'
import { gexp } from '../utils/qr-math.util'
 
/**
 * memoize getErrorCorrectPolynomial as it is called multiple times when generating QR Code
 */
const memoECPolynomials = [
  QrPolynomial([1], 0),
]
 
/**
 * @param {number} errorCorrectLength - error correction codeword count
 * @returns {ReturnType<QrPolynomial>} error correction polynomial
 */
export function getErrorCorrectPolynomial (errorCorrectLength) {
  if (memoECPolynomials.length > errorCorrectLength) {
    return memoECPolynomials[errorCorrectLength]
  }
  const lastIndex = memoECPolynomials.length - 1
  let polynomial = memoECPolynomials[lastIndex]
  for (let i = lastIndex; i < errorCorrectLength; i += 1) {
    polynomial = polynomial.multiply(QrPolynomial([1, gexp(i)], 0))
    memoECPolynomials.push(polynomial)
  }
  return polynomial
};