From f9e1a4d9299aa5f1fd46c3d838567e7573133b39 Mon Sep 17 00:00:00 2001 From: 59naga Date: Mon, 6 Jul 2015 04:33:52 +0900 Subject: [PATCH] Change parameter of average from the absolute(periods[].day) to relative(column.length) --- src/calculator.coffee | 23 +++++++++++++---------- test/calculator.spec.coffee | 27 +++++++++++++++------------ test/util.coffee | 13 +++++++++++++ 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/calculator.coffee b/src/calculator.coffee index e003a76..2399a0b 100644 --- a/src/calculator.coffee +++ b/src/calculator.coffee @@ -17,9 +17,9 @@ backSlice= (stats,i,volume,label=[])-> # Public class Calculator extends Utility periods: [ - {name:'weekly',days:7} - {name:'monthly',days:30} - {name:'yearly',days:365} + {name:'weekly',day:7} + {name:'monthly',day:30} + {name:'yearly',day:365} ] # eg. @@ -132,14 +132,14 @@ class Calculator extends Utility for pkg,pkgI in normalized.packages periods= for period in @periods - length= Math.ceil(pkg.stats.length / period.days) + length= Math.ceil(pkg.stats.length / period.day) i= 1 while i <= length - {start,end,column}= backSlice pkg.stats,i++,period.days,normalized.days + {start,end,column}= backSlice pkg.stats,i++,period.day,normalized.days total= _.sum column,(stat)-> stat - average= total/period.days + average= total/column.length {start,end,total,average,column} @@ -164,12 +164,15 @@ class Calculator extends Utility column= for cell,j in page.column - packages.reduce (left,right)-> - left= left[period.name]?[i].column[j] ? left - left + right[period.name]?[i].column[j] + if packages.length <= 1 + packages[0]?[period.name]?[i].column[j] ? 0 + else + packages.reduce (left,right)-> + left= left[period.name]?[i].column[j] ? left + left + right[period.name]?[i].column[j] ? 0 total= _.sum column,(stat)-> stat - average= total/(period.days) + average= total/page.column.length {start,end,total,average,column} diff --git a/test/calculator.spec.coffee b/test/calculator.spec.coffee index 50fb912..a19acc8 100644 --- a/test/calculator.spec.coffee +++ b/test/calculator.spec.coffee @@ -6,15 +6,6 @@ fixture= require './fixtures/pkgs' util= require './util' # Specs -describe 'issues:calculator',-> - it '#3 calculated.weekly[0].total is 0',-> - fixture= require './issues/003' - normalized= calculator.normalize fixture - calculated= calculator.calculate normalized - - expect(util.commas calculated.total).toBe util.commas 8 - expect(util.commas calculated.average).toBe util.commas 8/7 - describe 'calculator',-> normalized= calculator.normalize fixture calculated= calculator.calculate normalized @@ -66,7 +57,7 @@ describe 'calculator',-> should= {} should.total= util.grandTotalNormalized normalized,start,end - should.average= should.total / day + should.average= should.total / (pkg.stats.slice start,end).length expect(should.total).toBe period.total expect(should.average).toBe period.average @@ -96,6 +87,8 @@ describe 'calculator',-> console.log '%s: sum:%s avg:%s', period.start+':'+period.end,period.total,period.average + pkg= null + periodTotals= for pkg in normalized.packages if process.env.DEBUG @@ -107,7 +100,7 @@ describe 'calculator',-> should= {} should.column= pkg.stats.slice start,end should.total= util.totalNormalized normalized,pkg.name,start,end - should.average= should.total / day + should.average= should.total / should.column.length expect(should.column).toEqual column expect(util.commas should.total).toBe util.commas total @@ -117,7 +110,17 @@ describe 'calculator',-> # sum of the package's total of period equal to the grand total of period should= {} + should.column= pkg.stats.slice start,end should.total= periodTotals.reduce (a,b)-> a+b - should.average= should.total / day + should.average= should.total / should.column.length expect(util.commas should.total).toBe util.commas calculated[periodName][periodIndex].total expect(util.commas should.average).toBe util.commas calculated[periodName][periodIndex].average + +describe 'issues:calculator',-> + it '#3 calculated.weekly[0].total is 0',-> + fixture= require './issues/003' + normalized= calculator.normalize fixture + calculated= calculator.calculate normalized + + expect(util.commas calculated.total).toBe util.commas 8 + expect(util.commas calculated.average).toBe util.commas 8/7 diff --git a/test/util.coffee b/test/util.coffee index af134dc..322f69d 100644 --- a/test/util.coffee +++ b/test/util.coffee @@ -10,6 +10,17 @@ module.exports= else number + # https://github.com/59naga/npm-count/issues/3 + toPackages: (pkg)-> + if pkg.package + {downloads,start,end}= pkg + + packages= {} + packages[pkg.package]= {start,end,downloads} + packages + else + pkg + # eg: # util.total ({ # foo: { @@ -23,6 +34,8 @@ module.exports= # # -> 1 total: (packages)-> + packages= @toPackages packages + _.chain packages .pluck 'downloads' .flatten(true)