From bf58d10834459a76a1b8aefab5f35ac34c399a86 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 11 Jan 2022 10:36:47 +0100 Subject: [PATCH 1/2] feat: migrate hacker --- src/hacker.ts | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 3 +- 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/hacker.ts diff --git a/src/hacker.ts b/src/hacker.ts new file mode 100644 index 00000000000..ee36d814d43 --- /dev/null +++ b/src/hacker.ts @@ -0,0 +1,84 @@ +import type { Faker } from '.'; + +export class Hacker { + constructor(private readonly faker: Faker) { + // Bind `this` so namespaced is working correctly + for (const name of Object.getOwnPropertyNames(Hacker.prototype)) { + if (name === 'constructor' || typeof this[name] !== 'function') { + continue; + } + this[name] = this[name].bind(this); + } + } + + /** + * abbreviation + * + * @method faker.hacker.abbreviation + */ + abbreviation() { + return this.faker.random.arrayElement( + this.faker.definitions.hacker.abbreviation + ); + } + + /** + * adjective + * + * @method faker.hacker.adjective + */ + adjective() { + return this.faker.random.arrayElement( + this.faker.definitions.hacker.adjective + ); + } + + /** + * noun + * + * @method faker.hacker.noun + */ + noun() { + return this.faker.random.arrayElement(this.faker.definitions.hacker.noun); + } + + /** + * verb + * + * @method faker.hacker.verb + */ + verb() { + return this.faker.random.arrayElement(this.faker.definitions.hacker.verb); + } + + /** + * ingverb + * + * @method faker.hacker.ingverb + */ + ingverb() { + return this.faker.random.arrayElement( + this.faker.definitions.hacker.ingverb + ); + } + + /** + * phrase + * + * @method faker.hacker.phrase + */ + phrase() { + const data = { + abbreviation: this.abbreviation, + adjective: this.adjective, + ingverb: this.ingverb, + noun: this.noun, + verb: this.verb, + }; + + const phrase = this.faker.random.arrayElement( + this.faker.definitions.hacker.phrase + ); + return this.faker.helpers.mustache(phrase, data); + } +} diff --git a/src/index.ts b/src/index.ts index 38021f1e687..5fdb752c2c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import { Datatype } from './datatype'; import { _Date } from './date'; +import { Hacker } from './hacker'; import { Helpers } from './helpers'; import { Mersenne } from './mersenne'; import { Random } from './random'; @@ -171,7 +172,7 @@ export class Faker { readonly date: _Date = new _Date(this); readonly finance = new (require('./finance'))(this); readonly git = new (require('./git'))(this); - readonly hacker = new (require('./hacker'))(this); + readonly hacker: Hacker = new Hacker(this); // TODO @Shinigami92 2022-01-12: iban was not used // readonly iban = new (require('./iban'))(this); readonly image = new (require('./image'))(this); From 69562843eb8f4203563f7a47d3c9efa8f1d73ec4 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Fri, 14 Jan 2022 16:52:56 +0100 Subject: [PATCH 2/2] chore: fix type issue --- src/helpers.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index 548c1f1e4ca..6d6aa62fcd4 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -289,13 +289,24 @@ export class Helpers { * @param str * @param data */ - mustache(str: string | undefined, data: Record): string { + mustache( + str: string | undefined, + data: Record< + string, + string | ((substring: string, ...args: any[]) => string) + > + ): string { if (typeof str === 'undefined') { return ''; } for (const p in data) { const re = new RegExp('{{' + p + '}}', 'g'); - str = str.replace(re, data[p]); + str = str.replace( + re, + // TODO christopher 2022-01-14: Try to improve the type or maybe use `if` + // @ts-expect-error + data[p] + ); } return str; }