Faker
Index
Constructors
constructor
Parameters
opts: FakerOptions
Returns Faker
Properties
readonlyaddress
readonlyanimal
readonlycolor
readonlycommerce
readonlycompany
readonlydatabase
readonlydatatype
readonlydate
readonlydefinitions
readonlyfake
Type declaration
Parameters
str: string
The template string that will get interpolated. Must not be empty.
Returns string
readonlyfinance
readonlygit
readonlyhacker
readonlyhelpers
readonlyimage
readonlyinternet
locales
readonlylorem
readonlymersenne
readonlymusic
readonlyname
readonlyphone
readonlyrandom
readonlyscience
readonlysystem
readonlyunique
Type declaration
Generates a unique result using the results of the given method. Used unique entries will be stored internally and filtered from subsequent calls.
Type parameters
- Method: (...parameters: any[]) => RecordKey
The type of the method to execute.
Parameters
method: Method
The method used to generate the values.
optionalargs: Parameters<Method>
The arguments used to call the method.
optionaloptions: { compare?: (obj: Record<RecordKey, RecordKey>, key: RecordKey) => 0 | -1; currentIterations?: number; exclude?: RecordKey | RecordKey[]; maxRetries?: number; maxTime?: number; startTime?: number; store?: Record<RecordKey, RecordKey> }
The optional options used to configure this method.
optionalcompare: (obj: Record<RecordKey, RecordKey>, key: RecordKey) => 0 | -1
optionalcurrentIterations: number
This parameter does nothing.
optionalexclude: RecordKey | RecordKey[]
The value or values that should be excluded/skipped. Defaults to
[]
.optionalmaxRetries: number
The total number of attempts to try before throwing an error. Defaults to
50
.optionalmaxTime: number
The time in milliseconds this method may take before throwing an error. Defaults to
50
.optionalstartTime: number
This parameter does nothing.
optionalstore: Record<RecordKey, RecordKey>
The store of unique entries. Defaults to a global store.
Returns ReturnType<Method>
- Method: (...parameters: any[]) => RecordKey
readonlyvehicle
readonlyword
Accessors
locale
Returns UsableLocale
Parameters
locale: UsableLocale
Returns void
localeFallback
Returns UsableLocale
Parameters
localeFallback: UsableLocale
Returns void
Methods
seed
Sets the seed or generates a new one.
Please note that generated values are dependent on both the seed and the number of calls that have been made since it was set.
This method is intended to allow for consistent values in a tests, so you might want to use hardcoded values as the seed.
In addition to that it can be used for creating truly random tests (by passing no arguments), that still can be reproduced if needed, by logging the result and explicitly setting it if needed.
Parameters
optionalseed: number
The seed to use. Defaults to a random number.
Returns number
The seed that was set.
setLocale
Set Faker's locale
Parameters
locale: UsableLocale
The locale to set (e.g.
en
oren_AU
,en_AU_ocker
).
Returns void
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:
This would use the
faker.name.firstName()
andfaker.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:
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.
3.0.0
Use faker.helpers.fake() instead.