Skip to content

Commit

Permalink
extract a-data-view helper
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Dec 20, 2023
1 parent e8e1437 commit c8a8167
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
9 changes: 9 additions & 0 deletions packages/core-js/internals/a-data-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';
var classof = require('../internals/classof');

var $TypeError = TypeError;

module.exports = function (argument) {
if (classof(argument) === 'DataView') return argument;
throw new $TypeError('Argument is not a DataView');
};
5 changes: 2 additions & 3 deletions packages/core-js/modules/esnext.data-view.set-float16.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');
var classof = require('../internals/classof');
var aDataView = require('../internals/a-data-view');
var toIndex = require('../internals/to-index');
var packIEEE754 = require('../internals/ieee754').pack;
var f16round = require('../internals/math-f16round');

var $TypeError = TypeError;
// eslint-disable-next-line es/no-typed-arrays -- safe
var setUint16 = uncurryThis(DataView.prototype.setUint16);

// `DataView.prototype.setFloat16` method
// https://github.com/tc39/proposal-float16array
$({ target: 'DataView', proto: true }, {
setFloat16: function setFloat16(byteOffset, value /* , littleEndian */) {
if (classof(this) !== 'DataView') throw new $TypeError('Incorrect receiver');
aDataView(this);
var offset = toIndex(byteOffset);
var bytes = packIEEE754(f16round(value), 10, 2);
return setUint16(this, offset, bytes[1] << 8 | bytes[0], arguments.length > 2 ? arguments[2] : false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');
var classof = require('../internals/classof');
var aDataView = require('../internals/a-data-view');
var toIndex = require('../internals/to-index');
var toUint8Clamped = require('../internals/to-uint8-clamped');

var $TypeError = TypeError;
// eslint-disable-next-line es/no-typed-arrays -- safe
var setUint8 = uncurryThis(DataView.prototype.setUint8);

// `DataView.prototype.setUint8Clamped` method
// https://github.com/tc39/proposal-dataview-get-set-uint8clamped
$({ target: 'DataView', proto: true, forced: true }, {
setUint8Clamped: function setUint8Clamped(byteOffset, value) {
if (classof(this) !== 'DataView') throw new $TypeError('Incorrect receiver');
aDataView(this);
var offset = toIndex(byteOffset);
return setUint8(this, offset, toUint8Clamped(value));
}
Expand Down

0 comments on commit c8a8167

Please sign in to comment.