Skip to content

Commit 2209667

Browse files
committed
feat(core): faker as helper function instead of object
1 parent 038679e commit 2209667

File tree

4 files changed

+64
-55
lines changed

4 files changed

+64
-55
lines changed

index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ interface OptionsUrl {
5858
protocol?: string;
5959
tld?: string;
6060
}
61+
interface OptionsFaker {
62+
locale?: string;
63+
}
6164
declare class Faker {
6265
boolean(): boolean;
6366
integer(options?: OptionsInteger): number;
@@ -81,5 +84,4 @@ declare class Faker {
8184
firstName(options?: OptionsFirstname): string;
8285
lastName(options?: OptionsLastname): string;
8386
}
84-
declare const faker: Faker;
85-
export default faker;
87+
export default function faker(options?: OptionsFaker): Faker;

index.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ import firstName from '@fakerjs/firstname';
2121
import lastName from '@fakerjs/lastname';
2222

2323
class Faker {
24+
options = {};
25+
26+
constructor(options) {
27+
this.options = options || {};
28+
}
29+
2430
boolean() {
2531
return boolean();
2632
}
@@ -38,27 +44,27 @@ class Faker {
3844
}
3945

4046
gender(options) {
41-
return gender(options);
47+
return gender({locale: this.options.locale || 'en_US', ...options});
4248
}
4349

4450
ip() {
4551
return ip();
4652
}
4753

4854
letter(options) {
49-
return letter(options);
55+
return letter({locale: this.options.locale || 'en_US', ...options});
5056
}
5157

5258
string(options) {
5359
return string(options);
5460
}
5561

5662
profession(options) {
57-
return profession(options);
63+
return profession({locale: this.options.locale || 'en_US', ...options});
5864
}
5965

6066
animal(options) {
61-
return animal(options);
67+
return animal({locale: this.options.locale || 'en_US', ...options});
6268
}
6369

6470
superhero() {
@@ -86,13 +92,13 @@ class Faker {
8692
}
8793

8894
firstName(options) {
89-
return firstName(options);
95+
return firstName({locale: this.options.locale || 'en_US', ...options});
9096
}
9197

9298
lastName(options) {
93-
return lastName(options);
99+
return lastName({locale: this.options.locale || 'en_US', ...options});
94100
}
95-
101+
96102
browser() {
97103
return browser();
98104
}
@@ -106,5 +112,6 @@ class Faker {
106112
}
107113
}
108114

109-
const faker = new Faker();
110-
export default faker;
115+
export default function faker(options) {
116+
return new Faker(options);
117+
}

index.test-d.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import {expectType} from 'tsd';
22
import faker from './index.js';
33

4-
expectType<boolean>(faker.boolean());
5-
expectType<number>(faker.age());
6-
expectType<string>(faker.animal());
7-
expectType<string>(faker.gender());
8-
expectType<string>(faker.string());
9-
expectType<string>(faker.letter());
10-
expectType<string>(faker.profession());
11-
expectType<string>(faker.ip());
12-
expectType<string>(faker.superhero());
13-
expectType<number>(faker.integer());
14-
expectType<number>(faker.float());
15-
expectType<string>(faker.word());
16-
expectType<string>(faker.sentence());
17-
expectType<string>(faker.paragraph());
18-
expectType<string>(faker.domain());
19-
expectType<string>(faker.url());
20-
expectType<string>(faker.tld());
21-
expectType<string>(faker.browser());
22-
expectType<string>(faker.email());
23-
expectType<string>(faker.firstName());
24-
expectType<string>(faker.lastName());
4+
expectType<boolean>(faker().boolean());
5+
expectType<number>(faker().age());
6+
expectType<string>(faker().animal());
7+
expectType<string>(faker().gender());
8+
expectType<string>(faker().string());
9+
expectType<string>(faker().letter());
10+
expectType<string>(faker().profession());
11+
expectType<string>(faker().ip());
12+
expectType<string>(faker().superhero());
13+
expectType<number>(faker().integer());
14+
expectType<number>(faker().float());
15+
expectType<string>(faker().word());
16+
expectType<string>(faker().sentence());
17+
expectType<string>(faker().paragraph());
18+
expectType<string>(faker().domain());
19+
expectType<string>(faker().url());
20+
expectType<string>(faker().tld());
21+
expectType<string>(faker().browser());
22+
expectType<string>(faker().email());
23+
expectType<string>(faker().firstName());
24+
expectType<string>(faker().lastName());

test.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,89 @@ import test from 'ava';
22
import faker from './index.js';
33

44
test('faker return type to be object', t => {
5-
t.is(typeof faker, 'object');
5+
t.is(typeof faker(), 'object');
66
});
77

88
test('faker boolean return type to be boolean', t => {
9-
t.is(typeof faker.boolean(), 'boolean');
9+
t.is(typeof faker().boolean(), 'boolean');
1010
});
1111

1212
test('faker string return type to be string', t => {
13-
t.is(typeof faker.string(), 'string');
13+
t.is(typeof faker().string(), 'string');
1414
});
1515

1616
test('faker age return type to be number', t => {
17-
t.is(typeof faker.age(), 'number');
17+
t.is(typeof faker().age(), 'number');
1818
});
1919

2020
test('faker animal return type to be string', t => {
21-
t.is(typeof faker.animal(), 'string');
21+
t.is(typeof faker().animal(), 'string');
2222
});
2323

2424
test('faker integer return type to be number', t => {
25-
t.is(typeof faker.integer(), 'number');
25+
t.is(typeof faker().integer(), 'number');
2626
});
2727

2828
test('faker float return type to be number', t => {
29-
t.is(typeof faker.float(), 'number');
29+
t.is(typeof faker().float(), 'number');
3030
});
3131

3232
test('faker gende return type to be string', t => {
33-
t.is(typeof faker.gender(), 'string');
33+
t.is(typeof faker().gender(), 'string');
3434
});
3535

3636
test('faker superhero return type to be string', t => {
37-
t.is(typeof faker.superhero(), 'string');
37+
t.is(typeof faker().superhero(), 'string');
3838
});
3939

4040
test('faker profession return type to be string', t => {
41-
t.is(typeof faker.profession(), 'string');
41+
t.is(typeof faker().profession(), 'string');
4242
});
4343

4444
test('faker ip return type to be string', t => {
45-
t.is(typeof faker.ip(), 'string');
45+
t.is(typeof faker().ip(), 'string');
4646
});
4747

4848
test('faker letter return type to be string', t => {
49-
t.is(typeof faker.letter(), 'string');
49+
t.is(typeof faker().letter(), 'string');
5050
});
5151

5252
test('faker word return type to be string', t => {
53-
t.is(typeof faker.word(), 'string');
53+
t.is(typeof faker().word(), 'string');
5454
});
5555

5656
test('faker sentence return type to be string', t => {
57-
t.is(typeof faker.sentence(), 'string');
57+
t.is(typeof faker().sentence(), 'string');
5858
});
5959

6060
test('faker paragraph return type to be string', t => {
61-
t.is(typeof faker.paragraph(), 'string');
61+
t.is(typeof faker().paragraph(), 'string');
6262
});
6363

6464
test('faker domain return type to be string', t => {
65-
t.is(typeof faker.domain(), 'string');
65+
t.is(typeof faker().domain(), 'string');
6666
});
6767

6868
test('faker email return type to be string', t => {
69-
t.is(typeof faker.email(), 'string');
69+
t.is(typeof faker().email(), 'string');
7070
});
7171

7272
test('faker firstName return type to be string', t => {
73-
t.is(typeof faker.firstName(), 'string');
73+
t.is(typeof faker().firstName(), 'string');
7474
});
7575

7676
test('faker lastName return type to be string', t => {
77-
t.is(typeof faker.lastName(), 'string');
77+
t.is(typeof faker().lastName(), 'string');
7878
});
7979

8080
test('faker url return type to be string', t => {
81-
t.is(typeof faker.url(), 'string');
81+
t.is(typeof faker().url(), 'string');
8282
});
8383

8484
test('faker tld return type to be string', t => {
85-
t.is(typeof faker.tld(), 'string');
85+
t.is(typeof faker().tld(), 'string');
8686
});
8787

8888
test('faker browser return type to be string', t => {
89-
t.is(typeof faker.browser(), 'string');
90-
});
89+
t.is(typeof faker().browser(), 'string');
90+
});

0 commit comments

Comments
 (0)