Factor from text
64-bitA factor from a plain number, a percent of the size, or a percent change.
f = n; n ÷ 100; 1 ± n ÷ 100
The text is read with Number, a decimal comma is accepted, and the result is divided or added once. A factor at or below 1e-6, at or above 1e6, or not a number gives null.
/** A factor from text: a plain number, "101.5%" (of the size), or "+1.5%" / "-2%" (change). */
export function parseFactor(text) {
const s = String(text ?? '').trim().replace(',', '.').replace(/\s+/g, '');
if (!s) return null;
const m = /^([+-]?)(\d*\.?\d+(?:e[+-]?\d+)?)(%?)$/i.exec(s);
if (!m) return null;
const n = Number(m[2]);
if (!Number.isFinite(n)) return null;
let f;
if (m[3]) f = m[1] ? 1 + (m[1] === '-' ? -n : n) / 100 : n / 100;
else f = m[1] === '-' ? -n : n;
return f > 1e-6 && f < 1e6 ? f : null;
}