Skip to content

Instantly share code, notes, and snippets.

@iaindooley
Created August 4, 2020 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iaindooley/471d68af69412ba294efd9da9e542263 to your computer and use it in GitHub Desktop.
Save iaindooley/471d68af69412ba294efd9da9e542263 to your computer and use it in GitHub Desktop.
Go shopping
function goShopping(notification)
{
var notif = new Notification(notification);
notif.listCardWasAddedTo("Info");
if(notif.card().name() != "Go Shopping")
throw new InvalidDataException("Card created was not Go Shopping Card");
var quants = {};
var units = {};
var all_cards = notif.board().cards();
notif.board().list("Ingredients").cards().each(function(card)
{
var num = all_cards.clone().transform(function(loop)
{
try
{
loop.label("Leftovers");
return false;
}
catch(e)
{
if((loop.name() == card.name()) && (loop.currentList().name() != "Ingredients"))
return loop;
else
return false;
}
}).length();
if(num)
{
card.checklist("Ingredients").items().each(function(item)
{
var split = item.name().split(" x ");
if(
(split.length > 1) &&
(quantity = new RegExp("([0-9.]+)([ a-zA-Z]*)").exec(split[1].trim()))
)
{
var q = parseFloat(quantity[1].trim()*num);
var unit = quantity[2].trim();
units[split[0]] = unit;
if(!quants[split[0]])
quants[split[0]] = 0;
quants[split[0]] += q;
}
});
}
});
var shop = new Array();
notif.board().list("Info").card("Shopping List").addChecklist("Shopping List",function(list)
{
for(var key in quants)
list.addItem(key+" x "+quants[key]+units[key]);
});
var label_id = new Trellinator().board(notif.member().name()).label("Shopping List Item").data.id;
var cl = notif.board().list("Info").card("Shopping List").checklist("Shopping List");
cl.setName(notif.member().name()+" going shopping "+Trellinator.now().butlerDefaultDate());
cl.convertIntoLinkedCards(new Trellinator().board(notif.member().name()).findOrCreateList("Shopping"),{idLabels: label_id});
notif.card().archive();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment