Skip to content

Commit 365e3e1

Browse files
committed
fix: false positive EAN for longer strings
1 parent 9386dae commit 365e3e1

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

content_scripts/pola.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
text = text.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, ' ');
4444
text = text.replace(/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/gi, ' ');
4545
text = text.replace(/<[^>]+>/gi, ' ');
46-
result = text.match(/\d{13}|\d{8}/g);
46+
// Granice (?<!\d)/(?!\d) wykluczają fragmenty dłuższych ciągów cyfr,
47+
// np. 13-cyfrowy prefiks 14-cyfrowego kodu wewnętrznego sklepu.
48+
result = text.match(/(?<!\d)(?:\d{13}|\d{8})(?!\d)/g);
4749
let gtin13 = document.querySelectorAll('meta[itemprop="gtin13"][content]');
4850
let gtin8 = document.querySelectorAll('meta[itemprop="gtin8"][content]');
4951
let flix = document.querySelectorAll('script[data-flix-ean]');

lib/validateEAN.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Validates an EAN-8 or EAN-13 barcode using the check digit algorithm.
33
*/
44
function validateEAN(elem) {
5-
if (isNaN(elem)) {
5+
if (isNaN(elem) || (elem.length !== 8 && elem.length !== 13)) {
66
return false;
77
}
88
let sum = 0;

test/validateEAN.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ describe('validateEAN', () => {
4646
expect(validateEAN('12345')).toBe(false);
4747
});
4848

49+
test('14-digit internal code (00461455550000)', () => {
50+
expect(validateEAN('00461455550000')).toBe(false);
51+
});
52+
53+
test('valid EAN-13 with extra digit appended (59003977457800)', () => {
54+
expect(validateEAN('59003977457800')).toBe(false);
55+
});
56+
57+
test('12 digits (590039774578)', () => {
58+
expect(validateEAN('590039774578')).toBe(false);
59+
});
60+
4961
test('non-numeric string', () => {
5062
expect(validateEAN('abcdefghijklm')).toBe(false);
5163
});

0 commit comments

Comments
 (0)