gen-unit
A generic unit parser/formatter
Install
# Using npm npm i gen-unit # Using yarn yarn add gen-unit
API
createParser
Creates a parse
function using the given options.
; ;
Options
unit
The "unit"
option defines the unit to be used during parsing.
unit: string;
example
; parse'1'; // => 1parse'1 g'; // => 1parse'1 mg'; // => 0.001parse'1 K'; // => 1000parse'1 Kg'; // => 1000parse'1 ms'; // => NaN
- Precedence
This option takes precedence over any prefix or prefixed unit.
examples
; parse'1 m'; // => 1 (not 0.001)parse'1 mm'; // => 0.001
; parse'1 meg'; // => 0.001 (not 1000000)parse'1 megeg'; // => 1000000parse'1 Meg'; // => 1000000
find
The "find"
option describes how to find the number
by which the parsed value should be multiplied.
- "find" option as an object
find: ; default:
An object describing the base
and unit prefixes
to find the multiplier
.
example
; parse'1'; // => 1parse'1 K'; // => 1024parse'1 M'; // => 1048576parse'1 G'; // => NaN
- "find" option as a number
find: number;
A number to be used as base
during parsing.
example
; parse'2'; // => 2parse'2 K'; // => 2048parse'2 M'; // => 2097152parse'2 G'; // => 2147483648
- "find" option as an array
find: Array<>;
An array
of objects
describing the different units prefixes
.
example
; parse'1.3'; // => 1.3parse'1.3 K'; // => 1300parse'1.3 M'; // => 1300000parse'1.3 G'; // => NaN
- "find" option as a function
find:number;
A function that returns a number
by which the parsed value should be multiplied based on the captured unit
.
example
; parse'2'; // => 2parse'2 K'; // => 2048parse'2 M'; // => 2097152parse'2 G'; // => NaN
createFormatter
Creates a format
function using the given options.
; ;
Options
unit
The "unit"
option defines the unit
to be used during formatting.
- "unit" option as a string
unit: string;
A string
to be used as main unit
.
example
; format100; // => '100 m'format00012; // => '1.2 mm'format1200; // => '1.2 Km'
- "unit" option as a function
unit:string;
A function
that returns
the unit
based on parameters.
example
; format100; // => '100 m'format00012; // => '1.2 mm'format1200; // => '1.2 Km'
find
The "find"
option describes how to find the unit prefix
based on input value.
- "find" option as an object
find: default:
example
; format100; // => '100'format2048; // => '2 K'format2097152; // => '2048 K'
- "find" option as a number
find: number;
A number
to be used as base
during formatting.
example
; format100; // => '100'format2048; // => '2 K'format2097152; // => '2 M'
- "find" option as an array
find: Array<>;
An array
of objects
describing the different unit prefixes
.
example
; format2; // => '2'format2000; // => '2 K'format2000000; // => '2000 K'
- "find" option as a function
find:;
A function
that returns
an object
describing the unit prefix
.
example
; format2; // => '2'format2000; // => '2 K'format2000000; // => '2000 K'
round
The "round"
option describes how to round the value.
- "round" option as an object
round: ; default: ;
An object
describing how to format the value.
example
; format123; // => '1.230'format1230; // => '1.230 K'format000123; // => '1.230 m'
- "round" option as a number
round: number;
A number
defining the number of decimal places to round to.
example
; format123; // => '1.2'format1230; // => '1.2 K'format000123; // => '1.2 m'
- "round" option a function
round:string | number;
A function
that returns
a rounded value.
example
; format123; // => '1'format1230; // => '1 K'format000123; // => '1 m'
output
A function
to format the final output.
output:string | number;
example
format123; // => '1.23s'format1230; // => '1.23Ks'format000123; // => '1.23ms'
parse
A convenient function to parse an input in one step.
;
format
A convenient function to format a number
in one step.
;