refactor api.daysSince to a cron.js function not exposed on api

This commit is contained in:
Alys
2015-11-22 08:38:45 +10:00
parent 2f8b6d68e8
commit a380c1aa30
3 changed files with 20 additions and 27 deletions

View File

@@ -47,7 +47,6 @@ export function startOfWeek (options) {
return moment(o.now).startOf('week'); return moment(o.now).startOf('week');
} }
/* /*
This is designed for use with any date that has an important time portion (e.g., when comparing the current date-time with the previous cron's date-time for determing if cron should run now). This is designed for use with any date that has an important time portion (e.g., when comparing the current date-time with the previous cron's date-time for determing if cron should run now).
It changes the time portion of the date-time to be the Custom Day Start hour, so that the date-time is now the user's correct start of day. It changes the time portion of the date-time to be the Custom Day Start hour, so that the date-time is now the user's correct start of day.
@@ -68,3 +67,13 @@ export function startOfDay (options) {
return dayStart; return dayStart;
} }
/*
Absolute diff from "yesterday" till now
*/
export function daysSince (yesterday, options = {}) {
let o = sanitizeOptions(options);
return startOfDay(_.defaults({ now: o.now }, o)).diff(startOfDay(_.defaults({ now: yesterday }, o)), 'days');
}

View File

@@ -2,6 +2,7 @@ import {
DAY_MAPPING, // temporary, pending further refactoring DAY_MAPPING, // temporary, pending further refactoring
sanitizeOptions, // temporary, pending further refactoring sanitizeOptions, // temporary, pending further refactoring
startOfDay, // temporary, pending further refactoring startOfDay, // temporary, pending further refactoring
daysSince,
} from '../../common/script/cron'; } from '../../common/script/cron';
var $w, _, api, content, i18n, moment, preenHistory, sortOrder, var $w, _, api, content, i18n, moment, preenHistory, sortOrder,
@@ -74,24 +75,6 @@ api.planGemLimits = {
------------------------------------------------------ ------------------------------------------------------
*/ */
/*
Absolute diff from "yesterday" till now
*/
api.daysSince = function(yesterday, options) {
var o;
if (options == null) {
options = {};
}
o = sanitizeOptions(options);
return startOfDay(_.defaults({
now: o.now
}, o)).diff(startOfDay(_.defaults({
now: yesterday
}, o)), 'days');
};
/* /*
Should the user do this task on this date, given the task's repeat options and user.preferences.dayStart? Should the user do this task on this date, given the task's repeat options and user.preferences.dayStart?
*/ */
@@ -2276,7 +2259,7 @@ api.wrap = function(user, main) {
} }
} }
dropMultiplier = ((ref1 = user.purchased) != null ? (ref2 = ref1.plan) != null ? ref2.customerId : void 0 : void 0) ? 2 : 1; dropMultiplier = ((ref1 = user.purchased) != null ? (ref2 = ref1.plan) != null ? ref2.customerId : void 0 : void 0) ? 2 : 1;
if ((api.daysSince(user.items.lastDrop.date, user.preferences) === 0) && (user.items.lastDrop.count >= dropMultiplier * (5 + Math.floor(user._statsComputed.per / 25) + (user.contributor.level || 0)))) { if ((daysSince(user.items.lastDrop.date, user.preferences) === 0) && (user.items.lastDrop.count >= dropMultiplier * (5 + Math.floor(user._statsComputed.per / 25) + (user.contributor.level || 0)))) {
return; return;
} }
if (((ref3 = user.flags) != null ? ref3.dropsEnabled : void 0) && user.fns.predictableRandom(user.stats.exp) < chance) { if (((ref3 = user.flags) != null ? ref3.dropsEnabled : void 0) && user.fns.predictableRandom(user.stats.exp) < chance) {
@@ -2483,7 +2466,7 @@ api.wrap = function(user, main) {
options = {}; options = {};
} }
now = +options.now || +(new Date); now = +options.now || +(new Date);
daysMissed = api.daysSince(user.lastCron, _.defaults({ daysMissed = daysSince(user.lastCron, _.defaults({
now: now now: now
}, user.preferences)); }, user.preferences));
if (!(daysMissed > 0)) { if (!(daysMissed > 0)) {

View File

@@ -3,6 +3,7 @@ import {
DAY_MAPPING, DAY_MAPPING,
startOfWeek, startOfWeek,
startOfDay, startOfDay,
daysSince,
} from '../../common/script/cron'; } from '../../common/script/cron';
let expect = require('expect.js'); let expect = require('expect.js');
@@ -1208,7 +1209,7 @@ describe('Cron', () => {
}); });
it('daysSince cron before, now after', () => { it('daysSince cron before, now after', () => {
let lastCron = moment('2014-10-09 02: 30: 00'); let lastCron = moment('2014-10-09 02: 30: 00');
let days = shared.daysSince(lastCron, { let days = daysSince(lastCron, {
now: moment('2014-10-09 11: 30: 00'), now: moment('2014-10-09 11: 30: 00'),
dayStart, dayStart,
}); });
@@ -1217,7 +1218,7 @@ describe('Cron', () => {
}); });
it('daysSince cron before, now before', () => { it('daysSince cron before, now before', () => {
let lastCron = moment('2014-10-09 02: 30: 00'); let lastCron = moment('2014-10-09 02: 30: 00');
let days = shared.daysSince(lastCron, { let days = daysSince(lastCron, {
now: moment('2014-10-09 03: 30: 00'), now: moment('2014-10-09 03: 30: 00'),
dayStart, dayStart,
}); });
@@ -1226,7 +1227,7 @@ describe('Cron', () => {
}); });
it('daysSince cron after, now after', () => { it('daysSince cron after, now after', () => {
let lastCron = moment('2014-10-09 05: 30: 00'); let lastCron = moment('2014-10-09 05: 30: 00');
let days = shared.daysSince(lastCron, { let days = daysSince(lastCron, {
now: moment('2014-10-09 06: 30: 00'), now: moment('2014-10-09 06: 30: 00'),
dayStart, dayStart,
}); });
@@ -1235,7 +1236,7 @@ describe('Cron', () => {
}); });
it('daysSince cron after, now tomorrow before', () => { it('daysSince cron after, now tomorrow before', () => {
let lastCron = moment('2014-10-09 12: 30: 00'); let lastCron = moment('2014-10-09 12: 30: 00');
let days = shared.daysSince(lastCron, { let days = daysSince(lastCron, {
now: moment('2014-10-10 01: 30: 00'), now: moment('2014-10-10 01: 30: 00'),
dayStart, dayStart,
}); });
@@ -1244,7 +1245,7 @@ describe('Cron', () => {
}); });
it('daysSince cron after, now tomorrow after', () => { it('daysSince cron after, now tomorrow after', () => {
let lastCron = moment('2014-10-09 12: 30: 00'); let lastCron = moment('2014-10-09 12: 30: 00');
let days = shared.daysSince(lastCron, { let days = daysSince(lastCron, {
now: moment('2014-10-10 10: 30: 00'), now: moment('2014-10-10 10: 30: 00'),
dayStart, dayStart,
}); });
@@ -1253,7 +1254,7 @@ describe('Cron', () => {
}); });
xit('daysSince, last cron before new dayStart', () => { xit('daysSince, last cron before new dayStart', () => {
let lastCron = moment('2014-10-09 01: 00: 00'); let lastCron = moment('2014-10-09 01: 00: 00');
let days = shared.daysSince(lastCron, { let days = daysSince(lastCron, {
now: moment('2014-10-09 05: 00: 00'), now: moment('2014-10-09 05: 00: 00'),
dayStart, dayStart,
}); });