import { Level } from '../level' ; import { ecCodewordsPerVersion } from './ecCodewordsPerVersion' ; import { createGeneratorPolinomial } from './generatorPolinomial' ; import { groupCodewordsInBlocks } from './groupCodewordsInBlocks' ; import { Polinomial } from './Polinomial' ; import { splitIntoCodewords } from './splitIntoCodewords' ; export const errorCorrection = ( version , level , data ) => { const { ecCodewordsPerBlock , groups } = ecCodewordsPerVersion [ `${ version }${ level }` ] ; const codewords = splitIntoCodewords ( data ) ; const blocks = groupCodewordsInBlocks ( codewords , groups ) ; const generator = createGeneratorPolinomial ( ecCodewordsPerBlock ) ; const polies = blocks . map ( codewords => Polinomial . fromCoef ( codewords . slice ( ) . reverse ( ) ) ) . map ( poly => poly . multiply ( Polinomial . fromCoefExponents ( { [ generator . greaterPower ( ) ] : 0 } ) ) ) ; const ecCodewords = polies . map ( poly => poly . modulo ( generator ) ) . map ( poly => poly . toCoef ( ) ) . map ( codewords => codewords . reverse ( ) ) ; return blocks . map ( ( block , index ) => ( { codewords : block , errorCorrection : ecCodewords [ index ] } ) ) ; } ;