new Unitz.Class(className)
Instantiates a new class instance with a name. A class stores information on how to convert values between units, the set of valid strings to denote a unit group, the singular and plural versions of a unit, and the valid fraction denominators for that unit.
Name | Type | Description |
---|---|---|
className |
String |
The name of the class. |
Members
-
basesObject
-
A map of unit aliases to their base unit.
-
classNameString
-
The name of the class.
-
convertersObject
-
The map of numbers it takes to get from one unit to another.
-
groupMapObject
-
The map of unit aliases to their group.
-
groupsArray.<Unitz.Group>
-
The array of groups in this class.
-
mappingObject
-
The map of numbers it takes to get from one base unit to another base unit. There can be multiple systems in a single class (like Imperial vs Metric) and when conversion takes place in
Unitz.convert
from one system to another this map is used.
Methods
-
addBaseConversion(source, target, value)
-
Adds a bi-directional conversion from one base unit to another.
uc.addBaseConversion( 'in', 'cm', 2.54 ); // 1 in to 2.54 cm
Name Type Description source
String The source unit.
target
String The target unit.
value
Number The value the source number needs to be multiplied by to get to the target value.
-
addGroup(relativeValue, relativeTo, units, denominators, singular, plural){Unitz.Group}
-
Adds a unit group to this class. A unit group is a unit relative to another unit and has it's own aliases for the unit, singluar & plural representations, and denominators to make unit-friendly fractions. The group added to this class is returned.
Name Type Description relativeValue
Number The value to scale by to get from the
relativeTo
unit to this unit group. If this is a base unit then this value should be 1 and therelativeTo
should be not given.relativeTo
String The unit the unit group being added is relative to. This should not be given when defining a base unit.
units
Array.<String> The aliases for the unit group being added, each are valid ways to represent this group. These MUST be in lowercase form.
denominators
Array.<Number> The denominators that are valid for the group being added. This is used so you don't see odd fractions that don't make sense for the given unit.
singular
String The singular unit (when a |value| is 1) to use for the added group.
plural
String The plural unit (when a |value| is not 1) to use for the added group.
Returns:
The unit group added to this class.
-
addOneBaseConversion(source, target, value)
-
Adds a one direction conversion from one base unit to another.
Name Type Description source
String The source unit.
target
String The target unit.
value
Number The value the source number needs to be multiplied by to get to the target value.
-
removeGroup(unit){Boolean}
-
Removes the group which has the given unit. The group will be removed entirely from the system and can no longer be parsed or converted to and from.
Name Type Description unit
String The lowercase unit of the group to remove.
Returns:
True if the group was removed, false if it does not exist in this class.
-
removeUnit(unit){Boolean}
-
Removes a unit from this class. The group the unit to still exists in this class, but the unit won't be parsed to the group anymore.
Name Type Description unit
String The lowercase unit to remove from this class.
Returns:
True if the unit was removed, false if it does not exist in this class.