Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- convertToMetric = (function () {
- var conversionList = {
- /* Volumes */
- 'cup' : {'k' : 25, 'suffix' : 'cl'},
- 'fl oz' : {'k' : 3, 'suffix' : 'cl'},
- 'oz fl' : {'k' : 3, 'suffix' : 'cl'},
- 'fluid ounce' : {'k' : 3, 'suffix' : 'cl'},
- 'tbsp' : {'k' : 15, 'suffix' : 'ml'},
- 'tablespoon' : {'k' : 15, 'suffix' : 'ml'},
- 'tsp' : {'k' : 5, 'suffix' : 'ml'},
- 'teaspoon' : {'k' : 5, 'suffix' : 'ml'},
- /* Mass */
- 'oz' : {'k' : 28, 'suffix' : 'g'},
- 'ounce' : {'k' : 28, 'suffix' : 'g'},
- 'lb' : {'k' : 5, 'suffix' : 'hg'},
- 'pound' : {'k' : 5, 'suffix' : 'hg'}
- },
- partsList = {
- 189 : .5,
- 190 : (3/4),
- 188 : (1/4),
- 8541 : (5/8)
- },
- /* Whole, Parts, Type */
- ingredientMatch = /(\d[\s]*)*?(\d+\/\d+|[½¾¼⅝])?[\s\-]*(fl[\. ]{1,2}oz|oz[\. ]{1,2}fl|[a-z]+)/gi,
- /* Temperature */
- tempMatch = /(\d+)[° ]{1,2}F/gi
- return function (text) {
- text = text.replace(ingredientMatch, function (str, whole, parts, type) {
- var result = 1*whole
- if (!whole && !parts)
- return str
- type = (type.replace(/\./g, '')).toLowerCase()
- if (!conversionList[type]) {
- type = type.slice(0, -1)
- if (!conversionList[type])
- return str
- }
- if (/\//.test(parts)) {
- alert(parts)
- parts = parts.split('/')
- result += parts[0]/parts[1]
- } else if (partsList[parts.charCodeAt(0)]) {
- result += partsList[parts.charCodeAt(0)]
- }
- result *= conversionList[type].k
- return result + conversionList[type].suffix
- })
- text = text.replace(tempMatch, function (str, value) {
- return (1*value - 32) * (5/9)
- })
- return text
- }
- })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement