Skip to content

Commit

Permalink
Fix format and update dev dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
DavBfr committed Jan 5, 2024
1 parent 0be4a76 commit 4d38959
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 48 deletions.
86 changes: 41 additions & 45 deletions barcode/lib/src/upce.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ class BarcodeUpcE extends BarcodeEan {

/// Convert an UPC-A barcode to a short version UPC-E
String upcaToUpce(String data) {

//Basic checking of string headers and lengths.
if ( RegExp(r'^[01]\d{11}$').firstMatch(data) == null) {
if (RegExp(r'^[01]\d{11}$').firstMatch(data) == null) {
throw BarcodeException('Unable to convert "$data" to $name Barcode');
}
//Refer to https://en.wikipedia.org/wiki/Universal_Product_Code#UPC-E
Expand All @@ -83,34 +82,31 @@ class BarcodeUpcE extends BarcodeEan {
//Algorithm 2: https://www.keepautomation.com/upca/upca-to-upce-conversion.html

// Algorithm 1=>
final mc = data.substring(1,6); //manufacturer code
final pc = data.substring(6,11); //product code
final mc = data.substring(1, 6); //manufacturer code
final pc = data.substring(6, 11); //product code

if(['000', '100', '200'].contains(mc.substring(mc.length - 3)) && int.parse(pc) <= 999){
if (['000', '100', '200'].contains(mc.substring(mc.length - 3)) &&
int.parse(pc) <= 999) {
//if manufacturer_code[-3:] in ["000", "100", "200"] and int(product_code) <= 999:
// upce = manufacturer_code[:2] + product_code[-3:] + manufacturer_code[2]
return '${mc.substring(0,2)}${pc.substring(pc.length-3)}${mc[2]}';
}
else if(mc.substring(mc.length - 2) == '00' && int.parse(pc) <= 99){
return '${mc.substring(0, 2)}${pc.substring(pc.length - 3)}${mc[2]}';
} else if (mc.substring(mc.length - 2) == '00' && int.parse(pc) <= 99) {
//elif manufacturer_code[-2:] == '00' and int(product_code) <= 99:
// upce = manufacturer_code[:3] + product_code[-2:] + "3"
return '${mc.substring(0,3)}${pc.substring(pc.length-2)}3';
}
else if(mc.substring(mc.length - 1) == '0' && int.parse(pc) <= 9){
return '${mc.substring(0, 3)}${pc.substring(pc.length - 2)}3';
} else if (mc.substring(mc.length - 1) == '0' && int.parse(pc) <= 9) {
//elif manufacturer_code[-1] == "0" and int(product_code) <= 9:
// upce = manufacturer_code[:4] + product_code[-1] + "4"
return '${mc.substring(0,4)}${pc.substring(pc.length-1)}4';
}
else if(mc.substring(mc.length - 1) != '0' && [5, 6, 7, 8, 9].contains(int.parse(pc))){
return '${mc.substring(0, 4)}${pc.substring(pc.length - 1)}4';
} else if (mc.substring(mc.length - 1) != '0' &&
[5, 6, 7, 8, 9].contains(int.parse(pc))) {
//elif manufacturer_code[-1] != "0" and int(product_code) in [5,6,7,8,9]:
// upce = manufacturer_code + product_code[-1]
return mc + pc.substring(pc.length-1);
}
else {
return mc + pc.substring(pc.length - 1);
} else {
throw BarcodeException('Unable to convert "$data" to $name Barcode');
}


// Algorithm 2=>
/*
if([0x35, 0x36, 0x37, 0x38, 0x39].contains(data.codeUnits[10]) && data.substring(6,10) == '0000' && data[5] != '0') {
Expand Down Expand Up @@ -248,12 +244,12 @@ class BarcodeUpcE extends BarcodeEan {

@override
double marginLeft(
bool drawText,
double width,
double height,
double fontHeight,
double textPadding,
) {
bool drawText,
double width,
double height,
double fontHeight,
double textPadding,
) {
if (!drawText) {
return 0;
}
Expand All @@ -263,12 +259,12 @@ class BarcodeUpcE extends BarcodeEan {

@override
double marginRight(
bool drawText,
double width,
double height,
double fontHeight,
double textPadding,
) {
bool drawText,
double width,
double height,
double fontHeight,
double textPadding,
) {
if (!drawText) {
return 0;
}
Expand All @@ -278,14 +274,14 @@ class BarcodeUpcE extends BarcodeEan {

@override
double getHeight(
int index,
int count,
double width,
double height,
double fontHeight,
double textPadding,
bool drawText,
) {
int index,
int count,
double width,
double height,
double fontHeight,
double textPadding,
bool drawText,
) {
if (!drawText) {
return super.getHeight(
index,
Expand All @@ -309,13 +305,13 @@ class BarcodeUpcE extends BarcodeEan {

@override
Iterable<BarcodeElement> makeText(
String data,
double width,
double height,
double fontHeight,
double textPadding,
double lineWidth,
) sync* {
String data,
double width,
double height,
double fontHeight,
double textPadding,
double lineWidth,
) sync* {
if (data.length <= 8) {
// Try to convert UPC-E to UPC-A
data = upceToUpca(data);
Expand Down
2 changes: 1 addition & 1 deletion barcode/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ dependencies:

dev_dependencies:
convert: ^3.0.1
flutter_lints: ^2.0.1
flutter_lints: ^3.0.1
test: ^1.19.0
3 changes: 1 addition & 2 deletions barcode/test/upce_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void main() {
});

test('Barcode UPC-E normalize (fallback)', () {
final bc = Barcode.upcE( fallback: true);
final bc = Barcode.upcE(fallback: true);
if (bc is BarcodeUpcE) {
expect(bc.normalize('18740000015'), equals('18741538'));
expect(bc.normalize('48347295752'), equals('483472957520'));
Expand Down Expand Up @@ -120,5 +120,4 @@ void main() {
expect(bc.normalize('001054'), equals('00000514')); //another special case
}
});

}

0 comments on commit 4d38959

Please sign in to comment.