aboutsummaryrefslogtreecommitdiff
path: root/src/modules
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2022-11-03 20:37:16 +0100
committerGitHub <[email protected]>2022-11-03 20:37:16 +0100
commit838f8369196baabb28529470c27125756e6f671e (patch)
treec59bc433a2d44a13c7bc3515aa472f66fe97b715 /src/modules
parent4204a6ae925a4e0c761ece10ad21222dac7a9e7e (diff)
downloadfaker-838f8369196baabb28529470c27125756e6f671e.tar.xz
faker-838f8369196baabb28529470c27125756e6f671e.zip
feat(datatype): introduce probability option to boolean (#1476)
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/datatype/index.ts28
-rw-r--r--src/modules/finance/index.ts4
-rw-r--r--src/modules/helpers/index.ts3
3 files changed, 29 insertions, 6 deletions
diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts
index d4fcbbd6..b40f2f43 100644
--- a/src/modules/datatype/index.ts
+++ b/src/modules/datatype/index.ts
@@ -192,13 +192,37 @@ export class DatatypeModule {
/**
* 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`. Defaults to `0.5`.
+ * @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(): boolean {
- return !!this.number(1);
+ boolean(options: number | { 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.float({ min: 0, max: 1 }) < probability;
}
/**
diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts
index 3c60ec41..89b655bd 100644
--- a/src/modules/finance/index.ts
+++ b/src/modules/finance/index.ts
@@ -393,13 +393,13 @@ export class FinanceModule {
if (bban.type === 'a') {
s += this.faker.helpers.arrayElement(iban.alpha);
} else if (bban.type === 'c') {
- if (this.faker.datatype.number(100) < 80) {
+ if (this.faker.datatype.boolean(0.8)) {
s += this.faker.datatype.number(9);
} else {
s += this.faker.helpers.arrayElement(iban.alpha);
}
} else {
- if (c >= 3 && this.faker.datatype.number(100) < 30) {
+ if (c >= 3 && this.faker.datatype.boolean(0.3)) {
if (this.faker.datatype.boolean()) {
s += this.faker.helpers.arrayElement(iban.pattern100);
c -= 2;
diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts
index f3ad5a48..b8bbb725 100644
--- a/src/modules/helpers/index.ts
+++ b/src/modules/helpers/index.ts
@@ -354,8 +354,7 @@ export class HelpersModule {
callback: () => T,
options: { probability?: number } = {}
): T | undefined {
- const { probability = 0.5 } = options;
- if (this.faker.datatype.float({ min: 0, max: 1 }) < probability) {
+ if (this.faker.datatype.boolean(options)) {
return callback();
}
return undefined;