Skip to content

Instantly share code, notes, and snippets.

@koduki
Created October 3, 2010 12:27
Show Gist options
  • Save koduki/608542 to your computer and use it in GitHub Desktop.
Save koduki/608542 to your computer and use it in GitHub Desktop.
Array.prototype.uniq = function(predicate){
this.sort()
ys = []
for(var i=0;i<this.length;i++){
if(ys.length == 0 || this[i] != ys[0]) ys.unshift(this[i])
}
return ys
}
xs = $("div.itemBlock").map(function(){
var to_date = function(day){
var ds = day.replace('日購入', '').replace('月', '年').split('年');
return new Date (ds[0],ds[1] - 1,ds[2]).getTime();
}
var to_i = function(value){
return parseInt(value.replace(',', '').replace('円', ''))
}
return {
title:$(this).find('.itemName').text(),
date:to_date($(this).find('.day').text()),
value:to_i($(this).find('.itemDetail dd').eq(0).text()),
shop:$(this).find('.itemDetail dd').eq(1).text(),
url:$(this).find('.itemName a').attr('href')
}
})
var ys = []
for(var i=0;i<xs.length;i++) ys.push(xs[i])
xs = ys
// 最大購入単価
var max_value = Math.max.apply(null, xs.map(function(x) x.value))
// 購入単価累計
var sum_value = xs.map(function(x) x.value).reduce(function(r, x) r+x, 0)
// 3ヶ月あたりの購入数
var d = (new Date()).getTime() - 3*30 * 24 * 60 * 60 * 1000
var ys = xs.filter(function(x) x.date > d)
var item_length = ys.length
// 3ヶ月あたりの平均購入単価
var d = (new Date()).getTime() - 3*30 * 24 * 60 * 60 * 1000
var ys = xs.filter(function(x) x.date > d)
var avg_value = ys.map(function(x) x.value).reduce(function(r, x) r+x, 0) / ys.length
// 3ヶ月あたりの平均購入間隔(Mean Time To Buy)
var d = (new Date()).getTime() - 3*30 * 24 * 60 * 60 * 1000
var ys = xs.filter(function(x) x.date > d).map(function(x) x.date).uniq()
var mttb = 3 * 30 / ys.length
// 購入ショップ数
var shop_length = xs.map(function(x) x.shop).uniq().length;
console.log(
'購入単価累計: ' + sum_value + "\n" +
'最大購入単価: ' + max_value + "\n" +
'平均購入単価: ' + avg_value + "\n" +
'平均購入間隔(日): ' + mttb + "\n" +
'購入商品数: ' + item_length + "\n" +
'購入ショップ数: ' + shop_length
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment