Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update fake jsdocs #406

Merged
merged 7 commits into from
Feb 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/fake.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Faker } from '.';

/**
* Generator method for combining faker methods based on string input
* Generator method for combining faker methods based on string input.
*/
export class Fake {
constructor(private readonly faker: Faker) {
Expand All @@ -15,21 +15,30 @@ export class Fake {
}

/**
* Generator method for combining faker methods based on string input
* Generator method for combining faker methods based on string input.
*
* __Example:__
* This will check the given string for placeholders and replace them by calling the specified faker method.
* E.g. the input `Hi, my name is {{name.firstName}}!`,
* will use the `faker.name.firstName()` method to resolve the placeholder.
* It is also possible to combine static text with placeholders,
* since only the parts inside the double braces `{{placeholder}}` are replaced.
* The replacement process is repeated until all placeholders have been replaced by static text.
* It is also possible to provide the called method with additional parameters by adding parentheses.
* This method will first attempt to parse the parameters as json, if that isn't possible it will use them as string.
* E.g. `You can call me at {{phone.phoneNumber(+!# !## #### #####!)}}.`
* Currently it isn't possible to set more than a single parameter this way.
*
* ```
* console.log(faker.fake('{{name.lastName}}, {{name.firstName}} {{name.suffix}}'));
* // outputs: "Marks, Dean Sr."
* ```
* Please note that is NOT possible to use any non-faker methods or plain js script in there.
*
* This will interpolate the format string with the value of methods
* [name.lastName]{@link faker.name.lastName}, [name.firstName]{@link faker.name.firstName},
* and [name.suffix]{@link faker.name.suffix}
* @param str The format string that will get interpolated.
*
* @method faker.fake
* @param str
* @example
* faker.fake('{{name.lastName}}') // 'Barrows'
* faker.fake('{{name.lastName}}, {{name.firstName}} {{name.suffix}}') // 'Durgan, Noe MD'
* faker.fake('This is static test.') // 'This is static test.'
* faker.fake('Good Morning {{name.firstName}}!') // 'Good Morning Estelle!'
* faker.fake('You can call me at {{phone.phoneNumber(!## ### #####!)}}.') // 'You can call me at 202 555 973722.'
* faker.fake('I flipped the coin an got: {{random.arrayElement(["heads", "tails"])}}') // 'I flipped the coin an got: tails'
*/
fake(str: string): string {
// setup default response as empty string
Expand Down