mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
refactor sanitizeOptions from index.js to cron.js
This commit is contained in:
@@ -3,6 +3,9 @@
|
|||||||
Cron and time / day functions
|
Cron and time / day functions
|
||||||
------------------------------------------------------
|
------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
|
let moment = require('moment');
|
||||||
|
|
||||||
export const DAY_MAPPING = {
|
export const DAY_MAPPING = {
|
||||||
0: 'su',
|
0: 'su',
|
||||||
@@ -14,3 +17,23 @@ export const DAY_MAPPING = {
|
|||||||
6: 's',
|
6: 's',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Each time we perform date maths (cron, task-due-days, etc), we need to consider user preferences.
|
||||||
|
Specifically {dayStart} (custom day start) and {timezoneOffset}. This function sanitizes / defaults those values.
|
||||||
|
{now} is also passed in for various purposes, one example being the test scripts scripts testing different "now" times.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function sanitizeOptions (o) {
|
||||||
|
let ref = Number(o.dayStart);
|
||||||
|
let dayStart = !_.isNaN(ref) && ref >= 0 && ref <= 24 ? ref : 0;
|
||||||
|
let timezoneOffset = o.timezoneOffset ? Number(o.timezoneOffset) : Number(moment().zone());
|
||||||
|
// TODO: check and clean timezoneOffset as for dayStart (e.g., might not be a number)
|
||||||
|
let now = o.now ? moment(o.now).zone(timezoneOffset) : moment().zone(timezoneOffset);
|
||||||
|
|
||||||
|
// return a new object, we don't want to add "now" to user object
|
||||||
|
return {
|
||||||
|
dayStart,
|
||||||
|
timezoneOffset,
|
||||||
|
now,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import {
|
import {
|
||||||
DAY_MAPPING // temporary, pending further refactoring
|
DAY_MAPPING, // temporary, pending further refactoring
|
||||||
|
sanitizeOptions, // temporary, pending further refactoring
|
||||||
} from '../../common/script/cron';
|
} from '../../common/script/cron';
|
||||||
|
|
||||||
var $w, _, api, content, i18n, moment, preenHistory, sanitizeOptions, sortOrder,
|
var $w, _, api, content, i18n, moment, preenHistory, sortOrder,
|
||||||
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
|
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
|
||||||
|
|
||||||
moment = require('moment');
|
moment = require('moment');
|
||||||
@@ -72,25 +73,6 @@ api.planGemLimits = {
|
|||||||
------------------------------------------------------
|
------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
Each time we're performing date math (cron, task-due-days, etc), we need to take user preferences into consideration.
|
|
||||||
Specifically {dayStart} (custom day start) and {timezoneOffset}. This function sanitizes / defaults those values.
|
|
||||||
{now} is also passed in for various purposes, one example being the test scripts scripts testing different "now" times
|
|
||||||
*/
|
|
||||||
|
|
||||||
sanitizeOptions = function(o) {
|
|
||||||
var dayStart, now, ref, timezoneOffset;
|
|
||||||
dayStart = !_.isNaN(+o.dayStart) && (0 <= (ref = +o.dayStart) && ref <= 24) ? +o.dayStart : 0;
|
|
||||||
timezoneOffset = o.timezoneOffset ? +o.timezoneOffset : +moment().zone();
|
|
||||||
now = o.now ? moment(o.now).zone(timezoneOffset) : moment(+(new Date)).zone(timezoneOffset);
|
|
||||||
return {
|
|
||||||
dayStart: dayStart,
|
|
||||||
timezoneOffset: timezoneOffset,
|
|
||||||
now: now
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
api.startOfWeek = api.startOfWeek = function(options) {
|
api.startOfWeek = api.startOfWeek = function(options) {
|
||||||
var o;
|
var o;
|
||||||
if (options == null) {
|
if (options == null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user