blob: 6bcc4f83aee925e9b73ab9ba92a2fa61a51c5c4c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
import { SimpleModuleBase } from '../../internal/module-base';
/**
* Module to generate boolean values.
*
* ### Overview
*
* For a simple random true or false value, use [`boolean()`](https://fakerjs.dev/api/datatype.html#boolean).
*/
export class DatatypeModule extends SimpleModuleBase {
/**
* 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.
*
* @param options The optional options object or the probability (`[0.00, 1.00]`) of returning `true`.
* @param options.probability The probability (`[0.00, 1.00]`) of returning `true`. Defaults to `0.5`.
*
* @example
* faker.datatype.boolean() // false
* faker.datatype.boolean(0.9) // true
* faker.datatype.boolean({ probability: 0.1 }) // false
*
* @since 5.5.0
*/
boolean(
options:
| number
| {
/**
* The probability (`[0.00, 1.00]`) of returning `true`.
*
* @default 0.5
*/
probability?: number;
} = {}
): boolean {
if (typeof options === 'number') {
options = {
probability: options,
};
}
const { probability = 0.5 } = options;
if (probability <= 0) {
return false;
}
if (probability >= 1) {
// This check is required to avoid returning false when float() returns 1
return true;
}
return this.faker.number.float() < probability;
}
}
|