Creates a new instance of Base given some user input to parse or an existing list of ranges to use instead.
The input to parse if ranges is not provided.
The already parsed ranges to use for this instance.
The input parsed to generate this instance or the input passed from the parent instance when an operation was performed on it.
The list of ranges in this instance.
Returns whether this instance has actual ranges. An actual range is where the minimum and maximum values differ.
Returns whether this instance has values or ranges that are rates.
Returns true if this instance has a single fixed value.
Returns true if this instance has a single range.
Returns true if this instance has a single rate.
Returns whether this instance only has valid ranges. If any of the ranges in this instance are not valid false is returned, otherwise true.
Returns the number of ranges in this instance.
Adds the ranges of this instance and the given input together. When the ranges use the same units they are added together, otherwise they are added to the end of the range list.
For example:
uz('1pt').add('2pt, 1c'); // '3pt, 1c'
uz('1pt').add('2pt, 1c', 2); // '5pt, 2c'
An instance or input which can be parsed into an instance.
A number to multiple the input by when adding it to this instance.
A new instance.
Returns an array of the classes represented in this instance. If there are no classes in this instance then an empty array is returned.
An array of the classes in this instance.
Joins all ranges of the same classes together and uses the largest unit to represent the sum for the class.
For example:
uz('1c, 1pt').compact(); // '1.5pt'
Options to control which units and values are acceptable.
A new instance.
Joins all ranges of the same classes together and then calculates all equivalent ranges for each range for each valid group according to the given options.
For example:
uz('1.5pt').conversions(); // '3/16gal, 3/4qt, 1 1/2pt, 3c, 24floz, 48tbsp, 144tsp'
uz('20celsius, 45deg'); // '68F, 20celsius, 45deg, 0.785rad'
Options to control which units and values are acceptable.
A new instance.
Converts the appropriate ranges in this instance into the desired unit and returns their converted sum. If the given unit does not map to a group then null is returned. If there are no ranges in this instance in the same class then the range returned is equivalent to zero.
For example:
uz('1in, 1m, 1ft').convert('cm'); // '133.02 cm'
uz('60 mph').convert('miles per minute'); // '1 miles/minute'
The unit to calculate the sum of.
A new range which is the sum of ranges in the same class converted to the desired unit.
Iterates over each range in this instance in order or reversed and passes each one to the given iterate function. If the iterate function returns false the iteration will stop.
The function to invoke with each range and it's index.
The current range being iterated.
The index of the current range in this instance.
Whether the iteration should be done forward or backward.
The reference to this instance.
Joins all ranges of the same classes together and then separates them into whole number ranges for better readability.
For example:
uz('1.5pt').expand(); // '1pt, 1c'
uz('53in').expand(); // '4ft, 5in'
uz('2ft, 29in').expand(); // '4ft, 5in'
uz('6543mm').expand(); // '6 m, 54 cm, 3 mm'
Options to control which units and values are acceptable.
A new instance.
Removes the ranges from this instance that aren't valid according to the transform options provided taking into account the global options.
For example:
uz('1in, 2m').filter({system: Unitz.System.METRIC}); // '2m'
Options to control which units and values are acceptable.
A new instance.
Converts each range to fractions if a denominator for the specified units yields a fraction close enough to the original value.
For example:
uz('1/2 cup').fractions(); // '1/2 cup'
uz('0.3cm').fractions(); // '3/10 cm'
uz('0.33 decades').fractions(); // '0.33 decades' closest is 3/10 but that's not close enough
A new instance.
Calculates what this instance would need to be scaled by so that the given value & unit pair is equal to the sum of ranges in this instance of the same class. If there are no ranges with the same class then zero is returned. If the sum of ranges with the same class results in an actual range (where min != max) then you can specify how to pick a value from the range with rangeDetla. A value of 0 uses the min, 1 uses the max, and 0.5 uses the average between them.
For example:
uz('1m, 2 - 3c').getScaleTo('6c'); // 2
uz('1m, 2 - 3c').getScaleTo('6c', 0); // 3
uz('1m, 2 - 3c').getScaleTo('6c', 0.5); // 2.4
uz('1m, 2 - 3c').getScaleTo('45deg'); // 0
A value & unit pair to scale the ranges in this instance to.
When this instance contains ranges this value instructs how the scale factor is calculated. A value of 0 means it looks at the minimum, 1 is the maximum, and 0.5 is the average.
A value to scale by or zero if this instance cannot match the input.
Flattens any ranges to their maximum values.
For example:
uz('1 - 3c, 5m').max(); // '3c, 5m'
A new instance or this if this instance has no ranges.
Flattens any ranges to their minimum values.
For example:
uz('1 - 3c, 5m').max(); // '1c, 5m'
A new instance or this if this instance has no ranges.
Executes the given function on each range in this instance and if the function returns a valid range its added to the result.
For example:
uz('1.5pt').mutate(r => r.scale(2)); // '3pt'
The function which may return a range.
A new instance.
Drops positive ranges and modifies partially positive ranges so that all values are less than zero.
For example:
uz('0c, 2tbsp, -4tbsp').negative(); // '-4tbsp'
uz('-2 - 3 in').negative(); // '-2 - 0in'
A new instance.
Drops ranges that are equal to zero.
For example:
uz('0c, 2tbsp').negative(); // '2tbsp'
A new instance.
Converts each range to units that best represent the value.
For example:
uz('1.5pt, 12in, 3.14159rad').normalize(); // '3c, 1ft, 180deg'
Options to control which units and values are acceptable.
The output options that should be used to determine which value & unit is best.
A new instance.
Converts each range to numbers if they are fractions.
For example:
uz('1/2 cup').fractions(); // '0.5 cup'
uz('0.3cm').fractions(); // '0.3 cm'
A new instance.
Subtracts the given input from the ranges of this instance. When the ranges use the same units they are subtracted, otherwise they are added to the end of the range list and negated.
For example:
uz('3pt').sub('2pt, 1c'); // '1pt, -1c'
uz('1pt').add('2pt, 1c', 2); // '-3pt, -2c'
An instance or input which can be parsed into an instance.
A function to call when matching ranges are found and an operation should be performed between them. The range returned by this function ends up in the result.
A function to call on a range that did not have a match in this instance where the range returned is added to the result.
A new instance.
Converts the ranges in this instance to a string with the given output options taking into account the global options.
The options to override the global output options.
The string representation of this instance.
Drops negative ranges and modifies partially negative ranges so that all values are greater than or equal to zero.
For example:
uz('0c, 2tbsp, -4tbsp').positive(); // '0c, 2tbsp'
uz('-2 - 3 in').positive(); // '0 - 3in'
A new instance.
Changes the units used on each of the ranges in this instance to the preferred unit for each group.
For example:
uz('5 kilos').preferred(); // '5 kg'
A new instance.
Scales the ranges in this instance by the given factor and returns a new instance.
For example:
uz('1c, 2.3m').scale(2); // '2c, 4.6m'
The factor to scale the ranges in this instance by.
A new instance.
Scales the ranges in this instance up to some value with a unit and returns a new instance. Because this instance might contain ranges, a rangeDelta can be specified to instruct on which value (min or max) to use when calculating how much to scale by.
For example:
uz('1m, 2 - 3c').scaleTo('6c'); // '2m, 4 - 6c'
uz('1m, 2 - 3c').scaleTo('6c', 0); // '3m, 6 - 9c'
uz('1m, 2 - 3c').scaleTo('6c', 0.5); // '2.4m, 4.8 - 6c'
A value & unit pair to scale the ranges in this instance to.
When this instance contains ranges this value instructs how the scale factor is calculated. A value of 0 means it looks at the minimum, 1 is the maximum, and 0.5 is the average.
A new instance.
Sorts the ranges in this instance based on the options provided taking into account the global options.
For example:
uz('1in, 3ft, 1.3yd, 1m').sort(); // 1.3yd, 1m, 3ft, 1in
uz('1in, 3ft, 1.3yd, 1m').sort({ascending: true}); // 1in, 3ft, 1m, 1.3yd
uz('1-3cups, 2-2.5cups, 4in').sort({
type: Unitz.SortType.MIN,
classes: {
Volume: 1,
Length: 2
}
}); // 4in, 2 - 2.5cups, 1 - 3cups
Options to control how sorting is done.
A new instance.
Subtracts the given input from the ranges of this instance. When the ranges use the same units they are subtracted, otherwise they are added to the end of the range list and negated.
For example:
uz('3pt').sub('2pt, 1c'); // '1pt, -1c'
uz('1pt').add('2pt, 1c', 2); // '-3pt, -2c'
An instance or input which can be parsed into an instance.
A number to multiple the input by when subtracting it from this instance.
A new instance.
Performs a test on the ranges in this instance and returns whether the
ranges passed the test. If the tester
function returns something
different than expected
then the function ends early with !passed
.
If all tests pass then passed
is returned.
The expected result of the tester.
The value to return if all ranges pass the test.
The function to test a range.
Return passed
if all ranges return expected
from tester
.
Alias for Base.convert.
Generated using TypeDoc
The main class which contains a list of ranges and the user input.