aboutsummaryrefslogtreecommitdiff
path: root/src/random.ts
diff options
context:
space:
mode:
authorPiotr Kuczynski <[email protected]>2022-03-31 19:48:24 +0200
committerGitHub <[email protected]>2022-03-31 17:48:24 +0000
commit03041201c21ad599bbe1874c375f4f41b94961ba (patch)
treecd4de61bc8d8bfa40984ef60fbc224391ebc01e4 /src/random.ts
parent0b350f929624a1247f35549c61148ec93e5f0c0a (diff)
downloadfaker-03041201c21ad599bbe1874c375f4f41b94961ba.tar.xz
faker-03041201c21ad599bbe1874c375f4f41b94961ba.zip
fix: datatype.number when min = max + precision, throw when max > min (#664)
Diffstat (limited to 'src/random.ts')
-rw-r--r--src/random.ts8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/random.ts b/src/random.ts
index 0a3b3c5e..f41a67fa 100644
--- a/src/random.ts
+++ b/src/random.ts
@@ -107,8 +107,12 @@ export class Random {
arrayElement<T = string>(
array: ReadonlyArray<T> = ['a', 'b', 'c'] as unknown as ReadonlyArray<T>
): T {
- const r = this.faker.datatype.number({ max: array.length - 1 });
- return array[r];
+ const index =
+ array.length > 1
+ ? this.faker.datatype.number({ max: array.length - 1 })
+ : 0;
+
+ return array[index];
}
/**