test/browser/validid.umd.js
/**
  Validate checksum for TWID and TWRC.
  @module utils/is-twid-checksum-valid
  @param {string} id - The whole ID including checksum.
  @param {string} letterNum - Manually put how many letter(s) in the ID: Either 1 or 2.
  @return {boolean}
  */
Validate checksum for TWID and TWRC.
Validate checksum for TWID and TWRC .
function isTwidChecksumValid (id, letterNum) {
    var char, i, idLen, idLetters, idNumbers, len, letterIndex, letters, remainder, weight, weightedSum;
    idLetters = id.slice(0, letterNum);
    idNumbers = id.slice(letterNum); 
    idLen = 10; 
    letters = 'ABCDEFGHJKLMNPQRSTUVXYWZIO'; 
    letterIndex = letters.indexOf(idLetters[0]);
    weightedSum = Math.floor(letterIndex / 10 + 1) + letterIndex * (idLen - 1);
    if (letterNum === 2) {
      weightedSum += letters.indexOf(idLetters[1]) * (idLen - 2);
    } 
    weight = idLen - idLetters.length - 1; 
    for (i = 0, len = idNumbers.length; i < len; i++) {
      char = idNumbers[i];
      weightedSum += +char * weight;
      weight--;
    } 
    remainder = (weightedSum + +idNumbers.slice(-1)) % 10;
    return remainder === 0;
  }
function isTwidChecksumValid ( id , letterNum ) { var char , i , idLen , idLetters , idNumbers , len , letterIndex , letters , remainder , weight , weightedSum ; idLetters = id . slice ( 0 , letterNum ) ; idNumbers = id . slice ( letterNum ) ; idLen = 10 ; letters = 'ABCDEFGHJKLMNPQRSTUVXYWZIO' ; letterIndex = letters . indexOf ( idLetters [ 0 ] ) ; weightedSum = Math . floor ( letterIndex / 10 + 1 ) + letterIndex * ( idLen - 1 ) ; if ( letterNum === 2 ) { weightedSum += letters . indexOf ( idLetters [ 1 ] ) * ( idLen - 2 ) ; } weight = idLen - idLetters . length - 1 ; for ( i = 0 , len = idNumbers . length ; i < len ; i ++ ) { char = idNumbers [ i ] ; weightedSum += + char * weight ; weight -- ; } remainder = ( weightedSum + + idNumbers . slice ( - 1 ) ) % 10 ; return remainder === 0 ; }
Validate checksum for TWID and TWRC.
Validate checksum for TWID and TWRC .
// ID in format of 'AA0000000C' or 'A00000000C' // fixed // Each letter represents a value from [10..35] // weightedSum for idLetters // weightedSum for idNumbers // Minus letter digit and check digit // Note: the check digit of 'idNumbers' is weighted 0