From a380c1aa302d5ab16dbd52fada171501c1012428 Mon Sep 17 00:00:00 2001 From: Alys Date: Sun, 22 Nov 2015 08:38:45 +1000 Subject: [PATCH] refactor api.daysSince to a cron.js function not exposed on api --- common/script/cron.js | 11 ++++++++++- common/script/index.js | 23 +++-------------------- test/common/algos.mocha.js | 13 +++++++------ 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/common/script/cron.js b/common/script/cron.js index 2de430e73b..12b8ab60cd 100644 --- a/common/script/cron.js +++ b/common/script/cron.js @@ -47,7 +47,6 @@ export function startOfWeek (options) { 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). 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; } +/* + 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'); +} + diff --git a/common/script/index.js b/common/script/index.js index 350f8c4c5f..8d97b3fc58 100644 --- a/common/script/index.js +++ b/common/script/index.js @@ -2,6 +2,7 @@ import { DAY_MAPPING, // temporary, pending further refactoring sanitizeOptions, // temporary, pending further refactoring startOfDay, // temporary, pending further refactoring + daysSince, } from '../../common/script/cron'; 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? */ @@ -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; - 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; } 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 = {}; } now = +options.now || +(new Date); - daysMissed = api.daysSince(user.lastCron, _.defaults({ + daysMissed = daysSince(user.lastCron, _.defaults({ now: now }, user.preferences)); if (!(daysMissed > 0)) { diff --git a/test/common/algos.mocha.js b/test/common/algos.mocha.js index 293018c74c..b398a5bb72 100644 --- a/test/common/algos.mocha.js +++ b/test/common/algos.mocha.js @@ -3,6 +3,7 @@ import { DAY_MAPPING, startOfWeek, startOfDay, + daysSince, } from '../../common/script/cron'; let expect = require('expect.js'); @@ -1208,7 +1209,7 @@ describe('Cron', () => { }); it('daysSince cron before, now after', () => { 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'), dayStart, }); @@ -1217,7 +1218,7 @@ describe('Cron', () => { }); it('daysSince cron before, now before', () => { 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'), dayStart, }); @@ -1226,7 +1227,7 @@ describe('Cron', () => { }); it('daysSince cron after, now after', () => { 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'), dayStart, }); @@ -1235,7 +1236,7 @@ describe('Cron', () => { }); it('daysSince cron after, now tomorrow before', () => { 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'), dayStart, }); @@ -1244,7 +1245,7 @@ describe('Cron', () => { }); it('daysSince cron after, now tomorrow after', () => { 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'), dayStart, }); @@ -1253,7 +1254,7 @@ describe('Cron', () => { }); xit('daysSince, last cron before new dayStart', () => { 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'), dayStart, });