Skip to content

Instantly share code, notes, and snippets.

@mcwhittemore
Last active December 26, 2015 00:39
Show Gist options
  • Save mcwhittemore/7065383 to your computer and use it in GitHub Desktop.
Save mcwhittemore/7065383 to your computer and use it in GitHub Desktop.
Converts the Price Per to a common system.

The Unoffical Relay Foods Unit Converter

Below is a bookmarklet that will intelegently convert all the simular units on relayfoods.com to a common unit.

How to Use

javascript:(function(){var%20imported=document.createElement('script');imported.src=%22http://bit.ly/realy-foods-convert-units%22;document.head.appendChild(imported);})();
  1. Create a new bookmark
  2. Paste of above code into the url bar of the bookmark creator.
  3. Go to Relay Foods
  4. Search for something
  5. Click your new bookmark and watch the units conform!
;(function(){
var types = {
weight: {
base: "lb",
convertMap: {
lb: 1,
oz: 16,
g:453.592
},
best: undefined,
offers: []
},
volume: {
base: "gal",
convertMap: {
gal: 1,
qr: 4,
pt: 8,
cp: 16,
floz: 128,
tblspoon: 256,
tsp: 768,
l: 3.78541,
ml: 3785.41
},
best: undefined,
offers: []
},
unknown: {
offers: []
}
}
var theTypes = ["weight", "volume"];
var convert = function(type, num, from, to){
var map = types[type].convertMap;
var base = types[type].base;
var baseNum = from == base ? num : num/map[from];
var result = to == base ? baseNum : baseNum*map[to];
return result;
}
var pickBest = function(type, median){
// if(type=="weight"){
// if(median>.75){ return "lb"; }
// else if(median>.01){ return "oz"; }
// else { return "g"; }
// }
// else if(type=="volume"){
// return "gal";
// }
var map = types[type].convertMap;
var base = types[type].base;
var keys = Object.keys(map);
var best = keys[0];
var distance = Math.abs(1-convert(type, median, base, best));
for(var i=1; i<keys.length; i++){
var key = keys[i];
var dt = Math.abs(1-convert(type, median, base, key));
if(dt<distance){
distance=dt;
best=key;
}
}
return best;
}
var setTypeBest = function(type){
var offers = types[type].offers;
var stdNums = [];
for(var i=0; i<offers.length; i++){
var stdNum = convert(type, offers[i].num, offers[i].unit, types[type].base);
stdNums.push(stdNum);
}
stdNums.sort();
var middle = Math.floor(stdNums.length-(stdNums.length/2));
var median = stdNums[middle];
types[type].best = pickBest(type, median);
}
var addBestToOffer = function(type){
var best = types[type].best;
var offers = types[type].offers;
for(var i=0; i<offers.length; i++){
var pricePer = offers[i].pricePer;
var from = offers[i].unit;
var bestPricePer = convert(type, pricePer, best, from);
var displayBPP = bestPricePer.toFixed(2);
var el = $("#"+offers[i].id+" .price-per").text("$"+displayBPP+" per "+best);
}
}
var processType = function(data){
data.type = "unknown";
for(var i=0; i<theTypes.length; i++){
var map = types[theTypes[i]].convertMap;
var units = Object.keys(map);
if(units.indexOf(data.unit)!=-1){
data.type = theTypes[i];
break;
}
}
types[data.type].offers.push(data);
}
var processOffer = function(el){
var data = {};
data.id = $(el).attr("id");
var descPP = $(el).find(".price-per").text().replace("$", "").split(" ");
data.price = parseFloat($(el).find(".price strong").text().replace("$", ""));
data.pricePer = parseFloat(descPP[0]);
data.num = data.price/data.pricePer;
data.unit = descPP[2];
if(data.unit=="fl"){
data.unit+=descPP[3];
}
processType(data);
}
/******************************************************/
var offers = $(".offering");
for(var i=0; i<offers.length; i++){
processOffer(offers[i]);
}
for(var i=0; i<theTypes.length; i++){
setTypeBest(theTypes[i]);
addBestToOffer(theTypes[i]);
}
})();
@mcwhittemore
Copy link
Author

Sweet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment