Creates a standard measure that is used to express amounts.
In contrast to the Unit the use of prefixes, suffixes and similar is integrated without adding much complexity.
the unit's format (%
= variable)
the ratio between this and the group's base unit
the shift between this and the group's base unit
the unit system the unit is belonging to
the unit's variables
Generated using TypeDoc
Represents a standard measure that is used to express amounts.
In contrast to the Unit the use of prefixes, suffixes and similar is integrated without adding much complexity.
This is done via so-called "variables" (Variable), which are integrated into the format of the unit (Unit.format).
Example: We have the unit "meter per second" (as base unit):
Now we also want to create the units "meters per minute", "kilometers per second" and "kilometers per minute". We could now simply define three more units:
But as we see, we would have unnecessary repetitive code. This is exactly the kind of case where the FlexibleUnit comes in handy. This is how the code would look like with the FlexibleUnit:
Much shorter, isn't it? But what exactly is happening here? First, we create the Variable
var1
. A variable is nothing else than a placeholder that can take different states. We pass an array holding the different values the variable can take to the constructor. Because we only need the optional prefixk
(long term:kilo
) we just define one single value here. We know1km = 1000m
, so we specify1000
for the ratio. Withtrue
we set the variable as optional. After that we create the second variablevar2
. We pass an array holding the different values the variable can take. These are the different times we want to support. First we define the value for thesecond
(short term:s
). We set the ratio to 1 becausem/s
is our base. After that we define the value for theminute
(short term:min
). We know1m/s = 1/60m/min
. Therefore we set the ratio to1/60
. Withfalse
we set the variable as mandatory.The second new thing is the unit's different format. There are new weird looking
%number
terms. These serve as a indicators for the previously defined variables. Thus%0m/%1
becomeskm/s
,m/s
,m/min
andkm/min
. Of course, the FlexibleUnit knows nothing about the previous variables, which is why we pass them into the constructor. The number after the%
indicates the variable's index in the passed array.And that's it!