Skip to content

type tests #1337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function valueof(data, value, type) {
? maybeTypedMap(data, value, type)
: valueType === "number" || value instanceof Date || valueType === "boolean"
? map(data, constant(value), type)
: value && typeof value.transform === "function"
: typeof value?.transform === "function"
? maybeTypedArrayify(value.transform(data), type)
: maybeTypedArrayify(value, type);
}
Expand Down Expand Up @@ -128,7 +128,7 @@ export function arrayify(data) {
// An optimization of type.from(values, f): if the given values are already an
// instanceof the desired array type, the faster values.map method is used.
export function map(values, f, type = Array) {
return values instanceof type ? values.map(f) : type.from(values, f);
return values == null ? values : values instanceof type ? values.map(f) : type.from(values, f);
}

// An optimization of type.from(values): if the given values are already an
Expand Down
21 changes: 21 additions & 0 deletions test/options-d-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {valueof} from "../src/options.js";

// A function is not a valid input data
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
valueof(() => {}, "field");

// A Promise is not a valid input data
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
valueof(new Promise(() => {}), "field");

// A symbol is not a valid value
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
valueof(null, Symbol("field"));

// A bigint is not a valid value
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
valueof(null, 2n);
25 changes: 25 additions & 0 deletions test/options-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,28 @@ it("valueof returns arrays that match the specified type as-is", () => {
assert.strictEqual(valueof(m, identity, Float32Array), m);
assert.strictEqual(valueof(m, identity, My32Array), m);
});

it("valueof returns the given array value", () => {
const a = [1, 2, 3];
assert.strictEqual(valueof(null, a), a);
assert.strictEqual(valueof(undefined, a), a);
assert.strictEqual(valueof([], a), a);
});

it("valueof accepts complicated data with the proper accessor", () => {
const m = [(d) => d, new Promise(() => {})];
assert.deepStrictEqual(valueof(m, String), ["(d) => d", "[object Promise]"]);
});

it("data passed to valueof can be nullish and generated by the transform method", () => {
assert.deepStrictEqual(valueof(undefined, {transform: () => [1, "text"]}), [1, "text"]);
assert.deepStrictEqual(valueof(null, {transform: () => [1, "text"]}, Float32Array), Float32Array.of(1, NaN));
assert.deepStrictEqual(valueof(null, {transform: () => new Float64Array(2)}, Array), [0, 0]);
});

it("valueof does not crash on nullish data with an accessor", () => {
const a = () => 1;
assert.strictEqual(valueof(null, a), null);
assert.strictEqual(valueof(undefined, a), undefined);
assert.deepStrictEqual(valueof(0, a), []); // ill-defined, but not crashing
});