File size: 1,287 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
export interface QueryStatsParams {
	date?: string;
	start_date?: string;
	days?: number;
	max?: number;
	num?: number;
	period?: string;
	summarize?: number;
	filter_by_country?: string;
}

const getDaysOfMonthFromDate = ( date: string ): number => {
	const dateObj = new Date( date );
	const year = dateObj.getFullYear();
	const month = dateObj.getMonth() + 1;

	return new Date( year, month, 0 ).getDate();
};

const daysInYearFromDate = ( date: string ) => {
	const dateObj = new Date( date );
	const year = dateObj.getFullYear();

	return ( year % 4 === 0 && year % 100 > 0 ) || year % 400 === 0 ? 366 : 365;
};

export const processQueryParams = ( query: QueryStatsParams ) => {
	// `num` is only for the period `day`.
	const num = query.num || 1;
	// `max` is probably set to 0 to fetch all results.
	const max = query.max ?? 10;
	const date = query.date || new Date().toISOString().split( 'T' )[ 0 ];

	// Calculate the number of days to query based on the period.
	let days = num;
	switch ( query.period ) {
		case 'week':
			days = 7;
			break;
		case 'month':
			days = getDaysOfMonthFromDate( date );
			break;
		case 'year':
			days = daysInYearFromDate( date );
			break;
	}

	return {
		...query,
		start_date: query.start_date || '',
		num,
		max,
		date,
		days,
	};
};