Skip to content

Commit 355d869

Browse files
Filmbostock
andcommitted
Wrap all date/time intervals with isoparse
closes #1182 (Note: I considered doing this in d3-time, but it would mean adding d3-time-format as a dependency of d3-time.) Rebase on main Co-authored-by: Mike Bostock <[email protected]>
1 parent bbacf75 commit 355d869

File tree

6 files changed

+594
-12
lines changed

6 files changed

+594
-12
lines changed

src/options.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {parse as isoParse} from "isoformat";
22
import {color, descending, range as rangei, quantile} from "d3";
3-
import {maybeTimeInterval, maybeUtcInterval} from "./time.js";
3+
import {isoInterval, maybeTimeInterval, maybeUtcInterval} from "./time.js";
44

55
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
66
export const TypedArray = Object.getPrototypeOf(Uint8Array);
@@ -285,7 +285,7 @@ export function maybeInterval(interval, type) {
285285
if (typeof interval === "string") return (type === "time" ? maybeTimeInterval : maybeUtcInterval)(interval);
286286
if (typeof interval.floor !== "function") throw new Error("invalid interval; missing floor method");
287287
if (typeof interval.offset !== "function") throw new Error("invalid interval; missing offset method");
288-
return interval;
288+
return isTimeInterval(interval) ? isoInterval(interval) : interval;
289289
}
290290

291291
// This distinguishes between per-dimension options and a standalone value.
@@ -476,3 +476,11 @@ export function named(things) {
476476
export function maybeNamed(things) {
477477
return isIterable(things) ? named(things) : things;
478478
}
479+
480+
export function isTimeInterval(t) {
481+
return isInterval(t) && typeof t === "function" && t() instanceof Date;
482+
}
483+
484+
export function isInterval(t) {
485+
return t ? typeof t.range === "function" : false;
486+
}

src/time.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {utcSecond, utcMinute, utcHour, utcDay, utcWeek, utcMonth, utcYear} from
22
import {utcMonday, utcTuesday, utcWednesday, utcThursday, utcFriday, utcSaturday, utcSunday} from "d3";
33
import {timeSecond, timeMinute, timeHour, timeDay, timeWeek, timeMonth, timeYear} from "d3";
44
import {timeMonday, timeTuesday, timeWednesday, timeThursday, timeFriday, timeSaturday, timeSunday} from "d3";
5+
import {coerceDate} from "./options.js";
56

67
const timeIntervals = new Map([
78
["second", timeSecond],
@@ -41,14 +42,18 @@ const utcIntervals = new Map([
4142
["sunday", utcSunday]
4243
]);
4344

45+
export function isoInterval(interval) {
46+
return Object.assign((t) => interval(t), {...interval, floor: (t) => interval.floor(coerceDate(t))});
47+
}
48+
4449
export function maybeTimeInterval(interval) {
4550
const i = timeIntervals.get(`${interval}`.toLowerCase());
4651
if (!i) throw new Error(`unknown interval: ${interval}`);
47-
return i;
52+
return isoInterval(i);
4853
}
4954

5055
export function maybeUtcInterval(interval) {
5156
const i = utcIntervals.get(`${interval}`.toLowerCase());
5257
if (!i) throw new Error(`unknown interval: ${interval}`);
53-
return i;
58+
return isoInterval(i);
5459
}

src/transforms/bin.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
identity,
1414
coerceDate,
1515
coerceNumbers,
16+
isInterval,
17+
isTimeInterval,
1618
maybeColumn,
1719
maybeInterval,
1820
maybeTuple,
@@ -333,14 +335,6 @@ function isTimeThresholds(t) {
333335
return isTimeInterval(t) || (isIterable(t) && isTemporal(t));
334336
}
335337

336-
function isTimeInterval(t) {
337-
return isInterval(t) && typeof t === "function" && t() instanceof Date;
338-
}
339-
340-
function isInterval(t) {
341-
return t ? typeof t.range === "function" : false;
342-
}
343-
344338
function bing(EX, EY) {
345339
return EX && EY
346340
? function* (I) {

0 commit comments

Comments
 (0)