From b63ac10657fa4f152f23758484bff0eedf906112 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Mon, 24 Sep 2012 23:12:13 -0400 Subject: [PATCH] shared helpers.dayMapping --- lib/app/helpers.js | 25 +++++++++++++------------ lib/app/scoring.js | 14 ++------------ src/app/helpers.coffee | 3 ++- src/app/scoring.coffee | 3 +-- 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/lib/app/helpers.js b/lib/app/helpers.js index ddfe9b95ac..046e745234 100644 --- a/lib/app/helpers.js +++ b/lib/app/helpers.js @@ -1,5 +1,5 @@ // Generated by CoffeeScript 1.3.3 -var moment; +var dayMapping, moment; moment = require('moment'); @@ -7,20 +7,21 @@ module.exports.daysBetween = function(a, b) { return Math.abs(moment(a).sod().diff(moment(b).sod(), 'days')); }; +module.exports.dayMapping = dayMapping = { + 0: 'su', + 1: 'm', + 2: 't', + 3: 'w', + 4: 'th', + 5: 'f', + 6: 's', + 7: 'su' +}; + module.exports.viewHelpers = function(view) { view.fn('taskClasses', function(type, completed, value, repeat) { - var classes, dayMapping; + var classes; classes = type; - dayMapping = { - 0: 'su', - 1: 'm', - 2: 't', - 3: 'w', - 4: 'th', - 5: 'f', - 6: 's', - 7: 'su' - }; if (completed || (repeat && repeat[dayMapping[moment().day()]] === false)) { classes += " completed"; } diff --git a/lib/app/scoring.js b/lib/app/scoring.js index 54cb22e288..499fb1d378 100644 --- a/lib/app/scoring.js +++ b/lib/app/scoring.js @@ -216,7 +216,7 @@ cron = function() { todoTally = 0; hpTally = 0; tallyTask = function(taskObj, callback) { - var absVal, completed, dayMapping, daysFailed, id, repeat, task, type, value; + var absVal, completed, daysFailed, id, repeat, task, type, value; id = taskObj.id, type = taskObj.type, completed = taskObj.completed, repeat = taskObj.repeat; if (id == null) { return callback('a task had a null id during cron, this should not be happening'); @@ -226,21 +226,11 @@ cron = function() { if (!completed) { daysFailed = daysPassed; if (type === 'daily' && repeat) { - dayMapping = { - 0: 'su', - 1: 'm', - 2: 't', - 3: 'w', - 4: 'th', - 5: 'f', - 6: 's', - 7: 'su' - }; daysFailed = 0; _.times(daysPassed, function(n) { var thatDay; thatDay = moment().subtract('days', n + 1); - if (repeat[dayMapping[thatDay.day()]] === true) { + if (repeat[helpers.dayMapping[thatDay.day()]] === true) { return daysFailed++; } }); diff --git a/src/app/helpers.coffee b/src/app/helpers.coffee index 4fe6dd38be..29b0bc270f 100644 --- a/src/app/helpers.coffee +++ b/src/app/helpers.coffee @@ -3,6 +3,8 @@ moment = require('moment') # Absolute diff between two dates, based on 12am for both days module.exports.daysBetween = (a, b) -> Math.abs(moment(a).sod().diff(moment(b).sod(), 'days')) + +module.exports.dayMapping = dayMapping = {0:'su',1:'m',2:'t',3:'w',4:'th',5:'f',6:'s',7:'su'} module.exports.viewHelpers = (view) -> view.fn 'taskClasses', (type, completed, value, repeat) -> @@ -10,7 +12,6 @@ module.exports.viewHelpers = (view) -> classes = type # show as completed if completed (naturally) or not required for today - dayMapping = {0:'su',1:'m',2:'t',3:'w',4:'th',5:'f',6:'s',7:'su'} if completed or (repeat and repeat[dayMapping[moment().day()]]==false) classes += " completed" diff --git a/src/app/scoring.coffee b/src/app/scoring.coffee index 1aac03d3c6..e896065234 100644 --- a/src/app/scoring.coffee +++ b/src/app/scoring.coffee @@ -210,11 +210,10 @@ cron = -> # however, for dailys which have repeat dates, need # to calculate how many they've missed according to their own schedule if type=='daily' && repeat - dayMapping = {0:'su',1:'m',2:'t',3:'w',4:'th',5:'f',6:'s',7:'su'} daysFailed = 0 _.times daysPassed, (n) -> thatDay = moment().subtract('days', n+1) - if repeat[dayMapping[thatDay.day()]]==true + if repeat[helpers.dayMapping[thatDay.day()]]==true daysFailed++ hpTally += score(id, 'down', {cron:true, times:daysFailed})