diff --git a/src/day.js b/src/day.js index 6c3f9ca..2b5d4b6 100644 --- a/src/day.js +++ b/src/day.js @@ -1,12 +1,35 @@ -import interval from "./interval.js"; +import {timeInterval} from "./interval.js"; import {durationDay, durationMinute} from "./duration.js"; -var day = interval( +export const timeDay = timeInterval( date => date.setHours(0, 0, 0, 0), (date, step) => date.setDate(date.getDate() + step), (start, end) => (end - start - (end.getTimezoneOffset() - start.getTimezoneOffset()) * durationMinute) / durationDay, date => date.getDate() - 1 ); -export default day; -export var days = day.range; +export const timeDays = timeDay.range; + +export const utcDay = timeInterval((date) => { + date.setUTCHours(0, 0, 0, 0); +}, (date, step) => { + date.setUTCDate(date.getUTCDate() + step); +}, (start, end) => { + return (end - start) / durationDay; +}, (date) => { + return date.getUTCDate() - 1; +}); + +export const utcDays = utcDay.range; + +export const unixDay = timeInterval((date) => { + date.setUTCHours(0, 0, 0, 0); +}, (date, step) => { + date.setUTCDate(date.getUTCDate() + step); +}, (start, end) => { + return (end - start) / durationDay; +}, (date) => { + return Math.floor(date / durationDay); +}); + +export const unixDays = unixDay.range; diff --git a/src/hour.js b/src/hour.js index 9b944d6..77b77a3 100644 --- a/src/hour.js +++ b/src/hour.js @@ -1,15 +1,26 @@ -import interval from "./interval.js"; +import {timeInterval} from "./interval.js"; import {durationHour, durationMinute, durationSecond} from "./duration.js"; -var hour = interval(function(date) { +export const timeHour = timeInterval((date) => { date.setTime(date - date.getMilliseconds() - date.getSeconds() * durationSecond - date.getMinutes() * durationMinute); -}, function(date, step) { +}, (date, step) => { date.setTime(+date + step * durationHour); -}, function(start, end) { +}, (start, end) => { return (end - start) / durationHour; -}, function(date) { +}, (date) => { return date.getHours(); }); -export default hour; -export var hours = hour.range; +export const timeHours = timeHour.range; + +export const utcHour = timeInterval((date) => { + date.setUTCMinutes(0, 0, 0); +}, (date, step) => { + date.setTime(+date + step * durationHour); +}, (start, end) => { + return (end - start) / durationHour; +}, (date) => { + return date.getUTCHours(); +}); + +export const utcHours = utcHour.range; diff --git a/src/index.js b/src/index.js index 9ceb157..d6dfd90 100644 --- a/src/index.js +++ b/src/index.js @@ -1,86 +1,61 @@ export { - default as timeInterval + timeInterval } from "./interval.js"; export { - default as timeMillisecond, - milliseconds as timeMilliseconds, - default as utcMillisecond, - milliseconds as utcMilliseconds + millisecond as utcMillisecond, + milliseconds as utcMilliseconds, + millisecond as timeMillisecond, + milliseconds as timeMilliseconds } from "./millisecond.js"; export { - default as timeSecond, - seconds as timeSeconds, - default as utcSecond, - seconds as utcSeconds + second as utcSecond, + seconds as utcSeconds, + second as timeSecond, + seconds as timeSeconds } from "./second.js"; export { - default as timeMinute, - minutes as timeMinutes -} from "./minute.js"; - -export { - default as timeHour, - hours as timeHours -} from "./hour.js"; - -export { - default as timeDay, - days as timeDays -} from "./day.js"; - -export { - sunday as timeWeek, - sundays as timeWeeks, - sunday as timeSunday, - sundays as timeSundays, - monday as timeMonday, - mondays as timeMondays, - tuesday as timeTuesday, - tuesdays as timeTuesdays, - wednesday as timeWednesday, - wednesdays as timeWednesdays, - thursday as timeThursday, - thursdays as timeThursdays, - friday as timeFriday, - fridays as timeFridays, - saturday as timeSaturday, - saturdays as timeSaturdays -} from "./week.js"; - -export { - default as timeMonth, - months as timeMonths -} from "./month.js"; - -export { - default as timeYear, - years as timeYears -} from "./year.js"; - -export { - default as utcMinute, + timeMinute, + timeMinutes, + utcMinute, utcMinutes -} from "./utcMinute.js"; +} from "./minute.js"; export { - default as utcHour, + timeHour, + timeHours, + utcHour, utcHours -} from "./utcHour.js"; - -export { - default as utcDay, - utcDays -} from "./utcDay.js"; +} from "./hour.js"; export { - default as unixDay, + timeDay, + timeDays, + utcDay, + utcDays, + unixDay, unixDays -} from "./unixDay.js"; +} from "./day.js"; export { + timeSunday as timeWeek, + timeSundays as timeWeeks, + timeSunday, + timeSundays, + timeMonday, + timeMondays, + timeTuesday, + timeTuesdays, + timeWednesday, + timeWednesdays, + timeThursday, + timeThursdays, + timeFriday, + timeFridays, + timeSaturday, + timeSaturdays, utcSunday as utcWeek, utcSundays as utcWeeks, utcSunday, @@ -97,17 +72,21 @@ export { utcFridays, utcSaturday, utcSaturdays -} from "./utcWeek.js"; +} from "./week.js"; export { - default as utcMonth, + timeMonth, + timeMonths, + utcMonth, utcMonths -} from "./utcMonth.js"; +} from "./month.js"; export { - default as utcYear, + timeYear, + timeYears, + utcYear, utcYears -} from "./utcYear.js"; +} from "./year.js"; export { utcTicks, diff --git a/src/interval.js b/src/interval.js index 2714a81..9c4a37a 100644 --- a/src/interval.js +++ b/src/interval.js @@ -1,32 +1,31 @@ -var t0 = new Date, - t1 = new Date; +const t0 = new Date, t1 = new Date; -export default function newInterval(floori, offseti, count, field) { +export function timeInterval(floori, offseti, count, field) { function interval(date) { return floori(date = arguments.length === 0 ? new Date : new Date(+date)), date; } - interval.floor = function(date) { + interval.floor = (date) => { return floori(date = new Date(+date)), date; }; - interval.ceil = function(date) { + interval.ceil = (date) => { return floori(date = new Date(date - 1)), offseti(date, 1), floori(date), date; }; - interval.round = function(date) { - var d0 = interval(date), - d1 = interval.ceil(date); + interval.round = (date) => { + const d0 = interval(date), d1 = interval.ceil(date); return date - d0 < d1 - date ? d0 : d1; }; - interval.offset = function(date, step) { + interval.offset = (date, step) => { return offseti(date = new Date(+date), step == null ? 1 : Math.floor(step)), date; }; - interval.range = function(start, stop, step) { - var range = [], previous; + interval.range = (start, stop, step) => { + const range = []; + let previous; start = interval.ceil(start); step = step == null ? 1 : Math.floor(step); if (!(start < stop) || !(step > 0)) return range; // also handles Invalid Date @@ -35,10 +34,10 @@ export default function newInterval(floori, offseti, count, field) { return range; }; - interval.filter = function(test) { - return newInterval(function(date) { + interval.filter = (test) => { + return timeInterval((date) => { if (date >= date) while (floori(date), !test(date)) date.setTime(date - 1); - }, function(date, step) { + }, (date, step) => { if (date >= date) { if (step < 0) while (++step <= 0) { while (offseti(date, -1), !test(date)) {} // eslint-disable-line no-empty @@ -50,19 +49,19 @@ export default function newInterval(floori, offseti, count, field) { }; if (count) { - interval.count = function(start, end) { + interval.count = (start, end) => { t0.setTime(+start), t1.setTime(+end); floori(t0), floori(t1); return Math.floor(count(t0, t1)); }; - interval.every = function(step) { + interval.every = (step) => { step = Math.floor(step); return !isFinite(step) || !(step > 0) ? null : !(step > 1) ? interval : interval.filter(field - ? function(d) { return field(d) % step === 0; } - : function(d) { return interval.count(0, d) % step === 0; }); + ? (d) => field(d) % step === 0 + : (d) => interval.count(0, d) % step === 0); }; } diff --git a/src/millisecond.js b/src/millisecond.js index d89585c..e3aa380 100644 --- a/src/millisecond.js +++ b/src/millisecond.js @@ -1,26 +1,25 @@ -import interval from "./interval.js"; +import {timeInterval} from "./interval.js"; -var millisecond = interval(function() { +export const millisecond = timeInterval(() => { // noop -}, function(date, step) { +}, (date, step) => { date.setTime(+date + step); -}, function(start, end) { +}, (start, end) => { return end - start; }); // An optimized implementation for this simple case. -millisecond.every = function(k) { +millisecond.every = (k) => { k = Math.floor(k); if (!isFinite(k) || !(k > 0)) return null; if (!(k > 1)) return millisecond; - return interval(function(date) { + return timeInterval((date) => { date.setTime(Math.floor(date / k) * k); - }, function(date, step) { + }, (date, step) => { date.setTime(+date + step * k); - }, function(start, end) { + }, (start, end) => { return (end - start) / k; }); }; -export default millisecond; -export var milliseconds = millisecond.range; +export const milliseconds = millisecond.range; diff --git a/src/minute.js b/src/minute.js index 2ed894b..ba8e4d9 100644 --- a/src/minute.js +++ b/src/minute.js @@ -1,15 +1,26 @@ -import interval from "./interval.js"; +import {timeInterval} from "./interval.js"; import {durationMinute, durationSecond} from "./duration.js"; -var minute = interval(function(date) { +export const timeMinute = timeInterval((date) => { date.setTime(date - date.getMilliseconds() - date.getSeconds() * durationSecond); -}, function(date, step) { +}, (date, step) => { date.setTime(+date + step * durationMinute); -}, function(start, end) { +}, (start, end) => { return (end - start) / durationMinute; -}, function(date) { +}, (date) => { return date.getMinutes(); }); -export default minute; -export var minutes = minute.range; +export const timeMinutes = timeMinute.range; + +export const utcMinute = timeInterval((date) => { + date.setUTCSeconds(0, 0); +}, (date, step) => { + date.setTime(+date + step * durationMinute); +}, (start, end) => { + return (end - start) / durationMinute; +}, (date) => { + return date.getUTCMinutes(); +}); + +export const utcMinutes = utcMinute.range; diff --git a/src/month.js b/src/month.js index ac995de..6dab69c 100644 --- a/src/month.js +++ b/src/month.js @@ -1,15 +1,27 @@ -import interval from "./interval.js"; +import {timeInterval} from "./interval.js"; -var month = interval(function(date) { +export const timeMonth = timeInterval((date) => { date.setDate(1); date.setHours(0, 0, 0, 0); -}, function(date, step) { +}, (date, step) => { date.setMonth(date.getMonth() + step); -}, function(start, end) { +}, (start, end) => { return end.getMonth() - start.getMonth() + (end.getFullYear() - start.getFullYear()) * 12; -}, function(date) { +}, (date) => { return date.getMonth(); }); -export default month; -export var months = month.range; +export const timeMonths = timeMonth.range; + +export const utcMonth = timeInterval((date) => { + date.setUTCDate(1); + date.setUTCHours(0, 0, 0, 0); +}, (date, step) => { + date.setUTCMonth(date.getUTCMonth() + step); +}, (start, end) => { + return end.getUTCMonth() - start.getUTCMonth() + (end.getUTCFullYear() - start.getUTCFullYear()) * 12; +}, (date) => { + return date.getUTCMonth(); +}); + +export const utcMonths = utcMonth.range; diff --git a/src/second.js b/src/second.js index 62069ef..47b2f43 100644 --- a/src/second.js +++ b/src/second.js @@ -1,15 +1,14 @@ -import interval from "./interval.js"; +import {timeInterval} from "./interval.js"; import {durationSecond} from "./duration.js"; -var second = interval(function(date) { +export const second = timeInterval((date) => { date.setTime(date - date.getMilliseconds()); -}, function(date, step) { +}, (date, step) => { date.setTime(+date + step * durationSecond); -}, function(start, end) { +}, (start, end) => { return (end - start) / durationSecond; -}, function(date) { +}, (date) => { return date.getUTCSeconds(); }); -export default second; -export var seconds = second.range; +export const seconds = second.range; diff --git a/src/ticks.js b/src/ticks.js index ff71c76..c314789 100644 --- a/src/ticks.js +++ b/src/ticks.js @@ -1,19 +1,13 @@ import {bisector, tickStep} from "d3-array"; import {durationDay, durationHour, durationMinute, durationMonth, durationSecond, durationWeek, durationYear} from "./duration.js"; -import millisecond from "./millisecond.js"; -import second from "./second.js"; -import minute from "./minute.js"; -import hour from "./hour.js"; -import day from "./day.js"; -import {sunday as week} from "./week.js"; -import month from "./month.js"; -import year from "./year.js"; -import utcMinute from "./utcMinute.js"; -import utcHour from "./utcHour.js"; -import unixDay from "./unixDay.js"; -import {utcSunday as utcWeek} from "./utcWeek.js"; -import utcMonth from "./utcMonth.js"; -import utcYear from "./utcYear.js"; +import {millisecond} from "./millisecond.js"; +import {second} from "./second.js"; +import {timeMinute, utcMinute} from "./minute.js"; +import {timeHour, utcHour} from "./hour.js"; +import {timeDay, unixDay} from "./day.js"; +import {timeSunday, utcSunday} from "./week.js"; +import {timeMonth, utcMonth} from "./month.js"; +import {timeYear, utcYear} from "./year.js"; function ticker(year, month, week, day, hour, minute) { @@ -58,7 +52,7 @@ function ticker(year, month, week, day, hour, minute) { return [ticks, tickInterval]; } -const [utcTicks, utcTickInterval] = ticker(utcYear, utcMonth, utcWeek, unixDay, utcHour, utcMinute); -const [timeTicks, timeTickInterval] = ticker(year, month, week, day, hour, minute); +const [utcTicks, utcTickInterval] = ticker(utcYear, utcMonth, utcSunday, unixDay, utcHour, utcMinute); +const [timeTicks, timeTickInterval] = ticker(timeYear, timeMonth, timeSunday, timeDay, timeHour, timeMinute); export {utcTicks, utcTickInterval, timeTicks, timeTickInterval}; diff --git a/src/unixDay.js b/src/unixDay.js deleted file mode 100644 index bbb1bea..0000000 --- a/src/unixDay.js +++ /dev/null @@ -1,15 +0,0 @@ -import interval from "./interval.js"; -import {durationDay} from "./duration.js"; - -var unixDay = interval(function(date) { - date.setUTCHours(0, 0, 0, 0); -}, function(date, step) { - date.setUTCDate(date.getUTCDate() + step); -}, function(start, end) { - return (end - start) / durationDay; -}, function(date) { - return Math.floor(date / durationDay); -}); - -export default unixDay; -export var unixDays = unixDay.range; diff --git a/src/utcDay.js b/src/utcDay.js deleted file mode 100644 index 8023be1..0000000 --- a/src/utcDay.js +++ /dev/null @@ -1,15 +0,0 @@ -import interval from "./interval.js"; -import {durationDay} from "./duration.js"; - -var utcDay = interval(function(date) { - date.setUTCHours(0, 0, 0, 0); -}, function(date, step) { - date.setUTCDate(date.getUTCDate() + step); -}, function(start, end) { - return (end - start) / durationDay; -}, function(date) { - return date.getUTCDate() - 1; -}); - -export default utcDay; -export var utcDays = utcDay.range; diff --git a/src/utcHour.js b/src/utcHour.js deleted file mode 100644 index 98b8822..0000000 --- a/src/utcHour.js +++ /dev/null @@ -1,15 +0,0 @@ -import interval from "./interval.js"; -import {durationHour} from "./duration.js"; - -var utcHour = interval(function(date) { - date.setUTCMinutes(0, 0, 0); -}, function(date, step) { - date.setTime(+date + step * durationHour); -}, function(start, end) { - return (end - start) / durationHour; -}, function(date) { - return date.getUTCHours(); -}); - -export default utcHour; -export var utcHours = utcHour.range; diff --git a/src/utcMinute.js b/src/utcMinute.js deleted file mode 100644 index 4e5e7e5..0000000 --- a/src/utcMinute.js +++ /dev/null @@ -1,15 +0,0 @@ -import interval from "./interval.js"; -import {durationMinute} from "./duration.js"; - -var utcMinute = interval(function(date) { - date.setUTCSeconds(0, 0); -}, function(date, step) { - date.setTime(+date + step * durationMinute); -}, function(start, end) { - return (end - start) / durationMinute; -}, function(date) { - return date.getUTCMinutes(); -}); - -export default utcMinute; -export var utcMinutes = utcMinute.range; diff --git a/src/utcMonth.js b/src/utcMonth.js deleted file mode 100644 index 3991b18..0000000 --- a/src/utcMonth.js +++ /dev/null @@ -1,15 +0,0 @@ -import interval from "./interval.js"; - -var utcMonth = interval(function(date) { - date.setUTCDate(1); - date.setUTCHours(0, 0, 0, 0); -}, function(date, step) { - date.setUTCMonth(date.getUTCMonth() + step); -}, function(start, end) { - return end.getUTCMonth() - start.getUTCMonth() + (end.getUTCFullYear() - start.getUTCFullYear()) * 12; -}, function(date) { - return date.getUTCMonth(); -}); - -export default utcMonth; -export var utcMonths = utcMonth.range; diff --git a/src/utcWeek.js b/src/utcWeek.js deleted file mode 100644 index e36fac8..0000000 --- a/src/utcWeek.js +++ /dev/null @@ -1,29 +0,0 @@ -import interval from "./interval.js"; -import {durationWeek} from "./duration.js"; - -function utcWeekday(i) { - return interval(function(date) { - date.setUTCDate(date.getUTCDate() - (date.getUTCDay() + 7 - i) % 7); - date.setUTCHours(0, 0, 0, 0); - }, function(date, step) { - date.setUTCDate(date.getUTCDate() + step * 7); - }, function(start, end) { - return (end - start) / durationWeek; - }); -} - -export var utcSunday = utcWeekday(0); -export var utcMonday = utcWeekday(1); -export var utcTuesday = utcWeekday(2); -export var utcWednesday = utcWeekday(3); -export var utcThursday = utcWeekday(4); -export var utcFriday = utcWeekday(5); -export var utcSaturday = utcWeekday(6); - -export var utcSundays = utcSunday.range; -export var utcMondays = utcMonday.range; -export var utcTuesdays = utcTuesday.range; -export var utcWednesdays = utcWednesday.range; -export var utcThursdays = utcThursday.range; -export var utcFridays = utcFriday.range; -export var utcSaturdays = utcSaturday.range; diff --git a/src/utcYear.js b/src/utcYear.js deleted file mode 100644 index ee89764..0000000 --- a/src/utcYear.js +++ /dev/null @@ -1,26 +0,0 @@ -import interval from "./interval.js"; - -var utcYear = interval(function(date) { - date.setUTCMonth(0, 1); - date.setUTCHours(0, 0, 0, 0); -}, function(date, step) { - date.setUTCFullYear(date.getUTCFullYear() + step); -}, function(start, end) { - return end.getUTCFullYear() - start.getUTCFullYear(); -}, function(date) { - return date.getUTCFullYear(); -}); - -// An optimized implementation for this simple case. -utcYear.every = function(k) { - return !isFinite(k = Math.floor(k)) || !(k > 0) ? null : interval(function(date) { - date.setUTCFullYear(Math.floor(date.getUTCFullYear() / k) * k); - date.setUTCMonth(0, 1); - date.setUTCHours(0, 0, 0, 0); - }, function(date, step) { - date.setUTCFullYear(date.getUTCFullYear() + step * k); - }); -}; - -export default utcYear; -export var utcYears = utcYear.range; diff --git a/src/week.js b/src/week.js index c0f1b03..f75ff52 100644 --- a/src/week.js +++ b/src/week.js @@ -1,29 +1,56 @@ -import interval from "./interval.js"; +import {timeInterval} from "./interval.js"; import {durationMinute, durationWeek} from "./duration.js"; -function weekday(i) { - return interval(function(date) { +function timeWeekday(i) { + return timeInterval((date) => { date.setDate(date.getDate() - (date.getDay() + 7 - i) % 7); date.setHours(0, 0, 0, 0); - }, function(date, step) { + }, (date, step) => { date.setDate(date.getDate() + step * 7); - }, function(start, end) { + }, (start, end) => { return (end - start - (end.getTimezoneOffset() - start.getTimezoneOffset()) * durationMinute) / durationWeek; }); } -export var sunday = weekday(0); -export var monday = weekday(1); -export var tuesday = weekday(2); -export var wednesday = weekday(3); -export var thursday = weekday(4); -export var friday = weekday(5); -export var saturday = weekday(6); +export const timeSunday = timeWeekday(0); +export const timeMonday = timeWeekday(1); +export const timeTuesday = timeWeekday(2); +export const timeWednesday = timeWeekday(3); +export const timeThursday = timeWeekday(4); +export const timeFriday = timeWeekday(5); +export const timeSaturday = timeWeekday(6); -export var sundays = sunday.range; -export var mondays = monday.range; -export var tuesdays = tuesday.range; -export var wednesdays = wednesday.range; -export var thursdays = thursday.range; -export var fridays = friday.range; -export var saturdays = saturday.range; +export const timeSundays = timeSunday.range; +export const timeMondays = timeMonday.range; +export const timeTuesdays = timeTuesday.range; +export const timeWednesdays = timeWednesday.range; +export const timeThursdays = timeThursday.range; +export const timeFridays = timeFriday.range; +export const timeSaturdays = timeSaturday.range; + +function utcWeekday(i) { + return timeInterval((date) => { + date.setUTCDate(date.getUTCDate() - (date.getUTCDay() + 7 - i) % 7); + date.setUTCHours(0, 0, 0, 0); + }, (date, step) => { + date.setUTCDate(date.getUTCDate() + step * 7); + }, (start, end) => { + return (end - start) / durationWeek; + }); +} + +export const utcSunday = utcWeekday(0); +export const utcMonday = utcWeekday(1); +export const utcTuesday = utcWeekday(2); +export const utcWednesday = utcWeekday(3); +export const utcThursday = utcWeekday(4); +export const utcFriday = utcWeekday(5); +export const utcSaturday = utcWeekday(6); + +export const utcSundays = utcSunday.range; +export const utcMondays = utcMonday.range; +export const utcTuesdays = utcTuesday.range; +export const utcWednesdays = utcWednesday.range; +export const utcThursdays = utcThursday.range; +export const utcFridays = utcFriday.range; +export const utcSaturdays = utcSaturday.range; diff --git a/src/year.js b/src/year.js index a86872d..9eadff3 100644 --- a/src/year.js +++ b/src/year.js @@ -1,26 +1,49 @@ -import interval from "./interval.js"; +import {timeInterval} from "./interval.js"; -var year = interval(function(date) { +export const timeYear = timeInterval((date) => { date.setMonth(0, 1); date.setHours(0, 0, 0, 0); -}, function(date, step) { +}, (date, step) => { date.setFullYear(date.getFullYear() + step); -}, function(start, end) { +}, (start, end) => { return end.getFullYear() - start.getFullYear(); -}, function(date) { +}, (date) => { return date.getFullYear(); }); // An optimized implementation for this simple case. -year.every = function(k) { - return !isFinite(k = Math.floor(k)) || !(k > 0) ? null : interval(function(date) { +timeYear.every = (k) => { + return !isFinite(k = Math.floor(k)) || !(k > 0) ? null : timeInterval((date) => { date.setFullYear(Math.floor(date.getFullYear() / k) * k); date.setMonth(0, 1); date.setHours(0, 0, 0, 0); - }, function(date, step) { + }, (date, step) => { date.setFullYear(date.getFullYear() + step * k); }); }; -export default year; -export var years = year.range; +export const timeYears = timeYear.range; + +export const utcYear = timeInterval((date) => { + date.setUTCMonth(0, 1); + date.setUTCHours(0, 0, 0, 0); +}, (date, step) => { + date.setUTCFullYear(date.getUTCFullYear() + step); +}, (start, end) => { + return end.getUTCFullYear() - start.getUTCFullYear(); +}, (date) => { + return date.getUTCFullYear(); +}); + +// An optimized implementation for this simple case. +utcYear.every = (k) => { + return !isFinite(k = Math.floor(k)) || !(k > 0) ? null : timeInterval((date) => { + date.setUTCFullYear(Math.floor(date.getUTCFullYear() / k) * k); + date.setUTCMonth(0, 1); + date.setUTCHours(0, 0, 0, 0); + }, (date, step) => { + date.setUTCFullYear(date.getUTCFullYear() + step * k); + }); +}; + +export const utcYears = utcYear.range;