Datatype
Module to generate various primitive values and data types.
Overview
Most of the methods in this module are deprecated and have been moved to other modules like faker.number
and faker.string
, see individual entries for replacements.
For a simple random true or false value, use boolean()
.
array
Deprecated
This method is deprecated and will be removed in a future version.
Use your own function to build complex arrays.
Returns an array with random strings and numbers.
Available since v5.5.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
length | number | { ... } | 10 | Size of the returned array. |
length.max | number | The maximum size of the array. | |
length.min | number | The minimum size of the array. |
Returns: Array<number | string>
faker.datatype.array(length: number | {
max: number,
min: number
} = 10): (string | number)[]
faker.datatype.array() // [ 94099, 85352, 'Hz%T.C\\l;8', '|#gmtw3otS', '2>:rJ|3$&d', 56864, 'Ss2-p0RXSI', 51084, 2039, 'mNEU[.r0Vf' ]
faker.datatype.array(3) // [ 61845, 'SK7H$W3:d*', 'm[%7N8*GVK' ]
faker.datatype.array({ min: 3, max: 5 }) // [ 99403, 76924, 42281, "Q'|$&y\\G/9" ]
Source
bigInt
Deprecated
This method is deprecated and will be removed in a future version.
Use faker.number.bigInt()
instead.
Returns a BigInt number.
Available since v6.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options? | bigint | boolean | number | string | { ... } | Maximum value or options object. | |
options.max? | bigint | boolean | number | string | min + 999999999999999n | Upper bound for generated bigint. |
options.min? | bigint | boolean | number | string | 0n | Lower bound for generated bigint. |
Returns: bigint
Throws: When options define max < min
.
faker.datatype.bigInt(options?: bigint | boolean | number | string | {
max: bigint | boolean | number | string,
min: bigint | boolean | number | string
}): bigint
faker.datatype.bigInt() // 55422n
faker.datatype.bigInt(100n) // 52n
faker.datatype.bigInt({ min: 1000000n }) // 431433n
faker.datatype.bigInt({ max: 100n }) // 42n
faker.datatype.bigInt({ min: 10n, max: 100n }) // 36n
Source
boolean
Returns the boolean value true or false.
Note:
A probability of 0.75
results in true
being returned 75%
of the calls; likewise 0.3
=> 30%
.
If the probability is <= 0.0
, it will always return false
.
If the probability is >= 1.0
, it will always return true
.
The probability is limited to two decimal places.
Available since v5.5.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | number | { ... } | {} | The optional options object or the probability ( |
options.probability? | number | 0.5 | The probability ( |
Returns: boolean
faker.datatype.boolean(options: number | {
probability: number
} = {}): boolean
faker.datatype.boolean() // false
faker.datatype.boolean(0.9) // true
faker.datatype.boolean({ probability: 0.1 }) // false
Source
datetime
Deprecated
This method is deprecated and will be removed in a future version.
Use faker.date.between({ from: min, to: max })
or faker.date.anytime()
instead.
Returns a Date object using a random number of milliseconds since the Unix Epoch (1 January 1970 UTC).
Available since v5.5.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | number | { ... } | {} | Max number of milliseconds since unix epoch or options object. |
options.max? | number | 4102444800000 | Upper bound for milliseconds since base date. When not provided or larger than |
options.min? | number | 631152000000 | Lower bound for milliseconds since base date. When not provided or smaller than |
Returns: Date
faker.datatype.datetime(options: number | {
max: number,
min: number
} = {}): Date
faker.datatype.datetime() // '2089-04-17T18:03:24.956Z'
faker.datatype.datetime(1893456000000) // '2022-03-28T07:00:56.876Z'
faker.datatype.datetime({ min: 1577836800000, max: 1893456000000 }) // '2021-09-12T07:13:00.255Z'
See Also
Source
float
Deprecated
This method is deprecated and will be removed in a future version.
Use faker.number.float()
instead.
Returns a single random floating-point number for the given precision or range and precision.
Available since v5.5.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | number | { ... } | {} | Precision or options object. |
options.max? | number | min + 99999 | Upper bound for generated number. |
options.min? | number | 0 | Lower bound for generated number. |
options.precision? | number | 0.01 | Precision of the generated number. |
Returns: number
Throws: When min
is greater than max
.
When precision
is negative.
faker.datatype.float(options: number | {
max: number,
min: number,
precision: number
} = {}): number
faker.datatype.float() // 51696.36
faker.datatype.float(0.1) // 52023.2
faker.datatype.float({ min: 1000000 }) // 212859.76
faker.datatype.float({ max: 100 }) // 28.11
faker.datatype.float({ precision: 0.1 }) // 84055.3
faker.datatype.float({ min: 10, max: 100, precision: 0.001 }) // 57.315
Source
hexadecimal
Deprecated
This method is deprecated and will be removed in a future version.
Use faker.string.hexadecimal()
or faker.number.hex()
instead.
Returns a hexadecimal number.
Available since v6.1.2
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | The optional options object. |
options.case? | 'lower' | 'mixed' | 'upper' | 'mixed' | Case of the generated number. |
options.length? | number | 1 | Length of the generated number. |
options.prefix? | string | '0x' | Prefix for the generated number. |
Returns: string
faker.datatype.hexadecimal(options: {
case: 'lower' | 'mixed' | 'upper',
length: number,
prefix: string
} = {}): string
faker.datatype.hexadecimal() // '0xB'
faker.datatype.hexadecimal({ length: 10 }) // '0xaE13d044cB'
faker.datatype.hexadecimal({ prefix: '0x' }) // '0xE'
faker.datatype.hexadecimal({ case: 'lower' }) // '0xf'
faker.datatype.hexadecimal({ length: 10, prefix: '#' }) // '#f12a974eB1'
faker.datatype.hexadecimal({ length: 10, case: 'upper' }) // '0xE3F38014FB'
faker.datatype.hexadecimal({ prefix: '', case: 'lower' }) // 'd'
faker.datatype.hexadecimal({ length: 10, prefix: '0x', case: 'mixed' }) // '0xAdE330a4D1'
See Also
Source
json
Deprecated
This method is deprecated and will be removed in a future version.
Build your own function to generate complex objects.
Returns a string representing JSON object with 7 pre-defined properties.
Available since v5.5.0
Returns: string
faker.datatype.json(): string
faker.datatype.json() // `{"foo":"mxz.v8ISij","bar":29154,"bike":8658,"a":"GxTlw$nuC:","b":40693,"name":"%'<FTou{7X","prop":"X(bd4iT>77"}`
Source
number
Deprecated
This method is deprecated and will be removed in a future version.
Use faker.number.int()
or faker.number.float()
instead.
Returns a single random number between zero and the given max value or the given range with the specified precision. The bounds are inclusive.
Available since v5.5.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | number | { ... } | 99999 | Maximum value or options object. |
options.max? | number | min + 99999 | Upper bound for generated number. |
options.min? | number | 0 | Lower bound for generated number. |
options.precision? | number | 1 | Precision of the generated number. |
Returns: number
Throws: When min
is greater than max
.
When precision
is negative.
faker.datatype.number(options: number | {
max: number,
min: number,
precision: number
} = 99999): number
faker.datatype.number() // 55422
faker.datatype.number(100) // 52
faker.datatype.number({ min: 1000000 }) // 1031433
faker.datatype.number({ max: 100 }) // 42
faker.datatype.number({ precision: 0.01 }) // 64246.18
faker.datatype.number({ min: 10, max: 100, precision: 0.01 }) // 36.94
See Also
Source
string
Deprecated
This method is deprecated and will be removed in a future version.
Use faker.string.sample()
instead.
Returns a string containing UTF-16 chars between 33 and 125 (!
to }
).
Available since v5.5.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | number | { ... } | {} | Length of the generated string or an options object. |
options.length? | number | 10 | Length of the generated string. Max length is |
Returns: string
faker.datatype.string(options: number | {
length: number
} = {}): string
faker.datatype.string() // 'Zo!.:*e>wR'
faker.datatype.string(5) // '6Bye8'
faker.datatype.string({ length: 7 }) // 'dzOT00e'
Source
uuid
Deprecated
This method is deprecated and will be removed in a future version.
Use faker.string.uuid()
instead.
Returns a UUID v4 (Universally Unique Identifier).
Available since v5.5.0
Returns: string
faker.datatype.uuid(): string
faker.datatype.uuid() // '4136cd0b-d90b-4af7-b485-5d1ded8db252'