Skip to content

Commit 0304120

Browse files
authored
fix: datatype.number when min = max + precision, throw when max > min (#664)
1 parent 0b350f9 commit 0304120

7 files changed

Lines changed: 73 additions & 66 deletions

File tree

src/address.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,8 @@ export class Address {
407407
latitude(max: number = 90, min: number = -90, precision: number = 4): string {
408408
return this.faker.datatype
409409
.number({
410-
max: max,
411-
min: min,
410+
min,
411+
max,
412412
precision: parseFloat((0.0).toPrecision(precision) + '1'),
413413
})
414414
.toFixed(precision);

src/datatype.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ export class Datatype {
2020
*
2121
* @param options Maximum value or options object.
2222
* @param options.min Lower bound for generated number. Defaults to `0`.
23-
* @param options.max Upper bound for generated number. Defaults to `99999`.
23+
* @param options.max Upper bound for generated number. Defaults to `min + 99999`.
2424
* @param options.precision Precision of the generated number. Defaults to `1`.
2525
*
26+
* @throws When options define `max < min`
27+
*
2628
* @example
2729
* faker.datatype.number() // 55422
2830
* faker.datatype.number(100) // 52
@@ -34,39 +36,27 @@ export class Datatype {
3436
number(
3537
options?: number | { min?: number; max?: number; precision?: number }
3638
): number {
37-
if (typeof options === 'number') {
38-
options = { max: options };
39-
}
39+
const opts = typeof options === 'number' ? { max: options } : options ?? {};
4040

41-
options = options ?? {};
42-
43-
let max = 99999;
44-
let min = 0;
45-
let precision = 1;
46-
if (typeof options.min === 'number') {
47-
min = options.min;
48-
}
49-
50-
if (typeof options.max === 'number') {
51-
max = options.max;
52-
}
41+
const min = typeof opts.min === 'number' ? opts.min : 0;
42+
let max = typeof opts.max === 'number' ? opts.max : min + 99999;
43+
const precision = typeof opts.precision === 'number' ? opts.precision : 1;
5344

54-
if (typeof options.precision === 'number') {
55-
precision = options.precision;
45+
if (max < min) {
46+
throw new Error(`Max ${max} should be larger then min ${min}`);
5647
}
5748

5849
// Make the range inclusive of the max value
5950
if (max >= 0) {
6051
max += precision;
6152
}
6253

63-
let randomNumber = Math.floor(
54+
const randomNumber = Math.floor(
6455
this.faker.mersenne.rand(max / precision, min / precision)
6556
);
66-
// Workaround problem in Float point arithmetics for e.g. 6681493 / 0.01
67-
randomNumber = randomNumber / (1 / precision);
6857

69-
return randomNumber;
58+
// Workaround problem in float point arithmetics for e.g. 6681493 / 0.01
59+
return randomNumber / (1 / precision);
7060
}
7161

7262
/**

src/random.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ export class Random {
107107
arrayElement<T = string>(
108108
array: ReadonlyArray<T> = ['a', 'b', 'c'] as unknown as ReadonlyArray<T>
109109
): T {
110-
const r = this.faker.datatype.number({ max: array.length - 1 });
111-
return array[r];
110+
const index =
111+
array.length > 1
112+
? this.faker.datatype.number({ max: array.length - 1 })
113+
: 0;
114+
115+
return array[index];
112116
}
113117

114118
/**

test/address.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ describe('address', () => {
411411

412412
it('returns latitude with min and max and default precision', () => {
413413
for (let i = 0; i < 100; i++) {
414-
const latitude = faker.address.latitude(-5, 5);
414+
const latitude = faker.address.latitude(5, -5);
415415

416416
expect(latitude).toBeTypeOf('string');
417417
expect(

test/datatype.spec.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ const seededRuns = [
88
number: {
99
noArgs: 37454,
1010
numbers: [2, 5, 6, 1, 5],
11-
withMin: 37427,
11+
withMin: 37412,
1212
withMinAndMax: -1,
1313
withMax: 26,
1414
withMinAndMaxAndPrecision: -0.43,
1515
},
1616
float: {
1717
noArgs: 37453.64,
1818
numbers: [37452, 79656, 95076, 18342, 73200],
19-
withMin: 37427.37,
19+
withMin: 37411.64,
2020
withMinAndMax: -0.43,
2121
withMax: 25.84,
2222
withMinAndMaxAndPrecision: -0.4261,
@@ -80,15 +80,15 @@ const seededRuns = [
8080
number: {
8181
noArgs: 26202,
8282
numbers: [1, 3, 1, 1, 1],
83-
withMin: 26171,
83+
withMin: 26160,
8484
withMinAndMax: -13,
8585
withMax: 18,
8686
withMinAndMaxAndPrecision: -12.92,
8787
},
8888
float: {
8989
noArgs: 26202.2,
9090
numbers: [26202, 56052, 15864, 21258, 27810],
91-
withMin: 26171.21,
91+
withMin: 26160.2,
9292
withMinAndMax: -12.92,
9393
withMax: 18.08,
9494
withMinAndMaxAndPrecision: -12.9153,
@@ -152,15 +152,15 @@ const seededRuns = [
152152
number: {
153153
noArgs: 92852,
154154
numbers: [6, 3, 6, 5, 1],
155-
withMin: 92849,
155+
withMin: 92810,
156156
withMinAndMax: 61,
157157
withMax: 64,
158158
withMinAndMaxAndPrecision: 61.07,
159159
},
160160
float: {
161161
noArgs: 92851.09,
162162
numbers: [92856, 45900, 89346, 77826, 22554],
163-
withMin: 92848.09,
163+
withMin: 92809.09,
164164
withMinAndMax: 61.07,
165165
withMax: 64.07,
166166
withMinAndMaxAndPrecision: 61.0658,
@@ -288,6 +288,17 @@ describe('datatype', () => {
288288
});
289289
expect(actual).toEqual(expectations.number.withMinAndMaxAndPrecision);
290290
});
291+
292+
it('should throw when min > max', () => {
293+
const min = 10;
294+
const max = 9;
295+
296+
faker.seed(seed);
297+
298+
expect(() => {
299+
faker.datatype.number({ min, max });
300+
}).toThrowError(`Max ${max} should be larger then min ${min}`);
301+
});
291302
});
292303

293304
describe('float', () => {

test/system.spec.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,45 @@ const seededRuns = [
77
{
88
seed: 42,
99
expectations: {
10-
fileName: 'mobile_application.wad',
11-
commonFileName: 'mobile_application.gif',
10+
fileName: 'mobile_fish.gif',
11+
commonFileName: 'mobile_fish.mpe',
1212
mimeType: 'application/vnd.marlin.drm.license+xml',
1313
commonFileType: 'audio',
1414
commonFileExt: 'png',
1515
fileType: 'image',
1616
fileExt: 'chm',
1717
directoryPath: '/opt/bin',
18-
filePath: '/opt/bin/directives_savings_computer.qwd',
18+
filePath: '/opt/bin/directives_application_home.paw',
1919
semver: '3.7.9',
2020
},
2121
},
2222
{
2323
seed: 1337,
2424
expectations: {
25-
fileName: 'delaware.vcg',
26-
commonFileName: 'delaware.wav',
25+
fileName: 'delaware.uvvt',
26+
commonFileName: 'delaware.mp2',
2727
mimeType: 'application/vnd.dxr',
2828
commonFileType: 'audio',
2929
commonFileExt: 'wav',
3030
fileType: 'font',
3131
fileExt: 'gxt',
3232
directoryPath: '/Library',
33-
filePath: '/Library/bike_kiribati.kpr',
33+
filePath: '/Library/bike_interactive.qwt',
3434
semver: '2.5.1',
3535
},
3636
},
3737
{
3838
seed: 1211,
3939
expectations: {
40-
fileName: 'turnpike_supervisor_chicken.mka',
41-
commonFileName: 'turnpike_supervisor_chicken.mp4v',
40+
fileName: 'turnpike_frozen_handcrafted.mka',
41+
commonFileName: 'turnpike_frozen_handcrafted.mp4v',
4242
mimeType: 'text/vnd.fmi.flexstor',
4343
commonFileType: 'application',
4444
commonFileExt: 'htm',
4545
fileType: 'x-shader',
4646
fileExt: 'opml',
4747
directoryPath: '/var/log',
48-
filePath: '/var/log/forward_frozen.swf',
48+
filePath: '/var/log/forward_supervisor.swf',
4949
semver: '9.4.8',
5050
},
5151
},
@@ -101,6 +101,8 @@ describe('system', () => {
101101
'jpeg',
102102
'm2a',
103103
'm2v',
104+
'mp2',
105+
'mp3',
104106
'mp4',
105107
'mp4v',
106108
'mpeg',

test/word.spec.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,37 @@ const seededRuns = [
88
adjective: {
99
noArgs: 'harmonious',
1010
length10: 'gregarious',
11-
length20: 'stable',
11+
length20: 'harmonious',
1212
},
1313
adverb: {
1414
noArgs: 'jealously',
1515
length10: 'generously',
16-
length20: 'swiftly',
16+
length20: 'jealously',
1717
},
1818
conjunction: {
1919
noArgs: 'however',
2020
length10: 'as much as',
21-
length20: 'since',
21+
length20: 'however',
2222
},
2323
interjection: {
2424
noArgs: 'yahoo',
25-
length10: 'ack',
26-
length20: 'ack',
25+
length10: 'yahoo',
26+
length20: 'yahoo',
2727
},
2828
noun: {
2929
noArgs: 'gale',
3030
length10: 'exposition',
31-
length20: 'shift',
31+
length20: 'gale',
3232
},
3333
preposition: {
3434
noArgs: 'concerning',
3535
length10: 'throughout',
36-
length20: 'than',
36+
length20: 'concerning',
3737
},
3838
verb: {
3939
noArgs: 'function',
4040
length10: 'exasperate',
41-
length20: 'shred',
41+
length20: 'function',
4242
},
4343
},
4444
},
@@ -48,37 +48,37 @@ const seededRuns = [
4848
adjective: {
4949
noArgs: 'fabulous',
5050
length10: 'enchanting',
51-
length20: 'neat',
51+
length20: 'fabulous',
5252
},
5353
adverb: {
5454
noArgs: 'frankly',
5555
length10: 'enormously',
56-
length20: 'overconfidently',
56+
length20: 'frankly',
5757
},
5858
conjunction: {
5959
noArgs: 'even if',
6060
length10: 'as long as',
61-
length20: 'instead',
61+
length20: 'even if',
6262
},
6363
interjection: {
6464
noArgs: 'ew',
65-
length10: 'yippee',
66-
length20: 'yippee',
65+
length10: 'ew',
66+
length20: 'ew',
6767
},
6868
noun: {
6969
noArgs: 'digit',
7070
length10: 'depressive',
71-
length20: 'might',
71+
length20: 'digit',
7272
},
7373
preposition: {
7474
noArgs: 'barring',
7575
length10: 'concerning',
76-
length20: 'midst',
76+
length20: 'barring',
7777
},
7878
verb: {
7979
noArgs: 'dispense',
8080
length10: 'demoralize',
81-
length20: 'nearest',
81+
length20: 'dispense',
8282
},
8383
},
8484
},
@@ -88,37 +88,37 @@ const seededRuns = [
8888
adjective: {
8989
noArgs: 'verifiable',
9090
length10: 'unfinished',
91-
length20: 'joyous',
91+
length20: 'verifiable',
9292
},
9393
adverb: {
9494
noArgs: 'viciously',
9595
length10: 'unbearably',
96-
length20: 'loudly',
96+
length20: 'viciously',
9797
},
9898
conjunction: {
9999
noArgs: 'whereas',
100100
length10: 'as soon as',
101-
length20: 'in addition',
101+
length20: 'whereas',
102102
},
103103
interjection: {
104104
noArgs: 'er',
105-
length10: 'gah',
106-
length20: 'gah',
105+
length10: 'er',
106+
length20: 'er',
107107
},
108108
noun: {
109109
noArgs: 'trick',
110110
length10: 'trafficker',
111-
length20: 'infection',
111+
length20: 'trick',
112112
},
113113
preposition: {
114114
noArgs: 'upon',
115115
length10: 'underneath',
116-
length20: 'for',
116+
length20: 'upon',
117117
},
118118
verb: {
119119
noArgs: 'trick',
120120
length10: 'trampoline',
121-
length20: 'intercede',
121+
length20: 'trick',
122122
},
123123
},
124124
},

0 commit comments

Comments
 (0)