Skip to content

Commit e0005ad

Browse files
Shinigami92damienwebdev
authored andcommitted
feat: migrate hacker (#81)
1 parent 07f8b44 commit e0005ad

File tree

3 files changed

+99
-3
lines changed

3 files changed

+99
-3
lines changed

src/hacker.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import type { Faker } from '.';
2+
3+
export class Hacker {
4+
constructor(private readonly faker: Faker) {
5+
// Bind `this` so namespaced is working correctly
6+
for (const name of Object.getOwnPropertyNames(Hacker.prototype)) {
7+
if (name === 'constructor' || typeof this[name] !== 'function') {
8+
continue;
9+
}
10+
this[name] = this[name].bind(this);
11+
}
12+
}
13+
14+
/**
15+
* abbreviation
16+
*
17+
* @method faker.hacker.abbreviation
18+
*/
19+
abbreviation() {
20+
return this.faker.random.arrayElement(
21+
this.faker.definitions.hacker.abbreviation
22+
);
23+
}
24+
25+
/**
26+
* adjective
27+
*
28+
* @method faker.hacker.adjective
29+
*/
30+
adjective() {
31+
return this.faker.random.arrayElement(
32+
this.faker.definitions.hacker.adjective
33+
);
34+
}
35+
36+
/**
37+
* noun
38+
*
39+
* @method faker.hacker.noun
40+
*/
41+
noun() {
42+
return this.faker.random.arrayElement(this.faker.definitions.hacker.noun);
43+
}
44+
45+
/**
46+
* verb
47+
*
48+
* @method faker.hacker.verb
49+
*/
50+
verb() {
51+
return this.faker.random.arrayElement(this.faker.definitions.hacker.verb);
52+
}
53+
54+
/**
55+
* ingverb
56+
*
57+
* @method faker.hacker.ingverb
58+
*/
59+
ingverb() {
60+
return this.faker.random.arrayElement(
61+
this.faker.definitions.hacker.ingverb
62+
);
63+
}
64+
65+
/**
66+
* phrase
67+
*
68+
* @method faker.hacker.phrase
69+
*/
70+
phrase() {
71+
const data = {
72+
abbreviation: this.abbreviation,
73+
adjective: this.adjective,
74+
ingverb: this.ingverb,
75+
noun: this.noun,
76+
verb: this.verb,
77+
};
78+
79+
const phrase = this.faker.random.arrayElement(
80+
this.faker.definitions.hacker.phrase
81+
);
82+
return this.faker.helpers.mustache(phrase, data);
83+
}
84+
}

src/helpers.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,24 @@ export class Helpers {
289289
* @param str
290290
* @param data
291291
*/
292-
mustache(str: string | undefined, data: Record<string, string>): string {
292+
mustache(
293+
str: string | undefined,
294+
data: Record<
295+
string,
296+
string | ((substring: string, ...args: any[]) => string)
297+
>
298+
): string {
293299
if (typeof str === 'undefined') {
294300
return '';
295301
}
296302
for (const p in data) {
297303
const re = new RegExp('{{' + p + '}}', 'g');
298-
str = str.replace(re, data[p]);
304+
str = str.replace(
305+
re,
306+
// TODO christopher 2022-01-14: Try to improve the type or maybe use `if`
307+
// @ts-expect-error
308+
data[p]
309+
);
299310
}
300311
return str;
301312
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Datatype } from './datatype';
22
import { _Date } from './date';
3+
import { Hacker } from './hacker';
34
import { Helpers } from './helpers';
45
import { Mersenne } from './mersenne';
56
import { Random } from './random';
@@ -171,7 +172,7 @@ export class Faker {
171172
readonly date: _Date = new _Date(this);
172173
readonly finance = new (require('./finance'))(this);
173174
readonly git = new (require('./git'))(this);
174-
readonly hacker = new (require('./hacker'))(this);
175+
readonly hacker: Hacker = new Hacker(this);
175176
// TODO @Shinigami92 2022-01-12: iban was not used
176177
// readonly iban = new (require('./iban'))(this);
177178
readonly image = new (require('./image'))(this);

0 commit comments

Comments
 (0)