Espruino doesn't support search or {} Regex quantifier, replace with test

master
Eric de Boer 2024-04-09 09:17:10 +02:00
parent 67e7e71df1
commit fd2b347349
6 changed files with 10 additions and 10 deletions

View File

@ -24,7 +24,7 @@ class EAN13 extends EAN {
constructor(data, options) { constructor(data, options) {
// Add checksum if it does not exist // Add checksum if it does not exist
if (data.search(/^[0-9]{12}$/) !== -1) { if (/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(data)) {
data += checksum(data); data += checksum(data);
} }
@ -36,7 +36,7 @@ class EAN13 extends EAN {
valid() { valid() {
return ( return (
this.data.search(/^[0-9]{13}$/) !== -1 && /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(this.data) &&
+this.data[12] === checksum(this.data) +this.data[12] === checksum(this.data)
); );
} }

View File

@ -12,7 +12,7 @@ class EAN2 extends Barcode {
} }
valid() { valid() {
return this.data.search(/^[0-9]{2}$/) !== -1; return /^[0-9][0-9]$/.test(this.data);
} }
encode(){ encode(){

View File

@ -24,7 +24,7 @@ class EAN5 extends Barcode {
} }
valid() { valid() {
return this.data.search(/^[0-9]{5}$/) !== -1; return /^[0-9][0-9][0-9][0-9][0-9]$/.test(this.data);
} }
encode() { encode() {

View File

@ -20,7 +20,7 @@ class EAN8 extends EAN {
constructor(data, options) { constructor(data, options) {
// Add checksum if it does not exist // Add checksum if it does not exist
if (data.search(/^[0-9]{7}$/) !== -1) { if (/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(data)) {
data += checksum(data); data += checksum(data);
} }
@ -29,7 +29,7 @@ class EAN8 extends EAN {
valid() { valid() {
return ( return (
this.data.search(/^[0-9]{8}$/) !== -1 && /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(this.data) &&
+this.data[7] === checksum(this.data) +this.data[7] === checksum(this.data)
); );
} }

View File

@ -7,7 +7,7 @@ const Barcode = require("cards.Barcode.js");
class UPC extends Barcode{ class UPC extends Barcode{
constructor(data, options){ constructor(data, options){
// Add checksum if it does not exist // Add checksum if it does not exist
if(data.search(/^[0-9]{11}$/) !== -1){ if(/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(data)){
data += checksum(data); data += checksum(data);
} }
@ -15,7 +15,7 @@ class UPC extends Barcode{
} }
valid(){ valid(){
return this.data.search(/^[0-9]{12}$/) !== -1 && return /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(this.data) &&
this.data[11] == checksum(this.data); this.data[11] == checksum(this.data);
} }

View File

@ -41,14 +41,14 @@ class UPCE extends Barcode{
// is a UPC-A check or number system digit. // is a UPC-A check or number system digit.
super(data, options); super(data, options);
this.isValid = false; this.isValid = false;
if(data.search(/^[0-9]{6}$/) !== -1){ if(/^[0-9][0-9][0-9][0-9][0-9][0-9]$/.test(data)){
this.middleDigits = data; this.middleDigits = data;
this.upcA = expandToUPCA(data, "0"); this.upcA = expandToUPCA(data, "0");
this.text = options.text || this.text = options.text ||
`${this.upcA[0]}${data}${this.upcA[this.upcA.length - 1]}`; `${this.upcA[0]}${data}${this.upcA[this.upcA.length - 1]}`;
this.isValid = true; this.isValid = true;
} }
else if(data.search(/^[01][0-9]{7}$/) !== -1){ else if(/^[01][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(data)){
this.middleDigits = data.substring(1, data.length - 1); this.middleDigits = data.substring(1, data.length - 1);
this.upcA = expandToUPCA(this.middleDigits, data[0]); this.upcA = expandToUPCA(this.middleDigits, data[0]);