Generate Code 128 Barcode With Javascript (Length Optimization)

Generates a Code 128 Bar Code https://dev.to/saulodias/generating-valid-code-128-barcodes-with-javascript-5ana

by dias26

HTML

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Libre+Barcode+128">

   <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Libre+Barcode+128+Text">

   <input type="checkbox" id="withtext" name="Show Barcode With Text" checked>
   <label for="withtext">Show Barcode With Text</label>
   <br /> <br />


   This code always starts with the Set B, but alternates to C when an optimization is possible.
   <div class="barcode" data-barcode="000001111122222233333444444566666666666"></div>
   <br /> <br />
   <div class="barcode" data-barcode="294-294042997-01-01"></div>

CSS

.barcode {
  padding: 20px;
  font-family: "Libre Barcode 128", "Courier New", Courier, monospace;
  font-weight: normal;
  font-size: 42px;
  white-space: nowrap;
}

.with-text {
  font-family: "Libre Barcode 128 Text";
}

JavaScript

const checkbox = document.getElementById('withtext')

const BEGINNING_REGEX = /^(\d{4,})/g;
const MIDDLE_REGEX = /(?<=\D)\d{6,}(?=\D)/g;
const END_REGEX = /(\d{4,})$/g;

function splitSequence(match, delayedSwitch) {
  if (!(match.length % 2)) return [match];

  const processed = [];

  let chars = match.split('');

  if (delayedSwitch) {
    processed.push(chars.shift());
    processed.push(chars.join(''));
  } else {
    processed.push(chars.pop());
    processed.unshift(chars.join(''));
  }

  return processed;
}

function getPatches(data) {
  function fitIn(data, separator) {
    const [a, b] = data.split(new RegExp(separator, 's'));
    return [a, separator, b];
  }

  let patches = [data];

  const last = () => patches[patches.length - 1];

  function patch(regex, delayedSwitch) {
    if (regex.test(last())) {
      const match = last().match(regex)[0];

      const splited = splitSequence(match, delayedSwitch);

      const newPatches = fitIn(last(), delayedSwitch ? splited[1] || splited[0] : splited[0]);

      patches = [...patches.slice(0, -1), ...newPatches]
    } else {
      return;
    }

    patch(regex);
  }

  patch(BEGINNING_REGEX);
  patch(MIDDLE_REGEX);
  patch(END_REGEX, true);

  return patches.filter(p => !!p);
}


function toSetC(text) {
 
  return text.match(/\d{2}/g).map((ascii, index) => {
    var codeC = Number(ascii);
    var charCode = codeC > 94 ? codeC + 100 : codeC + 32;
  	return String.fromCharCode(charCode)
  }).join('');
}

function optimize(data) {
  const blocks = getPatches(data);
  console.log(blocks);
  const str = [];

  let startSetC = false;

  blocks.forEach((block, index) => {
    const isFirstBlock = index == 0;
    const isLastBlock = index == blocks.length - 1;
    const isSetC = /^(\d{2}|\d{4,})$/g.test(block);

    if (isSetC) {
      str.push(String.fromCharCode(199));
      str.push(toSetC(block));
    } else {
      !isFirstBlock && str.push(String.fromCharCode(200));
      str.push(block);
    }

    if...