Unit
Simple TypeScript/ES2017 classes to represent unit values (like time/data size)
Installation
npm install --save data-unit
Usage
Using data size units:
; ; // Easily print both the value and the unitdata.toString;data.toHumanString; // Converting and Retrieving the valuedata.convert DataUnit.MEGABYTES ; // returns a data amount objectdata.as DataUnit.MEGABYTES ; // returns a numberdata.value; data.unit; // Get the current value and unit // Math Operations alloweddata.plus 10 ; // When a number is provided, the same unit is useddata.minus '10GB' ; // A string allows to specify custom unitsdata.mul data ; // Can mul two objects as welldata.div data ; // All math operationsdata.plus 10 ;data.minus 10 ;data.mul 10 ;data.div 10 ; // Comparison operationsdata.atMost '10Mb' ; // clamps the value to a max of 10Mbdata.atLeast '1GB' ; // clamps the value to a min of 1GBdata.clamp '10Mb', '1GB' // clamps the value anywhere between 10Mb and 1GB // Also some auxiliary methods to handle decimal placesdata.ceil; data.floor; data.round; data.round 2 ;
Using duration units:
; ;; // 10 seconds; // 1 minute and 31 seconds; // 1 hour, 31 minutes, 22 seconds and 500 milliseconds // We can then use the same methods as in data amount objects