Skip to content

Commit 1b10032

Browse files
MohdImran001damienwebdev
authored andcommitted
feat: migrate word (#102)
1 parent c0a6277 commit 1b10032

File tree

2 files changed

+173
-1
lines changed

2 files changed

+173
-1
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Name } from './name';
1212
import { Random } from './random';
1313
import { System } from './system';
1414
import { Time } from './time';
15+
import { Word } from './word';
1516

1617
export interface FakerOptions {
1718
locales?: string[];
@@ -192,7 +193,7 @@ export class Faker {
192193
readonly system: System = new System(this);
193194
readonly time: Time = new Time();
194195
readonly vehicle = new (require('./vehicle'))(this);
195-
readonly word = new (require('./word'))(this);
196+
readonly word: Word = new Word(this);
196197

197198
constructor(opts: FakerOptions = {}) {
198199
this.locales = this.locales || opts.locales || {};

src/word.ts

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import type { Faker } from '.';
2+
3+
export class Word {
4+
constructor(private readonly faker: Faker) {
5+
// Bind `this` so namespaced is working correctly
6+
for (const name of Object.getOwnPropertyNames(Word.prototype)) {
7+
if (name === 'constructor' || typeof this[name] !== 'function') {
8+
continue;
9+
}
10+
this[name] = this[name].bind(this);
11+
}
12+
}
13+
14+
/**
15+
* Returns an adjective of random or optionally specified length.
16+
* If specified length is unresolvable, returns random adjective.
17+
*
18+
* @method faker.word.adjective
19+
* @param optional length of word to return
20+
* @returns a random adjective
21+
*/
22+
adjective(length?: number): string {
23+
var wordList = this.faker.definitions.word.adjective;
24+
if (length) {
25+
wordList = this.faker.definitions.word.adjective.filter(
26+
(word: string) => word.length == length
27+
);
28+
}
29+
30+
// If result of filtered word list is undefined, return an element
31+
// from the unfiltered list.
32+
return (
33+
this.faker.random.arrayElement(wordList) ||
34+
this.faker.random.arrayElement(this.faker.definitions.word.adjective)
35+
);
36+
}
37+
38+
/**
39+
* Returns an adverb of random or optionally specified length.
40+
* If specified length is unresolvable, returns random adverb.
41+
*
42+
* @method faker.word.adverb
43+
* @param optional length of word to return
44+
* @returns random adverb
45+
*/
46+
adverb(length?: number): string {
47+
var wordList = this.faker.definitions.word.adverb;
48+
if (length) {
49+
wordList = this.faker.definitions.word.adverb.filter(
50+
(word: string) => word.length == length
51+
);
52+
}
53+
// If result of filtered word list is undefined, return an element
54+
// from the unfiltered list.
55+
return (
56+
this.faker.random.arrayElement(wordList) ||
57+
this.faker.random.arrayElement(this.faker.definitions.word.adverb)
58+
);
59+
}
60+
61+
/**
62+
* Returns a conjunction of random or optionally specified length.
63+
* If specified length is unresolvable, returns random conjunction.
64+
*
65+
* @method faker.word.conjunction
66+
* @param optional length of word to return
67+
* @returns random conjunction
68+
*/
69+
conjunction(length?: number): string {
70+
var wordList = this.faker.definitions.word.conjunction;
71+
if (length) {
72+
wordList = this.faker.definitions.word.conjunction.filter(
73+
(word: string) => word.length == length
74+
);
75+
}
76+
// If result of filtered word list is undefined, return an element
77+
// from the unfiltered list.
78+
return (
79+
this.faker.random.arrayElement(wordList) ||
80+
this.faker.random.arrayElement(this.faker.definitions.word.conjunction)
81+
);
82+
}
83+
/**
84+
* Returns an interjection of random or optionally specified length.
85+
* If specified length is unresolvable, returns random interjection.
86+
*
87+
* @method faker.word.interjection
88+
* @param optional length of word to return
89+
* @returns random interjection
90+
*/
91+
interjection(length?: number): string {
92+
var wordList = this.faker.definitions.word.interjection;
93+
if (length) {
94+
wordList = this.faker.definitions.word.interjection.filter(
95+
(word: string) => word.length == length
96+
);
97+
}
98+
// If result of filtered word list is undefined, return an element
99+
// from the unfiltered list.
100+
return (
101+
this.faker.random.arrayElement(wordList) ||
102+
this.faker.random.arrayElement(this.faker.definitions.word.interjection)
103+
);
104+
}
105+
/**
106+
* Returns a noun of random or optionally specified length.
107+
* If specified length is unresolvable, returns random noun.
108+
*
109+
* @method faker.word.noun
110+
* @param optional length of word to return
111+
* @returns random noun
112+
*/
113+
noun(length?: number): string {
114+
var wordList = this.faker.definitions.word.noun;
115+
if (length) {
116+
wordList = this.faker.definitions.word.noun.filter(
117+
(word: string) => word.length == length
118+
);
119+
}
120+
// If result of filtered word list is undefined, return an element
121+
// from the unfiltered list.
122+
return (
123+
this.faker.random.arrayElement(wordList) ||
124+
this.faker.random.arrayElement(this.faker.definitions.word.noun)
125+
);
126+
}
127+
/**
128+
* Returns a preposition of random or optionally specified length.
129+
* If specified length is unresolvable, returns random preposition.
130+
*
131+
* @method faker.word.preposition
132+
* @param optional length of word to return
133+
* @returns random preposition
134+
*/
135+
preposition(length?: number): string {
136+
var wordList = this.faker.definitions.word.preposition;
137+
if (length) {
138+
wordList = this.faker.definitions.word.preposition.filter(
139+
(word: string) => word.length == length
140+
);
141+
}
142+
// If result of filtered word list is undefined, return an element
143+
// from the unfiltered list.
144+
return (
145+
this.faker.random.arrayElement(wordList) ||
146+
this.faker.random.arrayElement(this.faker.definitions.word.preposition)
147+
);
148+
}
149+
/**
150+
* Returns a verb of random or optionally specified length.
151+
* If specified length is unresolvable, returns random verb.
152+
*
153+
* @method faker.word.verb
154+
* @param optional length of word to return
155+
* @returns random verb
156+
*/
157+
verb(length?: number): string {
158+
var wordList = this.faker.definitions.word.verb;
159+
if (length) {
160+
wordList = this.faker.definitions.word.verb.filter(
161+
(word: string) => word.length == length
162+
);
163+
}
164+
// If result of filtered word list is undefined, return an element
165+
// from the unfiltered list.
166+
return (
167+
this.faker.random.arrayElement(wordList) ||
168+
this.faker.random.arrayElement(this.faker.definitions.word.verb)
169+
);
170+
}
171+
}

0 commit comments

Comments
 (0)