upcaToUpce method

String upcaToUpce(
  1. String data
)

Convert an UPC-A barcode to a short version UPC-E

Implementation

String upcaToUpce(String data) {
  final exp = RegExp(r'^[01](\d\d+)([0-2]000[05-9])(\d*)\d$');
  final match = exp.firstMatch(data);

  if (match == null) {
    throw BarcodeException('Unable to convert "$data" to $name Barcode');
  }

  final left = match.group(1);
  final right = match.group(3);
  String? last;

  switch (match.group(2)) {
    case '00000':
      if (left!.length == 2) {
        last = '0';
      } else if (left.length == 3) {
        last = '3';
      } else if (left.length == 4) {
        last = '4';
      }
      break;
    case '10000':
      last = '1';
      break;
    case '20000':
      last = '2';
      break;
    case '00005':
      last = '5';
      break;
    case '00006':
      last = '6';
      break;
    case '00007':
      last = '7';
      break;
    case '00008':
      last = '8';
      break;
    case '00009':
      last = '9';
      break;
  }

  if (last == null) {
    throw BarcodeException('Unable to convert "$data" to $name Barcode');
  }

  return left! + right! + last;
}