Calculate EAN & UPC-A check digit using the Javascript step

Abstract: Neither the Add a checksum nor the Data Validator step provide calculation or check for EAN / UPC checkdigit.
Authors: Ludwig Weiss
License: LGPL
Kettle version: Kettle 3.2.5 and up (and maybe also a bit down?)

  1. In a Javascript step create a new Script page and mark it as Start script
  2. paste the following code into this page
    // String reverse
    String.prototype.reverse =
    function()
    {
    	splitext = this.split("");
    	revertext = splitext.reverse();
    	reversed = revertext.join("");
    	return reversed;
    }
    
    // function to calculate EAN / UPC checkdigit
    function eanCheckDigit(s)
    {
    	var result = 0;
    	var rs = s.reverse();
    	for (counter = 0; counter < rs.length; counter++)
    	{
    		result = result + parseInt(rs.charAt(counter)) * Math.pow(3, ((counter+1) % 2));
    	}
    	return (10 - (result % 10)) % 10;
    }
    
  3. use the function in the transform script like
    var cd = eanCheckDigit(sUPC);