new Unitz.Parsed(value, unit, unitClass, normal)
Instantiates a new parsed instance with a number, unit, string representation, and optionally the unit class if the unit was recognized by Unitz.
| Name | Type | Description |
|---|---|---|
value |
Number |
The parsed number. |
unit |
String |
The unit for the number (empty string if no unit specified). |
unitClass |
Unitz.Class |
optional
The class for the unit (if any). |
normal |
String |
The normalized representation of the number and unit combined. If a unit class is given this will be re-calculated using the proper singular/plural unit for it's group. |
Members
-
groupUnitz.Group
-
The group of the unit (if any).
-
normalString
-
The string representation of the parsed value. If this instance has a group this will be the current value with the appropriate singular/plural unit - otherwise this will be the input passed to the
Unitz.parsefunction. -
unitString
-
The unit for the current value (if any).
-
unitClassUnitz.Class
-
The class of the unit (if any).
-
valueNumber
-
The current parsed value.
Methods
-
staticUnitz.Parsed.fromNumber(number){Unitz.Parsed}
-
Returns a unitless
Unitz.Parsedinstance for the given number.Name Type Description numberNumber The value of the parsed instance returned.
-
best(returnFraction, abbreviations, largestDenominator){Unitz.Parsed}
-
Determines the best way to represent the current value.
Unitz.bestName Type Default Description returnFractionBoolean false optional Whether the
Unitz.Parsed#normalreturned should be converted to a fraction (if a nice fraction exists).abbreviationsBoolean false optional If the returned value should use abbreviations if they're available.
largestDenominatorNumber optional See
Unitz.Fraction.- See:
-
convert(to, returnFraction, withUnit, largestDenominator, classlessDenominators, roundDown){Number|String|Unitz.Fraction|false}
-
Converts the current value to another unit. The default behavior is to return a number in the desired unit - however you can specify a truthy value for
returnFractionand a {@Unitz.Fraction} will be returned. If the unit to convert to is not a valid unit for this Parsed value - false will be returned.var p = Unitz.parse('2 cups'); p.convert('qt'); // 0.5 p.convert('qt', true); // '1/2' p.convert('qt', false, true); // '0.5 qt' p.convert('qt', true, true); // '1/2 qt' var q = Unitz.parse('0.125 trees'); // unknown unit q.convert('trees', true, true, 10, [2, 4, 8, 16]); // '1/8 trees'Name Type Default Description toString The unit to convert the current value to.
returnFractionBoolean false optional Whether the resulting conversion should return a
Unitz.Fraction.withUnitBoolean false optional Whether the value returned has the given unit appended to the end of it. If the returned value is a fraction the given unit will be appended to the
Unitz.Parsed#stringproperty.largestDenominatorNumber optional See
Unitz.Fraction.classlessDenominatorsArray.<Number> optional If the current value does not have a
Unitz.Group(which stores the valid fraction denominators for the unit) you can specify which denominators to use when calculating the nearest fraction to the converted value.roundDownBoolean false optional A fraction will try to find the closest value to
value- sometimes the closest fraction is larger than the givenvalueand it is used. You can pass true to this constructor and it will make sure the fraction determined is never over the givenvalue.- See:
-
fraction(withUnit, denominators, largestDenominator, roundDown){Unitz.Fraction}
-
Returns the fraction representation of this parsed value.
Name Type Default Description withUnitBoolean false optional If the
Unitz.Fraction#stringproperty should have the unit added to it.denominatorsArray.<Number> optional The array of denominators to use when converting the given value into a fraction. If a falsy value is given the denonimators of this parsed group will be used if a unit group has been determined.
largestDenominatorNumber optional Sometimes you don't want to use all of the denominators in the above array (it could be from a
Unitz.Group) and you would like to set a max. If the denominators given has something like[2, 4, 8, 100]and you don't want a fraction like3/100you can set thelargestDenominatorto a number lower than 100 and you won't ever get that denominator.roundDownBoolean false optional A fraction will try to find the closest value to
value- sometimes the closest fraction is larger than the givenvalueand it is used. You can pass true to this constructor and it will make sure the fraction determined is never over the givenvalue.Returns:
A new instance of Unitz.Fraction which is a representation of the parsed value.