Skip to content

Commit 5371fad

Browse files
committed
isIterable
1 parent 4324026 commit 5371fad

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

src/channel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ascending, descending, rollup, sort} from "d3";
22
import {ascendingDefined, descendingDefined} from "./defined.js";
3-
import {first, labelof, map, maybeValue, range, valueof} from "./options.js";
3+
import {first, isIterable, labelof, map, maybeValue, range, valueof} from "./options.js";
44
import {registry} from "./scales/index.js";
55
import {maybeReduce} from "./transforms/group.js";
66
import {composeInitializer} from "./transforms/initializer.js";
@@ -49,7 +49,7 @@ export function channelDomain(channels, facetChannels, data, options) {
4949
const X = findScaleChannel(channels, x) || facetChannels && findScaleChannel(facetChannels, x);
5050
if (!X) throw new Error(`missing channel for scale: ${x}`);
5151
const XV = X.value;
52-
const [lo = 0, hi = Infinity] = limit && typeof limit[Symbol.iterator] === "function" ? limit : limit < 0 ? [limit] : [0, limit];
52+
const [lo = 0, hi = Infinity] = isIterable(limit) ? limit : limit < 0 ? [limit] : [0, limit];
5353
if (y == null) {
5454
X.domain = () => {
5555
let domain = XV;

src/marks/text.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {create, namespaces} from "d3";
22
import {nonempty} from "../defined.js";
33
import {formatDefault} from "../format.js";
4-
import {indexOf, identity, string, maybeNumberChannel, maybeTuple, numberChannel, isNumeric, isTemporal, keyword, maybeFrameAnchor, isTextual} from "../options.js";
4+
import {indexOf, identity, string, maybeNumberChannel, maybeTuple, numberChannel, isNumeric, isTemporal, keyword, maybeFrameAnchor, isTextual, isIterable} from "../options.js";
55
import {Mark} from "../plot.js";
66
import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyAttr, applyTransform, offset, impliedString, applyFrameAnchor} from "../style.js";
77
import {maybeIntervalMidX, maybeIntervalMidY} from "../transforms/interval.js";
@@ -18,7 +18,7 @@ export class Text extends Mark {
1818
const {
1919
x,
2020
y,
21-
text = data != null && isTextual(data) ? identity : indexOf,
21+
text = isIterable(data) && isTextual(data) ? identity : indexOf,
2222
frameAnchor,
2323
textAnchor = /right$/i.test(frameAnchor) ? "end" : /left$/i.test(frameAnchor) ? "start" : "middle",
2424
lineAnchor = /^top/i.test(frameAnchor) ? "top" : /^bottom/i.test(frameAnchor) ? "bottom" : "middle",

src/options.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ export function numberChannel(source) {
228228
};
229229
}
230230

231+
export function isIterable(value) {
232+
return value && typeof value[Symbol.iterator] === "function";
233+
}
234+
231235
export function isTextual(values) {
232236
for (const value of values) {
233237
if (value == null) continue;

src/transforms/bin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {bin as binner, extent, thresholdFreedmanDiaconis, thresholdScott, thresholdSturges, utcTickInterval} from "d3";
2-
import {valueof, range, identity, maybeColumn, maybeTuple, maybeColorChannel, maybeValue, mid, labelof, isTemporal} from "../options.js";
2+
import {valueof, range, identity, maybeColumn, maybeTuple, maybeColorChannel, maybeValue, mid, labelof, isTemporal, isIterable} from "../options.js";
33
import {coerceDate, coerceNumber} from "../scales.js";
44
import {basic} from "./basic.js";
55
import {hasOutput, maybeEvaluator, maybeGroup, maybeOutput, maybeOutputs, maybeReduce, maybeSort, maybeSubgroup, reduceCount, reduceFirst, reduceIdentity} from "./group.js";
@@ -259,7 +259,7 @@ function thresholdAuto(values, min, max) {
259259
}
260260

261261
function isTimeThresholds(t) {
262-
return isTimeInterval(t) || t && t[Symbol.iterator] && isTemporal(t);
262+
return isTimeInterval(t) || (isIterable(t) && isTemporal(t));
263263
}
264264

265265
function isTimeInterval(t) {

test/marks/text-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,9 @@ it("text(data, {fontSize}) allows fontSize to be a channel", () => {
101101
assert.strictEqual(text.fontSize, undefined);
102102
assert.strictEqual(text.channels.find(c => c.name === "fontSize").value, "x");
103103
});
104+
105+
it("text({length}) can take length-only data", () => {
106+
const data = {length: 100};
107+
const text = Plot.text(data);
108+
assert.strictEqual(text.data, data);
109+
});

0 commit comments

Comments
 (0)