Skip to content

Commit

Permalink
refactor(git): reduce code duplication and follow code style (#1271)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Aug 15, 2022
1 parent fda44bb commit 83e5fd3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 42 deletions.
45 changes: 9 additions & 36 deletions src/modules/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,6 @@ import type { Faker } from '../..';
* Module to generate git related entries.
*/
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)) {
Expand Down Expand Up @@ -68,9 +49,14 @@ export class Git {
eol?: 'LF' | 'CRLF';
} = {}
): string {
const {
merge = this.faker.datatype.number({ min: 0, max: 4 }) === 0,
eol = 'CRLF',
} = options;

const lines = [`commit ${this.faker.git.commitSha()}`];

if (options.merge || this.faker.datatype.number({ min: 0, max: 4 }) === 0) {
if (merge) {
lines.push(`Merge: ${this.shortSha()} ${this.shortSha()}`);
}

Expand All @@ -83,8 +69,7 @@ export class Git {
''
);

const eolOption = options.eol ?? 'CRLF';
const eolChar = eolOption === 'CRLF' ? '\r\n' : '\n';
const eolChar = eol === 'CRLF' ? '\r\n' : '\n';
const entry = lines.join(eolChar);

return entry;
Expand All @@ -107,13 +92,7 @@ export class Git {
* faker.git.commitSha() // '2c6e3880fd94ddb7ef72d34e683cdc0c47bec6e6'
*/
commitSha(): string {
let commit = '';

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

return commit;
return this.faker.datatype.hexadecimal({ length: 40, case: 'lower' });
}

/**
Expand All @@ -123,12 +102,6 @@ export class Git {
* faker.git.shortSha() // '6155732'
*/
shortSha(): string {
let shortSha = '';

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

return shortSha;
return this.faker.datatype.hexadecimal({ length: 7, case: 'lower' });
}
}
12 changes: 6 additions & 6 deletions test/__snapshots__/git.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ exports[`git > 42 > branch 1`] = `"array-transmit"`;

exports[`git > 42 > commitMessage 1`] = `"navigate neural capacitor"`;

exports[`git > 42 > commitSha 1`] = `"5cf2bc99272107d592ba00fbdf302f2949806048"`;
exports[`git > 42 > commitSha 1`] = `"8be4abdd39321ad7d3fe01ffce404f4d6db0906b"`;

exports[`git > 42 > shortSha 1`] = `"5cf2bc9"`;
exports[`git > 42 > shortSha 1`] = `"8be4abd"`;

exports[`git > 1211 > branch 1`] = `"capacitor-connect"`;

exports[`git > 1211 > commitMessage 1`] = `"reboot online circuit"`;

exports[`git > 1211 > commitSha 1`] = `"e7ec32f0a2a3c652bbd0caabde64dfdf379e3259"`;
exports[`git > 1211 > commitSha 1`] = `"eadb42f0e3f4a973fab0aeefce96dfcf49cd438d"`;

exports[`git > 1211 > shortSha 1`] = `"e7ec32f"`;
exports[`git > 1211 > shortSha 1`] = `"eadb42f"`;

exports[`git > 1337 > branch 1`] = `"port-quantify"`;

exports[`git > 1337 > commitMessage 1`] = `"compress multi-byte panel"`;

exports[`git > 1337 > commitSha 1`] = `"48234870538945f4b41c61a52bf27dccc0576698"`;
exports[`git > 1337 > commitSha 1`] = `"5c346ba075bd57f5a62b82d72af39cbbb07a98cb"`;

exports[`git > 1337 > shortSha 1`] = `"4823487"`;
exports[`git > 1337 > shortSha 1`] = `"5c346ba"`;

0 comments on commit 83e5fd3

Please sign in to comment.