human-unit
Unit formatter for human
Install
npm install human-unit
Usage
- File size
// {value: 1, unit: 'KB'} // {value: 1, unit: 'GB'}// change units as you need// {value: 1, unit: 'G'}
- Time unit
const timePreset = factors: 1000 60 60 24 units: 'mili' 'sec' 'min' 'hour' 'day' // {value: 5, unit: 'hour'} // {value: 1, unit: 'min'}
API
humanUnit(value: number, options?:IOptions)
return { value: number; unit: string; }
The factors
is the divider when bumpping units levels, can be a fixed number
like in file size of 1024
, or can be array of numbers
like in time units.
The ceils
value will be used to determine whether the unit should be bumped into next level, say, ceils: [10240]
, will allow the result: {value: 10000, unit: 'MB'}
, better for human control.
Note
The file size calculation is easy if you look into the rule, I want to use SI standard when below GB
, so the units is B,KB,MB,GB
(notice the KB
is not kB
), but when the size is very big, after GB
I want to use IEC
standard, this is why the factors
is array of numbers by default that changed from 1024
to 1000
after GB
.