@smonn/builder
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

@smonn/builder

Build objects from schemas.

npm install @smonn/builder
import { Builder, type InferOptions, type InferResult, sequence } from "@smonn/builder";

const nested = Builder.from({
  // use functions for dynamic values
  shout: ({ message }: { message: string }) => message.toUpperCase(),
});

const builder = Builder.from({
  // automatically increment the id using the sequence helper
  id: sequence(),

  // non-function values are used as-is
  literal: 42,

  // nest builders to make more complex objects
  nested: (options: InferOptions<typeof nested> & { count: number }) =>
    nested.buildList(options.count, options),
});

// obj type === InferResult<typeof builder>
const obj = builder.build({
  // options will be an intersection of the first parameter to each function in the schema
  // note: take care to not mismatch the option types when reusing the same keys
  message: "Hello World",
  count: 2,
});
// => { id: 1, literal: 42, nested: [{ shout: "HELLO WORLD" }, { shout: "HELLO WORLD" }] }

Package Sidebar

Install

npm i @smonn/builder

Weekly Downloads

0

Version

0.0.3

License

MIT

Unpacked Size

17.6 kB

Total Files

9

Last publish

Collaborators

  • smonn