Returns a subset with random elements of the given array in random order.
The type of the elements to pick from.
Array to pick the value from.
Optional
count: number | { max: number; min: number }Number or range 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.
The maximum number of elements to pick.
The minimum number of elements to pick.
Returns a random value from an Enum object.
This does the same as objectValue
except that it ignores (the values assigned to) the numeric keys added for TypeScript enums.
Type of generic enums, automatically inferred by TypeScript.
Enum to pick the value from.
enum Color { Red, Green, Blue }
faker.helpers.enumValue(Color) // 1 (Green)
enum Direction { North = 'North', South = 'South'}
faker.helpers.enumValue(Direction) // 'South'
enum HttpStatus { Ok = 200, Created = 201, BadRequest = 400, Unauthorized = 401 }
faker.helpers.enumValue(HttpStatus) // 200 (Ok)
Generates a string matching the given regex like expressions.
This function doesn't provide full support of actual RegExp
.
Features such as grouping, anchors and character classes are not supported.
If you are looking for a library that randomly generates strings based on
RegExp
s, see randexp.js
Supported patterns:
x{times}
=> Repeat the x
exactly times
times.x{min,max}
=> Repeat the x
min
to max
times.[x-y]
=> Randomly get a character between x
and y
(inclusive).[x-y]{times}
=> Randomly get a character between x
and y
(inclusive) and repeat it times
times.[x-y]{min,max}
=> Randomly get a character between x
and y
(inclusive) and repeat it min
to max
times.[^...]
=> Randomly get an ASCII number or letter character that is not in the given range. (e.g. [^0-9]
will get a random non-numeric character).[-...]
=> Include dashes in the range. Must be placed after the negate character ^
and before any character sets if used (e.g. [^-0-9]
will not get any numeric characters or dashes)./[x-y]/i
=> Randomly gets an uppercase or lowercase character between x
and y
(inclusive).x?
=> Randomly decide to include or not include x
.[x-y]?
=> Randomly decide to include or not include characters between x
and y
(inclusive).x*
=> Repeat x
0 or more times.[x-y]*
=> Repeat characters between x
and y
(inclusive) 0 or more times.x+
=> Repeat x
1 or more times.[x-y]+
=> Repeat characters between x
and y
(inclusive) 1 or more times..
=> returns a wildcard ASCII character that can be any number, character or symbol. Can be combined with quantifiers as well.The template string/RegExp to generate a matching string for.
faker.helpers.fromRegExp('#{5}') // '#####'
faker.helpers.fromRegExp('#{2,9}') // '#######'
faker.helpers.fromRegExp('[1-7]') // '5'
faker.helpers.fromRegExp('#{3}test[1-5]') // '###test3'
faker.helpers.fromRegExp('[0-9a-dmno]') // '5'
faker.helpers.fromRegExp('[^a-zA-Z0-8]') // '9'
faker.helpers.fromRegExp('[a-d0-6]{2,8}') // 'a0dc45b0'
faker.helpers.fromRegExp('[-a-z]{5}') // 'a-zab'
faker.helpers.fromRegExp(/[A-Z0-9]{4}-[A-Z0-9]{4}/) // 'BS4G-485H'
faker.helpers.fromRegExp(/[A-Z]{5}/i) // 'pDKfh'
faker.helpers.fromRegExp(/.{5}/) // '14(#B'
faker.helpers.fromRegExp(/Joh?n/) // 'Jon'
faker.helpers.fromRegExp(/ABC*DE/) // 'ABDE'
faker.helpers.fromRegExp(/bee+p/) // 'beeeeeeeep'
Returns the result of the callback if the probability check was successful, otherwise undefined
.
The type of result of the given callback.
The callback to that will be invoked if the probability check was successful.
Optional
options: { probability?: number }The options to use.
Optional
probability?: numberThe probability ([0.00, 1.00]
) of the callback being invoked.
Generates an array containing values returned by the given method.
The type of elements.
The method used to generate the values.
The method will be called with (_, index)
, to allow using the index in the generated value e.g. as id.
Optional
options: { count?: number | { max: number; min: number } }The optional options object.
Optional
count?: number | { max: number; min: number }The number or range of elements to generate.
faker.helpers.multiple(() => faker.person.firstName()) // [ 'Aniya', 'Norval', 'Dallin' ]
faker.helpers.multiple(() => faker.person.firstName(), { count: 3 }) // [ 'Santos', 'Lavinia', 'Lavinia' ]
faker.helpers.multiple((_, i) => `${faker.color.human()}-${i + 1}`) // [ 'orange-1', 'orchid-2', 'sky blue-3' ]
Replaces the {{placeholder}}
patterns in the given string mustache style.
The template string to parse.
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 String.replace()
.
Helper method that converts the given number or range to a number.
The number or range to convert.
The maximum value for the range.
The minimum value for the range.
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.
Optional
string: stringThe credit card format pattern. Defaults to '6453-####-####-####-###L'
.
Optional
symbol: stringThe symbol to replace with a digit. Defaults to '#'
.
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')*
will be replaced with either a digit or letter.Optional
string: stringThe template string to parse. Defaults to ''
.
Takes an array and randomizes it in place then returns it.
The type of the elements to shuffle.
The array to shuffle.
The options to use when shuffling.
Whether to shuffle the array in place or return a new array.
Returns a randomized version of the array.
The type of the elements to shuffle.
The array to shuffle.
Optional
options: { inplace?: false }The options to use when shuffling.
Optional
inplace?: falseWhether to shuffle the array in place or return a new array.
Returns a randomized version of the array.
The type of the elements to shuffle.
The array to shuffle.
Optional
options: { inplace?: boolean }The options to use when shuffling.
Optional
inplace?: booleanWhether to shuffle the array in place or return a new array.
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.
Optional
string: stringThe input to slugify. Defaults to ''
.
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.
If there are not enough unique values to satisfy the length, if the source is an array, it will only return as many items as are in the array. If the source is a function, it will return after a maximum number of attempts has been reached.
The type of the elements.
faker.helpers.uniqueArray(faker.word.sample, 3) // ['mob', 'junior', 'ripe']
faker.helpers.uniqueArray(faker.definitions.person.first_name.generic, 6) // ['Silas', 'Montana', 'Lorenzo', 'Alayna', 'Aditya', 'Antone']
faker.helpers.uniqueArray(["Hello", "World", "Goodbye"], 2) // ['World', 'Goodbye']
Returns a weighted random element from the given array. Each element of the array should be an object with two keys weight
and value
.
weight
key should be a number representing the probability of selecting the value, relative to the sum of the weights. Weights can be any positive float or integer.value
key should be the corresponding value.For example, if there are two values A and B, with weights 1 and 2 respectively, then the probability of picking A is 1/3 and the probability of picking B is 2/3.
The type of the elements to pick from.
Array to pick the value from.
Module with various helper methods providing basic (seed-dependent) operations useful for implementing faker methods (without methods requiring localized data).