1 | {"version":3,"file":"metric.cjs","sources":["../metric.js"],"sourcesContent":["/**\n * Utility module to convert metric values.\n *\n * @module metric\n */\n\nimport * as math from './math.js'\n\nexport const yotta = 1e24\nexport const zetta = 1e21\nexport const exa = 1e18\nexport const peta = 1e15\nexport const tera = 1e12\nexport const giga = 1e9\nexport const mega = 1e6\nexport const kilo = 1e3\nexport const hecto = 1e2\nexport const deca = 10\nexport const deci = 0.1\nexport const centi = 0.01\nexport const milli = 1e-3\nexport const micro = 1e-6\nexport const nano = 1e-9\nexport const pico = 1e-12\nexport const femto = 1e-15\nexport const atto = 1e-18\nexport const zepto = 1e-21\nexport const yocto = 1e-24\n\nconst prefixUp = ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']\nconst prefixDown = ['', 'm', 'μ', 'n', 'p', 'f', 'a', 'z', 'y']\n\n/**\n * Calculate the metric prefix for a number. Assumes E.g. `prefix(1000) = { n: 1, prefix: 'k' }`\n *\n * @param {number} n\n * @param {number} [baseMultiplier] Multiplier of the base (10^(3*baseMultiplier)). E.g. `convert(time, -3)` if time is already in milli seconds\n * @return {{n:number,prefix:string}}\n */\nexport const prefix = (n, baseMultiplier = 0) => {\n const nPow = n === 0 ? 0 : math.log10(n)\n let mult = 0\n while (nPow < mult * 3 && baseMultiplier > -8) {\n baseMultiplier--\n mult--\n }\n while (nPow >= 3 + mult * 3 && baseMultiplier < 8) {\n baseMultiplier++\n mult++\n }\n const prefix = baseMultiplier < 0 ? prefixDown[-baseMultiplier] : prefixUp[baseMultiplier]\n return {\n n: math.round((mult > 0 ? n / math.exp10(mult * 3) : n * math.exp10(mult * -3)) * 1e12) / 1e12,\n prefix\n }\n}\n"],"names":["math.log10","math.round","math.exp10"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AAGA;AACY,MAAC,KAAK,GAAG,KAAI;AACb,MAAC,KAAK,GAAG,KAAI;AACb,MAAC,GAAG,GAAG,KAAI;AACX,MAAC,IAAI,GAAG,KAAI;AACZ,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