1/** 2 * Helper class for barcode processing 3 */ 4(function (sellsite) { 5 sellsite.barcode = {}; 6 7 /** 8 * Processes the barcode data and returns the barcode string 9 * @param data - The barcode data 10 * @param useChecksum - Whether to use the checksum or not 11 * @returns {Promise<unknown>} - Promise that resolves with the barcode string 12 */ 13 sellsite.barcode.processBarcode = function(data, useChecksum) { 14 let code; 15 let isITF; 16 17 // if data contains barcode and type, we can directly use it... 18 if (data.barcode !== undefined) { 19 code = data.barcode; 20 isITF = data.type === 'i25'; 21 } else { 22 // if not, we need to parse the JSON (this happens if the barcode is read from native app) 23 const json = JSON.parse(data); 24 code = json.barcode; 25 isITF = json.type === "INTERLEAVED_2_OF_5" || json.type === "ITF"; 26 } 27 28 return new Promise(function (resolve, reject) { 29 if (isITF && useChecksum && code.length > 0) { 30 if (isChecksumValid(code)) { 31 resolve(trimChecksum(code)); 32 } 33 reject("Invalid checksum"); 34 } 35 resolve(code); 36 }); 37 } 38 39 /** 40 * Checks if the checksum of the barcode is valid 41 * @param barcode - The barcode to check 42 * @returns {boolean} - True if the checksum is valid, false otherwise 43 */ 44 isChecksumValid = function(barcode) { 45 const remainingCode = trimChecksum(barcode); 46 const checksum = parseInt(barcode[barcode.length - 1]); 47 let sum = 0; 48 49 for (let i = 0; i < remainingCode.length; i++) { 50 if (i % 2 === 0) { 51 sum += parseInt(remainingCode[i]); 52 } else { 53 sum += 3 * parseInt(remainingCode[i]); 54 } 55 } 56 57 sum = sum % 10; 58 if (sum !== 0) { 59 sum = 10 - sum; 60 } 61 62 return checksum === sum; 63 } 64 65 /** 66 * Trims the checksum from the barcode 67 * @param barcode - The barcode to trim 68 * @returns {string|*|string} - The barcode without the checksum 69 */ 70 trimChecksum = function(barcode) { 71 return barcode.substring(1, barcode.length - 1); 72 } 73 74}(window.sellsite = window.sellsite || {}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.