@openfoxes/ts-builder
v0.2.0
Published
Builder pattern for typescript
Readme
Information
This library allows you to use the Builder Pattern for types and interfaces in TypeScript. It aims to work dynamically without the need for separately generated builder classes. It should be applicable to existing code without requiring additional modifications.
Usage
To generate a builder for the interface "Frog" just create a TSBuilder:
import TsBuilder from "@openfoxes/ts-builder";
interface Frog {
name: string;
color: "green" | "red";
calm: boolean;
}
// Use the TsBuilder:
const immutableFrogBuilder = TsBuilderImmutable.makeBuilderFor<Frog>();
const frog = immutableFrogBuilder
.set("name", "Klaus")
.set("color", "green")
.set("calm", true)
.build();
/**
* In the future the current usages should be possible:
*/
// The js-classic-builder, currently NOT possible:
const classicFrogBuilder = TsBuilder.makeBuilderFor<Frog>();
classicFrogBuilder.set("name", "Klaus").set("color", "green").set("calm", true);
const frog = classicFrogBuilder.build();
// The optimal TsBuilder (using Proxy), currently NOT possible:
const frogBuilder = TsBuilder.makeBuilderFor<Frog>();
const frog = frogBuilder
.set("name", "Klaus")
.set("color", "green")
.set("calm", true)
.build();Pre-requisites
Install the package:
npm install @openfoxes/ts-builderor
npm install -E @openfoxes/[email protected]Development
This repository strives to make the best possible use of TypeScript's capabilities. However, it is unfortunately not easily possible to provide a fully-fledged builder dynamically and in real-time. Work is ongoing to offer more user-friendly methods in the future and to improve error handling. For now, though, it is already an improvement over relying solely on constructors or manually creating builders.
Code Quality
This project aims to provide a clean and secure design. In order to optimize the underlying code and reducing the risk of bugs, quality tools are used:
