Skip to content

MappedFactory

Creates output values by mapping a completed input object.

Use this factory when the values you want to override are easiest to model as an object, but the final value should be a different shape or type. Each call to make builds the input defaults, applies deep partial overrides, and passes the completed input to the mapper function.

interface UrlParts {
protocol: "https";
host: string;
path: string;
}
const urlFactory = new MappedFactory<UrlParts, string>(
() => ({
protocol: "https",
host: "example.com",
path: "/users",
}),
(parts) => `${parts.protocol}://${parts.host}${parts.path}`,
);
const defaultUrl = urlFactory.make();
// "https://example.com/users"
const apiUrl = urlFactory.make({ path: "/api/users" });
// "https://example.com/api/users"

Source file: src/mapped/mapped-factory.ts