Skip to content

Commit 9ebcef8

Browse files
committed
fix!: Use Number.MAX_SAFE_INTEGER as the max for int() and float()
Since #30, the values generated for `int()` and `float()` have been very large: (e.g. `1.994546950711818e+38`). Such large numbers are probably more an edge case for most use cases of `int()` and `float()` though. A more safe bet would be a max number of [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER). ## Breaking changes **note:** This will change the generated results for `int()` and `float()` for any cases where a maximum number was not given.
1 parent 3dc2866 commit 9ebcef8

File tree

6 files changed

+166
-4009
lines changed

6 files changed

+166
-4009
lines changed

float.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
var hash = require('./hash')
22
var conj = require('./utils/conj')
33
var fit = require('./utils/fit')
4+
var defaults = require('./utils/defaults')
45
var Decimal = require('decimal.js')
56

67
function float(input, opts) {
78
opts = opts || 0
89

9-
var min = opts.min
10-
var max = opts.max
10+
var min = defaults(opts.min, 0)
11+
var max = defaults(opts.max, Number.MAX_SAFE_INTEGER)
1112

1213
// rehash to differentiate from `int`
1314
var whole = hash(hash(input))

int.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
var hash = require('./hash')
22
var fit = require('./utils/fit')
33
var conj = require('./utils/conj')
4+
var defaults = require('./utils/defaults')
45

56
function int(input, opts) {
67
opts = opts || 0
7-
var min = opts.min
8-
var max = opts.max
8+
var min = defaults(opts.min, 0)
9+
var max = defaults(opts.max, Number.MAX_SAFE_INTEGER)
910
var id = hash(input)
1011
return fit(id, min, max)
1112
}

readme.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,7 @@ int('id-23')
308308

309309
##### `options`
310310

311-
- **`min=0` and `max=Infinity`:** the minimum and maximum possible values for
312-
returned numbers
311+
- **`min=1` and `max=Number.MAX_SAFE_INTEGER`:** the minimum and maximum possible values for returned numbers
313312

314313
```js
315314
int('id-2', {
@@ -343,8 +342,7 @@ float('id-23')
343342

344343
##### `options`
345344

346-
- **`min=0` and `max=Infinity`:** the minimum and maximum possible values for
347-
returned numbers
345+
- **`min=1` and `max=Number.MAX_SAFE_INTEGER`:** the minimum and maximum possible values for returned numbers
348346

349347
```js
350348
float('id-2', {

tests/fictional.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fictional = require('..')
22
const test = require('ava')
3+
const Decimal = require('decimal.js')
34

45
const {
56
oneOf,
@@ -111,7 +112,13 @@ function callMakers(input) {
111112
const result = {}
112113

113114
for (const name of Object.keys(makers)) {
114-
result[name] = makers[name](input)
115+
let value = makers[name](input)
116+
117+
if (value instanceof Decimal) {
118+
value = `Decimal(${value.toString()})`
119+
}
120+
121+
result[name] = value
115122
}
116123

117124
return result

0 commit comments

Comments
 (0)