Add next calculator batch incl molarity and SWG

This commit is contained in:
Codex Agent
2026-03-08 02:19:18 +00:00
parent 5e974ea9c7
commit 1e341f051f
4 changed files with 161 additions and 43 deletions

View File

@@ -1,5 +1,5 @@
// THIS FILE IS AUTO-GENERATED BY migrate.py
export type CalcType = 'standard' | 'inverse' | '3col' | '3col-mul' | 'base' | 'text-bin' | 'bin-text' | 'dms-dd' | 'dd-dms' | 'dec-frac' | 'db-int' | 'db-spl' | 'db-v' | 'db-w' | 'awg' | 'brinell-rockwell' | 'ev-lux' | 'aov';
export type CalcType = 'standard' | 'inverse' | '3col' | '3col-mul' | 'base' | 'text-bin' | 'bin-text' | 'dms-dd' | 'dd-dms' | 'dec-frac' | 'db-int' | 'db-spl' | 'db-v' | 'db-w' | 'awg' | 'brinell-rockwell' | 'ev-lux' | 'aov' | 'swg' | 'rockwell-vickers' | 'sus-cst' | 'molarity';
export interface CalculatorDef {
slug: string;
@@ -1636,6 +1636,11 @@ export const calculators: CalculatorDef[] = [
{"slug": "f-stops-to-t-stops", "name": "f-stops to T-stops", "category": "other", "type": "standard", "teaser": "Treat f-number as t-stop under ideal transmission.", "labels": {"in1": "f-stops", "in2": "T-stops"}},
{"slug": "focal-length-to-angle-of-view", "name": "Focal length to Angle of view", "category": "other", "type": "aov", "teaser": "Approximate horizontal angle on 35mm full-frame (36mm width).", "labels": {"in1": "Focal length", "in2": "Angle of view"}},
{"slug": "millimeters-to-awg", "name": "Millimeters to AWG", "category": "electrical", "type": "awg", "teaser": "Convert conductor diameter in millimeters to AWG gauge.", "labels": {"in1": "Millimeters", "in2": "AWG"}},
{"slug": "molarity-to-grams-per-liter", "name": "Molarity to Grams per liter", "category": "other", "type": "molarity", "teaser": "Convert molar concentration to grams per liter using molar mass.", "labels": {"in1": "Molarity (mol/L)", "in2": "Grams per liter", "in3": "Molar mass (g/mol)"}},
{"slug": "rockwell-c-to-brinell", "name": "Rockwell C to Brinell", "category": "other", "type": "brinell-rockwell", "teaser": "Convert Rockwell C hardness into Brinell hardness numbers.", "labels": {"in1": "Rockwell C", "in2": "Brinell"}, "hidden": true},
{"slug": "rockwell-c-to-vickers", "name": "Rockwell C to Vickers", "category": "other", "type": "rockwell-vickers", "teaser": "Convert Rockwell C hardness into Vickers hardness values.", "labels": {"in1": "Rockwell C", "in2": "Vickers"}},
{"slug": "saybolt-universal-seconds-to-centistokes", "name": "Saybolt Universal Seconds to Centistokes", "category": "fluids", "type": "sus-cst", "teaser": "Translate Saybolt viscosity readings into kinematic centistokes.", "labels": {"in1": "Saybolt Universal Seconds", "in2": "Centistokes"}},
{"slug": "swg-to-millimeters", "name": "SWG to Millimeters", "category": "electrical", "type": "swg", "teaser": "Convert Standard Wire Gauge sizes to millimeter diameters.", "labels": {"in1": "SWG", "in2": "Millimeters"}},
];

View File

@@ -290,6 +290,100 @@ export function solve(
}
break;
}
case 'molarity': {
const m = v1; // mol/L
const gpl = v2; // grams/L
const molarMass = v3; // g/mol
if (source === 1) {
out.val2 = (!isNaN(m) && !isNaN(molarMass)) ? fmt(m * molarMass) : '';
} else if (source === 2) {
out.val1 = (!isNaN(gpl) && !isNaN(molarMass) && molarMass !== 0) ? fmt(gpl / molarMass) : '';
} else {
if (!isNaN(m) && !isNaN(molarMass)) out.val2 = fmt(m * molarMass);
else if (!isNaN(gpl) && !isNaN(molarMass) && molarMass !== 0) out.val1 = fmt(gpl / molarMass);
}
break;
}
case 'rockwell-vickers': {
const hrcToBhn = (h: number) => (1520000 - 4500 * h) / Math.pow(100 - h, 2);
const bhnToHrc = (b: number) => {
const disc = 4500 ** 2 + 4 * b * 1070000;
const y = (4500 + Math.sqrt(disc)) / (2 * b);
return 100 - y;
};
const bhnToHv = (b: number) => b * 0.95;
const hvToBhn = (hv: number) => hv / 0.95;
if (source === 1) {
const hrc = v1;
if (!isNaN(hrc) && hrc < 100) {
const hv = bhnToHv(hrcToBhn(hrc));
out.val2 = fmt(hv);
} else out.val2 = '';
} else {
const hv = v2;
if (!isNaN(hv) && hv > 0) {
const bhn = hvToBhn(hv);
out.val1 = fmt(bhnToHrc(bhn));
} else out.val1 = '';
}
break;
}
case 'sus-cst': {
const susToCst = (sus: number) => {
if (sus <= 0) return NaN;
if (sus < 100) return 0.226 * sus - 195 / sus;
return 0.22 * sus - 135 / sus;
};
const cstToSus = (cst: number) => {
if (cst <= 0) return NaN;
const low = (cst + Math.sqrt(cst * cst + 4 * 0.226 * 195)) / (2 * 0.226);
const high = (cst + Math.sqrt(cst * cst + 4 * 0.22 * 135)) / (2 * 0.22);
return low < 100 ? low : high;
};
if (source === 1) {
out.val2 = !isNaN(v1) ? fmt(susToCst(v1)) : '';
} else {
out.val1 = !isNaN(v2) ? fmt(cstToSus(v2)) : '';
}
break;
}
case 'swg': {
const swgTable: Record<number, number> = {
0: 8.23, 1: 7.62, 2: 7.01, 3: 6.4, 4: 5.89, 5: 5.38, 6: 4.88, 7: 4.47,
8: 4.06, 9: 3.66, 10: 3.25, 11: 2.95, 12: 2.64, 13: 2.34, 14: 2.03, 15: 1.83,
16: 1.63, 17: 1.42, 18: 1.22, 19: 1.02, 20: 0.91, 21: 0.81, 22: 0.71, 23: 0.61,
24: 0.56, 25: 0.51, 26: 0.46, 27: 0.42, 28: 0.38, 29: 0.35, 30: 0.32, 31: 0.29,
32: 0.27, 33: 0.25, 34: 0.23, 35: 0.21, 36: 0.19, 37: 0.17, 38: 0.15, 39: 0.14,
40: 0.12, 41: 0.11, 42: 0.1, 43: 0.09, 44: 0.08, 45: 0.07, 46: 0.064, 47: 0.058,
48: 0.051, 49: 0.045, 50: 0.04
};
const gaugeToMm = (g: number) => swgTable[Math.round(g)];
const mmToGauge = (mm: number) => {
let best = -1, bestDiff = Infinity;
for (const [gStr, diam] of Object.entries(swgTable)) {
const diff = Math.abs(mm - diam);
if (diff < bestDiff) { bestDiff = diff; best = parseInt(gStr, 10); }
}
return best;
};
if (calc.labels.in1.toLowerCase().includes('swg')) {
if (source === 1) out.val2 = !isNaN(v1) ? fmt(gaugeToMm(v1) ?? NaN) : '';
else out.val1 = (!isNaN(v2) && v2 > 0) ? fmt(mmToGauge(v2)) : '';
} else {
if (source === 1) out.val2 = (!isNaN(v1) && v1 > 0) ? fmt(mmToGauge(v1)) : '';
else out.val1 = !isNaN(v2) ? fmt(gaugeToMm(v2) ?? NaN) : '';
}
break;
}
}
return out;