main
Caleb 0.4.0: wiki page runs the live calculator (README.html loads the current source from the page repo), UI static mode, sh… fc3ee8f
// hwcalc self-test: every calculator against hand-checked reference values.  node test.js
const H = require('./calcs.js');
let fail = 0, n = 0;
const near = (a, b, tol) => Math.abs(a - b) <= Math.abs(b) * tol;
function check(name, got, want, tol = 0.01) {
  n++; const ok = typeof want === 'string' ? got === want : near(got, want, tol);
  if (!ok) { fail++; console.log(`FAIL ${name}: got ${got}, want ${want}`); }
}
const o = (r, label) => { const x = r.outputs.find(q => q.label.startsWith(label)); if (!x) throw new Error(`${r.calc}: no output "${label}" (${r.error || ''})`); return x.value; };

// parsing
check('parse 4k7', H.parseNum('4k7'), 4700); check('parse 10u', H.parseNum('10u'), 1e-5); check('parse 2.2M', H.parseNum('2.2M'), 2.2e6);
check('parse 100nF', H.parseNum('100nF'), 1e-7); check('parse 5m', H.parseNum('5m'), 5e-3); check('parse 1e-3', H.parseNum('1e-3'), 1e-3);
check('parse 3R3?', H.parseNum('3.3'), 3.3); check('parse plain mm', H.parseNum('5', false), 5);
// E-series
check('E96[0]', H.E_BASE.E96[0], 1.00); check('E96 1.02', H.E_BASE.E96[1], 1.02); check('E96 4.75', H.E_BASE.E96.includes(4.75) ? 1 : 0, 1);
check('E96 9.76', H.E_BASE.E96[95], 9.76); check('E192 9.20', H.E_BASE.E192.includes(9.2) ? 1 : 0, 1);
check('nearest E24 4.8k', H.eNearest(4800, 'E24').nearest, 4700); check('E12 above 470', H.eNearest(471, 'E12').above, 560);
check('EIA96 4.75k', H.eia96(4750), '66B'); check('EIA96 10k', H.eia96(10000), '01C');
check('bands 4.7k', H.colourBands(4700, 2).join(','), 'yellow,violet,red');

// calculators
let r = H.run('ohms', { V: '5', I: '10m' }); check('ohms R', o(r, 'Resistance'), 500); check('ohms P', o(r, 'Power'), 0.05);
r = H.run('ohms', { V: '', I: '', R: '100', P: '0.25' }); check('ohms RP→V', o(r, 'Voltage'), 5);
r = H.run('divider', { Vin: '12', R1: '10k', R2: '3k3' }); check('divider Vout', o(r, 'Output voltage'), 12 * 3300 / 13300);
r = H.run('divider', { Vin: '12', R1: '10k', R2: '10k', RL: '10k' }); check('divider loaded', o(r, 'Output voltage'), 4);
r = H.run('divider', { mode: 'design', Vin: '5', Vout: '3.3', Rtot: '100k', series: 'E96' }); check('divider design err<0.5%', Math.abs(o(r, 'Error')) < 0.5 ? 1 : 0, 1);
r = H.run('led', { Vs: '5', Vf: '2', n: '1', If: '10m', series: 'E24' }); check('led R', o(r, 'Exact'), 300); check('led E24', o(r, 'Next E24'), 300);
r = H.run('filter', { mode: 'RC', R: '10k', C: '100n' }); check('RC fc', o(r, '−3 dB'), 159.155);
r = H.run('filter', { mode: 'LC', L: '10u', C: '100n' }); check('LC f0', o(r, 'Resonant'), 159155);
// IPC-2221: 1 A, 10 °C rise, 1 oz external -> ~11.8 mil (standard calculators give 11.8)
r = H.run('trace', { std: 'IPC-2221', mode: 'width', I: '1', dT: '10', oz: '1', layer: 'external', len: '50' }); check('IPC-2221 1A ext', o(r, 'Width'), 11.8, 0.02);
r = H.run('trace', { std: 'IPC-2221', mode: 'width', I: '1', dT: '10', oz: '1', layer: 'internal', len: '50' }); check('IPC-2221 1A int', o(r, 'Width'), 30.7, 0.02);
r = H.run('trace', { std: 'IPC-2221', mode: 'current', w: String(11.8 * 0.0254), dT: '10', oz: '1', layer: 'external' }); check('IPC-2221 inverse', o(r, 'Maximum current'), 1, 0.02);
// IPC-2141 microstrip: h 0.2 mm, w 0.35, t 35 µm, er 4.5 -> 87/sqrt(5.91)*ln(1.196/0.3148) = 47.8
r = H.run('impedance', { geom: 'microstrip', mode: 'impedance', w: '0.35', h: '0.2', oz: '1', er: '4.5' }); check('microstrip Z0', o(r, 'Impedance'), 47.8, 0.01);
r = H.run('impedance', { geom: 'microstrip', mode: 'width', Z: '47.8', h: '0.2', oz: '1', er: '4.5' }); check('microstrip inverse', o(r, 'Trace width'), 0.35, 0.01);
r = H.run('ldo', { Vin: '5', Vout: '3.3', Iout: '300m', Iq: '0', theta: '184', Ta: '25', Tjmax: '125' }); check('ldo Pd', o(r, 'Dissipation'), 0.51); check('ldo Tj', o(r, 'Junction'), 118.84);
r = H.run('buck', { Vin: '12', Vout: '3.3', Iout: '2', fsw: '500k', rip: '30', eff: '90' }); check('buck L', o(r, 'Minimum inductance'), 3.3 * 8.7 / (0.6 * 500e3 * 12));
r = H.run('boost', { Vin: '3.3', Vout: '12', Iout: '0.5', fsw: '600k', rip: '30', eff: '85' }); check('boost D', o(r, 'Duty'), (1 - 3.3 * 0.85 / 12) * 100);
r = H.run('holdup', { mode: 'capacitance', load: 'constant power', P: '2', V1: '12', V2: '9', t: '10m' }); check('holdup C', o(r, 'Required'), 2 * 2 * 0.01 / (144 - 81));
r = H.run('battery', { cap: '2000', Ia: '25m', Is: '15u', duty: '2', derate: '85' }); check('battery h', o(r, 'Battery life'), 2 * 0.85 / (0.025 * 0.02 + 15e-6 * 0.98));
// UM10204: Fm, 3.3 V, 200 pF -> Rmax 1770, Rmin 967
r = H.run('i2c', { VDD: '3.3', Cb: '200p', speed: 'Fast 400 kHz' }); check('i2c Rmax', o(r, 'Maximum'), 1770, 0.005); check('i2c Rmin', o(r, 'Minimum'), 966.7, 0.005);
r = H.run('crystal', { CL: '12p', Cs: '4p', series: 'E12' }); check('xtal C', o(r, 'Exact'), 16e-12);
// AN2867 Rev 10 §3.3 example: CL 15 pF, Cs 5 pF -> C1 = C2 = 20 pF
r = H.run('crystal', { CL: '15p', Cs: '5p', series: 'E24' }); check('xtal AN2867 C1=C2', o(r, 'Exact'), 20e-12); check('xtal E24 20p', o(r, 'Nearest'), 20e-12);
r = H.run('crystal', { mode: 'CL from C1/C2', CL: '15p', Cs: '5p', C1: '20p', C2: '20p' }); check('xtal CL from caps', o(r, 'Load the crystal'), 15e-12);
r = H.run('crystal', { mode: 'CL from C1/C2', CL: '10p', Cs: '3p', C1: '10p', C2: '22p' }); check('xtal asym CL', o(r, 'Load the crystal'), 10e-12 * 22e-12 / 32e-12 + 3e-12);
// AN2867 Rev 10 §3.4 example: 8 MHz, C0 7 pF, CL 10 pF, ESR 80 Ω -> gm_crit 0.23 mA/V; gm 25 mA/V -> margin ~107
r = H.run('crystal', { CL: '10p', Cs: '5p', F: '8M', C0: '7p', ESR: '80', gm: '25m' });
check('xtal gm_crit', o(r, 'gm_crit'), 4 * 80 * Math.pow(2 * Math.PI * 8e6, 2) * Math.pow(17e-12, 2)); check('xtal gm_crit ~0.23 mA/V', o(r, 'gm_crit'), 0.23e-3, 0.02);
check('xtal gain margin ~107', o(r, 'Gain margin'), 107, 0.01);
r = H.run('crystal', { CL: '10p', Cs: '5p', F: '8M', C0: '7p', ESR: '80', gm: '1m' }); check('xtal low margin warns', r.warnings.length, 1);
// trim sensitivity: C0 5 pF, Cm 10 fF -> -22.22 ppm/pF at CL 10 pF (CL = 10p exactly: E12 10p caps with Cs 5p give 10p)
r = H.run('crystal', { CL: '10p', Cs: '5p', series: 'E12', C0: '5p', Cm: '10f' }); check('xtal TS', o(r, 'Trim sensitivity'), -22.222); check('xtal zero offset', o(r, 'Frequency offset') + 1, 1);
r = H.run('crystal', { mode: 'CL from C1/C2', CL: '12p', Cs: '4p', C1: '12p', C2: '12p', C0: '5p', Cm: '10f' });
check('xtal pull offset', o(r, 'Frequency offset'), (10e-15 / (2 * 15e-12) - 10e-15 / (2 * 17e-12)) * 1e6);
// drive level AN2867 §3.5.2: IQ = 2πF·Vrms·(C1 + Cs/2 + Cprobe), DL = ESR·IQ²
r = H.run('crystal', { CL: '15p', Cs: '5p', series: 'E24', F: '8M', ESR: '80', Vpp: '1', Cp: '1p', DLmax: '1m' });
{ const IQ = 2 * Math.PI * 8e6 * (1 / (2 * Math.SQRT2)) * 23.5e-12; check('xtal IQ', o(r, 'Crystal current'), IQ); check('xtal DL', o(r, 'Drive level'), 80 * IQ * IQ);
  check('xtal DL ~13.95 uW', o(r, 'Drive level'), 13.95e-6, 0.005); check('xtal IQmaxPP', o(r, 'Max IQ'), 2 * Math.SQRT2 * Math.sqrt(1e-3 / 80)); }
r = H.run('shunt', { I: '10', R: '5m', G: '50', bits: '12', Vref: '3.3' }); check('shunt P', o(r, 'Shunt'), 0.5);
r = H.run('adc', { bits: '12', Vref: '3.3', Vin: '1.65' }); check('adc code', o(r, 'Code'), 2048); check('adc SNR', o(r, 'Ideal SNR'), 74.0);
r = H.run('thermal', { mode: 'required heatsink', P: '5', Ta: '40', jc: '1.5', cs: '0.5', Tj: '125' }); check('heatsink', o(r, 'Heatsink'), 15);
// AWG 24: 0.511 mm, 84.2 mΩ/m
r = H.run('wire', { awg: '24', len: '1', I: '1', rt: 'no', T: '20' }); check('awg24 d', o(r, 'Diameter'), 0.511, 0.003); check('awg24 R/m', o(r, 'Resistance per metre'), 0.0842, 0.005);
r = H.run('rf', { val: '0', unit: 'dBm', Z: '50' }); check('0 dBm Vrms', o(r, 'RMS'), 0.2236);
r = H.run('wavelength', { f: '2.4G', er: '1' }); check('2.4G λ', o(r, 'Wavelength'), 124.91, 0.001);
r = H.run('eseries', { val: '4k8', series: 'E96' }); check('E96 4.8k', o(r, 'Nearest'), 4750);
// full published IEC 60063 E96 table
const E96 = '100 102 105 107 110 113 115 118 121 124 127 130 133 137 140 143 147 150 154 158 162 165 169 174 178 182 187 191 196 200 205 210 215 221 226 232 237 243 249 255 261 267 274 280 287 294 301 309 316 324 332 340 348 357 365 374 383 392 402 412 422 432 442 453 464 475 487 499 511 523 536 549 562 576 590 604 619 634 649 665 681 698 715 732 750 768 787 806 825 845 866 887 909 931 953 976'.split(' ').map(Number);
check('E96 table matches IEC 60063', H.E_BASE.E96.map(x => Math.round(x * 100)).join(), E96.join());

// ---- expansion 2026-09-25
// IPC-2152 baseline fit (smps.us): 1 A, 10 °C -> A = 117.555*10^-0.913 + 1.15 = 15.52 mil² -> 11.33 mil at 1 oz (34.79 µm)
r = H.run('trace', { mode: 'width', I: '1', dT: '10', oz: '1', len: '50' }); check('IPC-2152 fit 1A', o(r, 'Width'), 15.52 / (34.79 / 25.4), 0.005);
r = H.run('trace', { mode: 'current', w: String(15.52 / (34.79 / 25.4) * 0.0254), dT: '10', oz: '1' }); check('IPC-2152 inverse', o(r, 'Maximum current'), 1, 0.005);
// IPC-2221B Table 6-1 (two independent reproductions): B2 1000 V = 5.0, B4 1000 V = 2.325, B1 48 V = 0.1
r = H.run('spacing', { V: '1000', col: 'B2 external, uncoated (sea level – 3050 m)' }); check('spacing B2 1kV', o(r, 'Minimum spacing'), 5.0);
r = H.run('spacing', { V: '1000', col: 'B4 external, with permanent polymer coating / solder mask' }); check('spacing B4 1kV', o(r, 'Minimum spacing'), 2.325);
r = H.run('spacing', { V: '48', col: 'B1 internal layer' }); check('spacing B1 48V', o(r, 'Minimum spacing'), 0.1);
r = H.run('spacing', { V: '300', col: 'B2 external, uncoated (sea level – 3050 m)' }); check('spacing B2 300V', o(r, 'Minimum spacing'), 1.25);
r = H.run('diffpair', { geom: 'microstrip', w: '0.15', s: '0.15', h: '0.1', oz: '0.5', er: '4.3' }); check('diffpair Zdiff', o(r, 'Differential'), 94.9, 0.01);
r = H.run('attenuator', { dB: '6', Z: '50' }); check('6dB pi shunt', o(r, 'Pi: shunt'), 150.5, 0.002); check('6dB pi series', o(r, 'Pi: series'), 37.35, 0.002);
r = H.run('vswr', { val: '1.5', kind: 'VSWR', Z0: '50' }); check('VSWR1.5 RL', o(r, 'Return loss'), 13.98, 0.002); check('VSWR1.5 ML', o(r, 'Mismatch'), 0.177, 0.01);
r = H.run('timer555', { mode: 'astable', RA: '1k', RB: '10k', C: '10n' }); check('555 f', o(r, 'Frequency'), 1.44 / (21e3 * 10e-9));
r = H.run('regulator', { mode: 'R2 for target Vout', Vref: '1.25', Iadj: '50u', R1: '240', Vout: '5', series: 'E96' }); check('LM317 R2', o(r, 'Exact R2'), 3.75 / (1.25 / 240 + 50e-6));
r = H.run('ntc', { mode: 'resistance at temperature', R25: '10k', B: '3435', T: '25' }); check('NTC R25', o(r, 'Resistance'), 10000);
r = H.run('ntc', { mode: 'resistance at temperature', R25: '10k', B: '3435', T: '60' }); check('NTC 60C', o(r, 'Resistance'), 2981, 0.005);
r = H.run('ntc', { mode: 'temperature from resistance', R25: '10k', B: '3435', R: '2981' }); check('NTC inverse', o(r, 'Temperature'), 60, 0.005);
r = H.run('skin', { f: '100M', mat: 'copper' }); check('skin 100MHz', o(r, 'Skin depth'), 6.6, 0.01);
r = H.run('via', { d: '0.3', tp: '25', h: '1.6', pad: '0.6', anti: '1.0', er: '4.3', dT: '10', Z0: '50' }); check('via R', o(r, 'DC resistance'), 1.724e-8 * 0.0016 / (Math.PI * (0.175 ** 2 - 0.15 ** 2) * 1e-6)); check('via L', o(r, 'Inductance'), 1.30e-9, 0.01);
r = H.run('fusing', { shape: 'PCB trace', w: '0.25', oz: '1', t: '1', Ta: '25' }); check('Onderdonk', o(r, 'Fusing'), 17.17 * Math.sqrt(Math.log10(1 + 1058 / 259) / 33), 0.005);
r = H.run('rfind', { target: '3k57', series: 'E24', cfg: 'series or parallel' }); check('rfind <0.3%', Math.abs(o(r, 'Error')) < 0.3 ? 1 : 0, 1);
r = H.run('spiral', { shape: 'square', n: '5', dout: '5', din: '2' }); check('spiral L', o(r, 'Inductance'), 118.1e-9, 0.005);
r = H.run('bandwidth', { tr: '1n', er: '3.3', len: '100' }); check('BW 1ns', o(r, '−3 dB'), 350e6);
r = H.run('combine', { kind: 'resistors', cfg: 'parallel', list: '10k, 10k' }); check('R par', o(r, 'Equivalent'), 5000);
r = H.run('combine', { kind: 'capacitors', cfg: 'series', list: '10n, 10n' }); check('C ser', o(r, 'Equivalent'), 5e-9);
r = H.run('opamp', { cfg: 'non-inverting', Rf: '10k', Rg: '1k', gbw: '10M', sr: '10M', Vpk: '2' }); check('opamp G', o(r, 'Gain'), 11); check('opamp BW', o(r, 'Small-signal'), 10e6 / 11);
r = H.run('pdn', { V: '1', rip: '3', dI: '5', fmax: '100M' }); check('PDN Z', o(r, 'Target'), 0.006);
r = H.run('mosfet', { V: '24', I: '5', Rds: '10m', kT: '1.5', D: '50', fsw: '100k', tr: '20n', tf: '20n', Qg: '30n', Vgs: '10' }); check('mosfet Pc', o(r, 'Conduction'), 0.1875); check('mosfet Psw', o(r, 'Switching'), 0.5 * 24 * 5 * 40e-9 * 1e5);
r = H.run('ppm', { f: '32.768k', ppm: '20' }); check('ppm drift/day', o(r, 'Clock drift per day'), 1.728);
r = H.run('units', { val: '10', u: 'mil' }); check('10 mil', o(r, 'mm'), 0.254);
for (const c of H.CALCS) { const rr = H.run(c.id, {}); if (!rr.ok) { n++; fail++; console.log('FAIL defaults of', c.id, rr.error); } else n++; }


// schematics: every drawing renders for the defaults and for every option of every select input
{ const S = require('./schematics.js'); let n = 0, bad = [];
  for (const id of S.ids) { const c = H.CALCS.find(x => x.id === id); if (!c) { bad.push(id + ' (no calc)'); continue; }
    const variants = [{}]; for (const f of c.inputs) if (f.type === 'select') for (const opt of f.options) variants.push({ [f.key]: opt });
    for (const v of variants) { const svg = S.draw(id, v); n++; if (!svg || !svg.startsWith('<svg') || /undefined|NaN/.test(svg)) bad.push(id + ' ' + JSON.stringify(v)); } }
  check(`schematics render (${n} variants, ${S.ids.length} drawings)`, bad.length, 0); if (bad.length) console.log('  bad:', bad.join('; ')); }

// wiki page build: README.html inlines calcs.js / schematics.js by replacing these exact tags in public/index.html
{ const fs = require('fs'), html = fs.readFileSync(__dirname + '/public/index.html', 'utf8'), readme = fs.readFileSync(__dirname + '/README.html', 'utf8');
  check('index.html has the calcs.js tag README.html replaces', html.includes('<script src="calcs.js"></script>') ? 1 : 0, 1);
  check('index.html has the schematics.js tag README.html replaces', html.includes('<script src="schematics.js"></script>') ? 1 : 0, 1);
  check('index.html supports static mode', html.includes('window.HWCALC_STATIC') ? 1 : 0, 1);
  const hostJs = readme.slice(readme.indexOf('<script>'), readme.lastIndexOf('</script>') + 1);
  check('README.html host script has no literal closing script tag', (hostJs.match(/<\/script/gi) || []).length, 0); }
console.log(`${n - fail}/${n} checks passed`); process.exit(fail ? 1 : 0);