var WholeUnit = require('./whole_unit');

var Amount = function(quantity, unit) {
  
  this.parseQuantity = function(quantityAsString) {
    if (quantityAsString === "a" || quantityAsString === "an") {
      return 1;
    }
    if (quantityAsString.includes('/') && /\s+/.test(quantityAsString)) {
      var components = quantityAsString.split(/\s+/)
      return components.map(this.parseQuantity).reduce(function(sum, current) { return sum + current }, 0);
    }
    if (quantityAsString.includes('/')) {
      var numeratorAndDenominator = quantityAsString.split('/').map(function(part) {
        return parseInt(part);
      });
      return numeratorAndDenominator[0] / numeratorAndDenominator[1];
    }
    return parseFloat(quantityAsString)
  }.bind(this);
  
  if (typeof quantity === 'string') {
    this.quantityAsString = quantity;
    this.quantity = this.parseQuantity(this.quantityAsString);
  } else {
    this.quantity = quantity;
  }

  this.unit = unit || new WholeUnit();
  
  this.amountByScaling = function(scalingFactor) {
    return new Amount(this.quantity * scalingFactor, this.unit);
  }.bind(this);
  
  this.isValid = this.unit.isValidQuantity(this.quantity);
  
  this.toString = function() {
    return '' + this.quantity + ' ' + this.unit.name;
  }
};

module.exports = Amount;
@@ -43,67 +43,16 @@
 var 
-Amount = function(quantity, unit) %7B%0A  %0A  this.parse
 Quantity
  = f
@@ -47,16 +47,22 @@
 Quantity
+Parser
  = funct
@@ -85,18 +85,16 @@
 ring) %7B%0A
-  
   if (qu
@@ -156,29 +156,35 @@
     
-  return 1;%0A    %7D%0A   
+this.quantity = 1;%0A  %7D else
  if 
@@ -250,26 +250,24 @@
 ing)) %7B%0A    
-  
 var componen
@@ -309,51 +309,138 @@
     
-  return components.map(this.parseQuantity)
+this.quantity = components%0A      .map(function(component) %7B%0A        return new QuantityParser(component).quantity;%0A      %7D)%0A      
 .red
@@ -467,16 +467,24 @@
 rrent) %7B
+%0A       
  return 
@@ -496,16 +496,22 @@
  current
+%0A     
  %7D, 0);%0A
@@ -512,23 +512,22 @@
 , 0);%0A  
-  %7D%0A   
+%7D else
  if (qua
@@ -553,26 +553,24 @@
 des('/')) %7B%0A
-  
     var nume
@@ -642,26 +642,24 @@
 rt) %7B%0A      
-  
 return parse
@@ -677,18 +677,16 @@
     
-  
 %7D);%0A    
   re
@@ -681,24 +681,31 @@
 %7D);%0A    
-  return
+this.quantity =
  numerat
@@ -760,22 +760,36 @@
 ;%0A  
-  %7D%0A    return
+%7D else %7B%0A    this.quantity =
  par
@@ -813,24 +813,122 @@
 sString)%0A  %7D
+%0A%7D;%0A%0Avar Amount = function(quantity, unit) %7B%0A%0A  this.parseQuantity = function(quantityAsString) %7B%7D
 .bind(this);
@@ -928,18 +928,16 @@
 (this);%0A
-  
 %0A  if (t
@@ -1025,34 +1025,28 @@
 ntity = 
-this.parse
+new 
 Quantity
 (this.qu
@@ -1029,32 +1029,38 @@
 y = new Quantity
+Parser
 (this.quantityAs
@@ -1066,16 +1066,25 @@
 sString)
+.quantity
 ;%0A  %7D el
@@ -1162,19 +1162,17 @@
 Unit();%0A
-  
 %0A
+
   this.a
@@ -1295,19 +1295,17 @@
 (this);%0A
-  
 %0A
+
   this.i
@@ -1359,11 +1359,9 @@
 y);%0A
-  
 %0A
+
   th
@@ -1449,16 +1449,16 @@
   %7D%0A%7D;%0A%0A
-
 module.e
@@ -1473,8 +1473,9 @@
  Amount;
+%0A

refactor quantity parsing into its own type