Skip to content

Commit 4e4589d

Browse files
feat: migrate company (#132)
Co-authored-by: Shinigami92 <chrissi92@hotmail.de>
1 parent 746731c commit 4e4589d

2 files changed

Lines changed: 143 additions & 1 deletion

File tree

src/company.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import type { Faker } from '.';
2+
import type { Fake } from './fake';
3+
4+
let f: Fake['fake'];
5+
6+
export class Company {
7+
constructor(private readonly faker: Faker) {
8+
f = this.faker.fake;
9+
10+
// Bind `this` so namespaced is working correctly
11+
for (const name of Object.getOwnPropertyNames(Company.prototype)) {
12+
if (name === 'constructor' || typeof this[name] !== 'function') {
13+
continue;
14+
}
15+
this[name] = this[name].bind(this);
16+
}
17+
}
18+
19+
/**
20+
* suffixes
21+
*
22+
* @method faker.company.suffixes
23+
*/
24+
suffixes(): string[] {
25+
// Don't want the source array exposed to modification, so return a copy
26+
return this.faker.definitions.company.suffix.slice(0);
27+
}
28+
29+
/**
30+
* companyName
31+
*
32+
* @method faker.company.companyName
33+
* @param format
34+
*/
35+
companyName(format?: number): string {
36+
const formats = [
37+
'{{name.lastName}} {{company.companySuffix}}',
38+
'{{name.lastName}} - {{name.lastName}}',
39+
'{{name.lastName}}, {{name.lastName}} and {{name.lastName}}',
40+
];
41+
42+
if (typeof format !== 'number') {
43+
format = this.faker.datatype.number(formats.length - 1);
44+
}
45+
46+
return f(formats[format]);
47+
}
48+
49+
/**
50+
* companySuffix
51+
*
52+
* @method faker.company.companySuffix
53+
*/
54+
companySuffix(): string {
55+
return this.faker.random.arrayElement(this.faker.company.suffixes());
56+
}
57+
58+
/**
59+
* catchPhrase
60+
*
61+
* @method faker.company.catchPhrase
62+
*/
63+
catchPhrase(): string {
64+
return f(
65+
'{{company.catchPhraseAdjective}} {{company.catchPhraseDescriptor}} {{company.catchPhraseNoun}}'
66+
);
67+
}
68+
69+
/**
70+
* bs
71+
*
72+
* @method faker.company.bs
73+
*/
74+
bs(): string {
75+
return f('{{company.bsBuzz}} {{company.bsAdjective}} {{company.bsNoun}}');
76+
}
77+
78+
/**
79+
* catchPhraseAdjective
80+
*
81+
* @method faker.company.catchPhraseAdjective
82+
*/
83+
catchPhraseAdjective(): string {
84+
return this.faker.random.arrayElement(
85+
this.faker.definitions.company.adjective
86+
);
87+
}
88+
89+
/**
90+
* catchPhraseDescriptor
91+
*
92+
* @method faker.company.catchPhraseDescriptor
93+
*/
94+
catchPhraseDescriptor(): string {
95+
return this.faker.random.arrayElement(
96+
this.faker.definitions.company.descriptor
97+
);
98+
}
99+
100+
/**
101+
* catchPhraseNoun
102+
*
103+
* @method faker.company.catchPhraseNoun
104+
*/
105+
catchPhraseNoun(): string {
106+
return this.faker.random.arrayElement(this.faker.definitions.company.noun);
107+
}
108+
109+
/**
110+
* bsAdjective
111+
*
112+
* @method faker.company.bsAdjective
113+
*/
114+
bsAdjective(): string {
115+
return this.faker.random.arrayElement(
116+
this.faker.definitions.company.bs_adjective
117+
);
118+
}
119+
120+
/**
121+
* bsBuzz
122+
*
123+
* @method faker.company.bsBuzz
124+
*/
125+
bsBuzz(): string {
126+
return this.faker.random.arrayElement(
127+
this.faker.definitions.company.bs_verb
128+
);
129+
}
130+
131+
/**
132+
* bsNoun
133+
*
134+
* @method faker.company.bsNoun
135+
*/
136+
bsNoun(): string {
137+
return this.faker.random.arrayElement(
138+
this.faker.definitions.company.bs_noun
139+
);
140+
}
141+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Address } from './address';
22
import { Animal } from './animal';
33
import { Commerce } from './commerce';
4+
import { Company } from './company';
45
import { Database } from './database';
56
import { Datatype } from './datatype';
67
import { _Date } from './date';
@@ -184,7 +185,7 @@ export class Faker {
184185
readonly address: Address = new Address(this);
185186
readonly animal: Animal = new Animal(this);
186187
readonly commerce: Commerce = new Commerce(this);
187-
readonly company = new (require('./company'))(this);
188+
readonly company: Company = new Company(this);
188189
readonly database: Database = new Database(this);
189190
readonly date: _Date = new _Date(this);
190191
readonly finance = new Finance(this);

0 commit comments

Comments
 (0)