Skip to content

Commit

Permalink
feat!: another experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Aug 12, 2022
1 parent 462ee5a commit c66ddf2
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 85 deletions.
5 changes: 3 additions & 2 deletions src/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { Datatype } from './modules/datatype';
import { _Date } from './modules/date';
import { Fake } from './modules/fake';
import { Finance } from './modules/finance';
import { Git } from './modules/git';
import type { Git } from './modules/git';
import { git } from './modules/git';
import { Hacker } from './modules/hacker';
import { Helpers } from './modules/helpers';
import { Image } from './modules/image';
Expand Down Expand Up @@ -94,7 +95,7 @@ export class Faker {
readonly database: Database = new Database(this);
readonly date: _Date = new _Date(this);
readonly finance = new Finance(this);
readonly git: Git = new Git(this);
readonly git: Git = git(this);
readonly hacker: Hacker = new Hacker(this);
readonly image: Image = new Image(this);
readonly internet: Internet = new Internet(this);
Expand Down
12 changes: 12 additions & 0 deletions src/modules/git/branch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Git } from '.';
import type { Hacker } from '../hacker';

export function factory_branch(faker: {
hacker: Pick<Hacker, 'noun' | 'verb'>;
}): Git['branch'] {
return () => {
const noun = faker.hacker.noun().replace(' ', '-');
const verb = faker.hacker.verb().replace(' ', '-');
return `${noun}-${verb}`;
};
}
36 changes: 36 additions & 0 deletions src/modules/git/commitEntry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { Git } from '.';
import type { Datatype } from '../datatype';
import type { _Date } from '../date';
import type { Internet } from '../internet';
import type { Name } from '../name';

export function factory_commitEntry(faker: {
datatype: Pick<Datatype, 'number'>;
date: Pick<_Date, 'recent'>;
git: Pick<Git, 'commitMessage' | 'commitSha' | 'shortSha'>;
internet: Pick<Internet, 'email'>;
name: Pick<Name, 'firstName' | 'lastName'>;
}): Git['commitEntry'] {
return (options = {}) => {
const lines = [`commit ${faker.git.commitSha()}`];

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

lines.push(
`Author: ${faker.name.firstName()} ${faker.name.lastName()} <${faker.internet.email()}>`,
`Date: ${faker.date.recent().toString()}`,
'',
`\xa0\xa0\xa0\xa0${faker.git.commitMessage()}`,
// to end with a eol char
''
);

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

return entry;
};
}
9 changes: 9 additions & 0 deletions src/modules/git/commitMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Git } from '.';
import type { Hacker } from '../hacker';

export function factory_commitMessage(faker: {
hacker: Pick<Hacker, 'adjective' | 'noun' | 'verb'>;
}): Git['commitMessage'] {
return () =>
`${faker.hacker.verb()} ${faker.hacker.adjective()} ${faker.hacker.noun()}`;
}
17 changes: 17 additions & 0 deletions src/modules/git/commitSha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Git } from '.';
import type { Helpers } from '../helpers';
import { HEX_CHARS } from './hexChars';

export function factory_commitSha(faker: {
helpers: Pick<Helpers, 'arrayElement'>;
}): Git['commitSha'] {
return () => {
let commit = '';

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

return commit;
};
}
18 changes: 18 additions & 0 deletions src/modules/git/hexChars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const HEX_CHARS = [
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f',
] as const;
102 changes: 19 additions & 83 deletions src/modules/git/index.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,21 @@
import type { Faker } from '../..';
import { factory_branch } from './branch';
import { factory_commitEntry } from './commitEntry';
import { factory_commitMessage } from './commitMessage';
import { factory_commitSha } from './commitSha';
import { factory_shortSha } from './shortSha';

/**
* 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)) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}
this[name] = this[name].bind(this);
}
}

export interface Git {
/**
* Generates a random branch name.
*
* @example
* faker.git.branch() // 'feed-parse'
*/
branch(): string {
const noun = this.faker.hacker.noun().replace(' ', '-');
const verb = this.faker.hacker.verb().replace(' ', '-');
return `${noun}-${verb}`;
}
branch(): string;

/**
* Generates a random commit entry.
Expand All @@ -62,73 +34,37 @@ export class Git {
* //
* // copy primary system
*/
commitEntry(
options: {
merge?: boolean;
eol?: 'LF' | 'CRLF';
} = {}
): string {
const lines = [`commit ${this.faker.git.commitSha()}`];

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

lines.push(
`Author: ${this.faker.name.firstName()} ${this.faker.name.lastName()} <${this.faker.internet.email()}>`,
`Date: ${this.faker.date.recent().toString()}`,
'',
`\xa0\xa0\xa0\xa0${this.commitMessage()}`,
// to end with a eol char
''
);

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

return entry;
}
commitEntry(options?: { merge?: boolean; eol?: 'LF' | 'CRLF' }): string;

/**
* Generates a random commit message.
*
* @example
* faker.git.commitMessage() // 'reboot cross-platform driver'
*/
commitMessage(): string {
return `${this.faker.hacker.verb()} ${this.faker.hacker.adjective()} ${this.faker.hacker.noun()}`;
}
commitMessage(): string;

/**
* Generates a random commit sha (full).
*
* @example
* faker.git.commitSha() // '2c6e3880fd94ddb7ef72d34e683cdc0c47bec6e6'
*/
commitSha(): string {
let commit = '';

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

return commit;
}
commitSha(): string;

/**
* Generates a random commit sha (short).
*
* @example
* faker.git.shortSha() // '6155732'
*/
shortSha(): string {
let shortSha = '';

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

return shortSha;
}
shortSha(): string;
}

export const git = (faker: Faker): Git => ({
branch: factory_branch(faker),
commitEntry: factory_commitEntry(faker),
commitMessage: factory_commitMessage(faker),
commitSha: factory_commitSha(faker),
shortSha: factory_shortSha(faker),
});
17 changes: 17 additions & 0 deletions src/modules/git/shortSha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Git } from '.';
import type { Helpers } from '../helpers';
import { HEX_CHARS } from './hexChars';

export function factory_shortSha(faker: {
helpers: Pick<Helpers, 'arrayElement'>;
}): Git['shortSha'] {
return () => {
let shortSha = '';

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

return shortSha;
};
}

0 comments on commit c66ddf2

Please sign in to comment.