-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (76 loc) · 3.19 KB
/
index.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"use strict";
const moment = require("moment");
const GRANULARITY = {
day: "day",
year: "year",
month: "month",
quarter: "quarter"
};
const FORMATS = {
day: "YYYY-MM-DD",
month: "YYYY-MM",
quarter: "YYYY-Qo",
year: "YYYY",
};
let formatDate = (format, date)=>(new moment(date).format(format));
let isGranularityAcceptable = (restrictedGranularity, granularity)=> {
return restrictedGranularity[granularity] !== true
};
let isYearStart = date=>(date.month() === 0 && date.date() === 1);
let isFullYearIncluded = (startDate, lastDate)=>(+moment(+startDate).add(1, "y") <= +lastDate);
let isQuarterStart = date=>(date.quarter() !== moment(+date).subtract(1, "d").quarter());
let isFullQuarterIncluded = (startDate, lastDate)=>+moment(+startDate).add(1, "Q") <= +lastDate;
let isMonthStart = date=>date.date() === 1;
let isFullMonthIncluded = (startDate, lastDate)=>+moment(+startDate).add(1, "M") <= +lastDate;
function saveDate(granularity, unit, formatFn, dates, d) {
if (!dates[granularity]) dates[granularity] = [];
dates[granularity].push(formatFn(d));
d.add(1, unit);
}
const saveYear = saveDate.bind(null, GRANULARITY.year, 'y');
const saveQuarter = saveDate.bind(null, GRANULARITY.quarter, 'Q');
const saveMonth = saveDate.bind(null, GRANULARITY.month, 'M');
const saveDay = saveDate.bind(null, GRANULARITY.day, 'd');
function getRange(dateFrom, dateTo, config = {}) {
let restrictedGranularity = config.restrictedGranularity || {};
let formats = { ...FORMATS, ...(config.formats || {})};
let formatDay = formatDate.bind(null, formats.day);
let formatMonth = formatDate.bind(null, formats.month);
let formatQuarter = formatDate.bind(null, formats.quarter);
let formatYear = formatDate.bind(null, formats.year);
let dates = {};
let d = moment(dateFrom);
let to = moment(dateTo);
while (+d <= +to) {
if (isGranularityAcceptable(restrictedGranularity, GRANULARITY.year) && isYearStart(d) && isFullYearIncluded(d, to)) {
saveYear(formatYear, dates, d);
continue;
}
if (isGranularityAcceptable(restrictedGranularity, GRANULARITY.quarter) && isQuarterStart(d) && isFullQuarterIncluded(d, to)) {
saveQuarter(formatQuarter, dates, d);
continue;
}
if (isGranularityAcceptable(restrictedGranularity, GRANULARITY.month) && isMonthStart(d) && isFullMonthIncluded(d, to)) {
saveMonth(formatMonth, dates, d);
continue;
}
if (isGranularityAcceptable(restrictedGranularity, GRANULARITY.day)) {
saveDay(formatDay, dates, d);
continue;
}
if (!isGranularityAcceptable(restrictedGranularity, GRANULARITY.day)) {
if (restrictedGranularity.month !== true) {
saveMonth(formatMonth, dates, d);
continue;
} else if (restrictedGranularity.quarter !== true) {
saveQuarter(formatQuarter, dates, d);
continue;
} else if (restrictedGranularity.year !== true) {
saveYear(formatYear, dates, d);
continue;
}
}
}
return dates;
}
module.exports = getRange;