1 |
3a515b92
|
cagy
|
'use strict';
|
2 |
|
|
|
3 |
|
|
var GetIntrinsic = require('../GetIntrinsic');
|
4 |
|
|
|
5 |
|
|
var $Math = GetIntrinsic('%Math%');
|
6 |
|
|
|
7 |
|
|
var ToNumber = require('./ToNumber');
|
8 |
|
|
|
9 |
|
|
var $isNaN = require('../helpers/isNaN');
|
10 |
|
|
|
11 |
|
|
var $floor = $Math.floor;
|
12 |
|
|
|
13 |
|
|
// https://www.ecma-international.org/ecma-262/6.0/#sec-touint8clamp
|
14 |
|
|
|
15 |
|
|
module.exports = function ToUint8Clamp(argument) {
|
16 |
|
|
var number = ToNumber(argument);
|
17 |
|
|
if ($isNaN(number) || number <= 0) { return 0; }
|
18 |
|
|
if (number >= 0xFF) { return 0xFF; }
|
19 |
|
|
var f = $floor(argument);
|
20 |
|
|
if (f + 0.5 < number) { return f + 1; }
|
21 |
|
|
if (number < f + 0.5) { return f; }
|
22 |
|
|
if (f % 2 !== 0) { return f + 1; }
|
23 |
|
|
return f;
|
24 |
|
|
};
|