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

1.0.1 • Public • Published

Builder Guard

Uses TypeScript to guard builders when providing data

npm latest version Build states semantic-release

Installation

npm install --save @sebowy/builder-guard
# or
yarn add @sebowy/builder-guard

Usage

Examples

  • examples with timeouts
import createBuilderGuard, { Builder } from '@sebowy/builder-guard';

interface ICarData {
  color: 'red' | 'green' | 'blue';
  doors: 3 | 5;
  engine: 'V6' | 'V8';
}

interface ICarTypeResult {
  carType: 'sport' | 'van';
}

class CarTypeBuilder extends Builder<ICarData, ICarTypeResult> {
  build(): ICarTypeResult {
    if (this.isSportCar()) {
      return { carType: 'sport' };
    }
    return { carType: 'van' };
  }

  private isSportCar(): boolean {
    return this._data.color === 'red' && this._data.engine === 'V8' && this._data.doors === 3;
  }
}

const builder = createBuilderGuard(CarTypeBuilder);
const carTypeResult = builder
  .color('red')
  .doors(3)
  .engine('V8')
  // build method is not available until all properties are set
  .build();

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.1
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.1
    0
  • 1.0.0
    0

Package Sidebar

Install

npm i @sebowy/builder-guard

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

21.2 kB

Total Files

43

Last publish

Collaborators

  • sebowy.dev