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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | 11x 11x 11x 5x 5x 2x 2x 8x 8x 8x 8x 8x 4x 8x 4x 13x 1x 13x 8x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 8x | import { isDate, isString, isNumber, isObject, isArray, isFunction, } from './typeCheckers'; import defaults from './defaults'; import { mixinOptionalProps } from './helpers'; const millisecondsPerDay = 24 * 60 * 60 * 1000; export function addDays(date, days) { const result = new Date(date); result.setDate(result.getDate() + days); return result; } // function compareDates(date1, date2) { // if (!date1 && !date2) return 0; // if (!date1) return -1; // if (!date2) return 1; // return date1 - date2; // } // function minDate(date1, date2) { // return compareDates(date1, date2) === -1 ? date1 : date2; // } // Returns a date range that intersects two date info objects // This is a shallow calculation (does not take patterns into account), // so this method should only really be called for special conditions // where absolute accuracy is not necessarily needed // function findShallowIntersectingRange(date1, date2) { // const thisRange = date1.toRange(); // const otherRange = date2.toRange(); // // Start with infinite start and end dates // let start = null; // let end = null; // // This start date exists // if (thisRange.start) { // // Use this definite start date if other start date is infinite // if (!otherRange.start) start = thisRange.start; // // Otherwise, use the earliest start date // else start = thisRange.start < otherRange.start ? thisRange.start : otherRange.start; // // Other start date exists // } else if (otherRange.start) { // // Use other definite start date as this one is infinite // start = otherRange.start; // } // // Assign end date to this one if it is valid // if (thisRange.end && (!start || thisRange.end > start)) { // end = thisRange.end; // } // // Assign end date to other one if it is valid and before this one // if (otherRange.end && (!start || otherRange.end > start)) { // end = minDate(end, otherRange.end); // } // // Return calculated range // return { start, end }; // } // ======================================================== // Determines if first date completely includes second date // This is a shallow test (no patterns tested) function dateShallowIncludesDate(date1, date2) { // First date is simple date Eif (date1.isDate) { if (date2.isDate) return date1.dateTime === date2.dateTime; Iif (!date2.startTime || !date2.endTime) return false; return date1.dateTime === date2.startTime && date1.dateTime === date2.endTime; } // Second date is simple date and first is date range if (date2.isDate) { if (date1.start && date2.date < date1.start) return false; if (date1.end && date2.date > date1.end) return false; return true; } // Both dates are date ranges if (date1.start && (!date2.start || date2.start < date1.start)) return false; if (date1.end && (!date2.end || date2.end > date1.end)) return false; return true; } // ======================================================== // Determines if first date partially intersects second date // This is a shallow test (no patterns tested) function dateShallowIntersectsDate(date1, date2) { if (date1.isDate) return date2.isDate ? date1.dateTime === date2.dateTime : dateShallowIncludesDate(date2, date1); if (date2.isDate) return dateShallowIncludesDate(date1, date2); // Both ranges if (date1.start && date2.end && date1.start > date2.end) return false; if (date1.end && date2.start && date1.end < date2.start) return false; return true; } function startOfWeek(date) { const day = date.getDay() + 1; const { firstDayOfWeek } = defaults; const daysToAdd = day >= firstDayOfWeek ? firstDayOfWeek - day : -(7 - (firstDayOfWeek - day)); return addDays(date, daysToAdd); } function diffInDays(d1, d2) { return Math.round((d2 - d1) / millisecondsPerDay); } function diffInWeeks(d1, d2) { return diffInDays(startOfWeek(d1), startOfWeek(d2)); } function diffInYears(d1, d2) { return d2.getUTCFullYear() - d1.getUTCFullYear(); } function diffInMonths(d1, d2) { return (diffInYears(d1, d2) * 12) + (d2.getMonth() - d1.getMonth()); } const _patterns = { dailyInterval: { test: (dayInfo, interval, { start }) => diffInDays(start || new Date(), dayInfo.date) % interval === 0, }, weeklyInterval: { test: (dayInfo, interval, { start }) => diffInWeeks(start || new Date(), dayInfo.date) % interval === 0, }, monthlyInterval: { test: (dayInfo, interval, { start }) => diffInMonths(start || new Date(), dayInfo.date) % interval === 0, }, yearlyInterval: { test: () => (dayInfo, interval, { start }) => diffInYears(start || new Date(), dayInfo.date) % interval === 0, }, days: { validate: days => (isArray(days) ? days : [parseInt(days, 10)]), test: (dayInfo, days) => days.includes(dayInfo.day) || days.includes(-dayInfo.dayFromEnd), }, weekdays: { validate: weekdays => (isArray(weekdays) ? weekdays : [parseInt(weekdays, 10)]), test: (dayInfo, weekdays) => weekdays.includes(dayInfo.weekday), }, ordinalWeekdays: { validate: ordinalWeekdays => Object.keys(ordinalWeekdays) .reduce((obj, ck) => { const weekdays = ordinalWeekdays[ck]; if (!weekdays) return obj; obj[ck] = isArray(weekdays) ? weekdays : [parseInt(weekdays, 10)]; return obj; }, {}), test: (dayInfo, ordinalWeekdays) => Object.keys(ordinalWeekdays) .map(k => parseInt(k, 10)) .find(k => ordinalWeekdays[k].includes(dayInfo.weekday) && (k === dayInfo.weekdayOrdinal || k === -dayInfo.weekdayOrdinalFromEnd)), }, weekends: { validate: config => config, test: dayInfo => dayInfo.weekday === 1 || dayInfo.weekday === 7, }, workweek: { validate: config => config, test: dayInfo => dayInfo.weekday >= 2 && dayInfo.weekday <= 6, }, weeks: { validate: weeks => (isArray(weeks) ? weeks : [parseInt(weeks, 10)]), test: (dayInfo, weeks) => weeks.includes(dayInfo.week) || weeks.includes(-dayInfo.weekFromEnd), }, months: { validate: months => (isArray(months) ? months : [parseInt(months, 10)]), test: (dayInfo, months) => months.includes(dayInfo.month), }, years: { validate: years => (isArray(years) ? years : [parseInt(years, 10)]), test: (dayInfo, years) => years.includes(dayInfo.year), }, }; const _patternProps = Object.keys(_patterns).map(k => ({ name: k, validate: _patterns[k].validate })); const testConfig = (config, dayInfo, info) => { if (isFunction(config)) return config(dayInfo); if (isObject(config)) { return Object.keys(config).every(k => _patterns[k].test(dayInfo, config[k], info)); } return null; }; const DateInfo = (config, order) => { Iif (!config) return null; const info = { isDateInfo: true, isDate: isDate(config) || isString(config) || isNumber(config), isRange: isObject(config) || isFunction(config), order: order || 0, }; // Process date if (info.isDate) I{ info.type = 'date'; // Initialize date from config const date = new Date(config); // Can't accept invalid dates if (isNaN(date)) return null; // Strip date time date.setHours(0, 0, 0, 0); // Assign date info.date = date; info.dateTime = date.getTime(); } // Process date range if (info.isRange) I{ info.type = 'range'; // Date config is a function if (isFunction(config)) { info.on = { and: config }; // Date config is an object } else IEEI{ // Initialize start and end dates (null means infinity) let start = config.start && new Date(config.start); let end = config.end && new Date(config.end); // Reconfigure start and end dates if needed if (start && end && start > end) { const temp = start; start = end; end = temp; } else Iif (start && config.span >= 1) { end = addDays(start, config.span - 1); } // Reset invalid dates to null and strip times for valid dates if (start) { Iif (isNaN(start.getTime())) start = null; else start.setHours(0, 0, 0, 0); } Eif (end) { Iif (isNaN(end.getTime())) end = null; else end.setHours(0, 0, 0, 0); } // Assign start and end dates info.start = start; info.end = end; info.startTime = start && start.getTime(); info.endTime = end && end.getTime(); // Assign span info if (start && end) { info.daySpan = diffInDays(start, end); info.weekSpan = diffInWeeks(start, end); info.monthSpan = diffInMonths(start, end); info.yearSpan = diffInYears(start, end); } // Assign 'and' condition const andOpt = mixinOptionalProps(config, {}, _patternProps); Iif (andOpt.assigned) { info.on = { and: andOpt.target }; } // Assign 'or' conditions if (config.on) { const or = (isArray(config.on) ? config.on : [config.on]) .map((o) => { if (isFunction(o)) return o; const opt = mixinOptionalProps(o, {}, _patternProps); return opt.assigned ? opt.target : null; }) .filter(o => o); if (or.length) info.on = { ...info.on, or }; } } // Assign flag if date info is complex info.isComplex = !!info.on; } // ======================================================== // Determines if this date completely includes another date // This is a shallow test (no patterns tested) info.shallowIncludes = other => dateShallowIncludesDate(info, other.isDate ? other : DateInfo(other)); // ======================================================== info.includes = other => info.shallowIncludes(other); // ======================================================== // Determines if this date partially intersects another date // This is a shallow test (no patterns tested) info.shallowIntersects = other => dateShallowIntersectsDate(info, other.isDate ? other : DateInfo(other)); // ======================================================== info.intersects = (other) => { const i = info.shallowIntersects(other); // const intersectingRange = findShallowIntersectingRange(info, other.isDateInfo ? other : DateInfo(other)); return i; }; // ======================================================== // Finds the first match for the given day info.includesDay = (dayInfo) => { const date = DateInfo(dayInfo.date); // Date is outside general range - return null if (!info.shallowIncludes(date)) return null; if (!info.on) return info; // Fail if 'and' condition fails if (info.on.and && !testConfig(info.on.and, dayInfo, info)) return null; // Fail if every 'or' condition fails if (info.on.or && !info.on.or.find(or => testConfig(or, dayInfo, info))) return null; // Return date info for day date return date; }; info.toRange = () => { if (info.isDate) { return DateInfo({ start: info.date, end: info.date, }); } return DateInfo({ start: info.start, end: info.end, }); }; // Build the 'compare to other' function info.compare = (other) => { if (info.order !== other.order) return info.order - other.order; if (info.type !== other.type) return info.isDate ? 1 : -1; if (info.isDate) return 0; const diff = info.start - other.start; return diff !== 0 ? diff : info.end - other.end; }; // Return fully configured date info object return info; }; export default DateInfo; |