Skip to content

Commit

Permalink
adopt const, arrow; drop default exports
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Dec 2, 2022
1 parent e89a467 commit e32ec63
Show file tree
Hide file tree
Showing 18 changed files with 251 additions and 304 deletions.
31 changes: 27 additions & 4 deletions src/day.js
Original file line number Diff line number Diff line change
@@ -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;
25 changes: 18 additions & 7 deletions src/hour.js
Original file line number Diff line number Diff line change
@@ -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;
117 changes: 48 additions & 69 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down
35 changes: 17 additions & 18 deletions src/interval.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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);
};
}

Expand Down
19 changes: 9 additions & 10 deletions src/millisecond.js
Original file line number Diff line number Diff line change
@@ -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;
25 changes: 18 additions & 7 deletions src/minute.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit e32ec63

Please sign in to comment.