Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/channel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ascending, descending, rollup, sort} from "d3";
import {first, isColor, isEvery, isIterable, labelof, map, maybeValue, range, valueof} from "./options.js";
import {first, isColor, isEvery, isIterable, labelof, map, maybeValue, range, typedMap, valueof} from "./options.js";
import {registry} from "./scales/index.js";
import {isSymbol, maybeSymbol} from "./symbols.js";
import {maybeReduce} from "./transforms/group.js";
Expand Down Expand Up @@ -121,7 +121,7 @@ function findScaleChannel(channels, scale) {
function difference(channels, k1, k2) {
const X1 = values(channels, k1);
const X2 = values(channels, k2);
return map(X2, (x2, i) => Math.abs(x2 - X1[i]), Float64Array);
return typedMap(X2, (x2, i) => Math.abs(x2 - X1[i]), Float64Array);
}

function values(channels, name, alias) {
Expand Down
6 changes: 3 additions & 3 deletions src/marks/raster.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {blurImage, Delaunay, randomLcg, rgb} from "d3";
import {valueObject} from "../channel.js";
import {create} from "../context.js";
import {map, first, second, third, isTuples, isNumeric, isTemporal, take, identity} from "../options.js";
import {typedMap, first, second, third, isTuples, isNumeric, isTemporal, take, identity} from "../options.js";
import {maybeColorChannel, maybeNumberChannel} from "../options.js";
import {Mark} from "../mark.js";
import {applyAttr, applyDirectStyles, applyIndirectStyles, applyTransform, impliedString} from "../style.js";
Expand Down Expand Up @@ -115,8 +115,8 @@ export class Raster extends AbstractRaster {
if (this.interpolate) {
const kx = w / dx;
const ky = h / dy;
const IX = map(X, (x) => (x - x1) * kx, Float64Array);
const IY = map(Y, (y) => (y - y1) * ky, Float64Array);
const IX = typedMap(X, (x) => (x - x1) * kx, Float64Array);
const IY = typedMap(Y, (y) => (y - y1) * ky, Float64Array);
if (F) F = this.interpolate(index, w, h, IX, IY, F);
if (FO) FO = this.interpolate(index, w, h, IX, IY, FO);
}
Expand Down
18 changes: 13 additions & 5 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ const objectToString = Object.prototype.toString;
/** @jsdoc valueof */
export function valueof(data, value, type) {
const valueType = typeof value;
const access =
type && Object.getPrototypeOf(type) === TypedArray
? (value) => typedMap(data, value, type)
: (value) => map(data, value);
return valueType === "string"
? map(data, field(value), type)
? access(field(value))
: valueType === "function"
? map(data, value, type)
? access(value)
: valueType === "number" || value instanceof Date || valueType === "boolean"
? map(data, constant(value), type)
? access(constant(value))
: value && typeof value.transform === "function"
? arrayify(value.transform(data), type)
: arrayify(value, type); // preserve undefined type
Expand Down Expand Up @@ -92,7 +96,11 @@ export function arrayify(data, type) {
// instanceof the desired array type, the faster values.map method is used. Note
// that we don’t rely on the implicit coercion of typedArray.from, because it
// errors on BigInts.
export function map(values, f, type = Array) {
export function map(values, f) {
return values instanceof Array ? values.map(f) : Array.from(values, f);
}

export function typedMap(values, f, type) {
const g = type === Array ? f : (d, i) => Number(f(d, i));
return values instanceof type ? values.map(g) : type.from(values, g);
}
Expand Down Expand Up @@ -234,7 +242,7 @@ export function mid(x1, x2) {
const X2 = x2.transform(data);
return isTemporal(X1) || isTemporal(X2)
? map(X1, (_, i) => new Date((+X1[i] + +X2[i]) / 2))
: map(X1, (_, i) => (+X1[i] + +X2[i]) / 2, Float64Array);
: typedMap(X1, (_, i) => (+X1[i] + +X2[i]) / 2, Float64Array);
},
label: x1.label
};
Expand Down
5 changes: 3 additions & 2 deletions src/scales.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
isScaleOptions,
isTypedArray,
map,
slice
slice,
typedMap
} from "./options.js";
import {registry, color, position, radius, opacity, symbol, length} from "./scales/index.js";
import {
Expand Down Expand Up @@ -505,7 +506,7 @@ function coerceDates(values) {

// If the values are specified as a typed array, no coercion is required.
export function coerceNumbers(values) {
return isTypedArray(values) ? values : map(values, coerceNumber, Float64Array);
return isTypedArray(values) ? values : typedMap(values, coerceNumber, Float64Array);
}

// Unlike Mark’s number, here we want to convert null and undefined to NaN,
Expand Down
6 changes: 3 additions & 3 deletions src/transforms/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
labelof,
isTemporal,
isIterable,
map
typedMap
} from "../options.js";
import {coerceDate, coerceNumber} from "../scales.js";
import {basic} from "./basic.js";
Expand Down Expand Up @@ -230,7 +230,7 @@ function maybeBin(options) {
let V = valueof(data, value);
let T; // bin thresholds
if (isTemporal(V) || isTimeThresholds(thresholds)) {
V = map(V, coerceDate, Float64Array);
V = typedMap(V, coerceDate, Float64Array);
let [min, max] = typeof domain === "function" ? domain(V) : domain;
let t = typeof thresholds === "function" && !isInterval(thresholds) ? thresholds(V, min, max) : thresholds;
if (typeof t === "number") t = utcTickInterval(min, max, t);
Expand All @@ -243,7 +243,7 @@ function maybeBin(options) {
}
T = t;
} else {
V = map(V, coerceNumber, Float64Array); // TODO deduplicate with code above
V = typedMap(V, coerceNumber, Float64Array); // TODO deduplicate with code above
let [min, max] = typeof domain === "function" ? domain(V) : domain;
let t = typeof thresholds === "function" && !isInterval(thresholds) ? thresholds(V, min, max) : thresholds;
if (typeof t === "number") {
Expand Down