Options
All
  • Public
  • Public/Protected
  • All
Menu

Class FlexibleUnit

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):

const meterPerSecond = new Unit({ short: ["m/s"],
     long: {
         sg: ["meter per second", "metre per second"],
         pl: ["meters per second", "metres per second"]
     }
}, 1, 0, "metric");

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:

// 1 m/min = 1/60 m/s
const meterPerMinute = new Unit({ short: ["m/min"],
     long: {
         sg: ["meter per minute", "metre per minute"],
         pl: [["meters per minutes", "metres per minutes"]
     }
}, 1/60, 0, "metric");

// 1 km/s = 1000 m/s
const kilometerPerSecond = new Unit({ short: ["km/s"],
     long: {
         sg: ["kilometer per second", "kilometre per second"],
         pl: ["kilometers per second", "kilometres per second"]
     }
}, 1000, 0, "metric");

// 1 km/min = 16.666666667 m/s
const kilometerPerMinute = new Unit({ short: ["km/min"],
     long: {
         sg: ["kilometer per minute", "kilometre per minute"],
         pl: [["kilometers per minutes", "kilometres per minutes"]
     }
}, 16.666666667, 0, "metric");

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:

const var1 = new Variable([
     new Value("k", "kilo", 1000),
], true);
const var2 = new Variable([
     new Value("s", "second", 1),
     new Value("min", "minute", 1/60),
], false);

const unit = new FlexibleUnit({ short: ["%0m/%1"],
     long: {
         sg: ["%0meter per %1", "%0metre per %1"],
         pl: ["%0meters per %1", "%0metres per %1]"
     }
}, 1, 0, "metric", [var1, var2]);

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 prefix k (long term: kilo) we just define one single value here. We know 1km = 1000m, so we specify 1000 for the ratio. With true we set the variable as optional. After that we create the second variable var2. 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 the second (short term: s). We set the ratio to 1 because m/s is our base. After that we define the value for the minute (short term: min). We know 1m/s = 1/60m/min. Therefore we set the ratio to 1/60. With false 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 becomes km/s, m/s, m/min and km/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!

Hierarchy

  • FlexibleUnit

Index

Constructors

Constructors

constructor

  • new FlexibleUnit(format: UnitFormat, ratio: number, shift: number, system: string, variables: Variable[]): FlexibleUnit
  • 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.

    Parameters

    • format: UnitFormat

      the unit's format (% = variable)

    • ratio: number

      the ratio between this and the group's base unit

    • shift: number

      the shift between this and the group's base unit

    • system: string

      the unit system the unit is belonging to

    • variables: Variable[]

      the unit's variables

    Returns FlexibleUnit

Generated using TypeDoc