Helpers
Module with various helper methods that transform the method input rather than returning values from locales. The transformation process may call methods that use the locale data.
- Array Element
- Array Elements
- Contextual Card
- Create Card
- Create Transaction
- Maybe
- Mustache
- Object Key
- Object Value
- Randomize
- Regexp Style String Parse
- Repeat String
- Replace Credit Card Symbols
- Replace Symbol With Number
- Replace Symbols
- Shuffle
- Slugify
- Unique Array
- User Card
Array Element
Returns random element from the given array.
Parameters
Name | Type | Default | Description |
---|---|---|---|
<T> | The type of the entries to pick from. | ||
array | readonly T[] | ['a', 'b', 'c'] | Array to pick the value from. |
Returns: T
faker.helpers.arrayElement<T>(array: readonly T[] = ['a', 'b', 'c']): T
faker.helpers.arrayElement() // => "b"
faker.helpers.arrayElement(['cat', 'dog', 'mouse']) // 'dog'
Array Elements
Returns a subset with random elements of the given array in random order.
Parameters
Name | Type | Default | Description |
---|---|---|---|
<T> | The type of the entries to pick from. | ||
array | readonly T[] | ['a', 'b', 'c'] | Array to pick the value from. |
count? | number | Number of elements to pick. When not provided, random number of elements will be picked. When value exceeds array boundaries, it will be limited to stay inside. |
Returns: T[]
faker.helpers.arrayElements<T>(array: readonly T[] = ['a', 'b', 'c'], count?: number): T[]
faker.helpers.arrayElements() // => ["c","b"]
faker.helpers.arrayElements(['cat', 'dog', 'mouse']) // ['mouse', 'cat']
faker.helpers.arrayElements([1, 2, 3, 4, 5], 2) // [4, 2]
Contextual Card
Deprecated
This method is deprecated and will be removed in a future version.
Generates a persons card with various details attempting to use a consistent context.
Returns: ContextualCard
faker.helpers.contextualCard(): ContextualCard
faker.helpers.contextualCard() // => {"name":"Katelynn","username":"Katelynn_Medhurs...
faker.helpers.contextualCard()
// {
// name: 'Eveline',
// username: 'Eveline.Brekke56',
// avatar: 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/122.jpg',
// email: 'Eveline.Brekke56.Hoppe@yahoo.com',
// dob: 1964-05-06T05:14:37.874Z,
// ...
Create Card
Deprecated
This method is deprecated and will be removed in a future version.
Generates a full card with various random details.
Returns: Card
faker.helpers.createCard(): Card
faker.helpers.createCard() // => {"name":"Kelvin Sporer","username":"Leatha_Stie...
faker.helpers.createCard()
// {
// name: 'Maxine Abbott',
// username: 'Idell_Kautzer60',
// email: 'Nora_Bruen@hotmail.com',
// address: {
// streetA: 'Drake Avenue',
// ...
Create Transaction
Generates an example transaction.
Returns: Transaction
faker.helpers.createTransaction(): Transaction
faker.helpers.createTransaction() // => {"amount":"548.81","date":"2012-02-02T00:00:00....
faker.helpers.createTransaction()
// {
// amount: '551.32',
// date: 2012-02-01T23:00:00.000Z,
// business: 'Will, Fisher and Marks',
// name: 'Investment Account (...8755)',
// type: 'invoice',
// account: '41796240'
// }
Maybe
Returns the result of the callback if the probability check was successful, otherwise undefined
.
Parameters
Name | Type | Default | Description |
---|---|---|---|
<T> | The type of result of the given callback. | ||
callback | () => T | The callback to that will be invoked if the probability check was successful. | |
options | { ... } | {} | The options to use. |
options.probability? | number | 0.5 | The probability ( |
Returns: T
faker.helpers.maybe<T>(callback: () => T, options: {
probability: number
} = {}): T
faker.helpers.maybe(() => 'Hello World!') // 'Hello World!'
faker.helpers.maybe(() => 'Hello World!', { probability: 0.1 }) // undefined
faker.helpers.maybe(() => 'Hello World!', { probability: 0.9 }) // 'Hello World!'
Mustache
Replaces the {{placeholder}}
patterns in the given string mustache style.
Parameters
Name | Type | Default | Description |
---|---|---|---|
str | string | The template string to parse. | |
data | Record<string, (substring: string, args: any[]) => string | string> | The data used to populate the placeholders.
This is a record where the key is the template placeholder,
whereas the value is either a string or a function suitable for |
Returns: string
faker.helpers.mustache(str: string, data: Record<string, (substring: string, args: any[]) => string | string>): string
faker.helpers.mustache() // => ""
faker.helpers.mustache('I found {{count}} instances of "{{word}}".', {
count: () => `${faker.datatype.number()}`,
word: "this word",
}) // 'I found 57591 instances of "this word".'
Object Key
Returns a random key from given object or undefined
if no key could be found.
Parameters
Name | Type | Default | Description |
---|---|---|---|
<T> | Record<string, unknown> | Missing | |
object | T | The object to be used. |
Returns: keyof T
faker.helpers.objectKey<T>(object: T): keyof T
faker.helpers.objectKey({ myProperty: 'myValue' }) // 'myProperty'
Object Value
Returns a random value from given object or undefined
if no key could be found.
Parameters
Name | Type | Default | Description |
---|---|---|---|
<T> | Record<string, unknown> | Missing | |
object | T | The object to be used. |
Returns: T[keyof T]
faker.helpers.objectValue<T>(object: T): T[keyof T]
faker.helpers.objectValue({ myProperty: 'myValue' }) // 'myValue'
Randomize
Deprecated
This method is deprecated and will be removed in a future version.
Backward-compatibility. Use faker.helpers.arrayElement()
instead.
Takes an array and returns a random element of the array.
Parameters
Name | Type | Default | Description |
---|---|---|---|
<T> | The type of the entries to pick from. | ||
array | readonly T[] | ['a', 'b', 'c'] | The array to select an element from. |
Returns: T
faker.helpers.randomize<T>(array: readonly T[] = ['a', 'b', 'c']): T
faker.helpers.randomize() // => "b"
faker.helpers.randomize() // 'c'
faker.helpers.randomize([1, 2, 3]) // '2'
See Also
Regexp Style String Parse
Replaces the regex like expressions in the given string with matching values.
Supported patterns:
.{times}
=> Repeat the character exactlytimes
times..{min,max}
=> Repeat the charactermin
tomax
times.[min-max]
=> Generate a number between min and max (inclusive).
Parameters
Name | Type | Default | Description |
---|---|---|---|
string | string | '' | The template string to to parse. |
Returns: string
faker.helpers.regexpStyleStringParse(string: string = ''): string
faker.helpers.regexpStyleStringParse() // => ""
faker.helpers.regexpStyleStringParse() // ''
faker.helpers.regexpStyleStringParse('#{5}') // '#####'
faker.helpers.regexpStyleStringParse('#{2,9}') // '#######'
faker.helpers.regexpStyleStringParse('[500-15000]') // '8375'
faker.helpers.regexpStyleStringParse('#{3}test[1-5]') // '###test3'
Repeat String
Repeats the given string the given number of times.
Parameters
Name | Type | Default | Description |
---|---|---|---|
string | string | '' | The string to repeat. |
num | number | 0 | The number of times to repeat it. |
Returns: string
faker.helpers.repeatString(string: string = '', num: number = 0): string
faker.helpers.repeatString() // => ""
faker.helpers.repeatString('Hello world! ') // ''
faker.helpers.repeatString('Hello world! ', 1) // 'Hello world! '
faker.helpers.repeatString('Hello world! ', 2) // 'Hello world! Hello world! '
Replace Credit Card Symbols
Replaces the symbols and patterns in a credit card schema including Luhn checksum.
This method supports both range patterns [4-9]
as well as the patterns used by replaceSymbolWithNumber()
.
L
will be replaced with the appropriate Luhn checksum.
Parameters
Name | Type | Default | Description |
---|---|---|---|
string | string | '6453-####-####-####-###L' | The credit card format pattern. |
symbol | string | '#' | The symbol to replace with a digit. |
Returns: string
faker.helpers.replaceCreditCardSymbols(string: string = '6453-####-####-####-###L', symbol: string = '#'): string
faker.helpers.replaceCreditCardSymbols() // => "6453-5578-6858-4663-4287"
faker.helpers.replaceCreditCardSymbols() // '6453-4876-8626-8995-3779'
faker.helpers.replaceCreditCardSymbols('1234-[4-9]-##!!-L') // '1234-9-5298-2'
Replace Symbol With Number
Parses the given string symbol by symbol and replaces the placeholders with digits (0
- 9
).
!
will be replaced by digits >=2 (2
- 9
).
Parameters
Name | Type | Default | Description |
---|---|---|---|
string | string | '' | The template string to parse. |
symbol | string | '#' | The symbol to replace with digits. |
Returns: string
faker.helpers.replaceSymbolWithNumber(string: string = '', symbol: string = '#'): string
faker.helpers.replaceSymbolWithNumber() // => ""
faker.helpers.replaceSymbolWithNumber() // ''
faker.helpers.replaceSymbolWithNumber('#####') // '04812'
faker.helpers.replaceSymbolWithNumber('!####') // '27378'
faker.helpers.replaceSymbolWithNumber('Your pin is: !####') // '29841'
Replace Symbols
Parses the given string symbol by symbols and replaces the placeholder appropriately.
#
will be replaced with a digit (0
-9
).?
will be replaced with an upper letter ('A' - 'Z')- and
*
will be replaced with either a digit or letter.
Parameters
Name | Type | Default | Description |
---|---|---|---|
string | string | '' | The template string to parse. |
Returns: string
faker.helpers.replaceSymbols(string: string = ''): string
faker.helpers.replaceSymbols() // => ""
faker.helpers.replaceSymbols() // ''
faker.helpers.replaceSymbols('#####') // '98441'
faker.helpers.replaceSymbols('?????') // 'ZYRQQ'
faker.helpers.replaceSymbols('*****') // '4Z3P7'
faker.helpers.replaceSymbols('Your pin is: #?*#?*') // '0T85L1'
Shuffle
Takes an array and randomizes it in place then returns it.
Uses the modern version of the Fisher–Yates algorithm.
Parameters
Name | Type | Default | Description |
---|---|---|---|
<T> | The type of the entries to shuffle. | ||
o? | T[] | [] | The array to shuffle. |
Returns: T[]
faker.helpers.shuffle<T>(o?: T[] = []): T[]
faker.helpers.shuffle() // => []
faker.helpers.shuffle() // []
faker.helpers.shuffle(['a', 'b', 'c']) // [ 'b', 'c', 'a' ]
Slugify
Slugifies the given string.
For that all spaces (
) are replaced by hyphens (-
)
and most non word characters except for dots and hyphens will be removed.
Parameters
Name | Type | Default | Description |
---|---|---|---|
string | string | '' | The input to slugify. |
Returns: string
faker.helpers.slugify(string: string = ''): string
faker.helpers.slugify() // => ""
faker.helpers.slugify() // ''
faker.helpers.slugify("Hello world!") // 'Hello-world'
Unique Array
Takes an array of strings or function that returns a string and outputs a unique array of strings based on that source. This method does not store the unique state between invocations.
Parameters
Name | Type | Default | Description |
---|---|---|---|
<T> | The type of the entries. | ||
source | () => T | readonly T[] | The strings to choose from or a function that generates a string. | |
length | number | The number of elements to generate. |
Returns: T[]
faker.helpers.uniqueArray<T>(source: () => T | readonly T[], length: number): T[]
faker.helpers.uniqueArray() // => []
faker.helpers.uniqueArray(faker.random.word, 50)
faker.helpers.uniqueArray(faker.definitions.name.first_name, 6)
faker.helpers.uniqueArray(["Hello", "World", "Goodbye"], 2)
User Card
Deprecated
This method is deprecated and will be removed in a future version.
Generates a user card with various details.
Returns: UserCard
faker.helpers.userCard(): UserCard
faker.helpers.userCard() // => {"name":"Kelvin Sporer","username":"Leatha_Stie...
faker.helpers.userCard()
// {
// name: 'Jodi Ferry',
// username: 'Maybell.Kris',
// email: 'Zoey_Lubowitz@yahoo.com',
// address: {
// street: 'McKenzie Estates',
// ....