
var allUnitRegex = '(' + Unit.allUnitNames().map(function(unitName) { return '\\b' + unitName + '\\b'}).join('|') + ')';

var Pattern = function(template) {
  this.template = template;

  this.quantityRegexes = [
    '\\ba\\b', //the word a
    '\\ban\\b', //the word an
    '\\d+/\\d+', //any fraction
    '\\d*\\.\\d+', //any decimal
    '\\d+' //any number
    //whole word numbers
    //ranges
  ];

  this.quantityRegex = '(' + this.quantityRegexes.join('|') + ')';
  this.ingredientRegex = '(.+)';
  this.allUnitRegex = allUnitRegex;

  this.preparedTemplate = function() {
    var preparedTemplate = this.template;

    preparedTemplate = preparedTemplate.replace(/\s+/g, '\\s+');
    preparedTemplate = preparedTemplate.replace('{quantity}', this.quantityRegex);
    preparedTemplate = preparedTemplate.replace('{unit}', this.allUnitRegex);
    preparedTemplate = preparedTemplate.replace('{ingredient}', this.ingredientRegex);

    return preparedTemplate;
  }.bind(this)()

  this.matches = function(against) {
    var results = against.match(this.preparedTemplate);
    if (!results) {
      return null;
    }
    results.shift();
    var quantity, unit, ingredientName;
    for (var i = 0; i < results.length; i++) {
      var result = results[i];
      if (new RegExp(this.quantityRegex).test(result)) {
        quantity = result;
      } else if (new RegExp(this.allUnitRegex).test(result)) {
        unit = result;
      } else if (new RegExp(this.ingredientRegex).test(result)) {
        ingredientName = result;
      }
    }
    return new Amount(quantity, unit, ingredientName);
  }.bind(this);
};

var IngredientParser = function(text) {
  this.text = text;

  this.patterns = [
    new Pattern("{ingredient} to taste"),
    new Pattern("{ingredient} as desired"),
    new Pattern("{quantity} {unit} of {ingredient}"), // 2 sticks of butter, 2-3 tablespoons of sugar
    new Pattern("{quantity} {unit} {ingredient}"), //1 cup flour
    new Pattern("{quantity} {ingredient}"), //an egg, 
  ];

  for (var i = 0; i < this.patterns.length; i++) {
    var pattern = this.patterns[i];
    var match = pattern.matches(text)
    if (match) {
      this.amount = match;
      break;
    }
  }
  
  this.scale = function(scalingFactor) {
    var quantityForDisplay = new QuantityPresenter(this.amount.quantity * scalingFactor).quantityForDisplay
    return this.text.replace(this.amount.quantityAsString, quantityForDisplay);
  }.bind(this);
  
};

@@ -1,13 +1,12 @@
-%0A
 var allUnitR
@@ -62,16 +62,18 @@
 tName) %7B
+%0A 
  return 
@@ -96,16 +96,17 @@
  + '%5C%5Cb'
+%0A
 %7D).join(
@@ -982,147 +982,220 @@
 s)()
-%0A%0A  this.matches = function(against) %7B%0A    var results = against.match(this.preparedTemplate);%0A    if (!results) %7B%0A      return null;%0A    %7D
+;%0A%0A  this.regex = new RegExp(this.preparedTemplate);%0A%0A  this.matches = function(against) %7B%0A    return this.regex.test(against);%0A  %7D;%0A%0A  this.parse = function(against) %7B%0A    var results = this.regex.exec(against);
 %0A   
@@ -2089,108 +2089,78 @@
 %0A%0A  
-for (var i = 0; i %3C this.patterns.length; i++) %7B%0A    var pattern = this.patterns%5Bi%5D;%0A    var match =
+this.amount = this.patterns%0A    .find(function(pattern) %7B%0A      return
  pat
@@ -2176,83 +2176,61 @@
 hes(
+this.
 text)
+;
 %0A    
-if (match) %7B%0A      this.amount = match;%0A      break;%0A    %7D%0A  %7D%0A  
+%7D.bind(this))%0A    .parse(this.text);%0A
 %0A  t
@@ -2471,14 +2471,12 @@
 (this);%0A
-  
 %0A%7D;%0A

simplify matching