Skip to content

Commit f9f15c2

Browse files
committed
clamp no longer throws an error on NaN as min or max
tc39/proposal-math-clamp@d238779
1 parent 7066260 commit f9f15c2

File tree

6 files changed

+22
-19
lines changed

6 files changed

+22
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Changelog
22
##### Unreleased
3+
- `clamp` no longer throws an error on `NaN` as `min` or `max`, following [proposal-math-clamp#d2387791c265edf66fbe2455eab919016717ce6f](https://github.com/tc39/proposal-math-clamp/commit/d2387791c265edf66fbe2455eab919016717ce6f)
34
- Use `Get` in `Iterator.zipKeyed`, following [proposal-joint-iteration#43](https://github.com/tc39/proposal-joint-iteration/pull/43)
45
- Fixed [several V8 bugs](https://github.com/zloirock/core-js/issues/1439) in `Uint8Array.fromHex` and `Uint8Array.prototype.{ setFromBase64, toBase64, toHex }`, thanks [**@brc-dd**](https://github.com/brc-dd)
56
- Fixed some cases of `Set.prototype.{ symmetricDifference, union }` detection
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
'use strict';
22
var aNumber = require('../internals/a-number');
3-
var notANaN = require('../internals/not-a-nan');
43

54
var $min = Math.min;
65
var $max = Math.max;
76

87
module.exports = function clamp(value, min, max) {
9-
aNumber(value);
10-
notANaN(aNumber(min));
11-
notANaN(aNumber(max));
12-
return $min(max, $max(min, value));
8+
return $min($max(aNumber(value), aNumber(min)), aNumber(max));
139
};

tests/unit-global/esnext.math.clamp.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ QUnit.test('Math.clamp', assert => {
1010
assert.same(clamp(4, 2, 6), 4);
1111
assert.same(clamp(6, 2, 4), 4);
1212

13-
assert.same(clamp(NaN, 4, 6), NaN, 'If value is NaN, return NaN.');
1413
assert.same(clamp(-0, 0, 1), 0, 'If value is -0𝔽 and min is +0𝔽, return +0𝔽.');
1514
assert.same(clamp(0, -0, 1), 0, 'If value is +0𝔽 and min is -0𝔽, return +0𝔽.');
1615
assert.same(clamp(-0, -1, 0), -0, 'If value is -0𝔽 and max is +0𝔽, return -0𝔽.');
@@ -20,9 +19,11 @@ QUnit.test('Math.clamp', assert => {
2019
assert.same(clamp(2, 0, -0), -0, 'min is +0𝔽 and max is -0𝔽');
2120
assert.same(clamp(2, 3, 1), 1, 'min > max');
2221

23-
assert.throws(() => clamp(Object(2), 1, 3), TypeError, 'If value is not a Number, throw a TypeError exception.');
22+
assert.same(clamp(NaN, 3, 1), NaN, 'If value is NaN, return NaN.');
23+
assert.same(clamp(2, NaN, 1), NaN, 'If min is NaN, return NaN.');
24+
assert.same(clamp(2, 3, NaN), NaN, 'If max is NaN, return NaN.');
25+
26+
assert.throws(() => clamp({ valueOf: () => 2 }, 1, 3), TypeError, 'If value is not a Number, throw a TypeError exception');
2427
assert.throws(() => clamp(2, Object(1), 3), TypeError, 'If min is not a Number, throw a TypeError exception.');
25-
assert.throws(() => clamp(2, NaN, 3), RangeError, 'If min is NaN, throw a RangeError exception.');
2628
assert.throws(() => clamp(2, 1, Object(3)), TypeError, 'If max is not a Number, throw a TypeError exception.');
27-
assert.throws(() => clamp(2, 1, NaN), RangeError, 'If max is NaN, throw a RangeError exception.');
2829
});

tests/unit-global/esnext.number.clamp.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ QUnit.test('Number#clamp', assert => {
1010
assert.same(clamp.call(4, 2, 6), 4);
1111
assert.same(clamp.call(6, 2, 4), 4);
1212

13-
assert.same(clamp.call(NaN, 4, 6), NaN, 'If value is NaN, return NaN.');
1413
assert.same(clamp.call(-0, 0, 1), 0, 'If value is -0𝔽 and min is +0𝔽, return +0𝔽.');
1514
assert.same(clamp.call(0, -0, 1), 0, 'If value is +0𝔽 and min is -0𝔽, return +0𝔽.');
1615
assert.same(clamp.call(-0, -1, 0), -0, 'If value is -0𝔽 and max is +0𝔽, return -0𝔽.');
@@ -20,8 +19,11 @@ QUnit.test('Number#clamp', assert => {
2019
assert.same(clamp.call(2, 0, -0), -0, 'min is +0𝔽 and max is -0𝔽');
2120
assert.same(clamp.call(2, 3, 1), 1, 'min > max');
2221

22+
assert.same(clamp.call(NaN, 3, 1), NaN, 'If value is NaN, return NaN.');
23+
assert.same(clamp.call(2, NaN, 1), NaN, 'If min is NaN, return NaN.');
24+
assert.same(clamp.call(2, 3, NaN), NaN, 'If max is NaN, return NaN.');
25+
26+
assert.throws(() => clamp.call({ valueOf: () => 2 }, 1, 3), TypeError, 'If value is not a Number, throw a TypeError exception');
2327
assert.throws(() => clamp.call(2, Object(1), 3), TypeError, 'If min is not a Number, throw a TypeError exception.');
24-
assert.throws(() => clamp.call(2, NaN, 3), RangeError, 'If min is NaN, throw a RangeError exception.');
2528
assert.throws(() => clamp.call(2, 1, Object(3)), TypeError, 'If max is not a Number, throw a TypeError exception.');
26-
assert.throws(() => clamp.call(2, 1, NaN), RangeError, 'If max is NaN, throw a RangeError exception.');
2729
});

tests/unit-pure/esnext.math.clamp.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ QUnit.test('Math.clamp', assert => {
88
assert.same(clamp(4, 2, 6), 4);
99
assert.same(clamp(6, 2, 4), 4);
1010

11-
assert.same(clamp(NaN, 4, 6), NaN, 'If value is NaN, return NaN.');
1211
assert.same(clamp(-0, 0, 1), 0, 'If value is -0𝔽 and min is +0𝔽, return +0𝔽.');
1312
assert.same(clamp(0, -0, 1), 0, 'If value is +0𝔽 and min is -0𝔽, return +0𝔽.');
1413
assert.same(clamp(-0, -1, 0), -0, 'If value is -0𝔽 and max is +0𝔽, return -0𝔽.');
@@ -18,9 +17,11 @@ QUnit.test('Math.clamp', assert => {
1817
assert.same(clamp(2, 0, -0), -0, 'min is +0𝔽 and max is -0𝔽');
1918
assert.same(clamp(2, 3, 1), 1, 'min > max');
2019

21-
assert.throws(() => clamp(Object(2), 1, 3), TypeError, 'If value is not a Number, throw a TypeError exception.');
20+
assert.same(clamp(NaN, 3, 1), NaN, 'If value is NaN, return NaN.');
21+
assert.same(clamp(2, NaN, 1), NaN, 'If min is NaN, return NaN.');
22+
assert.same(clamp(2, 3, NaN), NaN, 'If max is NaN, return NaN.');
23+
24+
assert.throws(() => clamp({ valueOf: () => 2 }, 1, 3), TypeError, 'If value is not a Number, throw a TypeError exception');
2225
assert.throws(() => clamp(2, Object(1), 3), TypeError, 'If min is not a Number, throw a TypeError exception.');
23-
assert.throws(() => clamp(2, NaN, 3), RangeError, 'If min is NaN, throw a RangeError exception.');
2426
assert.throws(() => clamp(2, 1, Object(3)), TypeError, 'If max is not a Number, throw a TypeError exception.');
25-
assert.throws(() => clamp(2, 1, NaN), RangeError, 'If max is NaN, throw a RangeError exception.');
2627
});

tests/unit-pure/esnext.number.clamp.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ QUnit.test('Number#clamp', assert => {
77
assert.same(clamp(4, 2, 6), 4);
88
assert.same(clamp(6, 2, 4), 4);
99

10-
assert.same(clamp(NaN, 4, 6), NaN, 'If value is NaN, return NaN.');
1110
assert.same(clamp(-0, 0, 1), 0, 'If value is -0𝔽 and min is +0𝔽, return +0𝔽.');
1211
assert.same(clamp(0, -0, 1), 0, 'If value is +0𝔽 and min is -0𝔽, return +0𝔽.');
1312
assert.same(clamp(-0, -1, 0), -0, 'If value is -0𝔽 and max is +0𝔽, return -0𝔽.');
@@ -17,8 +16,11 @@ QUnit.test('Number#clamp', assert => {
1716
assert.same(clamp(2, 0, -0), -0, 'min is +0𝔽 and max is -0𝔽');
1817
assert.same(clamp(2, 3, 1), 1, 'min > max');
1918

19+
assert.same(clamp(NaN, 3, 1), NaN, 'If value is NaN, return NaN.');
20+
assert.same(clamp(2, NaN, 1), NaN, 'If min is NaN, return NaN.');
21+
assert.same(clamp(2, 3, NaN), NaN, 'If max is NaN, return NaN.');
22+
23+
assert.throws(() => clamp({ valueOf: () => 2 }, 1, 3), TypeError, 'If value is not a Number, throw a TypeError exception');
2024
assert.throws(() => clamp(2, Object(1), 3), TypeError, 'If min is not a Number, throw a TypeError exception.');
21-
assert.throws(() => clamp(2, NaN, 3), RangeError, 'If min is NaN, throw a RangeError exception.');
2225
assert.throws(() => clamp(2, 1, Object(3)), TypeError, 'If max is not a Number, throw a TypeError exception.');
23-
assert.throws(() => clamp(2, 1, NaN), RangeError, 'If max is NaN, throw a RangeError exception.');
2426
});

0 commit comments

Comments
 (0)