Skip to content

Instantly share code, notes, and snippets.

@ayende

ayende/i3.js Secret

Created September 9, 2021 07:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ayende/51a67efff7e625f671d3c357d51d063e to your computer and use it in GitHub Desktop.
Save ayende/51a67efff7e625f671d3c357d51d063e to your computer and use it in GitHub Desktop.
// map
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
map("Orders", o => {
var results = [];
for(var i = 0; i < o.Lines.length; i++){
var cur = {
Product: o.Lines[i].Product,
Year: new Date(o.OrderedAt).getFullYear(),
};
var month = monthNames[new Date(o.OrderedAt).getMonth()];
cur[month + " Sales"] = o.Lines[i].Quantity;
cur[month + " Totals"] = o.Lines[i].Quantity * o.Lines[i].PricePerUnit * (1-o.Lines[i].Discount)
results.push(cur);
}
return results;
})
// reduce
groupBy(x => ({Product: x.Product, Year: x.Year}))
.aggregate(g=>{
var final = {};
for(var i = 0; i < g.values.length; i++) {
var keys = Object.keys(g.values[i]);
for(var j = 0; j < keys.length; j++) {
if (keys[j].endsWith("Sales") == false && keys[j].endsWith("Totals") == false)
continue;
final[keys[j]] = (final[keys[j]] || 0) + g.values[i][keys[j]];
}
}
final.Product = g.key.Product;
final.Year = g.key.Year;
return final;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment