From d0d0b70249c276802584df02e43b3e51ddf6a3e2 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Mon, 5 Aug 2024 09:44:51 -0400 Subject: [PATCH] handle Arrow in a few more places --- src/interactions/pointer.js | 7 ++++++- src/options.js | 4 ++-- src/transforms/basic.js | 7 +++++-- src/transforms/group.js | 42 ++++++------------------------------- src/transforms/stack.js | 7 +++---- 5 files changed, 22 insertions(+), 45 deletions(-) diff --git a/src/interactions/pointer.js b/src/interactions/pointer.js index c92addcff7..f0c0f765b0 100644 --- a/src/interactions/pointer.js +++ b/src/interactions/pointer.js @@ -1,5 +1,6 @@ import {pointer as pointof} from "d3"; import {composeRender} from "../mark.js"; +import {isArray} from "../options.js"; import {applyFrameAnchor} from "../style.js"; const states = new WeakMap(); @@ -126,7 +127,11 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op // Dispatch the value. When simultaneously exiting this facet and // entering a new one, prioritize the entering facet. - if (!(i == null && facetState?.size > 1)) context.dispatchValue(i == null ? null : data[i]); + if (!(i == null && facetState?.size > 1)) { + const value = i == null ? null : isArray(data) ? data[i] : data.get(i); + context.dispatchValue(value); + } + return r; } diff --git a/src/options.js b/src/options.js index 934a5966cf..ac9caca472 100644 --- a/src/options.js +++ b/src/options.js @@ -7,7 +7,7 @@ import {timeInterval, utcInterval} from "./time.js"; export const TypedArray = Object.getPrototypeOf(Uint8Array); const objectToString = Object.prototype.toString; -function isArray(value) { +export function isArray(value) { return value instanceof Array || value instanceof TypedArray; } @@ -608,7 +608,7 @@ export function maybeClip(clip) { } // https://github.com/observablehq/stdlib/blob/746ca2e69135df6178e4f3a17244def35d8d6b20/src/arrow.js#L4C1-L17C1 -export function isArrowTable(value) { +function isArrowTable(value) { return ( value && typeof value.getChild === "function" && diff --git a/src/transforms/basic.js b/src/transforms/basic.js index cd846d8ef1..8146ae9381 100644 --- a/src/transforms/basic.js +++ b/src/transforms/basic.js @@ -1,6 +1,7 @@ import {randomLcg} from "d3"; import {ascendingDefined, descendingDefined} from "../defined.js"; -import {dataify, isDomainSort, isOptions, maybeValue, valueof} from "../options.js"; +import {isArray, isDomainSort, isOptions} from "../options.js"; +import {dataify, maybeValue, valueof} from "../options.js"; export function basic({filter: f1, sort: s1, reverse: r1, transform: t1, initializer: i1, ...options} = {}, transform) { // If both t1 and t2 are defined, returns a composite transform that first @@ -101,7 +102,9 @@ function sortTransform(value) { function sortData(compare) { return (data, facets) => { - const compareData = (i, j) => compare(data[i], data[j]); + const compareData = isArray(data) + ? (i, j) => compare(data[i], data[j]) + : (i, j) => compare(data.get(i), data.get(j)); return {data, facets: facets.map((I) => I.slice().sort(compareData))}; }; } diff --git a/src/transforms/group.js b/src/transforms/group.js index 0f9ad95a57..b3320efd65 100644 --- a/src/transforms/group.js +++ b/src/transforms/group.js @@ -1,38 +1,9 @@ -import { - InternSet, - deviation, - group as grouper, - max, - maxIndex, - mean, - median, - min, - minIndex, - mode, - rollup, - sort, - sum, - variance -} from "d3"; +import {InternSet, group as grouper, rollup, sort} from "d3"; +import {deviation, max, maxIndex, mean, median, min, minIndex, mode, sum, variance} from "d3"; import {ascendingDefined} from "../defined.js"; -import { - arrayify, - column, - identity, - isObject, - isTemporal, - labelof, - maybeApplyInterval, - maybeColorChannel, - maybeColumn, - maybeInput, - maybeTuple, - percentile, - range, - second, - take, - valueof -} from "../options.js"; +import {maybeApplyInterval, maybeColorChannel, maybeColumn, maybeInput, maybeTuple} from "../options.js"; +import {isArray, isObject, isTemporal} from "../options.js"; +import {column, identity, labelof, percentile, range, second, take, valueof} from "../options.js"; import {basic} from "./basic.js"; // Group on {z, fill, stroke}. @@ -445,8 +416,7 @@ export function find(test) { if (typeof test !== "function") throw new Error(`invalid test function: ${test}`); return { reduceIndex(I, V, {data}) { - data = arrayify(data); - return V[I.find((i) => test(data[i], i, data))]; + return V[I.find(isArray(data) ? (i) => test(data[i], i, data) : (i) => test(data.get(i), i, data))]; } }; } diff --git a/src/transforms/stack.js b/src/transforms/stack.js index 5a03bbf84e..20d7342e03 100644 --- a/src/transforms/stack.js +++ b/src/transforms/stack.js @@ -2,7 +2,7 @@ import {InternMap, cumsum, greatest, group, groupSort, max, min, rollup, sum} fr import {ascendingDefined, descendingDefined} from "../defined.js"; import {withTip} from "../mark.js"; import {maybeApplyInterval, maybeColumn, maybeZ, maybeZero} from "../options.js"; -import {arrayify, column, field, lengthof, mid, one, range, valueof} from "../options.js"; +import {column, field, isArray, lengthof, mid, one, range, valueof} from "../options.js"; import {basic} from "./basic.js"; import {exclusiveFacets} from "./exclusiveFacets.js"; @@ -252,7 +252,7 @@ function maybeOrder(order, offset, ky) { return orderAccessor(field(order)); } if (typeof order === "function") return (order.length === 1 ? orderAccessor : orderComparator)(order); - if (Array.isArray(order)) return orderGiven(order); + if (isArray(order)) return orderGiven(order); throw new Error(`invalid order: ${order}`); } @@ -328,8 +328,7 @@ function orderAccessor(f) { function orderComparator(f) { return (data) => { - data = arrayify(data); - return (i, j) => f(data[i], data[j]); + return isArray(data) ? (i, j) => f(data[i], data[j]) : (i, j) => f(data.get(i), data.get(j)); }; }