|
1 | 1 | import {parse as isoParse} from "isoformat";
|
2 |
| -import {isColor, isEvery, isOrdinal, isFirst, isSymbol, isTemporal, maybeSymbol, order, isTemporalString, isNumericString, isScaleOptions} from "./options.js"; |
| 2 | +import {isColor, isEvery, isOrdinal, isFirst, isSymbol, isTemporal, isTemporalString, isNumericString, isScaleOptions, isTypedArray, map, maybeSymbol, order} from "./options.js"; |
3 | 3 | import {registry, color, position, radius, opacity, symbol, length} from "./scales/index.js";
|
4 | 4 | import {ScaleLinear, ScaleSqrt, ScalePow, ScaleLog, ScaleSymlog, ScaleQuantile, ScaleQuantize, ScaleThreshold, ScaleIdentity} from "./scales/quantitative.js";
|
5 | 5 | import {ScaleDiverging, ScaleDivergingSqrt, ScaleDivergingPow, ScaleDivergingLog, ScaleDivergingSymlog} from "./scales/diverging.js";
|
@@ -171,17 +171,17 @@ function Scale(key, channels = [], options = {}) {
|
171 | 171 | case "pow":
|
172 | 172 | case "log":
|
173 | 173 | case "symlog":
|
174 |
| - options = coerceType(channels, options, coerceNumber, Float64Array); |
| 174 | + options = coerceType(channels, options, coerceNumbers); |
175 | 175 | break;
|
176 | 176 | case "identity":
|
177 | 177 | switch (registry.get(key)) {
|
178 |
| - case position: options = coerceType(channels, options, coerceNumber, Float64Array); break; |
179 |
| - case symbol: options = coerceType(channels, options, maybeSymbol); break; |
| 178 | + case position: options = coerceType(channels, options, coerceNumbers); break; |
| 179 | + case symbol: options = coerceType(channels, options, coerceSymbols); break; |
180 | 180 | }
|
181 | 181 | break;
|
182 | 182 | case "utc":
|
183 | 183 | case "time":
|
184 |
| - options = coerceType(channels, options, coerceDate); |
| 184 | + options = coerceType(channels, options, coerceDates); |
185 | 185 | break;
|
186 | 186 | }
|
187 | 187 |
|
@@ -352,13 +352,29 @@ export function isCollapsed(scale) {
|
352 | 352 | }
|
353 | 353 |
|
354 | 354 | // Mutates channel.value!
|
355 |
| -function coerceType(channels, options, coerce, type) { |
356 |
| - for (const c of channels) c.value = coerceArray(c.value, coerce, type); |
357 |
| - return {...options, domain: coerceArray(options.domain, coerce, type)}; |
| 355 | +function coerceType(channels, {domain, ...options}, coerceValues) { |
| 356 | + for (const c of channels) { |
| 357 | + if (c.value !== undefined) { |
| 358 | + c.value = coerceValues(c.value); |
| 359 | + } |
| 360 | + } |
| 361 | + return { |
| 362 | + domain: domain === undefined ? domain : coerceValues(domain), |
| 363 | + ...options |
| 364 | + }; |
| 365 | +} |
| 366 | + |
| 367 | +function coerceSymbols(values) { |
| 368 | + return map(values, maybeSymbol); |
| 369 | +} |
| 370 | + |
| 371 | +function coerceDates(values) { |
| 372 | + return map(values, coerceDate); |
358 | 373 | }
|
359 | 374 |
|
360 |
| -function coerceArray(array, coerce, type = Array) { |
361 |
| - if (array !== undefined) return type.from(array, coerce); |
| 375 | +// If the values are specified as a typed array, no coercion is required. |
| 376 | +function coerceNumbers(values) { |
| 377 | + return isTypedArray(values) ? values : map(values, coerceNumber, Float64Array); |
362 | 378 | }
|
363 | 379 |
|
364 | 380 | // Unlike Mark’s number, here we want to convert null and undefined to NaN,
|
|
0 commit comments