UNPKG

fox-block-builder

Version:

Maintainable code for loop slack-block-kit-like modal builder

44 lines 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Builder = void 0; const exception_1 = require("../exception"); class Builder { constructor(params) { this.props = params ? { ...params } : {}; Object.keys(this.props).forEach((prop) => this.props[prop] === undefined && delete this.props[prop]); Object.seal(this); } set(value, prop) { if (this.props[prop] !== undefined) { throw new exception_1.BlockBuilderError(`Property ${prop} can only be assigned once.`); } if (value !== undefined) { this.props[prop] = value; } return this; } append(value, prop) { const prunedValue = Builder.pruneUndefinedFromArray(value); if (prunedValue.length > 0) { this.props[prop] = this.props[prop] === undefined ? prunedValue : this.props[prop].concat(prunedValue); } return this; } getResult(Clazz, overrideProps) { return new Clazz({ ...this.props, ...overrideProps }); } /** @internal */ // eslint-disable-next-line max-len // eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars, @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any build(params) { throw new exception_1.BlockBuilderError("Builder must have a declared 'build' method"); } static pruneUndefinedFromArray(array) { return array.filter((value) => (value !== undefined ? value : false)); } } exports.Builder = Builder; //# sourceMappingURL=builder.js.map