https://meals.kymcampbell.com/wp-content/plugins/wp-ultimate-recip…/js/unit-conversion.js?ver=3.13.8
1/** 2 * RecipeUnitConversion.js 3 * 4 * @author Bootstrapped Ventures 5 * @version 0.6.4 6 */ 7 8var RecipeUnitConversion = RecipeUnitConversion || {}; 9 10RecipeUnitConversion.getUnitFromAlias = function(alias) { 11 if(alias === null || alias === undefined) { 12 return undefined; 13 } 14 var clean = ('' + alias).replace(/[\(\).,\s;:=\-+]/gi, ''); 15 16 if(wpurp_unit_conversion.alias_to_unit[clean] !== undefined) { 17 var unit = wpurp_unit_conversion.alias_to_unit[clean]; 18 } else { 19 var unit = wpurp_unit_conversion.alias_to_unit[clean.toLowerCase()]; 20 } 21 22 return unit; 23} 24 25RecipeUnitConversion.getUnitType = function(unit) { 26 return wpurp_unit_conversion.unit_to_type[unit]; 27} 28 29RecipeUnitConversion.getUnitSystems = function(unit) { 30 var type = RecipeUnitConversion.getUnitType(unit); 31 32 if(!type) { 33 return []; 34 } 35 36 var systems = wpurp_unit_conversion.systems; 37 var unit_systems = []; 38 39 for(var i = 0, l = systems.length; i < l; i++) 40 { 41 if(jQuery.inArray(unit, systems[i]['units_'+type]) != -1) { 42 unit_systems.push(i); 43 } 44 } 45 46 return unit_systems; 47} 48 49RecipeUnitConversion.isUniversal = function(unit) { 50 if(jQuery.inArray(unit, wpurp_unit_conversion.universal_units) != -1) { 51 return true; 52 } 53 54 return false; 55} 56 57RecipeUnitConversion.getAbbreviation = function(unit) { 58 var abbr = wpurp_unit_conversion.unit_abbreviations[unit]; 59 if(abbr === undefined) { 60 abbr = unit; 61 } 62 return abbr; 63} 64 65RecipeUnitConversion.getUserAbbreviation = function(unit, amount) { 66 var type = 'plural'; 67 if( amount == 1) { 68 type = 'singular'; 69 } 70 71 var abbr_unit = wpurp_unit_conversion.user_abbreviations[unit]; 72 73 if(abbr_unit !== undefined) { 74 var abbr = wpurp_unit_conversion.user_abbreviations[unit][type]; 75 if(abbr === undefined) { 76 abbr = unit; 77 } 78 } else { 79 var abbr = unit 80 } 81 82 return abbr; 83} 84 85RecipeUnitConversion.determineIngredientListSystem = function(list) { 86 // Prepare counter 87 var counter = []; 88 var systems = wpurp_unit_conversion.systems; 89 90 for(var i = 0, l = systems.length; i < l; i++) { 91 counter[i] = 0; 92 } 93 94 list.find('.wpurp-recipe-ingredient').each(function(){ 95 var alias = jQuery(this).find('.wpurp-recipe-ingredient-unit').text(); 96 var unit = RecipeUnitConversion.getUnitFromAlias(alias); 97 var systems = RecipeUnitConversion.getUnitSystems(unit); 98 99 for(var i = 0, l = systems.length; i < l; i++) { 100 counter[systems[i]]++; 101 } 102 }); 103 104 // Get index of largest number 105 var max = counter[0]; 106 var maxIndex = 0; 107 108 for(var i = 1, l = counter.length; i < l; i++) { 109 if(counter[i] > max) { 110 maxIndex = i; 111 max = counter[i]; 112 } 113 } 114 115 return maxIndex; 116}; 117 118RecipeUnitConversion.convertUnitToSystem = function(amount, unit, old_system, new_system) { 119 120 var systems = wpurp_unit_conversion.systems; 121 122 // Adjust for cup type 123 if(unit == 'cup') { 124 var old_cup = parseFloat(systems[old_system].cup_type); 125 var qty_cup = new Qty('1 cup').to('ml').scalar; 126 127 if(Math.abs(old_cup - qty_cup) > 0.1) { // 236.6 == 236.588238 128 amount = amount * (old_cup / qty_cup); 129 } 130 } 131 132 var unit_type = RecipeUnitConversion.getUnitType(unit); 133 var quantity = new Qty(amount + ' ' + RecipeUnitConversion.getAbbreviation(unit)); 134 135 var possible_units = systems[new_system]['units_' + unit_type]; 136 137 var new_quantities = []; 138 139 for(var i = 0, l = possible_units.length; i < l; i++) { 140 try { 141 var possible_amount = quantity.to(RecipeUnitConversion.getAbbreviation(possible_units[i])).scalar; 142 143 // Adjust for cup type 144 if(possible_units[i] == 'cup') { 145 var new_cup = parseFloat(systems[new_system].cup_type); 146 var qty_cup = new Qty(new_cup + ' ml').to('cup').scalar; 147 148 if(Math.abs(1 - qty_cup) > 0.01) { // 236.6 == 236.588238 149 possible_amount = possible_amount * (1 / qty_cup); 150 } 151 } 152 153 new_quantities.push({ 154 unit: possible_units[i], 155 amount: possible_amount 156 }); 157 } catch (err) { 158 console.log(err); 159 } 160 } 161 162 if(new_quantities.length === 0) { 163 // No conversion found, return same amount and unit 164 return { 165 unit: unit, 166 amount: amount 167 } 168 } 169 170 var sorted_quantities = new_quantities.sort(RecipeUnitConversion.compareAmou
170nts); 171 172 var new_amount = sorted_quantities[0].amount; 173 var new_unit = sorted_quantities[0].unit; 174 175 dance: 176 for(var i = 1, l = sorted_quantities.length; i < l; i++) { 177 if(new_amount > 999 || ( new_amount > 5 && ( new_unit == 'teaspoon' || new_unit == 'tablespoon' ) ) ) { 178 new_amount = sorted_quantities[i].amount; 179 new_unit = sorted_quantities[i].unit; 180 } else { 181 break dance; 182 } 183 } 184 185 return { 186 unit: new_unit, 187 amount: new_amount 188 } 189} 190 191RecipeUnitConversion.compareAmounts = function(a, b) { 192 return b.amount - a.amount; 193} 194 195/** 196 * Format final number 197 * 198 * @param string 199 * @return {String} 200 */ 201RecipeUnitConversion.formatNumber = function(amount, fraction, precision) { 202 if(fraction) { 203 var fractioned_amount = Fraction(amount.toString()).snap(); 204 if(fractioned_amount.denominator < 100) { 205 return fractioned_amount; 206 } 207 } 208 209 if(amount == '' || amount == 0) { 210 return ''; 211 } 212 // reformat to fixed 213 var precision = precision == undefined ? parseInt(wpurp_servings.precision) : precision; 214 var formatted = amount.toFixed(precision); 215 216 // increase the precision if reformated to 0.00, failsafe for endless loop 217 while(parseFloat(formatted) == 0) { 218 precision++; 219 formatted = amount.toFixed(precision); 220 221 if(precision > 10) { 222 return ''; 223 } 224 } 225 226 // ends with .00, remove 227 if(precision > 0) { 228 var zeroes = Array(precision+1).join('0'); 229 formatted = formatted.replace(new RegExp('\.' + zeroes + '$'),''); 230 } 231 232 // Change decimal character 233 if(typeof wpurp_servings !== 'undefined') { 234 formatted = formatted.replace('.', wpurp_servings.decimal_character); 235 } 236 237 return formatted; 238}; 239 240/** 241 * Update the ingredient list 242 * @param ingredients 243 * @param old_system 244 * @param new_system 245 */ 246RecipeUnitConversion.updateIngredients = function(ingredientList, old_system, new_system) 247{ 248 ingredientList.find('.wpurp-recipe-ingredient').each(function() { 249 var ingredient_amount = jQuery(this).find('.wpurp-recipe-ingredient-quantity'); 250 var ingredient_unit = jQuery(this).find('.wpurp-recipe-ingredient-unit'); 251 252 var amount = ingredient_amount.data('normalized'); 253 var fraction = ingredient_amount.data('fraction'); 254 var alias = ingredient_unit.data('original'); 255 256 var unit = RecipeUnitConversion.getUnitFromAlias(alias); 257 258 // Only continue if we have a non-universal unit and a non-zero quantity 259 if(unit !== undefined && !RecipeUnitConversion.isUniversal(unit) && amount) 260 { 261 var systems = RecipeUnitConversion.getUnitSystems(unit); 262 var cup_systems = RecipeUnitConversion.getUnitSystems('cup'); 263 264 // Only continue if the current unit isn't used in the new system (unless it's a cup and the old system used cups) 265 if(jQuery.inArray(new_system, systems) == -1 || ( unit == 'cup' && jQuery.inArray(old_system, cup_systems) != -1 )) 266 { 267 // TODO change amount to serving size 268 var new_quantity = RecipeUnitConversion.convertUnitToSystem(amount, unit, old_system, new_system); 269 270 ingredient_amount.text(RecipeUnitConversion.formatNumber(new_quantity.amount, fraction)); 271 ingredient_unit.text(RecipeUnitConversion.getUserAbbreviation(new_quantity.unit, new_quantity.amount)); 272 273 ingredient_amount.data('normalized', new_quantity.amount); 274 ingredient_unit.data('original', new_quantity.unit); 275 276 // Ingredient name singular or plural 277 var ingredient_name = jQuery(this).find('.wpurp-recipe-ingredient-name'); 278 RecipeUnitConversion.checkIngredientPlural(ingredient_name, new_quantity.amount, alias); 279 } 280 } 281 }); 282 283 ingredientList.data('system', new_system); 284} 285 286/** 287 * Handles onchange dropdown list 288 * 289 * @param ulList 290 */ 291RecipeUnitConversion.recalculate = function(dropdown) { 292 293 var recipe = jQuery(dropdown).parents('.wpurp-container'); 294 var ingredientList = recipe.find('.wpurp-recipe-ingredients'); 295 var old_system = parseInt(ingredientList.data('system'));
296 var new_system = parseInt(jQuery(dropdown).val()); 297 298 if(old_system != new_system) 299 { 300 RecipeUnitConversion.updateIngredients(ingredientList, old_system, new_system); 301 RecipePrintButton.update(recipe); 302 } 303}; 304 305/** 306 * Recipe unit conversion init 307 */ 308RecipeUnitConversion.init = function(){ 309 jQuery('.wpurp-recipe-ingredients').each(function(i) { 310 var ingredientList = jQuery(this); 311 312 // Set current system as selected in dropdown 313 var system = RecipeUnitConversion.determineIngredientListSystem(ingredientList); 314 var dropdown = ingredientList.parents('.wpurp-container').find('.adjust-recipe-unit'); 315 dropdown.val(system); 316 317 ingredientList.parents('.wpurp-container').data('system-original', system); 318 ingredientList.data('system', system); 319 }); 320}; 321 322/** 323 * Jquery document ready (init) 324 */ 325 326jQuery(document).ready(function(){ 327 if(window.wpurp_unit_conversion !== undefined) { 328 RecipeUnitConversion.init(); 329 } 330}); 331 332RecipeUnitConversion.adjustServings = function(ingredientList, servings_original, servings_new) 333{ 334 ingredientList.find('.wpurp-recipe-ingredient').each(function() { 335 var ingredient_amount = jQuery(this).find('.wpurp-recipe-ingredient-quantity'); 336 var ingredient_unit = jQuery(this).find('.wpurp-recipe-ingredient-unit'); 337 338 var amount = ingredient_amount.data('normalized'); 339 var fraction = ingredient_amount.data('fraction'); 340 341 if(!isFinite(amount)) { 342 ingredient_amount.addClass('recipe-ingredient-nan'); 343 } else { 344 // Calculate and set new amount 345 var new_amount = servings_new * amount/servings_original; 346 var new_amount_text = RecipeUnitConversion.formatNumber(new_amount, fraction); 347 348 ingredient_amount 349 .text(new_amount_text) 350 .data('normalized', new_amount); 351 352 // Get alias again as we might have to switch from plural to singular or back 353 var alias = ingredient_unit.data('original'); 354 var unit = RecipeUnitConversion.getUnitFromAlias(alias); 355 ingredient_unit.text(RecipeUnitConversion.getUserAbbreviation(unit, new_amount)); 356 357 // Ingredient name singular or plural 358 var ingredient_name = jQuery(this).find('.wpurp-recipe-ingredient-name'); 359 RecipeUnitConversion.checkIngredientPlural(ingredient_name, new_amount, alias); 360 } 361 362 }); 363}; 364 365RecipeUnitConversion.checkIngredientPlural = function(ingredient_name, amount, unit) { 366 var ingredient_version = ingredient_name.data('plural'); 367 368 if(amount.toFixed(2) == 1 && (unit == '' || unit == undefined)) { 369 var ingredient_version = ingredient_name.data('singular'); 370 } 371 372 if(ingredient_version !== undefined) { 373 if(ingredient_name.find('a').length > 0) { 374 ingredient_name.find('a').text(ingredient_version); 375 } else { 376 ingredient_name.text(ingredient_version); 377 } 378 } 379}; 380 381/** 382 * Addition to temporarily solve servings adjust 383 * TODO: Better (integrated) solution 384 */ 385jQuery(document).on('keyup change', '.advanced-adjust-recipe-servings', function(e) { 386 var servings_input = jQuery(this); 387 388 var ingredientList = servings_input.parents('.wpurp-container').find('.wpurp-recipe-ingredients'); 389 390 var servings_original = parseFloat(servings_input.data('original')); 391 var servings_new = servings_input.val(); 392 393 if(isNaN(servings_new) || servings_new <= 0){ 394 servings_new = 1; 395 } 396 397 RecipeUnitConversion.adjustServings(ingredientList, servings_original, servings_new); 398 wpurp_adjustable_servings.updateShortcode(servings_input.parents('.wpurp-container'), servings_new); 399 400 // Update current servings 401 servings_input.parents('.wpurp-container').find('.advanced-adjust-recipe-servings').each(function() { 402 jQuery(this).data('original', servings_new); 403 }); 404 405 RecipePrintButton.update(servings_input.parents('.wpurp-container')); 406}); 407 408jQuery(document).on('blur', '.advanced-adjust-recipe-servings', function(e) { 409 var servings_input = jQuery(this); 410 411 var servings_new = servings_input.data('original'); 412 servings_input.parents('.wpurp-container').find('.advanced-adjust-recipe-servings').each(function() { 413 jQuery(this).val(servings_new); 414 }); 415 416 RecipePrintButton.update(servings_input.parents('.wpurp-container')); 417});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.