Skip to content

Commit 2a4f835

Browse files
authored
docs: improve helpers jsdocs (#442)
1 parent c208777 commit 2a4f835

1 file changed

Lines changed: 143 additions & 57 deletions

File tree

src/helpers.ts

Lines changed: 143 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { Faker } from '.';
22

3+
/**
4+
* A full card with various details.
5+
*/
36
export interface Card {
47
name: string;
58
username: string;
@@ -41,6 +44,9 @@ export interface Card {
4144
}>;
4245
}
4346

47+
/**
48+
* A persons card with various details attempting to use a consistent context.
49+
*/
4450
export interface ContextualCard {
4551
name: string;
4652
username: string;
@@ -66,6 +72,9 @@ export interface ContextualCard {
6672
};
6773
}
6874

75+
/**
76+
* A user card with various details.
77+
*/
6978
export interface UserCard {
7079
name: string;
7180
username: string;
@@ -89,6 +98,9 @@ export interface UserCard {
8998
};
9099
}
91100

101+
/**
102+
* A transaction info.
103+
*/
92104
export interface Transaction {
93105
amount: string;
94106
date: Date;
@@ -110,22 +122,33 @@ export class Helpers {
110122
}
111123

112124
/**
113-
* backward-compatibility
125+
* Backward-compatibility. Use `faker.random.arrayElement()` instead.
114126
*
115-
* @method faker.helpers.randomize
116-
* @param array
127+
* Takes an array and returns a random element of the array.
128+
*
129+
* @param array The array to select an element from.
130+
*
131+
* @example
132+
* faker.helpers.randomize() // 'c'
133+
* faker.helpers.randomize([1, 2, 3]) // '2'
117134
*/
135+
// TODO ST-DDT 2022-02-06: Mark as deprecated
118136
randomize<T = string>(
119137
array: ReadonlyArray<T> = ['a', 'b', 'c'] as unknown as ReadonlyArray<T>
120138
): T {
121139
return this.faker.random.arrayElement(array);
122140
}
123141

124142
/**
125-
* slugifies string
143+
* Slugifies the given string.
144+
* For that all spaces (` `) are replaced by hyphens (`-`)
145+
* and most non word characters except for dots and hyphens will be removed.
126146
*
127-
* @method faker.helpers.slugify
128-
* @param string
147+
* @param string The input to slugify.
148+
*
149+
* @example
150+
* faker.helpers.slugify() // ''
151+
* faker.helpers.slugify("Hello world!") // 'Hello-world'
129152
*/
130153
slugify(string: string = ''): string {
131154
return string
@@ -134,11 +157,17 @@ export class Helpers {
134157
}
135158

136159
/**
137-
* Parses string for a symbol and replace it with a random number from 1-10
160+
* Parses the given string symbol by symbol and replaces the placeholders with digits (`0` - `9`).
161+
* `!` will be replaced by digits >=2 (`2` - `9`).
162+
*
163+
* @param string The template string to parse.
164+
* @param symbol The symbol to replace with digits. Defaults to `'#'`.
138165
*
139-
* @method faker.helpers.replaceSymbolWithNumber
140-
* @param string
141-
* @param symbol defaults to `"#"`
166+
* @example
167+
* faker.helpers.replaceSymbolWithNumber() // ''
168+
* faker.helpers.replaceSymbolWithNumber('#####') // '04812'
169+
* faker.helpers.replaceSymbolWithNumber('!####') // '27378'
170+
* faker.helpers.replaceSymbolWithNumber('Your pin is: !####') // '29841'
142171
*/
143172
replaceSymbolWithNumber(string: string = '', symbol: string = '#'): string {
144173
let str = '';
@@ -155,11 +184,20 @@ export class Helpers {
155184
}
156185

157186
/**
158-
* Parses string for symbols (numbers or letters) and replaces them appropriately (# will be replaced with number,
159-
* ? with letter and * will be replaced with number or letter)
187+
* Parses the given string symbol by symbols and replaces the placeholder appropriately.
160188
*
161-
* @method faker.helpers.replaceSymbols
162-
* @param string
189+
* - `#` will be replaced with a digit (`0` - `9`).
190+
* - `?` will be replaced with an upper letter ('A' - 'Z')
191+
* - and `*` will be replaced with either a digit or letter.
192+
*
193+
* @param string The template string to parse.
194+
*
195+
* @example
196+
* faker.helpers.replaceSymbols() // ''
197+
* faker.helpers.replaceSymbols('#####') // '98441'
198+
* faker.helpers.replaceSymbols('?????') // 'ZYRQQ'
199+
* faker.helpers.replaceSymbols('*****') // '4Z3P7'
200+
* faker.helpers.replaceSymbols('Your pin is: #?*#?*') // '0T85L1'
163201
*/
164202
replaceSymbols(string: string = ''): string {
165203
const alpha = [
@@ -209,11 +247,17 @@ export class Helpers {
209247
}
210248

211249
/**
212-
* replace symbols in a credit card schems including Luhn checksum
250+
* Replaces the symbols and patterns in a credit card schema including Luhn checksum.
251+
*
252+
* This method supports both range patterns `[4-9]` as well as the patterns used by `replaceSymbolWithNumber()`.
253+
* `L` will be replaced with the appropriate Luhn checksum.
254+
*
255+
* @param string The credit card format pattern. Defaults to `6453-####-####-####-###L`.
256+
* @param symbol The symbol to replace with a digit.
213257
*
214-
* @method faker.helpers.replaceCreditCardSymbols
215-
* @param string
216-
* @param symbol
258+
* @example
259+
* faker.helpers.replaceCreditCardSymbols() // '6453-4876-8626-8995-3779'
260+
* faker.helpers.replaceCreditCardSymbols('1234-[4-9]-##!!-L') // '1234-9-5298-2'
217261
*/
218262

219263
replaceCreditCardSymbols(
@@ -252,11 +296,15 @@ export class Helpers {
252296
}
253297

254298
/**
255-
* String repeat helper, alternative to String.prototype.repeat.... See PR #382
299+
* Repeats the given string the given number of times.
256300
*
257-
* @method faker.helpers.repeatString
258-
* @param string
259-
* @param num
301+
* @param string The string to repeat.
302+
* @param num The number of times to repeat it. Defaults to `0`.
303+
*
304+
* @example
305+
* faker.helpers.repeatString('Hello world! ') // ''
306+
* faker.helpers.repeatString('Hello world! ', 1) // 'Hello world! '
307+
* faker.helpers.repeatString('Hello world! ', 2) // 'Hello world! Hello world! '
260308
*/
261309
repeatString(string: string, num = 0): string {
262310
let text = '';
@@ -267,12 +315,21 @@ export class Helpers {
267315
}
268316

269317
/**
270-
* parse string patterns in a similar way to RegExp
318+
* Replaces the regex like expressions in the given string with matching values.
319+
*
320+
* Supported patterns:
321+
* - `.{times}` => Repeat the character exactly `times` times.
322+
* - `.{min,max}` => Repeat the character `min` to `max` times.
323+
* - `[min-max]` => Generate a number between min and max (inclusive).
271324
*
272-
* e.g. "#{3}test[1-5]" -> "###test4"
325+
* @param string The template string to to parse.
273326
*
274-
* @method faker.helpers.regexpStyleStringParse
275-
* @param string
327+
* @example
328+
* faker.helpers.regexpStyleStringParse() // ''
329+
* faker.helpers.regexpStyleStringParse('#{5}') // '#####'
330+
* faker.helpers.regexpStyleStringParse('#{2,9}') // '#######'
331+
* faker.helpers.regexpStyleStringParse('[500-15000]') // '8375'
332+
* faker.helpers.regexpStyleStringParse('#{3}test[1-5]') // '###test3'
276333
*/
277334
regexpStyleStringParse(string: string = ''): string {
278335
// Deal with range repeat `{min,max}`
@@ -330,18 +387,22 @@ export class Helpers {
330387
}
331388

332389
/**
333-
* Takes an array and randomizes it in place then returns it
390+
* Takes an array and randomizes it in place then returns it.
334391
*
335-
* Uses the modern version of the Fisher–Yates algorithm
392+
* Uses the modern version of the Fisher–Yates algorithm.
336393
*
337-
* @method faker.helpers.shuffle
338-
* @param o
394+
* @param o The array to shuffle. Defaults to `[]`.
395+
*
396+
* @example
397+
* faker.helpers.shuffle() // []
398+
* faker.helpers.shuffle(['a', 'b', 'c']) // [ 'b', 'c', 'a' ]
339399
*/
340400
shuffle<T>(o?: T[]): T[] {
341401
if (typeof o === 'undefined' || o.length === 0) {
342402
return o || [];
343403
}
344404

405+
// TODO ST-DDT 2022-02-06: This default will never be taken!?
345406
o = o || (['a', 'b', 'c'] as unknown as T[]);
346407
for (let x: T, j: number, i = o.length - 1; i > 0; --i) {
347408
j = this.faker.datatype.number(i);
@@ -354,15 +415,15 @@ export class Helpers {
354415

355416
/**
356417
* Takes an array of strings or function that returns a string
357-
* and outputs a unique array of strings based on that source
418+
* and outputs a unique array of strings based on that source.
419+
* This method does not store the unique state between invocations.
420+
*
421+
* @param source The strings to choose from or a function that generates a string.
422+
* @param length The number of elements to generate.
358423
*
359424
* @example uniqueArray(faker.random.word, 50)
360425
* @example uniqueArray(faker.definitions.name.first_name, 6)
361426
* @example uniqueArray(["Hello", "World", "Goodbye"], 2)
362-
*
363-
* @method faker.helpers.uniqueArray
364-
* @param source
365-
* @param length
366427
*/
367428
uniqueArray<T>(source: T[] | (() => T), length: number): T[] {
368429
if (Array.isArray(source)) {
@@ -385,11 +446,14 @@ export class Helpers {
385446
}
386447

387448
/**
388-
* mustache
449+
* Replaces the `{{placeholder}}` patterns in the given string mustache style.
450+
*
451+
* @param str The template string to parse.
452+
* @param data The data used to populate the placeholders.
453+
* This is a record where the key is the template placeholder,
454+
* whereas the value is either a string or a function suitable for `String.replace()`.
389455
*
390456
* @method faker.helpers.mustache
391-
* @param str
392-
* @param data
393457
*/
394458
mustache(
395459
str: string | undefined,
@@ -413,13 +477,18 @@ export class Helpers {
413477
return str;
414478
}
415479

416-
// TODO @Shinigami92 2022-01-11: We might have a bug with the `phone` definition
417-
// I may be `phone_number` instead
418-
419480
/**
420-
* createCard
481+
* Generates a full card with various random details.
421482
*
422-
* @method faker.helpers.createCard
483+
* @example
484+
* faker.helpers.createCard()
485+
* // {
486+
* // name: 'Maxine Abbott',
487+
* // username: 'Idell_Kautzer60',
488+
* // email: 'Nora_Bruen@hotmail.com',
489+
* // address: {
490+
* // streetA: 'Drake Avenue',
491+
* // ...
423492
*/
424493
createCard(): Card {
425494
return {
@@ -476,9 +545,17 @@ export class Helpers {
476545
}
477546

478547
/**
479-
* contextualCard
548+
* Generates a persons card with various details attempting to use a consistent context.
480549
*
481-
* @method faker.helpers.contextualCard
550+
* @example
551+
* faker.helpers.contextualCard()
552+
* // {
553+
* // name: 'Eveline',
554+
* // username: 'Eveline.Brekke56',
555+
* // avatar: 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/122.jpg',
556+
* // email: 'Eveline.Brekke56.Hoppe@yahoo.com',
557+
* // dob: 1964-05-06T05:14:37.874Z,
558+
* // ...
482559
*/
483560
contextualCard(): ContextualCard {
484561
const name = this.faker.name.firstName();
@@ -515,9 +592,17 @@ export class Helpers {
515592
}
516593

517594
/**
518-
* userCard
595+
* Generates a user card with various details.
519596
*
520-
* @method faker.helpers.userCard
597+
* @example
598+
* faker.helpers.userCard()
599+
* // {
600+
* // name: 'Jodi Ferry',
601+
* // username: 'Maybell.Kris',
602+
* // email: 'Zoey_Lubowitz@yahoo.com',
603+
* // address: {
604+
* // street: 'McKenzie Estates',
605+
* // ....
521606
*/
522607
userCard(): UserCard {
523608
return {
@@ -545,9 +630,18 @@ export class Helpers {
545630
}
546631

547632
/**
548-
* createTransaction
633+
* Generates an example transaction.
549634
*
550-
* @method faker.helpers.createTransaction
635+
* @example
636+
* faker.helpers.createTransaction()
637+
* // {
638+
* // amount: '551.32',
639+
* // date: 2012-02-01T23:00:00.000Z,
640+
* // business: 'Will, Fisher and Marks',
641+
* // name: 'Investment Account (...8755)',
642+
* // type: 'invoice',
643+
* // account: '41796240'
644+
* // }
551645
*/
552646
createTransaction(): Transaction {
553647
return {
@@ -562,11 +656,3 @@ export class Helpers {
562656
};
563657
}
564658
}
565-
566-
/*
567-
String.prototype.capitalize = function () { //v1.0
568-
return this.replace(/\w+/g, function (a) {
569-
return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
570-
});
571-
};
572-
*/

0 commit comments

Comments
 (0)