1.0.1 • Published 3 years ago

@sebowy/builder-guard v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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();