upcaToUpce method

String upcaToUpce(
  1. String data
)

Implementation

String upcaToUpce(String data) {
  if (RegExp(r"^[01]\d{11}$").firstMatch(data) == null) {
    throw BarcodeException('Unable to convert "$data" to $name Barcode');
  }

  final String mc = data.substring(1, 6);
  final String pc = data.substring(6, 11);

  if (<String>["000", "100", "200"].contains(mc.substring(mc.length - 3)) && int.parse(pc) <= 999) {
    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, 3)}${pc.substring(pc.length - 2)}3";
  } else if (mc.substring(mc.length - 1) == "0" && int.parse(pc) <= 9) {
    return "${mc.substring(0, 4)}${pc.substring(pc.length - 1)}4";
  } else if (mc.substring(mc.length - 1) != "0" && <int>[5, 6, 7, 8, 9].contains(int.parse(pc))) {
    return mc + pc.substring(pc.length - 1);
  } else {
    throw BarcodeException('Unable to convert "$data" to $name Barcode');
  }
}