Fork me on GitHub
Unitz

Unitz is a library for parsing user entered values and units and performing actions on them.

User input is often in many different formats using known and unknown units.

Unitz can parse numbers (0.3), fractions (1/2 or 1 4/5), and ranges (2 - 3) with or without units.

Units can be provided by the user in many acceptable formats - and if its not registered (1 loaf or 2 loaves of bread) Unitz can intelligbly figure them out.

This tool allows you to experiment with your own input and see how the various methods of the library operate.

This library should be treated as if each returned value is immutable. Each action performed generates a new value, so the previous value(s) are not affected.

Uses

Unitz was created for an application which takes an item (like milk) and a quantity (like 1 pt) and allows the user to create grocery lists, recipes, and inventory lists and perform operations on them. This library allows queries to be made against these lists like:

  • Do I have the ingredients in my inventory to do recipe X?
  • I have this much of ingredients Y, scale the recipe accordingly to match what I have.
  • What recipes can I make based on my inventory?
  • What do I need to get at the store for recipe Z for W servings based on my inventory?
  • Move my gotten grocery list items into my inventory list.
  • I made this recipe, remove the ingredients used from my inventory.

Features
  • Mutate: A mutation performs a simple operation on each of the values parsed. Sometimes mutations can remove values that don't fit some criteria.
  • Transform: A transformation a mutation that can be given some criteria. This are often used to make the user input or the results of some other actions more user friendly.
  • Operations: An operation is something mathematical that is done with another set of values or a number.
  • Sort: A sort will rearrange the values in a set optionally taking some options.
  • Convert: A conversion will require a unit to convert to, and it will take the sum of all the values in the set with the same class and return the value in the desired unit.
  • Conversions: Generates a set of equivalent values in the different units for a class. For example, if your input is 12in a conversion to that is 1ft or 1/3 yard.
{{ output.range( range ) }}
Class
{{ range.min.group.parent.name }}
Dynamic
None
Group
{{ range.min.group.singularLong }} - {{ range.max.group.singularLong }} {{ range.min.group.singularLong }}
Rate
{{ range.min.rateGroup.singularLong }} - {{ range.max.rateGroup.singularLong }} {{ range.min.rateGroup.singularLong }}
Range
{{ range.isRange ? 'Yes' : 'No' }}
Valid
{{ range.isValid ? 'Yes' : 'No' }}
Fractions
{{ range.isFraction ? 'Yes' : 'No' }}


Options


Options
Options
uz( input ).add( addend, scale )
Add
This operation adds two values together when their units match.

+  (
   x
)  =
uz( input ).sub( addend, scale )
Subtract
This operation subtracts one value from another when their units match.

-  (
   x
)  =
uz( input ).scale( amount )
Scale
This operation multiplies the values and ranges by some number.

   x
   =
uz( input ).scaleTo( to, rangeDelta )
Scale To
This operation takes a set of values and given a value & unit scales the entire set up to that value. For example, if you have 12in, 1cup and you scale it to 18in the result will be 18in, 1.5cup. Another parameter rangeDelta can be given when dealing with sets that have ranges. The default value is 0.5 which means the value which is used to scale against will be equally between the minimum and maximum of the range. 0 = the minimum and 1 = the maximum. If a set contains multiple ranges which share the same class they are joined together to determine how to scale the set.

x to
 at
   =
Options

Output
Options



Output
Options
{{ range }}