Chapter 03 of 22 Part I: Digital Foundations

The Secret Language of Barcodes

6 Verified Book Snippets

Every .js file below is byte-identical (SHA-256 verified) to the printed code in Volume 1. Runnable blocks execute inside an isolated sandboxed iframe and Web Worker with a 2-second timeout and zero network access.

Download Chapter ZIP

Check Digit (Ch. 03 Example 1)

File: block-01.js (byte-identical) · 907 bytes · SHA-256: ceafcf00f12b…
EAN-13 Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions or data; runs cleanly in the sandbox (call console.log in a test harness to inspect values).

const L = ['0001101','0011001','0010011','0111101','0100011',
  '0110001','0101111','0111011','0110111','0001011'];
const invert = s => [...s].map(b => b === '0' ? '1' : '0').join('');
const reverse = s => [...s].reverse().join('');
const R = L.map(invert);
const G = R.map(reverse);
const P = ['LLLLLL','LLGLGG','LLGGLG','LLGGGL','LGLLGG','LGGLLG',
  'LGGGLL','LGLGLG','LGLGGL','LGGLGL'];
function checkDigit(s) {
  if (!/^\d{12}$/.test(s)) throw Error('Enter 12 digits');
  let sum = 0;
  for (let i = 0; i < 12; i++) {
    sum += Number(s[i]) * (i % 2 ? 3 : 1);
  }
  return String((10 - sum % 10) % 10);
}
function encode(s) {
  const n = s + checkDigit(s);
  let bits = '101';
  for (let i = 1; i <= 6; i++) {
    const table = P[Number(n[0])][i-1] === 'L' ? L : G;
    bits += table[Number(n[i])];
  }
  bits += '01010';
  for (let i = 7; i < 13; i++) bits += R[Number(n[i])];
  return bits + '101';
}
Download .js

Draw Barcode (Ch. 03 Example 2)

File: block-02.js (byte-identical) · 388 bytes · SHA-256: ca06b0068f5b…
EAN-13 Fragment / Lab-dependent

Dependencies: Browser DOM or chapter Interactive Lab environment. Interacts with browser UI elements or dialogs; inspect alongside the chapter Interactive Lab.

function drawBarcode(canvas, bits, scale = 4) {
  canvas.width = (95 + 11 + 7) * scale;
  canvas.height = 100;
  const ctx = canvas.getContext('2d');
  ctx.fillStyle = 'white';
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  ctx.fillStyle = 'black';
  for (let i = 0; i < bits.length; i++) {
    if (bits[i] === '1') {
      ctx.fillRect((11 + i) * scale, 0, scale, 100);
    }
  }
}
Download .js

Decode Forward (Ch. 03 Example 3)

File: block-03.js (byte-identical) · 769 bytes · SHA-256: 066f5c7d9b7a…
EAN-13 Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions or data; runs cleanly in the sandbox (call console.log in a test harness to inspect values).

function decodeForward(b) {
  if (!/^[01]{95}$/.test(b) || b.slice(0,3) !== '101' ||
      b.slice(45,50) !== '01010' || b.slice(92) !== '101')
    return null;
  let left = '', right = '', pattern = '';
  for (let i = 0; i < 6; i++) {
    const group = b.slice(3+7*i,10+7*i);
    let d = L.indexOf(group);
    if (d >= 0) pattern += 'L';
    else { d = G.indexOf(group); pattern += 'G'; }
    if (d < 0) return null;
    left += d;
    d = R.indexOf(b.slice(50+7*i,57+7*i));
    if (d < 0) return null;
    right += d;
  }
  const first = P.indexOf(pattern);
  if (first < 0) return null;
  const n = String(first) + left + right;
  return checkDigit(n.slice(0,12)) === n[12] ? n : null;
}
function decode(b) {
  return decodeForward(b) || decodeForward(reverse(b));
}
Download .js

Scan Row (Ch. 03 Example 4)

File: block-04.js (byte-identical) · 595 bytes · SHA-256: 53ae97e4981e…
EAN-13 Fragment / Lab-dependent

Dependencies: Browser DOM or chapter Interactive Lab environment. Interacts with browser UI elements or dialogs; inspect alongside the chapter Interactive Lab.

function scanRow(canvas) {
  const w = canvas.width;
  const ctx = canvas.getContext('2d');
  const rgba = ctx.getImageData(0, Math.floor(canvas.height/2),
                                w, 1).data;
  const row = Array.from({length:w}, (_,i) =>
    (rgba[i*4]+rgba[i*4+1]+rgba[i*4+2])/3 < 128
      ? '1' : '0').join('');
  const start = row.indexOf('1'), end = row.lastIndexOf('1');
  if (start < 0 || end-start+1 < 95) return null;
  const width = (end-start+1)/95;
  const bits = Array.from({length:95}, (_,i) =>
    row[start + Math.floor((i+0.5)*width)]).join('');
  return decode(bits);
}
Download .js

Checkout (Ch. 03 Example 5)

File: block-05.js (byte-identical) · 536 bytes · SHA-256: a0ae965a8cf7…
EAN-13 Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated Web Worker sandbox and prints console output.

const catalog = {
  '5901234123457': { name: 'Moon cereal', cents: 349 },
  '1234567890128': { name: 'Star juice', cents: 225 }
};
let total = 0;
function checkout(bits) {
  const id = decode(bits);
  if (id === null) return 'Scan rejected';
  const item = catalog[id];
  if (!item) return 'Unknown item';
  total += item.cents;
  return item.name + ': $' + (item.cents / 100).toFixed(2);
}
console.log(checkout(encode('590123412345')));
console.log(checkout(encode('123456789012')));
console.log('Total: $' + (total / 100).toFixed(2));
Download .js

The Secret Language of Barcodes — Code Example 6

File: block-06.js (byte-identical) · 49 bytes · SHA-256: cc43abdbb751…
EAN-13 Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions or data; runs cleanly in the sandbox (call console.log in a test harness to inspect values).

Moon cereal: $3.49
Star juice: $2.25
Total: $5.74
Download .js