diff --git a/addons/barcodes_gs1_nomenclature/static/src/js/barcode_parser.js b/addons/barcodes_gs1_nomenclature/static/src/js/barcode_parser.js index 29b6b9d22e9..2c6f447098a 100644 --- a/addons/barcodes_gs1_nomenclature/static/src/js/barcode_parser.js +++ b/addons/barcodes_gs1_nomenclature/static/src/js/barcode_parser.js @@ -22,6 +22,13 @@ patch(BarcodeParser, { }); patch(BarcodeParser.prototype, { + setup(attributes) { + super.setup(...arguments); + // Use the nomenclature's separaor regex, else use an impossible one. + const nomenclatureSeparator = this.nomenclature && this.nomenclature.gs1_separator_fnc1; + this.gs1SeparatorRegex = new RegExp(nomenclatureSeparator || '.^', 'g'); + }, + /** * Convert YYMMDD GS1 date into a Date object * @@ -106,7 +113,8 @@ patch(BarcodeParser.prototype, { gs1_decompose_extanded(barcode) { const results = []; const rules = this.nomenclature.rules.filter(rule => rule.encoding === 'gs1-128'); - const separatorReg = FNC1_CHAR + "?"; + const separatorReg = `(?:${FNC1_CHAR}+)?`; + barcode = this._convertGS1Separators(barcode); while (barcode.length > 0) { const barcodeLength = barcode.length; @@ -143,4 +151,17 @@ patch(BarcodeParser.prototype, { } return super.parse_barcode(...arguments); }, + + /** + * The FNC1 is the default GS1 separator character, but through the field `gs1_separator_fnc1`, + * the user has the possibility to define one or multiple characters to use as separator as + * a regex. This method replaces all of the matches in the given barcode by the FNC1. + * + * @param {string} barcode + * @returns {string} + */ + _convertGS1Separators: function (barcode) { + barcode = barcode.replace(this.gs1SeparatorRegex, FNC1_CHAR); + return barcode; + }, }); diff --git a/addons/barcodes_gs1_nomenclature/static/src/js/tests/barcode_parser_tests.js b/addons/barcodes_gs1_nomenclature/static/src/js/tests/barcode_parser_tests.js index d563f6c27c0..9490d912ca3 100644 --- a/addons/barcodes_gs1_nomenclature/static/src/js/tests/barcode_parser_tests.js +++ b/addons/barcodes_gs1_nomenclature/static/src/js/tests/barcode_parser_tests.js @@ -224,7 +224,8 @@ QUnit.module('Barcode GS1 Parser', { QUnit.test('Test Alternative GS1 Separator (fnc1)', async function (assert) { assert.expect(6); - const barcodeNomenclature = new BarcodeParser({nomenclature: this.nomenclature}); + let barcodeNomenclature = new BarcodeParser({ nomenclature: this.nomenclature }); + await barcodeNomenclature.loaded; // (21)12345(15)090101(16)100101 const code128 = "2112345#1509010116100101"; @@ -238,7 +239,10 @@ QUnit.module('Barcode GS1 Parser', { ); } - barcodeService.gs1SeparatorRegex = '#'; + // Reload the nomenclature but this time using '#' as separator. + this.nomenclature.gs1_separator_fnc1 = '#'; + barcodeNomenclature = new BarcodeParser({ nomenclature: this.nomenclature }); + await barcodeNomenclature.loaded; res = barcodeNomenclature.gs1_decompose_extanded(barcodeService.cleanBarcode(code128)); assert.equal(res.length, 3); assert.equal(res[0].ai, "21");