use moment().diff instead of custom days between function

This commit is contained in:
Tyler Renelle 2012-09-24 09:04:21 -04:00
parent d929905c48
commit f21a365a13
6 changed files with 20 additions and 33 deletions

View file

@ -3,14 +3,6 @@ var moment;
moment = require('moment');
module.exports.daysBetween = function(a, b) {
var DAY;
DAY = 1000 * 60 * 60 * 24;
a = new Date((new Date(a)).toDateString());
b = new Date((new Date(b)).toDateString());
return Math.abs(Math.floor((a.getTime() - b.getTime()) / DAY));
};
module.exports.viewHelpers = function(view) {
view.fn('taskClasses', function(type, completed, value, repeat) {
var classes, dayMapping;

View file

@ -1,8 +1,10 @@
// Generated by CoffeeScript 1.3.3
var content, userSchema;
var content, moment, userSchema;
content = require('./content');
moment = require('moment');
userSchema = {
balance: 2,
stats: {
@ -61,7 +63,7 @@ module.exports.updateSchema = function(model) {
model.del(userPath);
return;
}
daysOld = require('./helpers').daysBetween(userObj.lastCron, new Date());
daysOld = moment().sod().diff(moment(userObj.lastCron), 'days');
if (daysOld > 30) {
sameTasks = _.filter(require('./content').defaultTasks, function(defaultTask) {
var foundSame;

View file

@ -191,15 +191,15 @@ score = function(spec) {
cron = function() {
var daysPassed, lastCron, today;
today = new Date();
user.setNull('lastCron', today);
lastCron = user.get('lastCron');
daysPassed = helpers.daysBetween(lastCron, today);
today = moment().sod();
user.setNull('lastCron', today.toDate());
lastCron = moment(user.get('lastCron'));
daysPassed = today.diff(lastCron, 'days');
if (daysPassed > 0) {
user.set('lastCron', today);
return _(daysPassed).times(function(n) {
user.set('lastCron', today.toDate());
return _.times(daysPassed, function(n) {
var tallyFor;
tallyFor = moment(lastCron).add('d', n);
tallyFor = lastCron.add('d', n);
return tally(tallyFor);
});
}

View file

@ -1,13 +1,5 @@
moment = require('moment')
module.exports.daysBetween = (a, b) ->
#TODO replace this function with moment().diff()?
DAY = 1000 * 60 * 60 * 24
# calculate as midnight
a = new Date( (new Date(a)).toDateString() )
b = new Date( (new Date(b)).toDateString() )
return Math.abs(Math.floor((a.getTime() - b.getTime()) / DAY))
module.exports.viewHelpers = (view) ->
view.fn 'taskClasses', (type, completed, value, repeat) ->
#TODO figure out how to just pass in the task model, so i can access all these properties from one object

View file

@ -1,4 +1,5 @@
content = require './content'
moment = require 'moment'
userSchema = {
balance: 2
@ -40,7 +41,7 @@ module.exports.updateSchema = (model) ->
return
# Remove all users who haven't logged in for a month
daysOld = require('./helpers').daysBetween(userObj.lastCron, new Date())
daysOld = moment().sod().diff(moment(userObj.lastCron), 'days')
if daysOld > 30
# and who have mostly the default tasks
sameTasks = _.filter require('./content').defaultTasks, (defaultTask) ->

View file

@ -166,14 +166,14 @@ score = (spec = {task:null, direction:null, cron:null}) ->
return delta
cron = ->
today = new Date()
user.setNull('lastCron', today)
lastCron = user.get('lastCron')
daysPassed = helpers.daysBetween(lastCron, today)
today = moment().sod() # start of day
user.setNull 'lastCron', today.toDate()
lastCron = moment(user.get('lastCron'))
daysPassed = today.diff(lastCron, 'days')
if daysPassed > 0
user.set('lastCron', today) # reset cron
_(daysPassed).times (n) ->
tallyFor = moment(lastCron).add('d',n)
user.set('lastCron', today.toDate()) # reset cron
_.times daysPassed, (n) ->
tallyFor = lastCron.add('d',n)
tally(tallyFor)
# At end of day, add value to all incomplete Daily & Todo tasks (further incentive)