0.0.3 • Published 11 months ago

@smonn/builder v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

@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" }] }
0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago

0.0.0

11 months ago