Skip to content

Commit d670a2b

Browse files
Filmbostock
andauthored
type tests (#1337)
* tests salvaged from observablehq/plot#1024 * fix crash in valueof when the data is not iterable * avoid the nullish test in map * move null test back to map * consolidate tests * remove "valid input data" type tests * test array value --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent 1798d6f commit d670a2b

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/options.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function valueof(data, value, type) {
1414
? maybeTypedMap(data, value, type)
1515
: valueType === "number" || value instanceof Date || valueType === "boolean"
1616
? map(data, constant(value), type)
17-
: value && typeof value.transform === "function"
17+
: typeof value?.transform === "function"
1818
? maybeTypedArrayify(value.transform(data), type)
1919
: maybeTypedArrayify(value, type);
2020
}
@@ -128,7 +128,7 @@ export function arrayify(data) {
128128
// An optimization of type.from(values, f): if the given values are already an
129129
// instanceof the desired array type, the faster values.map method is used.
130130
export function map(values, f, type = Array) {
131-
return values instanceof type ? values.map(f) : type.from(values, f);
131+
return values == null ? values : values instanceof type ? values.map(f) : type.from(values, f);
132132
}
133133

134134
// An optimization of type.from(values): if the given values are already an

test/options-d-test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {valueof} from "../src/options.js";
2+
3+
// A function is not a valid input data
4+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5+
// @ts-expect-error
6+
valueof(() => {}, "field");
7+
8+
// A Promise is not a valid input data
9+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
10+
// @ts-expect-error
11+
valueof(new Promise(() => {}), "field");
12+
13+
// A symbol is not a valid value
14+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
15+
// @ts-expect-error
16+
valueof(null, Symbol("field"));
17+
18+
// A bigint is not a valid value
19+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
20+
// @ts-expect-error
21+
valueof(null, 2n);

test/options-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,28 @@ it("valueof returns arrays that match the specified type as-is", () => {
114114
assert.strictEqual(valueof(m, identity, Float32Array), m);
115115
assert.strictEqual(valueof(m, identity, My32Array), m);
116116
});
117+
118+
it("valueof returns the given array value", () => {
119+
const a = [1, 2, 3];
120+
assert.strictEqual(valueof(null, a), a);
121+
assert.strictEqual(valueof(undefined, a), a);
122+
assert.strictEqual(valueof([], a), a);
123+
});
124+
125+
it("valueof accepts complicated data with the proper accessor", () => {
126+
const m = [(d) => d, new Promise(() => {})];
127+
assert.deepStrictEqual(valueof(m, String), ["(d) => d", "[object Promise]"]);
128+
});
129+
130+
it("data passed to valueof can be nullish and generated by the transform method", () => {
131+
assert.deepStrictEqual(valueof(undefined, {transform: () => [1, "text"]}), [1, "text"]);
132+
assert.deepStrictEqual(valueof(null, {transform: () => [1, "text"]}, Float32Array), Float32Array.of(1, NaN));
133+
assert.deepStrictEqual(valueof(null, {transform: () => new Float64Array(2)}, Array), [0, 0]);
134+
});
135+
136+
it("valueof does not crash on nullish data with an accessor", () => {
137+
const a = () => 1;
138+
assert.strictEqual(valueof(null, a), null);
139+
assert.strictEqual(valueof(undefined, a), undefined);
140+
assert.deepStrictEqual(valueof(0, a), []); // ill-defined, but not crashing
141+
});

0 commit comments

Comments
 (0)