Skip to content

Commit e3b0fdb

Browse files
committed
Math.clamp: remove a RangeError if min <= max or +0 min and -0 max
1 parent 67b362f commit e3b0fdb

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- Built-ins:
3838
- `Math.clamp`
3939
- Moved to stage 2, [May 2025 TC39 meeting](https://github.com/tc39/proposal-math-clamp/commit/a005f28a6a03e175b9671de1c8c70dd5b7b08c2d)
40+
- Removed a `RangeError` if `min <= max` or `+0` min and `-0` max, [tc39/proposal-math-clamp/#22](https://github.com/tc39/proposal-math-clamp/issues/22)
4041
- Always check regular expression flags by `flags` getter [PR](https://github.com/tc39/ecma262/pull/2791). Native methods are not fixed, only own implementation updated for:
4142
- `RegExp.prototype[@@match]`
4243
- `RegExp.prototype[@@replace]`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
var aNumber = require('../internals/a-number');
3+
var notANaN = require('../internals/not-a-nan');
4+
5+
var $min = Math.min;
6+
var $max = Math.max;
7+
8+
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));
13+
};
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
'use strict';
22
var $ = require('../internals/export');
3-
var aNumber = require('../internals/a-number');
4-
var notANaN = require('../internals/not-a-nan');
5-
var sameValue = require('../internals/same-value');
6-
7-
var $RangeError = RangeError;
8-
var $min = Math.min;
9-
var $max = Math.max;
3+
var clamp = require('../internals/math-clamp');
104

115
// `Math.clamp` method
126
// https://github.com/tc39/proposal-math-clamp
137
$({ target: 'Math', stat: true, forced: true }, {
14-
clamp: function clamp(value, min, max) {
15-
aNumber(value);
16-
notANaN(aNumber(min));
17-
notANaN(aNumber(max));
18-
if ((sameValue(min, 0) && sameValue(max, -0)) || min > max) throw new $RangeError('`min` should be smaller than `max`');
19-
return $min(max, $max(min, value));
20-
}
8+
clamp: clamp
219
});

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ QUnit.test('Math.clamp', assert => {
1717
assert.same(clamp(0, -1, -0), -0, 'If value is +0𝔽 and max is -0𝔽, return -0𝔽.');
1818
assert.same(clamp(0, -0, -0), -0, 'If min = max return min.');
1919

20+
assert.same(clamp(2, 0, -0), -0, 'min is +0𝔽 and max is -0𝔽');
21+
assert.same(clamp(2, 3, 1), 1, 'min > max');
22+
2023
assert.throws(() => clamp(Object(2), 1, 3), TypeError, 'If value is not a Number, throw a TypeError exception.');
2124
assert.throws(() => clamp(2, Object(1), 3), TypeError, 'If min is not a Number, throw a TypeError exception.');
2225
assert.throws(() => clamp(2, NaN, 3), RangeError, 'If min is NaN, throw a RangeError exception.');
2326
assert.throws(() => clamp(2, 1, Object(3)), TypeError, 'If max is not a Number, throw a TypeError exception.');
2427
assert.throws(() => clamp(2, 1, NaN), RangeError, 'If max is NaN, throw a RangeError exception.');
25-
assert.throws(() => clamp(2, 0, -0), RangeError, 'If min is +0𝔽 and max is -0𝔽, throw a RangeError exception.');
26-
assert.throws(() => clamp(2, 3, 1), RangeError, 'If min > max, throw a RangeError exception.');
2728
});

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ QUnit.test('Math.clamp', assert => {
1515
assert.same(clamp(0, -1, -0), -0, 'If value is +0𝔽 and max is -0𝔽, return -0𝔽.');
1616
assert.same(clamp(0, -0, -0), -0, 'If min = max return min.');
1717

18+
assert.same(clamp(2, 0, -0), -0, 'min is +0𝔽 and max is -0𝔽');
19+
assert.same(clamp(2, 3, 1), 1, 'min > max');
20+
1821
assert.throws(() => clamp(Object(2), 1, 3), TypeError, 'If value is not a Number, throw a TypeError exception.');
1922
assert.throws(() => clamp(2, Object(1), 3), TypeError, 'If min is not a Number, throw a TypeError exception.');
2023
assert.throws(() => clamp(2, NaN, 3), RangeError, 'If min is NaN, throw a RangeError exception.');
2124
assert.throws(() => clamp(2, 1, Object(3)), TypeError, 'If max is not a Number, throw a TypeError exception.');
2225
assert.throws(() => clamp(2, 1, NaN), RangeError, 'If max is NaN, throw a RangeError exception.');
23-
assert.throws(() => clamp(2, 0, -0), RangeError, 'If min is +0𝔽 and max is -0𝔽, throw a RangeError exception.');
24-
assert.throws(() => clamp(2, 3, 1), RangeError, 'If min > max, throw a RangeError exception.');
2526
});

0 commit comments

Comments
 (0)