Skip to content

Commit 23ffe23

Browse files
committed
chore: move at/from/to guessing to method
1 parent 8d4e1b9 commit 23ffe23

File tree

1 file changed

+51
-64
lines changed

1 file changed

+51
-64
lines changed

source/mite-list.js

Lines changed: 51 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -170,97 +170,84 @@ Examples:
170170
`)
171171
.parse();
172172

173-
/**
174-
* Returns the request options for requesting the time entries or grouped
175-
* time entries for the given time entries and filtering options.
176-
*
177-
* @param {string} period
178-
* @param {object} program
179-
* @return {Object<String, any>} time entries or grouped time entries
180-
*/
181-
function getRequestOptions(period, opts) {
173+
function guessRequestParamsFromPeriod(period) {
174+
if (!period) return {};
175+
176+
const periodLc = period.toLowerCase();
177+
const now = new Date();
182178
// use notation of "3d" or "5w" to translate into from and to time periods
183-
const matches = String(period).match(/^(\d+)(d|w|m|day|week|month)s?$/);
179+
const matches = String(periodLc).match(/^(\d+)(d|w|m|y|day|week|month|year)s?$/);
184180
if (matches) {
185-
const from = new Date();
186-
switch(matches[2]) {
181+
const from = new Date(now.getTime());
182+
const amount = parseInt(matches[1], 10);
183+
switch(matches[2].substr(0, 1)) {
187184
case 'd':
188-
case 'day':
189-
from.setDate(from.getDate() - parseInt(matches[1], 10));
185+
from.setDate(from.getDate() - amount);
190186
break;
191187
case 'w':
192-
case 'week':
193-
from.setDate(from.getDate() - parseInt(matches[1], 10) * 7);
188+
from.setDate(from.getDate() - amount * 7);
194189
break;
195190
case 'm':
196-
case 'month':
197-
from.setMonth(from.getMonth() - parseInt(matches[1], 10));
191+
from.setMonth(from.getMonth() - amount);
198192
break;
199193
case 'y':
200-
case 'year':
201-
from.setFullYear(from.getFullYear() - parseInt(matches[1], 10));
194+
console.log(parseInt(matches[1]));
195+
from.setFullYear(from.getFullYear() - amount);
202196
break;
203197
}
204-
opts.from = from.toISOString().substr(0, 10);
205-
opts.to = (new Date).toISOString().substr(0, 10);
206-
period = undefined;
198+
return {
199+
from: from.toISOString().substr(0, 10),
200+
to: now.toISOString().substr(0, 10),
201+
};
207202
}
208203

209-
// mite’s "periods" strings using underscore like in "this_month", "this_week"
210-
// but sometimes the user enters a minus instead of underscore, then replace
211-
// it
212-
if (typeof period === 'string') {
213-
if (!period.match(/[\d]+/)) {
214-
period = period.replace(/-/g, '_');
215-
}
216-
217-
// check if the period is a week day name and calculate the date of this
218-
// weekday, f.e. "friday" from last week becomes the date a string
219-
const weekdays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
220-
const matchingWeekday = weekdays.find(weekday => {
221-
return (
222-
weekday === period ||
223-
period.toLowerCase() === weekday.toLowerCase() ||
224-
period.toLowerCase() === weekday.substr(0, 2)
225-
);
226-
});
227-
if (matchingWeekday) {
228-
const weekdayIndex = weekdays.indexOf(matchingWeekday);
229-
const todayIndex = (new Date).getDay();
230-
const now = new Date();
231-
if (todayIndex <= weekdayIndex) {
232-
now.setDate(now.getDate() - (7 - weekdayIndex + todayIndex));
233-
} else {
234-
now.setDate(now.getDate() - weekdayIndex - 1);
235-
}
236-
period = now.toISOString().substr(0, 10);
204+
// check if the period is a week day name and calculate the date of this
205+
// weekday, f.e. "friday" from last week becomes the date a string
206+
const weekdays = new Set(['su', 'mo', 'tu', 'we', 'th', 'fr', 'sa']);
207+
const weekdayIndex = [...weekdays].indexOf(periodLc.substr(0, 2));
208+
if (weekdayIndex >= 0) {
209+
const todayIndex = (new Date).getDay();
210+
if (todayIndex <= weekdayIndex) {
211+
now.setDate(now.getDate() - (7 - weekdayIndex + todayIndex));
212+
} else {
213+
now.setDate(now.getDate() - weekdayIndex - 1);
237214
}
215+
return {
216+
at: now.toISOString().substr(0, 10)
217+
};
238218
}
239219

240-
const data = {
220+
return {
221+
at: String(period).replace(/-/g, '_'),
222+
};
223+
}
224+
225+
/**
226+
* Returns the request options for requesting the time entries or grouped
227+
* time entries for the given time entries and filtering options.
228+
*
229+
* @param {string} period
230+
* @param {object} opts
231+
* @return {Object<String, any>} time entries or grouped time entries
232+
*/
233+
function getRequestOptions(period, opts) {
234+
return {
235+
...guessRequestParamsFromPeriod(period),
241236
...(typeof opts.billable === 'boolean' && { billable: opts.billable }),
242237
...(opts.customerId && { customer_id: opts.customerId }),
243-
...(opts.reversed && { direction: 'asc' }),
238+
...(opts.from && { from: opts.from}),
244239
...(opts.groupBy && { group_by: opts.groupBy }),
245240
...(opts.limit && { limit: opts.limit }),
246241
...(typeof opts.locked === 'boolean' && { locked: opts.locked }),
247-
...(opts.search && { note: opts.search }),
248242
...(opts.projectId && { project_id: opts.projectId }),
243+
...(opts.reversed && { direction: 'asc' }),
244+
...(opts.search && { note: opts.search }),
249245
...(opts.serviceId && { service_id: opts.serviceId }),
250246
...(opts.sort && { sort: opts.sort }),
251247
...(typeof opts.tracking === 'boolean' && { tracking: opts.tracking }),
248+
...(opts.to && { to: opts.to}),
252249
...(opts.userId && { user_id: opts.userId}),
253250
};
254-
255-
// add optional time period using from & to
256-
if (opts.from && opts.to) {
257-
data.from = opts.from;
258-
data.to = opts.to;
259-
} else if (period) {
260-
data.at = period;
261-
}
262-
263-
return data;
264251
}
265252

266253
/**

0 commit comments

Comments
 (0)