aboutsummaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/address.ts4
-rw-r--r--src/datatype.ts34
-rw-r--r--src/random.ts8
3 files changed, 20 insertions, 26 deletions
diff --git a/src/address.ts b/src/address.ts
index 5a130cb2..264b0281 100644
--- a/src/address.ts
+++ b/src/address.ts
@@ -407,8 +407,8 @@ export class Address {
latitude(max: number = 90, min: number = -90, precision: number = 4): string {
return this.faker.datatype
.number({
- max: max,
- min: min,
+ min,
+ max,
precision: parseFloat((0.0).toPrecision(precision) + '1'),
})
.toFixed(precision);
diff --git a/src/datatype.ts b/src/datatype.ts
index ad37c3da..79b0b61c 100644
--- a/src/datatype.ts
+++ b/src/datatype.ts
@@ -20,9 +20,11 @@ export class Datatype {
*
* @param options Maximum value or options object.
* @param options.min Lower bound for generated number. Defaults to `0`.
- * @param options.max Upper bound for generated number. Defaults to `99999`.
+ * @param options.max Upper bound for generated number. Defaults to `min + 99999`.
* @param options.precision Precision of the generated number. Defaults to `1`.
*
+ * @throws When options define `max < min`
+ *
* @example
* faker.datatype.number() // 55422
* faker.datatype.number(100) // 52
@@ -34,25 +36,14 @@ export class Datatype {
number(
options?: number | { min?: number; max?: number; precision?: number }
): number {
- if (typeof options === 'number') {
- options = { max: options };
- }
+ const opts = typeof options === 'number' ? { max: options } : options ?? {};
- options = options ?? {};
-
- let max = 99999;
- let min = 0;
- let precision = 1;
- if (typeof options.min === 'number') {
- min = options.min;
- }
-
- if (typeof options.max === 'number') {
- max = options.max;
- }
+ const min = typeof opts.min === 'number' ? opts.min : 0;
+ let max = typeof opts.max === 'number' ? opts.max : min + 99999;
+ const precision = typeof opts.precision === 'number' ? opts.precision : 1;
- if (typeof options.precision === 'number') {
- precision = options.precision;
+ if (max < min) {
+ throw new Error(`Max ${max} should be larger then min ${min}`);
}
// Make the range inclusive of the max value
@@ -60,13 +51,12 @@ export class Datatype {
max += precision;
}
- let randomNumber = Math.floor(
+ const randomNumber = Math.floor(
this.faker.mersenne.rand(max / precision, min / precision)
);
- // Workaround problem in Float point arithmetics for e.g. 6681493 / 0.01
- randomNumber = randomNumber / (1 / precision);
- return randomNumber;
+ // Workaround problem in float point arithmetics for e.g. 6681493 / 0.01
+ return randomNumber / (1 / precision);
}
/**
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];
}
/**