Skip to content

Instantly share code, notes, and snippets.

@t3h2mas
Created Jun 9, 2020
Embed
What would you like to do?
JavaScript Fluent Builder w/ JSDOC3 comments
class Burrito {
constructor(carb, protein, salsa, cheese = null, guac = False) {
this.carb = carb;
this.protein = protein;
this.salsa = salsa;
this.cheese = cheese;
this.guac = guac;
}
/* ... the many behaviors of a burrito go here... */
}
/**
* @interface CarbStation
*/
/**
* @function
* @name CarbStation#withCarb
* @param {string} carb
* @returns {ExtraStation}
*/
/**
* @interface FluentBuilder
*/
/**
* @function
* @name FluentBuilder#withProtein
* @param {string} protein
* @returns {CarbStation}
*/
// Methods to add optional toppings
/**
* An interface to add extra toppings or skip to building
* @interface ExtraStation
*/
/**
* @function
* @name ExtraStation#withSalsa
* @param {string} salsa
* @returns {ExtraStation}
*/
/**
* @function
* @name ExtraStation#withCheese
* @param {string} cheese
* @returns {ExtraStation}
*/
/**
* @function
* @name ExtraStation#withGuac
* @param {boolean} guac
* @returns {ExtraStation}
*/
/**
* @function
* @name ExtraStation#build
* @returns {Burrito}
*/
/**
* @implements {FluentBuilder}
* @implements {CarbStation}
* @implements {ExtraStation}
*/
class FluentBurritoBuilder {
/**
* @returns {FluentBuilder}
*/
constructor() {
this.toppings = {};
}
/**
* @param {string} protein
* @returns {CarbStation}
*/
withProtein(protein) {
this.toppings.protein = protein;
return this;
}
/**
* @param {string} carb
* @returns {ExtraStation}
*/
withCarb(carb) {
this.toppings.carb = carb;
return this;
}
/**
* @param {string} salsa
* @returns {ExtraStation}
*/
withSalsa(salsa) {
this.toppings.salsa = salsa;
return this;
}
/**
* @param {string} cheese
* @returns {ExtraStation}
*/
withCheese(cheese) {
this.toppings.cheese = cheese;
return this;
}
/**
* @param {boolean} guac
* @returns {ExtraStation}
*/
withGuac(guac) {
this.toppings.guac = guac;
return this;
}
/**
* @returns {Burrito}
*/
build() {
const { carb, protein, salsa, cheese, guac } = this.toppings;
return new Burrito(carb, protein, salsa, cheese, guac);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment