From 762e8cfed149e0b31577fec2c9de416627eaae07 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Mon, 10 Jun 2019 19:26:41 +0300 Subject: [PATCH] chore(deps): upgrade date-math (#1354) The diff is quite nice. ```diff - "minified": 150810, + "minified": 148190, - "gzipped": 45487 + "gzipped": 44792 ``` --- .size-snapshot.json | 24 +-- package.json | 2 +- src/Agenda.js | 2 +- src/BackgroundCells.js | 2 +- src/DateContentRow.js | 2 +- src/Day.js | 2 +- src/DayColumn.js | 2 +- src/EventCell.js | 2 +- src/Month.js | 2 +- src/Popup.js | 2 +- src/TimeGrid.js | 2 +- src/TimeGridHeader.js | 2 +- src/Week.js | 2 +- src/localizers/globalize.js | 2 +- src/localizers/moment.js | 2 +- src/localizers/oldGlobalize.js | 2 +- src/utils/DateSlotMetrics.js | 2 +- src/utils/TimeSlots.js | 2 +- src/utils/dates.js | 265 +++++++++++++++++---------------- src/utils/eventLevels.js | 2 +- yarn.lock | 8 +- 21 files changed, 173 insertions(+), 160 deletions(-) diff --git a/.size-snapshot.json b/.size-snapshot.json index 8e0faf8cc..cee50edc3 100644 --- a/.size-snapshot.json +++ b/.size-snapshot.json @@ -1,25 +1,25 @@ { "./dist/react-big-calendar.js": { - "bundled": 554087, - "minified": 170209, - "gzipped": 49950 + "bundled": 548465, + "minified": 167635, + "gzipped": 49330 }, "./dist/react-big-calendar.min.js": { - "bundled": 490328, - "minified": 150824, - "gzipped": 45415 + "bundled": 484706, + "minified": 148190, + "gzipped": 44792 }, "dist/react-big-calendar.esm.js": { - "bundled": 169648, - "minified": 81524, - "gzipped": 20082, + "bundled": 167242, + "minified": 79833, + "gzipped": 19791, "treeshaked": { "rollup": { - "code": 59035, - "import_statements": 1402 + "code": 57605, + "import_statements": 1572 }, "webpack": { - "code": 62470 + "code": 62052 } } } diff --git a/package.json b/package.json index 2fb291c80..1a02d36c7 100644 --- a/package.json +++ b/package.json @@ -123,7 +123,7 @@ "@babel/runtime": "^7.1.5", "bootstrap-sass": "^3.4.1", "classnames": "^2.2.6", - "date-arithmetic": "^3.0.0", + "date-arithmetic": "^4.0.0", "dom-helpers": "^3.4.0", "invariant": "^2.2.4", "lodash": "^4.17.11", diff --git a/src/Agenda.js b/src/Agenda.js index 77188ef5f..a3cb07634 100644 --- a/src/Agenda.js +++ b/src/Agenda.js @@ -4,7 +4,7 @@ import classes from 'dom-helpers/class' import getWidth from 'dom-helpers/query/width' import scrollbarSize from 'dom-helpers/util/scrollbarSize' -import dates from './utils/dates' +import * as dates from './utils/dates' import { navigate } from './utils/constants' import { inRange } from './utils/eventLevels' import { isSelected } from './utils/selection' diff --git a/src/BackgroundCells.js b/src/BackgroundCells.js index 62dab92e2..c4a00958e 100644 --- a/src/BackgroundCells.js +++ b/src/BackgroundCells.js @@ -3,7 +3,7 @@ import React from 'react' import { findDOMNode } from 'react-dom' import cn from 'classnames' -import dates from './utils/dates' +import * as dates from './utils/dates' import { notify } from './utils/helpers' import { dateCellSelection, getSlotAtX, pointInBox } from './utils/selection' import Selection, { getBoundsForNode, isEvent } from './Selection' diff --git a/src/DateContentRow.js b/src/DateContentRow.js index 38e3a16fc..87a04b318 100644 --- a/src/DateContentRow.js +++ b/src/DateContentRow.js @@ -5,7 +5,7 @@ import PropTypes from 'prop-types' import React from 'react' import { findDOMNode } from 'react-dom' -import dates from './utils/dates' +import * as dates from './utils/dates' import BackgroundCells from './BackgroundCells' import EventRow from './EventRow' import EventEndingRow from './EventEndingRow' diff --git a/src/Day.js b/src/Day.js index 8176973e5..ad059872e 100644 --- a/src/Day.js +++ b/src/Day.js @@ -1,7 +1,7 @@ import PropTypes from 'prop-types' import React from 'react' -import dates from './utils/dates' +import * as dates from './utils/dates' import { navigate } from './utils/constants' import TimeGrid from './TimeGrid' diff --git a/src/DayColumn.js b/src/DayColumn.js index 6a03b0c0c..03b04c112 100644 --- a/src/DayColumn.js +++ b/src/DayColumn.js @@ -4,7 +4,7 @@ import { findDOMNode } from 'react-dom' import cn from 'classnames' import Selection, { getBoundsForNode, isEvent } from './Selection' -import dates from './utils/dates' +import * as dates from './utils/dates' import * as TimeSlotUtils from './utils/TimeSlots' import { isSelected } from './utils/selection' diff --git a/src/EventCell.js b/src/EventCell.js index 28781875a..c68d1952f 100644 --- a/src/EventCell.js +++ b/src/EventCell.js @@ -1,7 +1,7 @@ import PropTypes from 'prop-types' import React from 'react' import cn from 'classnames' -import dates from './utils/dates' +import * as dates from './utils/dates' class EventCell extends React.Component { render() { diff --git a/src/Month.js b/src/Month.js index 4b07ab455..69ae8818c 100644 --- a/src/Month.js +++ b/src/Month.js @@ -3,7 +3,7 @@ import React from 'react' import { findDOMNode } from 'react-dom' import cn from 'classnames' -import dates from './utils/dates' +import * as dates from './utils/dates' import chunk from 'lodash/chunk' import { navigate, views } from './utils/constants' diff --git a/src/Popup.js b/src/Popup.js index e5484278b..c376c490a 100644 --- a/src/Popup.js +++ b/src/Popup.js @@ -3,7 +3,7 @@ import React from 'react' import getOffset from 'dom-helpers/query/offset' import getScrollTop from 'dom-helpers/query/scrollTop' import getScrollLeft from 'dom-helpers/query/scrollLeft' -import dates from './utils/dates' +import * as dates from './utils/dates' import EventCell from './EventCell' import { isSelected } from './utils/selection' diff --git a/src/TimeGrid.js b/src/TimeGrid.js index 62d9e4c86..5785916fd 100644 --- a/src/TimeGrid.js +++ b/src/TimeGrid.js @@ -5,7 +5,7 @@ import React, { Component } from 'react' import { findDOMNode } from 'react-dom' import memoize from 'memoize-one' -import dates from './utils/dates' +import * as dates from './utils/dates' import DayColumn from './DayColumn' import TimeGutter from './TimeGutter' diff --git a/src/TimeGridHeader.js b/src/TimeGridHeader.js index 0d68e3a7b..aaa46ce5c 100644 --- a/src/TimeGridHeader.js +++ b/src/TimeGridHeader.js @@ -3,7 +3,7 @@ import cn from 'classnames' import scrollbarSize from 'dom-helpers/util/scrollbarSize' import React from 'react' -import dates from './utils/dates' +import * as dates from './utils/dates' import DateContentRow from './DateContentRow' import Header from './Header' import ResourceHeader from './ResourceHeader' diff --git a/src/Week.js b/src/Week.js index 3d5202608..3b5771ab9 100644 --- a/src/Week.js +++ b/src/Week.js @@ -1,6 +1,6 @@ import PropTypes from 'prop-types' import React from 'react' -import dates from './utils/dates' +import * as dates from './utils/dates' import { navigate } from './utils/constants' import TimeGrid from './TimeGrid' diff --git a/src/localizers/globalize.js b/src/localizers/globalize.js index c5419b638..325ff9d52 100644 --- a/src/localizers/globalize.js +++ b/src/localizers/globalize.js @@ -1,4 +1,4 @@ -import dates from '../utils/dates' +import * as dates from '../utils/dates' import oldGlobalize from './oldGlobalize' import { DateLocalizer } from '../localizer' import warning from 'warning' diff --git a/src/localizers/moment.js b/src/localizers/moment.js index ad1a52c13..cb21cf458 100644 --- a/src/localizers/moment.js +++ b/src/localizers/moment.js @@ -1,4 +1,4 @@ -import dates from '../utils/dates' +import * as dates from '../utils/dates' import { DateLocalizer } from '../localizer' let dateRangeFormat = ({ start, end }, culture, local) => diff --git a/src/localizers/oldGlobalize.js b/src/localizers/oldGlobalize.js index 56c01bb66..22591b8c2 100644 --- a/src/localizers/oldGlobalize.js +++ b/src/localizers/oldGlobalize.js @@ -1,4 +1,4 @@ -import dates from '../utils/dates' +import * as dates from '../utils/dates' import { DateLocalizer } from '../localizer' let dateRangeFormat = ({ start, end }, culture, local) => diff --git a/src/utils/DateSlotMetrics.js b/src/utils/DateSlotMetrics.js index 8e630e7b2..269d56ee9 100644 --- a/src/utils/DateSlotMetrics.js +++ b/src/utils/DateSlotMetrics.js @@ -1,5 +1,5 @@ import memoize from 'memoize-one' -import dates from './dates' +import * as dates from './dates' import { eventSegments, endOfRange, eventLevels } from './eventLevels' let isSegmentInSlot = (seg, slot) => seg.left <= slot && seg.right >= slot diff --git a/src/utils/TimeSlots.js b/src/utils/TimeSlots.js index 85a2de2c7..e0118b554 100644 --- a/src/utils/TimeSlots.js +++ b/src/utils/TimeSlots.js @@ -1,4 +1,4 @@ -import dates from './dates' +import * as dates from './dates' const getDstOffset = (start, end) => start.getTimezoneOffset() - end.getTimezoneOffset() diff --git a/src/utils/dates.js b/src/utils/dates.js index 23f6811d1..7e6eabfa7 100644 --- a/src/utils/dates.js +++ b/src/utils/dates.js @@ -1,5 +1,24 @@ /* eslint no-fallthrough: off */ -import dateMath from 'date-arithmetic' +import * as dates from 'date-arithmetic' + +export { + milliseconds, + seconds, + minutes, + hours, + month, + startOf, + endOf, + add, + eq, + gte, + gt, + lte, + lt, + inRange, + min, + max, +} from 'date-arithmetic' const MILLI = { seconds: 1000, @@ -10,148 +29,142 @@ const MILLI = { const MONTHS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] -let dates = { - ...dateMath, +export function monthsInYear(year) { + let date = new Date(year, 0, 1) - monthsInYear(year) { - let date = new Date(year, 0, 1) + return MONTHS.map(i => dates.month(date, i)) +} - return MONTHS.map(i => dates.month(date, i)) - }, +export function firstVisibleDay(date, localizer) { + let firstOfMonth = dates.startOf(date, 'month') - firstVisibleDay(date, localizer) { - let firstOfMonth = dates.startOf(date, 'month') + return dates.startOf(firstOfMonth, 'week', localizer.startOfWeek()) +} - return dates.startOf(firstOfMonth, 'week', localizer.startOfWeek()) - }, +export function lastVisibleDay(date, localizer) { + let endOfMonth = dates.endOf(date, 'month') - lastVisibleDay(date, localizer) { - let endOfMonth = dates.endOf(date, 'month') + return dates.endOf(endOfMonth, 'week', localizer.startOfWeek()) +} - return dates.endOf(endOfMonth, 'week', localizer.startOfWeek()) - }, +export function visibleDays(date, localizer) { + let current = firstVisibleDay(date, localizer), + last = lastVisibleDay(date, localizer), + days = [] - visibleDays(date, localizer) { - let current = dates.firstVisibleDay(date, localizer), - last = dates.lastVisibleDay(date, localizer), - days = [] + while (dates.lte(current, last, 'day')) { + days.push(current) + current = dates.add(current, 1, 'day') + } - while (dates.lte(current, last, 'day')) { - days.push(current) - current = dates.add(current, 1, 'day') - } + return days +} - return days - }, +export function ceil(date, unit) { + let floor = dates.startOf(date, unit) - ceil(date, unit) { - let floor = dates.startOf(date, unit) + return dates.eq(floor, date) ? floor : dates.add(floor, 1, unit) +} - return dates.eq(floor, date) ? floor : dates.add(floor, 1, unit) - }, +export function range(start, end, unit = 'day') { + let current = start, + days = [] - range(start, end, unit = 'day') { - let current = start, - days = [] + while (dates.lte(current, end, unit)) { + days.push(current) + current = dates.add(current, 1, unit) + } - while (dates.lte(current, end, unit)) { - days.push(current) - current = dates.add(current, 1, unit) - } + return days +} - return days - }, +export function merge(date, time) { + if (time == null && date == null) return null - merge(date, time) { - if (time == null && date == null) return null + if (time == null) time = new Date() + if (date == null) date = new Date() - if (time == null) time = new Date() - if (date == null) date = new Date() + date = dates.startOf(date, 'day') + date = dates.hours(date, dates.hours(time)) + date = dates.minutes(date, dates.minutes(time)) + date = dates.seconds(date, dates.seconds(time)) + return dates.milliseconds(date, dates.milliseconds(time)) +} - date = dates.startOf(date, 'day') - date = dates.hours(date, dates.hours(time)) - date = dates.minutes(date, dates.minutes(time)) - date = dates.seconds(date, dates.seconds(time)) - return dates.milliseconds(date, dates.milliseconds(time)) - }, +export function eqTime(dateA, dateB) { + return ( + dates.hours(dateA) === dates.hours(dateB) && + dates.minutes(dateA) === dates.minutes(dateB) && + dates.seconds(dateA) === dates.seconds(dateB) + ) +} - eqTime(dateA, dateB) { - return ( - dates.hours(dateA) === dates.hours(dateB) && - dates.minutes(dateA) === dates.minutes(dateB) && - dates.seconds(dateA) === dates.seconds(dateB) - ) - }, - - isJustDate(date) { - return ( - dates.hours(date) === 0 && - dates.minutes(date) === 0 && - dates.seconds(date) === 0 && - dates.milliseconds(date) === 0 - ) - }, +export function isJustDate(date) { + return ( + dates.hours(date) === 0 && + dates.minutes(date) === 0 && + dates.seconds(date) === 0 && + dates.milliseconds(date) === 0 + ) +} - duration(start, end, unit, firstOfWeek) { - if (unit === 'day') unit = 'date' - return Math.abs( - dates[unit](start, undefined, firstOfWeek) - - dates[unit](end, undefined, firstOfWeek) - ) - }, - - diff(dateA, dateB, unit) { - if (!unit || unit === 'milliseconds') return Math.abs(+dateA - +dateB) - - // the .round() handles an edge case - // with DST where the total won't be exact - // since one day in the range may be shorter/longer by an hour - return Math.round( - Math.abs( - +dates.startOf(dateA, unit) / MILLI[unit] - - +dates.startOf(dateB, unit) / MILLI[unit] - ) +export function duration(start, end, unit, firstOfWeek) { + if (unit === 'day') unit = 'date' + return Math.abs( + dates[unit](start, undefined, firstOfWeek) - + dates[unit](end, undefined, firstOfWeek) + ) +} + +export function diff(dateA, dateB, unit) { + if (!unit || unit === 'milliseconds') return Math.abs(+dateA - +dateB) + + // the .round() handles an edge case + // with DST where the total won't be exact + // since one day in the range may be shorter/longer by an hour + return Math.round( + Math.abs( + +dates.startOf(dateA, unit) / MILLI[unit] - + +dates.startOf(dateB, unit) / MILLI[unit] ) - }, - - total(date, unit) { - let ms = date.getTime(), - div = 1 - - switch (unit) { - case 'week': - div *= 7 - case 'day': - div *= 24 - case 'hours': - div *= 60 - case 'minutes': - div *= 60 - case 'seconds': - div *= 1000 - } - - return ms / div - }, - - week(date) { - var d = new Date(date) - d.setHours(0, 0, 0) - d.setDate(d.getDate() + 4 - (d.getDay() || 7)) - return Math.ceil(((d - new Date(d.getFullYear(), 0, 1)) / 8.64e7 + 1) / 7) - }, - - today() { - return dates.startOf(new Date(), 'day') - }, - - yesterday() { - return dates.add(dates.startOf(new Date(), 'day'), -1, 'day') - }, - - tomorrow() { - return dates.add(dates.startOf(new Date(), 'day'), 1, 'day') - }, -} - -export default dates + ) +} + +export function total(date, unit) { + let ms = date.getTime(), + div = 1 + + switch (unit) { + case 'week': + div *= 7 + case 'day': + div *= 24 + case 'hours': + div *= 60 + case 'minutes': + div *= 60 + case 'seconds': + div *= 1000 + } + + return ms / div +} + +export function week(date) { + var d = new Date(date) + d.setHours(0, 0, 0) + d.setDate(d.getDate() + 4 - (d.getDay() || 7)) + return Math.ceil(((d - new Date(d.getFullYear(), 0, 1)) / 8.64e7 + 1) / 7) +} + +export function today() { + return dates.startOf(new Date(), 'day') +} + +export function yesterday() { + return dates.add(dates.startOf(new Date(), 'day'), -1, 'day') +} + +export function tomorrow() { + return dates.add(dates.startOf(new Date(), 'day'), 1, 'day') +} diff --git a/src/utils/eventLevels.js b/src/utils/eventLevels.js index 900fabc4b..cc2034a76 100644 --- a/src/utils/eventLevels.js +++ b/src/utils/eventLevels.js @@ -1,5 +1,5 @@ import findIndex from 'lodash/findIndex' -import dates from './dates' +import * as dates from './dates' export function endOfRange(dateRange, unit = 'day') { return { diff --git a/yarn.lock b/yarn.lock index 3630dc80c..f5166cf52 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4339,10 +4339,10 @@ data-urls@^1.0.0: whatwg-mimetype "^2.2.0" whatwg-url "^7.0.0" -date-arithmetic@^3.0.0: - version "3.1.0" - resolved "https://registry.yarnpkg.com/date-arithmetic/-/date-arithmetic-3.1.0.tgz#1fcd03dbd504b9dbee2b9078c85a5f1c7d3cc2d3" - integrity sha1-H80D29UEudvuK5B4yFpfHH08wtM= +date-arithmetic@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/date-arithmetic/-/date-arithmetic-4.0.0.tgz#20514510adc5cbfc507151d5f133333ba95c891e" + integrity sha512-dIeY8FEapS0T0VK0YaHH+69ouYR429Xb9pXugBLAEAs4WfRYOVCyPE5EF7gpILFnA2M8U9WfF5WPS/hHvCg7qg== date-fns@^1.27.2: version "1.30.1"