Socialify

Folder ..

Viewing git.ts
105 lines (91 loc) • 2.2 KB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
import type { Faker } from '.';

export class Git {
  private hexChars = [
    '0',
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    'a',
    'b',
    'c',
    'd',
    'e',
    'f',
  ];

  constructor(private readonly faker: Faker) {
    // Bind `this` so namespaced is working correctly
    for (const name of Object.getOwnPropertyNames(Git.prototype)) {
      if (name === 'constructor' || typeof this[name] !== 'function') {
        continue;
      }
      this[name] = this[name].bind(this);
    }
  }

  /**
   * branch
   *
   * @method faker.git.branch
   */
  branch(): string {
    const noun = this.faker.hacker.noun().replace(' ', '-');
    const verb = this.faker.hacker.verb().replace(' ', '-');
    return noun + '-' + verb;
  }

  /**
   * commitEntry
   *
   * @method faker.git.commitEntry
   * @param options
   */
  commitEntry(options: { merge?: boolean } = {}): string {
    // TODO @Shinigami92 2022-01-11: We may want to make it configurable to use just `\n` instead of `\r\n`
    let entry = 'commit {{git.commitSha}}\r\n';

    if (options.merge || this.faker.datatype.number({ min: 0, max: 4 }) === 0) {
      entry += 'Merge: {{git.shortSha}} {{git.shortSha}}\r\n';
    }

    entry +=
      'Author: {{name.firstName}} {{name.lastName}} <{{internet.email}}>\r\n';
    entry += 'Date: ' + this.faker.date.recent().toString() + '\r\n';
    entry += '\r\n\xa0\xa0\xa0\xa0{{git.commitMessage}}\r\n';

    return this.faker.fake(entry);
  }

  /**
   * commitMessage
   *
   * @method faker.git.commitMessage
   */
  commitMessage(): string {
    const format = '{{hacker.verb}} {{hacker.adjective}} {{hacker.noun}}';
    return this.faker.fake(format);
  }

  /**
   * commitSha
   *
   * @method faker.git.commitSha
   */
  commitSha(): string {
    let commit = '';

    for (let i = 0; i < 40; i++) {
      commit += this.faker.random.arrayElement(this.hexChars);
    }

    return commit;
  }

  /**
   * shortSha
   *
   * @method faker.git.shortSha
   */
  shortSha(): string {
    let shortSha = '';

    for (let i = 0; i < 7; i++) {
      shortSha += this.faker.random.arrayElement(this.hexChars);
    }

    return shortSha;
  }
}