Class: Parsed

Unitz. Parsed

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

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.parse function.

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.Parsed instance for the given number.

Name Type Description
number Number

The value of the parsed instance returned.

best(returnFraction, abbreviations, largestDenominator){Unitz.Parsed}

Determines the best way to represent the current value. Unitz.best

Name Type Default Description
returnFraction Boolean false optional

Whether the Unitz.Parsed#normal returned should be converted to a fraction (if a nice fraction exists).

abbreviations Boolean false optional

If the returned value should use abbreviations if they're available.

largestDenominator Number 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 returnFraction and 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
to String

The unit to convert the current value to.

returnFraction Boolean false optional

Whether the resulting conversion should return a Unitz.Fraction.

withUnit Boolean 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#string property.

largestDenominator Number optional

See Unitz.Fraction.

classlessDenominators Array.<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.

roundDown Boolean false optional

A fraction will try to find the closest value to value - sometimes the closest fraction is larger than the given value and it is used. You can pass true to this constructor and it will make sure the fraction determined is never over the given value.

See:

fraction(withUnit, denominators, largestDenominator, roundDown){Unitz.Fraction}

Returns the fraction representation of this parsed value.

Name Type Default Description
withUnit Boolean false optional

If the Unitz.Fraction#string property should have the unit added to it.

denominators Array.<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.

largestDenominator Number 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 like 3/100 you can set the largestDenominator to a number lower than 100 and you won't ever get that denominator.

roundDown Boolean false optional

A fraction will try to find the closest value to value - sometimes the closest fraction is larger than the given value and it is used. You can pass true to this constructor and it will make sure the fraction determined is never over the given value.

Returns:
A new instance of Unitz.Fraction which is a representation of the parsed value.