buildLargeCode.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. export function buildLargeCode (linesOfCode: number): string {
  2. return new LargeCodeBuilder(linesOfCode).build();
  3. }
  4. class LargeCodeBuilder {
  5. /**
  6. * @type {string}
  7. */
  8. private code: string = '';
  9. /**
  10. * @type {number}
  11. */
  12. private readonly linesOfCode: number;
  13. /**
  14. * @param {number} linesOfCode
  15. */
  16. constructor (linesOfCode: number) {
  17. this.linesOfCode = linesOfCode;
  18. }
  19. public build (): string {
  20. const lastLineIndex: number = this.linesOfCode - 1;
  21. let funcIndex: number = 0,
  22. isFuncWasOpened: boolean = false;
  23. this.addLine(`function Foo () {`);
  24. this.addLine(`var var0 = 0;`);
  25. for (let index = 0; index < this.linesOfCode; index++) {
  26. const step: number = index % 10;
  27. const newIndex: number = index + 1;
  28. const isLastLine: boolean = index === lastLineIndex;
  29. if (step === 3) {
  30. funcIndex = index;
  31. isFuncWasOpened = true;
  32. this.addLine(`function func${funcIndex} () {`);
  33. this.addLine(`var var${newIndex} = var${index};`);
  34. }
  35. if (step === 4) {
  36. this.addLine(`if (true) {`);
  37. }
  38. if (step === 6) {
  39. this.addLine(`if (true) {`);
  40. }
  41. this.addLine(`var var${newIndex} = var${index};`);
  42. this.addLine(`var${newIndex}++;`);
  43. if (step === 9 || (isLastLine && isFuncWasOpened)) {
  44. isFuncWasOpened = false;
  45. this.addLine(`}`);
  46. this.addLine(`}`);
  47. this.addLine(`return var${newIndex};`);
  48. this.addLine(`}`);
  49. this.addLine(`var var${newIndex} = func${funcIndex}();`);
  50. }
  51. if (isLastLine) {
  52. this.addLine(`result = var${newIndex};`);
  53. }
  54. }
  55. this.addLine(`return result;`);
  56. this.addLine(`}`);
  57. this.addLine(`Foo();`);
  58. return this.code;
  59. }
  60. /**
  61. * @param {string} line
  62. * @returns {string}
  63. */
  64. private addLine (line: string): void {
  65. this.code += `\n${line}`;
  66. }
  67. }