Skip to content
On this page

Fake

fake

Deprecated

This method is deprecated and will be removed in a future version.

Generator for combining faker methods based on a static string input.

Note: We recommend using string template literals instead of fake(), which are faster and strongly typed (if you are using TypeScript), e.g. const address = `${faker.address.zipCode()} ${faker.address.city()}`;

This method is useful if you have to build a random string from a static, non-executable source (e.g. string coming from a user, stored in a database or a file).

It checks the given string for placeholders and replaces them by calling faker methods:

js
const hello = faker.fake('Hi, my name is {{name.firstName}} {{name.lastName}}!')

This would use the faker.name.firstName() and faker.name.lastName() method to resolve the placeholders respectively.

It is also possible to provide parameters. At first, they will be parsed as json, and if that isn't possible, we will fall back to string:

js
const message = faker.fake(`You can call me at {{phone.number(+!# !## #### #####!)}}.')

Currently it is not possible to set more than a single parameter.

It is also NOT possible to use any non-faker methods or plain javascript in such templates.

Available since v3.0.0

Parameters

NameTypeDefaultDescription
strstring

The template string that will get interpolated. Must not be empty.

Returns: string

ts
faker.fake(str: string): string
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.number(!## ### #####!)}}.') // 'You can call me at 202 555 973722.'
faker.fake('I flipped the coin and got: {{helpers.arrayElement(["heads", "tails"])}}') // 'I flipped the coin and got: tails'

Released under the MIT License.