From 83e5fd3db1624a2eebaa3d3714be8919e294a018 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Mon, 15 Aug 2022 10:24:50 +0200 Subject: [PATCH] refactor(git): reduce code duplication and follow code style (#1271) --- src/modules/git/index.ts | 45 ++++++----------------------- test/__snapshots__/git.spec.ts.snap | 12 ++++---- 2 files changed, 15 insertions(+), 42 deletions(-) diff --git a/src/modules/git/index.ts b/src/modules/git/index.ts index e86f66488d7..215962731d0 100644 --- a/src/modules/git/index.ts +++ b/src/modules/git/index.ts @@ -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)) { @@ -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()}`); } @@ -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; @@ -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' }); } /** @@ -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' }); } } diff --git a/test/__snapshots__/git.spec.ts.snap b/test/__snapshots__/git.spec.ts.snap index 9c7367ac7cc..d8e43f6cab5 100644 --- a/test/__snapshots__/git.spec.ts.snap +++ b/test/__snapshots__/git.spec.ts.snap @@ -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"`;