|
| 1 | +// @angular-package/type |
| 2 | +import { is, guard, ResultCallback } from '@angular-package/type'; |
| 3 | +// Class. |
| 4 | +import { MessageBuilderTemplate } from './message-builder-template.class'; |
| 5 | + |
| 6 | +// export type RegExpPreDefined = 'class' | 'function' | 'method' | 'param.name' | 'param.type'; |
| 7 | + |
| 8 | +export class MessageBuilder { |
| 9 | + #regExp = { |
| 10 | + class: /\[class\]/i, |
| 11 | + function: /\[function\]/i, |
| 12 | + method: /\[method\]/i, |
| 13 | + param: { |
| 14 | + name: /\[param.name\]/i, |
| 15 | + type: /\[param.type\]/i, |
| 16 | + }, |
| 17 | + return: /\[return\]/i |
| 18 | + }; |
| 19 | + |
| 20 | + #template: string; |
| 21 | + |
| 22 | + get get(): string { |
| 23 | + return this.#template; |
| 24 | + } |
| 25 | + |
| 26 | + constructor(template: 'class' | 'function' | 'method') { |
| 27 | + this.#template = new MessageBuilderTemplate(template).get; |
| 28 | + } |
| 29 | + |
| 30 | + public setClassName(name: string, callback?: ResultCallback): this { |
| 31 | + if (guard.string(name, callback)) { |
| 32 | + this.replace(this.#regExp.class, name); |
| 33 | + } |
| 34 | + return this; |
| 35 | + } |
| 36 | + |
| 37 | + public setFunctionName(name: string, callback?: ResultCallback): this { |
| 38 | + if (guard.string(name, callback)) { |
| 39 | + this.replace(this.#regExp.function, name); |
| 40 | + } |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public setMethodName(name: string, callback?: ResultCallback): this { |
| 45 | + if (guard.string(name, callback)) { |
| 46 | + this.replace(this.#regExp.method, name); |
| 47 | + } |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + public setParam(name: string, type: string = ''): this { |
| 52 | + if (guard.string(name)) { |
| 53 | + const param = `${name}${type}`; |
| 54 | + this |
| 55 | + .replace(this.#regExp.param.name, name) |
| 56 | + .replace(this.#regExp.param.type, type); |
| 57 | + |
| 58 | + if (type.length > 0) { |
| 59 | + this.replace(type, `: ${type}`); |
| 60 | + } |
| 61 | + } |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + public setReturn(returns: string, callback?: ResultCallback): this { |
| 66 | + if (guard.string(returns, callback)) { |
| 67 | + this.replace(this.#regExp.return, returns); |
| 68 | + if (returns.length > 0) { |
| 69 | + this.replace(returns, `: ${returns}`); |
| 70 | + } |
| 71 | + } |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + private replace(searchValue: string | RegExp, replaceValue: string): this { |
| 76 | + if (is.defined(searchValue)) { |
| 77 | + this.#template = this.#template.replace( |
| 78 | + searchValue, |
| 79 | + is.string(replaceValue) ? replaceValue : '' |
| 80 | + ); |
| 81 | + } |
| 82 | + return this; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// console.log( |
| 87 | +// new MessageBuilder('function') |
| 88 | +// .param('firstName?', 'string') |
| 89 | +// .function('isComponentLoader') |
| 90 | +// .get |
| 91 | +// ); |
0 commit comments