Last active
January 3, 2021 19:02
-
-
Save masyukun/64a3490a464acaa3b9af6819f10dfbf4 to your computer and use it in GitHub Desktop.
JavaScript functions to work with exported Cronometer recipes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Recipe Tools v0.1 | |
* | |
* This version exists as a GitHub Gist at https://gist.github.com/masyukun/64a3490a464acaa3b9af6819f10dfbf4 | |
* | |
* @license | |
* Copyright (c) 2020 Matthew Royal | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
'use strict'; | |
const cronometerIdToNutrient = { "-1205": { "name": "Net Carbs" }, "-203": { "NutritionInformation": "proteinContent", "name": "Calories from Protein" }, "-204": { "name": "Calories from Fat" }, "-205": { "name": "Calories from Carbs" }, "-221": { "name": "Calories from Alcohol" }, "10001": { "name": "Omega-3" }, "10002": { "name": "Omega-6" }, "10003": { "name": "Chromium" }, "10004": { "name": "Biotin" }, "10005": { "name": "Iodine" }, "10007": { "name": "Sugar Alcohol" }, "10008": { "name": "Molybdenum" }, "10009": { "name": "Added Sugars" }, "203": { "name": "Protein" }, "204": { "NutritionInformation": "fatContent", "name": "Fat" }, "205": { "NutritionInformation": "carbohydrateContent", "name": "Carbs" }, "207": { "name": "Ash" }, "208": { "NutritionInformation": "calories", "name": "Energy" }, "209": { "name": "Starch" }, "210": { "name": "Sucrose" }, "211": { "name": "Glucose" }, "212": { "name": "Fructose" }, "213": { "name": "Lactose" }, "214": { "name": "Maltose" }, "221": { "name": "Alcohol" }, "255": { "name": "Water" }, "262": { "name": "Caffeine" }, "269": { "NutritionInformation": "sugarContent", "name": "Sugars" }, "287": { "name": "Galactose" }, "291": { "NutritionInformation": "fiberContent", "name": "Fiber" }, "301": { "name": "Calcium" }, "303": { "name": "Iron" }, "304": { "name": "Magnesium" }, "305": { "name": "Phosphorus" }, "306": { "name": "Potassium" }, "307": { "NutritionInformation": "sodiumContent", "name": "Sodium" }, "309": { "name": "Zinc" }, "312": { "name": "Copper" }, "313": { "name": "Fluoride" }, "315": { "name": "Manganese" }, "317": { "name": "Selenium" }, "318": { "name": "Vitamin A" }, "319": { "name": "Retinol" }, "320": { "name": "Retinol Activity Equivalent" }, "321": { "name": "Beta-carotene" }, "322": { "name": "Alpha-carotene" }, "323": { "name": "Vitamin E" }, "324": { "name": "Vitamin D" }, "334": { "name": "Beta-cryptoxanthin" }, "337": { "name": "Lycopene" }, "338": { "name": "Lutein+Zeaxanthin" }, "341": { "name": "Beta Tocopherol" }, "342": { "name": "Gamma Tocopherol" }, "343": { "name": "Delta Tocopherol" }, "401": { "name": "Vitamin C" }, "404": { "name": "B1 (Thiamine)" }, "405": { "name": "B2 (Riboflavin)" }, "406": { "name": "B3 (Niacin)" }, "410": { "name": "B5 (Pantothenic Acid)" }, "415": { "name": "B6 (Pyridoxine)" }, "417": { "name": "Folate" }, "418": { "name": "B12 (Cobalamin)" }, "421": { "name": "Choline" }, "430": { "name": "Vitamin K" }, "501": { "name": "Tryptophan" }, "502": { "name": "Threonine" }, "503": { "name": "Isoleucine" }, "504": { "name": "Leucine" }, "505": { "name": "Lysine" }, "506": { "name": "Methionine" }, "507": { "name": "Cystine" }, "508": { "name": "Phenylalanine" }, "509": { "name": "Tyrosine" }, "510": { "name": "Valine" }, "511": { "name": "Arginine" }, "512": { "name": "Histidine" }, "513": { "name": "Alanine" }, "514": { "name": "Aspartic acid" }, "515": { "name": "Glutamic acid" }, "516": { "name": "Glycine" }, "517": { "name": "Proline" }, "518": { "name": "Serine" }, "521": { "name": "Hydroxyproline" }, "601": { "NutritionInformation": "cholesterolContent", "name": "Cholesterol" }, "605": { "NutritionInformation": "transFatContent", "name": "Trans-Fats" }, "606": { "NutritionInformation": "saturatedFatContent", "name": "Saturated" }, "636": { "name": "Phytosterol" }, "645": { "name": "Monounsaturated" }, "646": { "name": "Polyunsaturated" } }; | |
const cronometerIdToSchema = { | |
"208": "calories", | |
"205": "carbohydrateContent", | |
"601": "cholesterolContent", | |
"204": "fatContent", | |
"291": "fiberContent", | |
"-203": "proteinContent", | |
"606": "saturatedFatContent", | |
"307": "sodiumContent", | |
"269": "sugarContent", | |
"605": "transFatContent" | |
} | |
const cronometerFoodTagToRestrictedDiet = { | |
"Vegan": "VeganDiet", | |
"Vegetarian": "VegetarianDiet" | |
}; | |
/** | |
* Add nutrient names to the Cronometer export object. (Modifies existing JSON.) | |
* NOTE: Uncomment the one-line comment declaring a new recipeJson var in order to return a new object, rather than modifying the incoming recipeJson object. | |
* | |
* @param {JSON} recipeJson A JSON recipe exported from Cronometer | |
* @return {JSON} recipeJson A reference to the same JSON object, for convenience, with nutrient names added to each nutrient. | |
*/ | |
const enrichCronometerRecipeExport = function(recipeJson) { | |
// var recipeJson = JSON.parse(JSON.stringify(recipeJson)); | |
for (let nutrient of recipeJson.nutrients) { | |
nutrient.name = cronometerIdToNutrient[nutrient.id].name; | |
} | |
return recipeJson; | |
}; | |
/** | |
* Convert cronometer recipe export JSON to a schema.org Recipe | |
* | |
* @param {JSON} recipeJson A JSON recipe exported from Cronometer | |
* @return {JSON} schemaRecipe A JSON of the recipe in schema.org NutritionInformation format. | |
*/ | |
const convertCronometerRecipeToSchema = function(recipeJson) { | |
// Convert nutrients | |
let schemaNutrients = { "@type": "NutritionInformation", "calories": "" }; | |
for (let n of recipeJson.nutrients) { | |
if (cronometerIdToSchema[n.id]) { | |
schemaNutrients[cronometerIdToSchema[n.id]] = n.amount.toFixed(2) | |
+ ((cronometerIdToSchema[n.id] === "calories") ? " calories" : " grams"); | |
} | |
} | |
// Convert recipe | |
let schemarecipe = { | |
"@context": "http://schema.org", | |
"@type": "Recipe", | |
"author": recipeJson.owner, | |
"description": recipeJson.comments, | |
"recipeIngredient": recipeJson.ingredients.map(function(i) { return "cronometer:"+i.foodId; }), | |
"name": recipeJson.name, | |
"nutrition": schemaNutrients, | |
"recipeInstructions": recipeJson.comments, | |
"suitableForDiet": recipeJson.foodTags.map(function(t){ | |
if (cronometerFoodTagToRestrictedDiet[t]) { | |
return "http://schema.org/" + cronometerFoodTagToRestrictedDiet[t]; | |
} | |
}) | |
} | |
return schemarecipe; | |
}; | |
// Sample exported recipe | |
let jsonrecipe = {"owner":116699,"labelType":"AMERICAN_2016","foodTags":["Vegetarian","Vegan"],"source":"Custom","barcodes":[],"nutrients":[{"amount":0.21475896749999998,"id":512},{"amount":0.37855817999999997,"id":513},{"amount":1.1829943125,"id":514},{"amount":1.972870515,"id":515},{"amount":0.3239584425,"id":516},{"amount":0.40039807499999996,"id":517},{"amount":0.11283945749999999,"id":645},{"amount":0,"id":262},{"amount":0.4404378825,"id":518},{"amount":0.40767804,"id":646},{"amount":6.187970249999999,"id":269},{"amount":324.686439,"id":401},{"amount":0.2293188975,"id":10001},{"amount":0.1783591425,"id":10002},{"amount":0.25843875749999995,"id":404},{"amount":0.4258779525,"id":405},{"amount":2.3259488175,"id":406},{"amount":0.0109199475,"id":10007},{"amount":0,"id":10009},{"amount":2.0857099724999997,"id":410},{"amount":0,"id":287},{"amount":0.6369969375,"id":415},{"amount":229.3188975,"id":417},{"amount":0,"id":418},{"amount":9.4639545,"id":291},{"amount":68.06767275,"id":421},{"amount":171.96794999999997,"id":301},{"amount":371.278215,"id":430},{"amount":2.657187225,"id":303},{"amount":76.73589,"id":304},{"amount":240.238845,"id":305},{"amount":1150.2344699999999,"id":306},{"amount":141.43868068791946,"id":307},{"amount":20.305529299999996,"id":-1205},{"amount":1.4953553999999998,"id":309},{"amount":0.1813217175,"id":312},{"amount":0.764396325,"id":315},{"amount":9.09995625,"id":317},{"amount":2267.7090975,"id":318},{"amount":0,"id":319},{"amount":113.4473345775,"id":320},{"amount":1314.0336825,"id":321},{"amount":90.9995625,"id":322},{"amount":5.2415747999999995,"id":323},{"amount":0,"id":324},{"amount":27.489077818471888,"id":-203},{"amount":112.03253337754047,"id":-204},{"amount":10.86475065,"id":203},{"amount":109.08311855533762,"id":-205},{"amount":12.346793525,"id":204},{"amount":29.769483799999996,"id":205},{"amount":3.6399825,"id":334},{"amount":3.196410525,"id":207},{"amount":249.75940500000002,"id":208},{"amount":0,"id":209},{"amount":0,"id":337},{"amount":0.36399825,"id":210},{"amount":5106.8954475,"id":338},{"amount":1.783591425,"id":211},{"amount":2.4751881,"id":212},{"amount":0.764396325,"id":213},{"amount":0,"id":341},{"amount":0.764396325,"id":214},{"amount":1.128394575,"id":342},{"amount":0,"id":343},{"amount":0,"id":601},{"amount":0,"id":-221},{"amount":0,"id":221},{"amount":0,"id":605},{"amount":0.414958005,"id":606},{"amount":0.1201194225,"id":501},{"amount":0.32031845999999997,"id":502},{"amount":0.2875586175,"id":503},{"amount":0.4695577425,"id":504},{"amount":0.49139763750000004,"id":505},{"amount":0.138319335,"id":506},{"amount":0.10191951,"id":507},{"amount":0.4258779525,"id":508},{"amount":0.181999125,"id":509},{"amount":0.4549978125,"id":510},{"amount":354.64656149999996,"id":255},{"amount":0.6952366575,"id":511}],"measures":[{"amount":1,"name":"g","id":23311549,"type":"Recipe","value":453.62399999999997},{"amount":1,"name":"Serving","id":23311550,"type":"Recipe","value":2},{"amount":1,"name":"full recipe","id":23311551,"type":"Recipe","value":1}],"defaultMeasureId":23311550,"translations":[{"translationId":9579296,"name":"Matt's Famous, veggies, Broccoli steamed with vinegar, sesame dressing","languageCode":"en"}],"name":"Matt's Famous, veggies, Broccoli steamed with vinegar, sesame dressing","ingredients":[{"measureId":1772216,"translationId":0,"foodId":755150,"grams":60,"value":60},{"measureId":1023804,"translationId":0,"foodId":455493,"grams":29.62575,"value":29.62575},{"measureId":1074194,"translationId":0,"foodId":465073,"grams":363.99825,"value":363.99825},{"measureId":8644144,"translationId":0,"foodId":3316525,"grams":200,"value":200}],"id":9087595,"category":26} | |
// Sample function invocations | |
enrichCronometerRecipeExport(jsonrecipe) | |
convertCronometerRecipeToSchema(jsonrecipe) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment