Decent conversion of measurement units.
const decentUnit = require('@lakca/decent-unit')
// decent result: { value: 2, unit: 'G' }
decentUnit(2 * 1024 ** 3)
// decent result: { value: 2, unit: 'E' }
decentUnit(2 * 1024 ** 3, { fromUnit: 'G' })
// decent result { value: 2, unit: 'E' }
decentUnit(2 * 1000 ** 3, { fromUnit: 'G', radix: 1000 })
// decent result { value: 1, unit: 'G' }
decentUnit(20000, { fromUnit: '', radix: [100, 20, 10, 2], units: ['', 'K', 'M', 'G', 'T'] })
// expected result { value: 2048, unit: 'P' }
decentUnit(2 * 1024 ** 3, { fromUnit: 'G', toUnit: 'P' })
More in test.
unit of
num
.
target unit, if empty, decent unit will be returned.
conversion radix. If is an
array
, element is radix between every nearby units, and filled with the last array element if length is more than 1 less thanoptions.units
.
available units
See decentUnit
MIT