Skip to content

Commit 3a36d16

Browse files
ST-DDTdemipel8
authored andcommitted
docs: improve image jsdocs (faker-js#437)
1 parent c6fdb2f commit 3a36d16

File tree

1 file changed

+152
-86
lines changed

1 file changed

+152
-86
lines changed

src/image.ts

Lines changed: 152 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { Unsplash } from './image_providers/unsplash';
44
import { LoremPicsum } from './image_providers/lorempicsum';
55

66
/**
7+
* Module to generate placeholder images.
8+
*
79
* Default provider is unsplash image provider.
810
*/
911
export class Image {
@@ -26,12 +28,16 @@ export class Image {
2628
}
2729

2830
/**
29-
* image
31+
* Generates a random image url from one of the supported categories.
3032
*
31-
* @method faker.image.image
32-
* @param width
33-
* @param height
34-
* @param randomize
33+
* @param width The width of the image.
34+
* @param height The height of the image.
35+
* @param randomize Whether to randomize the image or not.
36+
*
37+
* @example
38+
* faker.image.image() // 'http://placeimg.com/640/480/city'
39+
* faker.image.image(1234, 2345) // 'http://placeimg.com/1234/2345/sports'
40+
* faker.image.image(1234, 2345, true) // 'http://placeimg.com/1234/2345/nature?56789'
3541
*/
3642
image(width?: number, height?: number, randomize?: boolean): string {
3743
const categories = [
@@ -57,22 +63,31 @@ export class Image {
5763
}
5864

5965
/**
60-
* avatar
66+
* Generates a random avatar image url.
6167
*
62-
* @method faker.image.avatar
68+
* @example
69+
* faker.image.avatar()
70+
* // 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/170.jpg'
6371
*/
6472
avatar(): string {
6573
return this.faker.internet.avatar();
6674
}
6775

6876
/**
69-
* imageUrl
77+
* Generates a random image url.
78+
*
79+
* @param width The width of the image. Defaults to `640`.
80+
* @param height The height of the image. Defaults to `480`.
81+
* @param category The category of the image. By default, a random one will be selected.
82+
* @param randomize Whether to randomize the image or not.
83+
* @param https When true, return a `https` url. Otherwise, return a `http` url.
7084
*
71-
* @method faker.image.imageUrl
72-
* @param width
73-
* @param height
74-
* @param category
75-
* @param randomize
85+
* @example
86+
* faker.image.imageUrl() // 'http://placeimg.com/640/480'
87+
* faker.image.imageUrl(1234, 2345) // 'http://placeimg.com/1234/2345'
88+
* faker.image.imageUrl(1234, 2345, 'cat') // 'http://placeimg.com/1234/2345/cat'
89+
* faker.image.imageUrl(1234, 2345, 'cat', true) // 'http://placeimg.com/1234/2345/cat?6849'
90+
* faker.image.imageUrl(1234, 2345, 'cat', true, true) // 'https://placeimg.com/1234/2345/cat?56789'
7691
*/
7792
imageUrl(
7893
width?: number,
@@ -100,168 +115,222 @@ export class Image {
100115
}
101116

102117
/**
103-
* abstract
118+
* Generates a random abstract image url.
119+
*
120+
* @param width The width of the image.
121+
* @param height The height of the image.
122+
* @param randomize Whether to randomize the image or not.
104123
*
105-
* @method faker.image.abstract
106-
* @param width
107-
* @param height
108-
* @param randomize
124+
* @example
125+
* faker.image.abstract() // 'http://placeimg.com/640/480/abstract'
126+
* faker.image.abstract(1234, 2345) // 'http://placeimg.com/1234/2345/abstract'
127+
* faker.image.abstract(1234, 2345, true) // 'http://placeimg.com/1234/2345/abstract?56789'
109128
*/
110129
abstract(width?: number, height?: number, randomize?: boolean): string {
111130
return this.faker.image.imageUrl(width, height, 'abstract', randomize);
112131
}
113132

114133
/**
115-
* animals
134+
* Generates a random animal image url.
116135
*
117-
* @method faker.image.animals
118-
* @param width
119-
* @param height
120-
* @param randomize
136+
* @param width The width of the image.
137+
* @param height The height of the image.
138+
* @param randomize Whether to randomize the image or not.
139+
*
140+
* @example
141+
* faker.image.animals() // 'http://placeimg.com/640/480/animals'
142+
* faker.image.animals(1234, 2345) // 'http://placeimg.com/1234/2345/animals'
143+
* faker.image.animals(1234, 2345, true) // 'http://placeimg.com/1234/2345/animals?56789'
121144
*/
122145
animals(width?: number, height?: number, randomize?: boolean): string {
123146
return this.faker.image.imageUrl(width, height, 'animals', randomize);
124147
}
125148

126149
/**
127-
* business
150+
* Generates a random business image url.
151+
*
152+
* @param width The width of the image.
153+
* @param height The height of the image.
154+
* @param randomize Whether to randomize the image or not.
128155
*
129-
* @method faker.image.business
130-
* @param width
131-
* @param height
132-
* @param randomize
156+
* @example
157+
* faker.image.business() // 'http://placeimg.com/640/480/business'
158+
* faker.image.business(1234, 2345) // 'http://placeimg.com/1234/2345/business'
159+
* faker.image.business(1234, 2345, true) // 'http://placeimg.com/1234/2345/business?56789'
133160
*/
134161
business(width?: number, height?: number, randomize?: boolean): string {
135162
return this.faker.image.imageUrl(width, height, 'business', randomize);
136163
}
137164

138165
/**
139-
* cats
166+
* Generates a random cat image url.
140167
*
141-
* @method faker.image.cats
142-
* @param width
143-
* @param height
144-
* @param randomize
168+
* @param width The width of the image.
169+
* @param height The height of the image.
170+
* @param randomize Whether to randomize the image or not.
171+
*
172+
* @example
173+
* faker.image.cats() // 'http://placeimg.com/640/480/cats'
174+
* faker.image.cats(1234, 2345) // 'http://placeimg.com/1234/2345/cats'
175+
* faker.image.cats(1234, 2345, true) // 'http://placeimg.com/1234/2345/cats?56789'
145176
*/
146177
cats(width?: number, height?: number, randomize?: boolean): string {
147178
return this.faker.image.imageUrl(width, height, 'cats', randomize);
148179
}
149180

150181
/**
151-
* city
182+
* Generates a random city image url.
183+
*
184+
* @param width The width of the image.
185+
* @param height The height of the image.
186+
* @param randomize Whether to randomize the image or not.
152187
*
153-
* @method faker.image.city
154-
* @param width
155-
* @param height
156-
* @param randomize
188+
* @example
189+
* faker.image.city() // 'http://placeimg.com/640/480/city'
190+
* faker.image.city(1234, 2345) // 'http://placeimg.com/1234/2345/city'
191+
* faker.image.city(1234, 2345, true) // 'http://placeimg.com/1234/2345/city?56789'
157192
*/
158193
city(width?: number, height?: number, randomize?: boolean): string {
159194
return this.faker.image.imageUrl(width, height, 'city', randomize);
160195
}
161196

162197
/**
163-
* food
198+
* Generates a random food image url.
164199
*
165-
* @method faker.image.food
166-
* @param width
167-
* @param height
168-
* @param randomize
200+
* @param width The width of the image.
201+
* @param height The height of the image.
202+
* @param randomize Whether to randomize the image or not.
203+
*
204+
* @example
205+
* faker.image.food() // 'http://placeimg.com/640/480/food'
206+
* faker.image.food(1234, 2345) // 'http://placeimg.com/1234/2345/food'
207+
* faker.image.food(1234, 2345, true) // 'http://placeimg.com/1234/2345/food?56789'
169208
*/
170209
food(width?: number, height?: number, randomize?: boolean): string {
171210
return this.faker.image.imageUrl(width, height, 'food', randomize);
172211
}
173212

174213
/**
175-
* nightlife
214+
* Generates a random nightlife image url.
215+
*
216+
* @param width The width of the image.
217+
* @param height The height of the image.
218+
* @param randomize Whether to randomize the image or not.
176219
*
177-
* @method faker.image.nightlife
178-
* @param width
179-
* @param height
180-
* @param randomize
220+
* @example
221+
* faker.image.nightlife() // 'http://placeimg.com/640/480/nightlife'
222+
* faker.image.nightlife(1234, 2345) // 'http://placeimg.com/1234/2345/nightlife'
223+
* faker.image.nightlife(1234, 2345, true) // 'http://placeimg.com/1234/2345/nightlife?56789'
181224
*/
182225
nightlife(width?: number, height?: number, randomize?: boolean): string {
183226
return this.faker.image.imageUrl(width, height, 'nightlife', randomize);
184227
}
185228

186229
/**
187-
* fashion
230+
* Generates a random fashion image url.
188231
*
189-
* @method faker.image.fashion
190-
* @param width
191-
* @param height
192-
* @param randomize
232+
* @param width The width of the image.
233+
* @param height The height of the image.
234+
* @param randomize Whether to randomize the image or not.
235+
*
236+
* @example
237+
* faker.image.fashion() // 'http://placeimg.com/640/480/fashion'
238+
* faker.image.fashion(1234, 2345) // 'http://placeimg.com/1234/2345/fashion'
239+
* faker.image.fashion(1234, 2345, true) // 'http://placeimg.com/1234/2345/fashion?56789'
193240
*/
194241
fashion(width?: number, height?: number, randomize?: boolean): string {
195242
return this.faker.image.imageUrl(width, height, 'fashion', randomize);
196243
}
197244

198245
/**
199-
* people
246+
* Generates a random people image url.
247+
*
248+
* @param width The width of the image.
249+
* @param height The height of the image.
250+
* @param randomize Whether to randomize the image or not.
200251
*
201-
* @method faker.image.people
202-
* @param width
203-
* @param height
204-
* @param randomize
252+
* @example
253+
* faker.image.people() // 'http://placeimg.com/640/480/people'
254+
* faker.image.people(1234, 2345) // 'http://placeimg.com/1234/2345/people'
255+
* faker.image.people(1234, 2345, true) // 'http://placeimg.com/1234/2345/people?56789'
205256
*/
206257
people(width?: number, height?: number, randomize?: boolean): string {
207258
return this.faker.image.imageUrl(width, height, 'people', randomize);
208259
}
209260

210261
/**
211-
* nature
262+
* Generates a random nature image url.
212263
*
213-
* @method faker.image.nature
214-
* @param width
215-
* @param height
216-
* @param randomize
264+
* @param width The width of the image.
265+
* @param height The height of the image.
266+
* @param randomize Whether to randomize the image or not.
267+
*
268+
* @example
269+
* faker.image.nature() // 'http://placeimg.com/640/480/nature'
270+
* faker.image.nature(1234, 2345) // 'http://placeimg.com/1234/2345/nature'
271+
* faker.image.nature(1234, 2345, true) // 'http://placeimg.com/1234/2345/nature?56789'
217272
*/
218273
nature(width?: number, height?: number, randomize?: boolean): string {
219274
return this.faker.image.imageUrl(width, height, 'nature', randomize);
220275
}
221276

222277
/**
223-
* sports
278+
* Generates a random sports image url.
279+
*
280+
* @param width The width of the image.
281+
* @param height The height of the image.
282+
* @param randomize Whether to randomize the image or not.
224283
*
225-
* @method faker.image.sports
226-
* @param width
227-
* @param height
228-
* @param randomize
284+
* @example
285+
* faker.image.sports() // 'http://placeimg.com/640/480/sports'
286+
* faker.image.sports(1234, 2345) // 'http://placeimg.com/1234/2345/sports'
287+
* faker.image.sports(1234, 2345, true) // 'http://placeimg.com/1234/2345/sports?56789'
229288
*/
230289
sports(width?: number, height?: number, randomize?: boolean): string {
231290
return this.faker.image.imageUrl(width, height, 'sports', randomize);
232291
}
233292

234293
/**
235-
* technics
294+
* Generates a random technics image url.
236295
*
237-
* @method faker.image.technics
238-
* @param width
239-
* @param height
240-
* @param randomize
296+
* @param width The width of the image.
297+
* @param height The height of the image.
298+
* @param randomize Whether to randomize the image or not.
299+
*
300+
* @example
301+
* faker.image.technics() // 'http://placeimg.com/640/480/technics'
302+
* faker.image.technics(1234, 2345) // 'http://placeimg.com/1234/2345/technics'
303+
* faker.image.technics(1234, 2345, true) // 'http://placeimg.com/1234/2345/technics?56789'
241304
*/
242305
technics(width?: number, height?: number, randomize?: boolean): string {
243306
return this.faker.image.imageUrl(width, height, 'technics', randomize);
244307
}
245308

246309
/**
247-
* transport
310+
* Generates a random transport image url.
311+
*
312+
* @param width The width of the image.
313+
* @param height The height of the image.
314+
* @param randomize Whether to randomize the image or not.
248315
*
249-
* @method faker.image.transport
250-
* @param width
251-
* @param height
252-
* @param randomize
316+
* @example
317+
* faker.image.transport() // 'http://placeimg.com/640/480/transport'
318+
* faker.image.transport(1234, 2345) // 'http://placeimg.com/1234/2345/transport'
319+
* faker.image.transport(1234, 2345, true) // 'http://placeimg.com/1234/2345/transport?56789'
253320
*/
254321
transport(width?: number, height?: number, randomize?: boolean): string {
255322
return this.faker.image.imageUrl(width, height, 'transport', randomize);
256323
}
257324

258325
/**
259-
* dataUri
326+
* Generates a random data uri containing an svg image.
260327
*
261-
* @method faker.image.dataUri
262-
* @param width
263-
* @param height
264-
* @param color
328+
* @param width The width of the image.
329+
* @param height The height of the image.
330+
* @param color The color to use. Defaults to `grey`.
331+
*
332+
* @example
333+
* faker.image.dataUri() // 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http...'
265334
*/
266335
dataUri(width?: number, height?: number, color: string = 'grey'): string {
267336
const svgString = `<svg xmlns="http://www.w3.org/2000/svg" version="1.1" baseProfile="full" width="${width}" height="${height}"><rect width="100%" height="100%" fill="${color}"/><text x="${
@@ -272,7 +341,4 @@ export class Image {
272341
const rawPrefix = 'data:image/svg+xml;charset=UTF-8,';
273342
return rawPrefix + encodeURIComponent(svgString);
274343
}
275-
276-
// Object.assign(self, self.unsplash);
277-
// How to set default as unsplash? should be image.default?
278344
}

0 commit comments

Comments
 (0)