Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. convertToMetric = (function () {
  2.     var conversionList = {
  3.     /* Volumes */
  4.         'cup' : {'k' : 25, 'suffix' : 'cl'},
  5.         'fl oz' : {'k' : 3, 'suffix' : 'cl'},
  6.         'oz fl' : {'k' : 3, 'suffix' : 'cl'},
  7.         'fluid ounce' : {'k' : 3, 'suffix' : 'cl'},
  8.         'tbsp' : {'k' : 15, 'suffix' : 'ml'},
  9.         'tablespoon' : {'k' : 15, 'suffix' : 'ml'},
  10.         'tsp' : {'k' : 5, 'suffix' : 'ml'},
  11.         'teaspoon' : {'k' : 5, 'suffix' : 'ml'},
  12.  
  13.     /* Mass */
  14.         'oz' : {'k' : 28, 'suffix' : 'g'},
  15.         'ounce' : {'k' : 28, 'suffix' : 'g'},
  16.         'lb' : {'k' : 5, 'suffix' : 'hg'},
  17.         'pound' : {'k' : 5, 'suffix' : 'hg'}
  18.     },
  19.  
  20.     partsList = {
  21.         189 : .5,
  22.         190 : (3/4),
  23.         188 : (1/4),
  24.         8541 : (5/8)
  25.     },
  26.  
  27.     /* Whole, Parts, Type */
  28.     ingredientMatch = /(\d[\s]*)*?(\d+\/\d+|[½¾¼⅝])?[\s\-]*(fl[\. ]{1,2}oz|oz[\. ]{1,2}fl|[a-z]+)/gi,
  29.  
  30.     /* Temperature */
  31.     tempMatch = /(\d+)[° ]{1,2}F/gi
  32.  
  33.     return function (text) {
  34.         text = text.replace(ingredientMatch, function (str, whole, parts, type) {
  35.             var result = 1*whole
  36.  
  37.             if (!whole && !parts)
  38.                 return str
  39.  
  40.             type = (type.replace(/\./g, '')).toLowerCase()
  41.  
  42.             if (!conversionList[type]) {
  43.                 type = type.slice(0, -1)
  44.                 if (!conversionList[type])
  45.                     return str
  46.             }
  47.  
  48.             if (/\//.test(parts)) {
  49.                 alert(parts)
  50.                 parts = parts.split('/')
  51.                 result += parts[0]/parts[1]
  52.             } else if (partsList[parts.charCodeAt(0)]) {
  53.                 result += partsList[parts.charCodeAt(0)]
  54.             }
  55.  
  56.             result *= conversionList[type].k
  57.  
  58.             return result + conversionList[type].suffix
  59.         })
  60.  
  61.         text = text.replace(tempMatch, function (str, value) {
  62.             return (1*value - 32) * (5/9)
  63.         })
  64.  
  65.         return text
  66.     }
  67. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement