@petamoriken/float16
Advanced tools
Comparing version 3.2.2 to 3.2.3
/** | ||
* @petamoriken/float16 v3.2.2 | MIT License - https://git.io/float16 | ||
* @petamoriken/float16 v3.2.3 | MIT License - https://git.io/float16 | ||
* | ||
@@ -22,25 +22,25 @@ * @license | ||
if (e < -27) { | ||
baseTable[i | 0x000] = 0x0000; | ||
baseTable[i] = 0x0000; | ||
baseTable[i | 0x100] = 0x8000; | ||
shiftTable[i | 0x000] = 24; | ||
shiftTable[i] = 24; | ||
shiftTable[i | 0x100] = 24; // small number (denorm) | ||
} else if (e < -14) { | ||
baseTable[i | 0x000] = 0x0400 >> -e - 14; | ||
baseTable[i] = 0x0400 >> -e - 14; | ||
baseTable[i | 0x100] = 0x0400 >> -e - 14 | 0x8000; | ||
shiftTable[i | 0x000] = -e - 1; | ||
shiftTable[i] = -e - 1; | ||
shiftTable[i | 0x100] = -e - 1; // normal number | ||
} else if (e <= 15) { | ||
baseTable[i | 0x000] = e + 15 << 10; | ||
baseTable[i] = e + 15 << 10; | ||
baseTable[i | 0x100] = e + 15 << 10 | 0x8000; | ||
shiftTable[i | 0x000] = 13; | ||
shiftTable[i] = 13; | ||
shiftTable[i | 0x100] = 13; // large number (Infinity, -Infinity) | ||
} else if (e < 128) { | ||
baseTable[i | 0x000] = 0x7c00; | ||
baseTable[i] = 0x7c00; | ||
baseTable[i | 0x100] = 0xfc00; | ||
shiftTable[i | 0x000] = 24; | ||
shiftTable[i] = 24; | ||
shiftTable[i | 0x100] = 24; // stay (NaN, Infinity, -Infinity) | ||
} else { | ||
baseTable[i | 0x000] = 0x7c00; | ||
baseTable[i] = 0x7c00; | ||
baseTable[i | 0x100] = 0xfc00; | ||
shiftTable[i | 0x000] = 13; | ||
shiftTable[i] = 13; | ||
shiftTable[i | 0x100] = 13; | ||
@@ -283,3 +283,3 @@ } | ||
var asyncTag = '[object AsyncFunction]', | ||
funcTag = '[object Function]', | ||
funcTag$1 = '[object Function]', | ||
genTag = '[object GeneratorFunction]', | ||
@@ -313,3 +313,3 @@ proxyTag = '[object Proxy]'; | ||
var tag = baseGetTag(value); | ||
return tag == funcTag || tag == genTag || tag == asyncTag || tag == proxyTag; | ||
return tag == funcTag$1 || tag == genTag || tag == asyncTag || tag == proxyTag; | ||
} | ||
@@ -1032,2 +1032,29 @@ | ||
* @param {unknown} target | ||
* @returns {number} | ||
*/ | ||
function ToLength(target) { | ||
const length = ToIntegerOrInfinity(target); | ||
if (length < 0) { | ||
return 0; | ||
} | ||
return length < Number.MAX_SAFE_INTEGER ? length : Number.MAX_SAFE_INTEGER; | ||
} | ||
/** | ||
* @param {object} arrayLike | ||
* @returns {number} | ||
*/ | ||
function LengthOfArrayLike(arrayLike) { | ||
if (!isObject(arrayLike)) { | ||
throw TypeError("this is not a object"); | ||
} | ||
return ToLength(arrayLike.length); | ||
} | ||
/** | ||
* @param {object} target | ||
* @param {Function} defaultConstructor | ||
@@ -1132,3 +1159,3 @@ * @returns {Function} | ||
var arrayBufferTag = '[object ArrayBuffer]'; | ||
var arrayBufferTag$1 = '[object ArrayBuffer]'; | ||
/** | ||
@@ -1143,3 +1170,3 @@ * The base implementation of `_.isArrayBuffer` without Node.js optimizations. | ||
function baseIsArrayBuffer(value) { | ||
return isObjectLike(value) && baseGetTag(value) == arrayBufferTag; | ||
return isObjectLike(value) && baseGetTag(value) == arrayBufferTag$1; | ||
} | ||
@@ -1214,11 +1241,127 @@ | ||
/** Used as references for various `Number` constants. */ | ||
var MAX_SAFE_INTEGER = 9007199254740991; | ||
/** | ||
* @param {unknown} view | ||
* Checks if `value` is a valid array-like length. | ||
* | ||
* **Note:** This method is loosely based on | ||
* [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength). | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 4.0.0 | ||
* @category Lang | ||
* @param {*} value The value to check. | ||
* @returns {boolean} Returns `true` if `value` is a valid length, else `false`. | ||
* @example | ||
* | ||
* _.isLength(3); | ||
* // => true | ||
* | ||
* _.isLength(Number.MIN_VALUE); | ||
* // => false | ||
* | ||
* _.isLength(Infinity); | ||
* // => false | ||
* | ||
* _.isLength('3'); | ||
* // => false | ||
*/ | ||
function isLength(value) { | ||
return typeof value == 'number' && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER; | ||
} | ||
/** `Object#toString` result references. */ | ||
var argsTag = '[object Arguments]', | ||
arrayTag = '[object Array]', | ||
boolTag = '[object Boolean]', | ||
dateTag = '[object Date]', | ||
errorTag = '[object Error]', | ||
funcTag = '[object Function]', | ||
mapTag = '[object Map]', | ||
numberTag = '[object Number]', | ||
objectTag = '[object Object]', | ||
regexpTag = '[object RegExp]', | ||
setTag = '[object Set]', | ||
stringTag = '[object String]', | ||
weakMapTag = '[object WeakMap]'; | ||
var arrayBufferTag = '[object ArrayBuffer]', | ||
dataViewTag = '[object DataView]', | ||
float32Tag = '[object Float32Array]', | ||
float64Tag = '[object Float64Array]', | ||
int8Tag = '[object Int8Array]', | ||
int16Tag = '[object Int16Array]', | ||
int32Tag = '[object Int32Array]', | ||
uint8Tag = '[object Uint8Array]', | ||
uint8ClampedTag = '[object Uint8ClampedArray]', | ||
uint16Tag = '[object Uint16Array]', | ||
uint32Tag = '[object Uint32Array]'; | ||
/** Used to identify `toStringTag` values of typed arrays. */ | ||
var typedArrayTags = {}; | ||
typedArrayTags[float32Tag] = typedArrayTags[float64Tag] = typedArrayTags[int8Tag] = typedArrayTags[int16Tag] = typedArrayTags[int32Tag] = typedArrayTags[uint8Tag] = typedArrayTags[uint8ClampedTag] = typedArrayTags[uint16Tag] = typedArrayTags[uint32Tag] = true; | ||
typedArrayTags[argsTag] = typedArrayTags[arrayTag] = typedArrayTags[arrayBufferTag] = typedArrayTags[boolTag] = typedArrayTags[dataViewTag] = typedArrayTags[dateTag] = typedArrayTags[errorTag] = typedArrayTags[funcTag] = typedArrayTags[mapTag] = typedArrayTags[numberTag] = typedArrayTags[objectTag] = typedArrayTags[regexpTag] = typedArrayTags[setTag] = typedArrayTags[stringTag] = typedArrayTags[weakMapTag] = false; | ||
/** | ||
* The base implementation of `_.isTypedArray` without Node.js optimizations. | ||
* | ||
* @private | ||
* @param {*} value The value to check. | ||
* @returns {boolean} Returns `true` if `value` is a typed array, else `false`. | ||
*/ | ||
function baseIsTypedArray(value) { | ||
return isObjectLike(value) && isLength(value.length) && !!typedArrayTags[baseGetTag(value)]; | ||
} | ||
/* Node.js helper references. */ | ||
var nodeIsTypedArray = nodeUtil$1 && nodeUtil$1.isTypedArray; | ||
/** | ||
* Checks if `value` is classified as a typed array. | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 3.0.0 | ||
* @category Lang | ||
* @param {*} value The value to check. | ||
* @returns {boolean} Returns `true` if `value` is a typed array, else `false`. | ||
* @example | ||
* | ||
* _.isTypedArray(new Uint8Array); | ||
* // => true | ||
* | ||
* _.isTypedArray([]); | ||
* // => false | ||
*/ | ||
var isTypedArray = nodeIsTypedArray ? baseUnary(nodeIsTypedArray) : baseIsTypedArray; | ||
var isTypedArray$1 = isTypedArray; | ||
/** | ||
* @param {unknown} value | ||
* @returns {boolean} | ||
*/ | ||
function isDataView(view) { | ||
return ArrayBuffer.isView(view) && Object.prototype.toString.call(view) === "[object DataView]"; | ||
function isDataView(value) { | ||
return ArrayBuffer.isView(value) && Object.prototype.toString.call(value) === "[object DataView]"; | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {boolean} | ||
*/ | ||
function isSharedArrayBuffer(value) { | ||
return Object.prototype.toString.call(value) === "[object SharedArrayBuffer]"; | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {boolean} | ||
*/ | ||
function isIterable(value) { | ||
return isObject(value) && typeof value[Symbol.iterator] === "function"; | ||
} | ||
/** | ||
* @param {unknown} key | ||
@@ -1239,7 +1382,16 @@ * @returns {boolean} | ||
function isFloat16Array(target) { | ||
return target instanceof Float16Array; | ||
function isFloat16ArrayProxy(target) { | ||
return target instanceof Float16Array && _(target).target !== undefined; | ||
} | ||
/** | ||
* @param {unknown} target | ||
* @returns {boolean} | ||
*/ | ||
function isFloat16ArrayBits(target) { | ||
return target instanceof Float16Array && _(target).proxy !== undefined; | ||
} | ||
/** | ||
* @param {unknown} target | ||
* @throws {TypeError} | ||
@@ -1249,4 +1401,4 @@ */ | ||
function assertFloat16Array(target) { | ||
if (!isFloat16Array(target)) { | ||
function assertFloat16ArrayBits(target) { | ||
if (!isFloat16ArrayBits(target)) { | ||
throw new TypeError("This is not a Float16Array"); | ||
@@ -1286,3 +1438,3 @@ } | ||
// peel off proxy | ||
if (isFloat16Array(thisArg)) { | ||
if (isFloat16ArrayProxy(thisArg)) { | ||
return Reflect.apply(func, _(thisArg).target, args); | ||
@@ -1329,16 +1481,37 @@ } | ||
class Float16Array extends Uint16Array { | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-typedarray | ||
*/ | ||
constructor(input, byteOffset, length) { | ||
// input Float16Array | ||
if (isFloat16Array(input)) { | ||
super(_(input).target); // 22.2.1.3, 22.2.1.4 TypedArray, Array, ArrayLike, Iterable | ||
} else if (input !== null && typeof input === "object" && !isArrayBuffer$1(input)) { | ||
// if input is not ArrayLike and Iterable, get Array | ||
const arrayLike = !Reflect.has(input, "length") && input[Symbol.iterator] !== undefined ? [...input] : input; | ||
const length = arrayLike.length; | ||
super(length); | ||
if (isFloat16ArrayProxy(input)) { | ||
super(_(input).target); // object without ArrayBuffer | ||
} else if (isObject(input) && !isArrayBuffer$1(input)) { | ||
let list; | ||
let length; // TypedArray | ||
if (isTypedArray$1(input)) { | ||
const buffer = input.buffer; | ||
list = input; | ||
length = input.length; | ||
/** @type {ArrayBufferConstructor} */ | ||
const BufferConstructor = !isSharedArrayBuffer(buffer) ? SpeciesConstructor(buffer, ArrayBuffer) : ArrayBuffer; | ||
const data = new BufferConstructor(length * 2); | ||
super(data); // Iterable (Array) | ||
} else if (isIterable(input)) { | ||
list = [...input]; | ||
length = list.length; | ||
super(length); // ArrayLike | ||
} else { | ||
list = input; | ||
length = LengthOfArrayLike(input); | ||
super(length); | ||
} // set values | ||
for (let i = 0; i < length; ++i) { | ||
// super (Uint16Array) | ||
this[i] = roundToFloat16Bits(arrayLike[i]); | ||
} // 22.2.1.2, 22.2.1.5 primitive, ArrayBuffer | ||
this[i] = roundToFloat16Bits(list[i]); | ||
} // primitive, ArrayBuffer | ||
@@ -1374,3 +1547,6 @@ } else { | ||
return proxy; | ||
} // static methods | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.from | ||
*/ | ||
@@ -1389,3 +1565,7 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.of | ||
*/ | ||
static of(...items) { | ||
@@ -1402,19 +1582,20 @@ const length = items.length; | ||
return proxy; | ||
} // iterate methods | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.keys | ||
*/ | ||
[Symbol.iterator]() { | ||
const arrayIterator = super[Symbol.iterator](); | ||
return wrapInArrayIterator(function* () { | ||
for (const val of arrayIterator) { | ||
yield convertToNumber(val); | ||
} | ||
}()); | ||
} | ||
keys() { | ||
assertFloat16ArrayBits(this); | ||
return super.keys(); | ||
} | ||
/** | ||
* limitation: returns a object whose prototype is not `%ArrayIteratorPrototype%` | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.values | ||
*/ | ||
values() { | ||
assertFloat16ArrayBits(this); | ||
const arrayIterator = super.values(); | ||
@@ -1427,4 +1608,10 @@ return wrapInArrayIterator(function* () { | ||
} | ||
/** | ||
* limitation: returns a object whose prototype is not `%ArrayIteratorPrototype%` | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.entries | ||
*/ | ||
entries() { | ||
assertFloat16ArrayBits(this); | ||
const arrayIterator = super.entries(); | ||
@@ -1437,5 +1624,9 @@ return wrapInArrayIterator(function* () { | ||
} | ||
/** | ||
* @see https://tc39.es/proposal-relative-indexing-method/#sec-%typedarray%.prototype.at | ||
*/ | ||
at(index) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const length = this.length; | ||
@@ -1450,27 +1641,45 @@ const relativeIndex = ToIntegerOrInfinity(index); | ||
return convertToNumber(this[k]); | ||
} // functional methods | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.map | ||
*/ | ||
map(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
const length = this.length; | ||
const Constructor = SpeciesConstructor(this, Float16Array); | ||
const proxy = new Constructor(length); | ||
assertFloat16Array(proxy); | ||
const Constructor = SpeciesConstructor(this, Float16Array); // for optimization | ||
const float16bits = _(proxy).target; | ||
if (Constructor === Float16Array) { | ||
const proxy = new Float16Array(length); | ||
const float16bits = _(proxy).target; | ||
for (let i = 0; i < length; ++i) { | ||
const val = convertToNumber(this[i]); | ||
float16bits[i] = roundToFloat16Bits(callback.call(thisArg, val, i, _(this).proxy)); | ||
} | ||
return proxy; | ||
} | ||
const array = new Constructor(length); | ||
for (let i = 0; i < length; ++i) { | ||
const val = convertToNumber(this[i]); | ||
float16bits[i] = roundToFloat16Bits(callback.call(thisArg, val, i, _(this).proxy)); | ||
array[i] = callback.call(thisArg, val, i, _(this).proxy); | ||
} | ||
return proxy; | ||
return array; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.filter | ||
*/ | ||
filter(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
const array = []; | ||
const kept = []; | ||
@@ -1481,3 +1690,3 @@ for (let i = 0, l = this.length; i < l; ++i) { | ||
if (callback.call(thisArg, val, i, _(this).proxy)) { | ||
array.push(val); | ||
kept.push(val); | ||
} | ||
@@ -1487,9 +1696,12 @@ } | ||
const Constructor = SpeciesConstructor(this, Float16Array); | ||
const proxy = new Constructor(array); | ||
assertFloat16Array(proxy); | ||
return proxy; | ||
const array = new Constructor(kept); | ||
return array; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduce | ||
*/ | ||
reduce(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const length = this.length; | ||
@@ -1517,5 +1729,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduceright | ||
*/ | ||
reduceRight(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const length = this.length; | ||
@@ -1543,5 +1759,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.foreach | ||
*/ | ||
forEach(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
@@ -1553,5 +1773,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.find | ||
*/ | ||
find(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
@@ -1567,5 +1791,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.findindex | ||
*/ | ||
findIndex(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
@@ -1583,5 +1811,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/proposal-array-find-from-last/index.html#sec-%typedarray%.prototype.findlast | ||
*/ | ||
findLast(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
@@ -1597,5 +1829,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/proposal-array-find-from-last/index.html#sec-%typedarray%.prototype.findlastindex | ||
*/ | ||
findLastIndex(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
@@ -1613,5 +1849,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.every | ||
*/ | ||
every(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
@@ -1627,5 +1867,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.some | ||
*/ | ||
some(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
@@ -1640,19 +1884,26 @@ | ||
return false; | ||
} // change element methods | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.set | ||
*/ | ||
set(input, ...opts) { | ||
assertFloat16Array(this); | ||
const offset = opts[0]; | ||
assertFloat16ArrayBits(this); | ||
const offset = ToIntegerOrInfinity(opts[0]); | ||
if (offset < 0) { | ||
throw RangeError("offset is out of bounds"); | ||
} | ||
let float16bits; // input Float16Array | ||
if (isFloat16Array(input)) { | ||
if (isFloat16ArrayProxy(input)) { | ||
float16bits = _(input).target; // input others | ||
} else { | ||
const arrayLike = !Reflect.has(input, "length") && input[Symbol.iterator] !== undefined ? [...input] : input; | ||
const length = arrayLike.length; | ||
const length = LengthOfArrayLike(input); | ||
float16bits = new Uint16Array(length); | ||
for (let i = 0, l = arrayLike.length; i < l; ++i) { | ||
float16bits[i] = roundToFloat16Bits(arrayLike[i]); | ||
for (let i = 0; i < length; ++i) { | ||
float16bits[i] = roundToFloat16Bits(input[i]); | ||
} | ||
@@ -1663,29 +1914,41 @@ } | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reverse | ||
*/ | ||
reverse() { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
super.reverse(); | ||
return _(this).proxy; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.fill | ||
*/ | ||
fill(value, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
super.fill(roundToFloat16Bits(value), ...opts); | ||
return _(this).proxy; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.copywithin | ||
*/ | ||
copyWithin(target, start, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
super.copyWithin(target, start, ...opts); | ||
return _(this).proxy; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort | ||
*/ | ||
sort(...opts) { | ||
assertFloat16Array(this); | ||
let compareFunction = opts[0]; | ||
assertFloat16ArrayBits(this); | ||
const compareFunction = opts[0] !== undefined ? opts[0] : defaultCompareFunction; | ||
if (compareFunction === undefined) { | ||
compareFunction = defaultCompareFunction; | ||
} | ||
const _convertToNumber = memoize(convertToNumber); | ||
@@ -1697,28 +1960,79 @@ | ||
return _(this).proxy; | ||
} // copy element methods | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.slice | ||
*/ | ||
slice(...opts) { | ||
assertFloat16Array(this); | ||
const uint16 = new Uint16Array(this.buffer, this.byteOffset, this.length); | ||
const float16bits = uint16.slice(...opts); | ||
const Constructor = SpeciesConstructor(this, Float16Array); | ||
const proxy = new Constructor(float16bits.buffer); | ||
assertFloat16Array(proxy); | ||
return proxy; | ||
assertFloat16ArrayBits(this); | ||
const Constructor = SpeciesConstructor(this, Float16Array); // for optimization | ||
if (Constructor === Float16Array) { | ||
const uint16 = new Uint16Array(this.buffer, this.byteOffset, this.length); | ||
const float16bits = uint16.slice(...opts); | ||
const proxy = new Float16Array(float16bits.buffer); | ||
return proxy; | ||
} | ||
const length = this.length; | ||
const start = ToIntegerOrInfinity(opts[0]); | ||
const end = opts[1] === undefined ? length : ToIntegerOrInfinity(opts[1]); | ||
let k; | ||
if (start === -Infinity) { | ||
k = 0; | ||
} else if (start < 0) { | ||
k = length + start > 0 ? length + start : 0; | ||
} else { | ||
k = length < start ? length : start; | ||
} | ||
let final; | ||
if (end === -Infinity) { | ||
final = 0; | ||
} else if (end < 0) { | ||
final = length + end > 0 ? length + end : 0; | ||
} else { | ||
final = length < end ? length : end; | ||
} | ||
const count = final - k > 0 ? final - k : 0; | ||
const array = new Constructor(count); | ||
if (count <= 0) { | ||
return array; | ||
} | ||
let n = 0; | ||
while (k < final) { | ||
array[n] = convertToNumber(this[k]); | ||
++k; | ||
++n; | ||
} | ||
return array; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.subarray | ||
*/ | ||
subarray(...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const uint16 = new Uint16Array(this.buffer, this.byteOffset, this.length); | ||
const float16bits = uint16.subarray(...opts); | ||
const Constructor = SpeciesConstructor(this, Float16Array); | ||
const proxy = new Constructor(float16bits.buffer, float16bits.byteOffset, float16bits.length); | ||
assertFloat16Array(proxy); | ||
return proxy; | ||
} // contains methods | ||
const array = new Constructor(float16bits.buffer, float16bits.byteOffset, float16bits.length); | ||
return array; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.indexof | ||
*/ | ||
indexOf(element, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const length = this.length; | ||
@@ -1747,5 +2061,9 @@ let from = ToIntegerOrInfinity(opts[0]); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.lastindexof | ||
*/ | ||
lastIndexOf(element, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const length = this.length; | ||
@@ -1772,5 +2090,9 @@ let from = opts.length >= 1 ? ToIntegerOrInfinity(opts[0]) : length - 1; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.includes | ||
*/ | ||
includes(element, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const length = this.length; | ||
@@ -1806,19 +2128,30 @@ let from = ToIntegerOrInfinity(opts[0]); | ||
return false; | ||
} // string methods | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.join | ||
*/ | ||
join(...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const array = copyToArray(this); | ||
return array.join(...opts); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.tolocalestring | ||
*/ | ||
toLocaleString(...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const array = copyToArray(this); | ||
return array.toLocaleString(...opts); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-get-%typedarray%.prototype-@@tostringtag | ||
*/ | ||
get [Symbol.toStringTag]() { | ||
if (isFloat16Array(this)) { | ||
if (isFloat16ArrayBits(this)) { | ||
return "Float16Array"; | ||
@@ -1829,7 +2162,16 @@ } | ||
} | ||
const Float16Array$prototype = Float16Array.prototype; | ||
const Float16ArrayPrototype = Float16Array.prototype; | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype-@@iterator | ||
*/ | ||
Object.defineProperty(Float16ArrayPrototype, Symbol.iterator, { | ||
value: Float16ArrayPrototype.values, | ||
writable: true, | ||
configurable: true | ||
}); | ||
const defaultFloat16ArrayMethods = new WeakSet(); | ||
for (const key of Reflect.ownKeys(Float16Array$prototype)) { | ||
const val = Float16Array$prototype[key]; | ||
for (const key of Reflect.ownKeys(Float16ArrayPrototype)) { | ||
const val = Float16ArrayPrototype[key]; | ||
@@ -1836,0 +2178,0 @@ if (typeof val === "function") { |
@@ -29,7 +29,16 @@ "use strict"; | ||
function isFloat16Array(target) { | ||
return target instanceof Float16Array; | ||
function isFloat16ArrayProxy(target) { | ||
return target instanceof Float16Array && _(target).target !== undefined; | ||
} | ||
/** | ||
* @param {unknown} target | ||
* @returns {boolean} | ||
*/ | ||
function isFloat16ArrayBits(target) { | ||
return target instanceof Float16Array && _(target).proxy !== undefined; | ||
} | ||
/** | ||
* @param {unknown} target | ||
* @throws {TypeError} | ||
@@ -39,4 +48,4 @@ */ | ||
function assertFloat16Array(target) { | ||
if (!isFloat16Array(target)) { | ||
function assertFloat16ArrayBits(target) { | ||
if (!isFloat16ArrayBits(target)) { | ||
throw new TypeError("This is not a Float16Array"); | ||
@@ -76,3 +85,3 @@ } | ||
// peel off proxy | ||
if (isFloat16Array(thisArg)) { | ||
if (isFloat16ArrayProxy(thisArg)) { | ||
return Reflect.apply(func, _(thisArg).target, args); | ||
@@ -120,16 +129,37 @@ } | ||
class Float16Array extends Uint16Array { | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-typedarray | ||
*/ | ||
constructor(input, byteOffset, length) { | ||
// input Float16Array | ||
if (isFloat16Array(input)) { | ||
super(_(input).target); // 22.2.1.3, 22.2.1.4 TypedArray, Array, ArrayLike, Iterable | ||
} else if (input !== null && typeof input === "object" && !(0, _is.isArrayBuffer)(input)) { | ||
// if input is not ArrayLike and Iterable, get Array | ||
const arrayLike = !Reflect.has(input, "length") && input[Symbol.iterator] !== undefined ? [...input] : input; | ||
const length = arrayLike.length; | ||
super(length); | ||
if (isFloat16ArrayProxy(input)) { | ||
super(_(input).target); // object without ArrayBuffer | ||
} else if ((0, _is.isObject)(input) && !(0, _is.isArrayBuffer)(input)) { | ||
let list; | ||
let length; // TypedArray | ||
if ((0, _is.isTypedArray)(input)) { | ||
const buffer = input.buffer; | ||
list = input; | ||
length = input.length; | ||
/** @type {ArrayBufferConstructor} */ | ||
const BufferConstructor = !(0, _is.isSharedArrayBuffer)(buffer) ? (0, _spec.SpeciesConstructor)(buffer, ArrayBuffer) : ArrayBuffer; | ||
const data = new BufferConstructor(length * 2); | ||
super(data); // Iterable (Array) | ||
} else if ((0, _is.isIterable)(input)) { | ||
list = [...input]; | ||
length = list.length; | ||
super(length); // ArrayLike | ||
} else { | ||
list = input; | ||
length = (0, _spec.LengthOfArrayLike)(input); | ||
super(length); | ||
} // set values | ||
for (let i = 0; i < length; ++i) { | ||
// super (Uint16Array) | ||
this[i] = (0, _lib.roundToFloat16Bits)(arrayLike[i]); | ||
} // 22.2.1.2, 22.2.1.5 primitive, ArrayBuffer | ||
this[i] = (0, _lib.roundToFloat16Bits)(list[i]); | ||
} // primitive, ArrayBuffer | ||
@@ -165,3 +195,6 @@ } else { | ||
return proxy; | ||
} // static methods | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.from | ||
*/ | ||
@@ -180,3 +213,7 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.of | ||
*/ | ||
static of(...items) { | ||
@@ -193,19 +230,20 @@ const length = items.length; | ||
return proxy; | ||
} // iterate methods | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.keys | ||
*/ | ||
[Symbol.iterator]() { | ||
const arrayIterator = super[Symbol.iterator](); | ||
return (0, _arrayIterator.wrapInArrayIterator)(function* () { | ||
for (const val of arrayIterator) { | ||
yield (0, _lib.convertToNumber)(val); | ||
} | ||
}()); | ||
} | ||
keys() { | ||
assertFloat16ArrayBits(this); | ||
return super.keys(); | ||
} | ||
/** | ||
* limitation: returns a object whose prototype is not `%ArrayIteratorPrototype%` | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.values | ||
*/ | ||
values() { | ||
assertFloat16ArrayBits(this); | ||
const arrayIterator = super.values(); | ||
@@ -218,4 +256,10 @@ return (0, _arrayIterator.wrapInArrayIterator)(function* () { | ||
} | ||
/** | ||
* limitation: returns a object whose prototype is not `%ArrayIteratorPrototype%` | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.entries | ||
*/ | ||
entries() { | ||
assertFloat16ArrayBits(this); | ||
const arrayIterator = super.entries(); | ||
@@ -228,5 +272,9 @@ return (0, _arrayIterator.wrapInArrayIterator)(function* () { | ||
} | ||
/** | ||
* @see https://tc39.es/proposal-relative-indexing-method/#sec-%typedarray%.prototype.at | ||
*/ | ||
at(index) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const length = this.length; | ||
@@ -241,27 +289,45 @@ const relativeIndex = (0, _spec.ToIntegerOrInfinity)(index); | ||
return (0, _lib.convertToNumber)(this[k]); | ||
} // functional methods | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.map | ||
*/ | ||
map(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
const length = this.length; | ||
const Constructor = (0, _spec.SpeciesConstructor)(this, Float16Array); | ||
const proxy = new Constructor(length); | ||
assertFloat16Array(proxy); | ||
const Constructor = (0, _spec.SpeciesConstructor)(this, Float16Array); // for optimization | ||
const float16bits = _(proxy).target; | ||
if (Constructor === Float16Array) { | ||
const proxy = new Float16Array(length); | ||
const float16bits = _(proxy).target; | ||
for (let i = 0; i < length; ++i) { | ||
const val = (0, _lib.convertToNumber)(this[i]); | ||
float16bits[i] = (0, _lib.roundToFloat16Bits)(callback.call(thisArg, val, i, _(this).proxy)); | ||
} | ||
return proxy; | ||
} | ||
const array = new Constructor(length); | ||
for (let i = 0; i < length; ++i) { | ||
const val = (0, _lib.convertToNumber)(this[i]); | ||
float16bits[i] = (0, _lib.roundToFloat16Bits)(callback.call(thisArg, val, i, _(this).proxy)); | ||
array[i] = callback.call(thisArg, val, i, _(this).proxy); | ||
} | ||
return proxy; | ||
return array; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.filter | ||
*/ | ||
filter(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
const array = []; | ||
const kept = []; | ||
@@ -272,3 +338,3 @@ for (let i = 0, l = this.length; i < l; ++i) { | ||
if (callback.call(thisArg, val, i, _(this).proxy)) { | ||
array.push(val); | ||
kept.push(val); | ||
} | ||
@@ -278,9 +344,12 @@ } | ||
const Constructor = (0, _spec.SpeciesConstructor)(this, Float16Array); | ||
const proxy = new Constructor(array); | ||
assertFloat16Array(proxy); | ||
return proxy; | ||
const array = new Constructor(kept); | ||
return array; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduce | ||
*/ | ||
reduce(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const length = this.length; | ||
@@ -308,5 +377,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduceright | ||
*/ | ||
reduceRight(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const length = this.length; | ||
@@ -334,5 +407,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.foreach | ||
*/ | ||
forEach(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
@@ -344,5 +421,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.find | ||
*/ | ||
find(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
@@ -358,5 +439,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.findindex | ||
*/ | ||
findIndex(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
@@ -374,5 +459,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/proposal-array-find-from-last/index.html#sec-%typedarray%.prototype.findlast | ||
*/ | ||
findLast(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
@@ -388,5 +477,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/proposal-array-find-from-last/index.html#sec-%typedarray%.prototype.findlastindex | ||
*/ | ||
findLastIndex(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
@@ -404,5 +497,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.every | ||
*/ | ||
every(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
@@ -418,5 +515,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.some | ||
*/ | ||
some(callback, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const thisArg = opts[0]; | ||
@@ -431,19 +532,26 @@ | ||
return false; | ||
} // change element methods | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.set | ||
*/ | ||
set(input, ...opts) { | ||
assertFloat16Array(this); | ||
const offset = opts[0]; | ||
assertFloat16ArrayBits(this); | ||
const offset = (0, _spec.ToIntegerOrInfinity)(opts[0]); | ||
if (offset < 0) { | ||
throw RangeError("offset is out of bounds"); | ||
} | ||
let float16bits; // input Float16Array | ||
if (isFloat16Array(input)) { | ||
if (isFloat16ArrayProxy(input)) { | ||
float16bits = _(input).target; // input others | ||
} else { | ||
const arrayLike = !Reflect.has(input, "length") && input[Symbol.iterator] !== undefined ? [...input] : input; | ||
const length = arrayLike.length; | ||
const length = (0, _spec.LengthOfArrayLike)(input); | ||
float16bits = new Uint16Array(length); | ||
for (let i = 0, l = arrayLike.length; i < l; ++i) { | ||
float16bits[i] = (0, _lib.roundToFloat16Bits)(arrayLike[i]); | ||
for (let i = 0; i < length; ++i) { | ||
float16bits[i] = (0, _lib.roundToFloat16Bits)(input[i]); | ||
} | ||
@@ -454,29 +562,41 @@ } | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reverse | ||
*/ | ||
reverse() { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
super.reverse(); | ||
return _(this).proxy; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.fill | ||
*/ | ||
fill(value, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
super.fill((0, _lib.roundToFloat16Bits)(value), ...opts); | ||
return _(this).proxy; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.copywithin | ||
*/ | ||
copyWithin(target, start, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
super.copyWithin(target, start, ...opts); | ||
return _(this).proxy; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort | ||
*/ | ||
sort(...opts) { | ||
assertFloat16Array(this); | ||
let compareFunction = opts[0]; | ||
assertFloat16ArrayBits(this); | ||
const compareFunction = opts[0] !== undefined ? opts[0] : _spec.defaultCompareFunction; | ||
if (compareFunction === undefined) { | ||
compareFunction = _spec.defaultCompareFunction; | ||
} | ||
const _convertToNumber = (0, _memoize.default)(_lib.convertToNumber); | ||
@@ -488,28 +608,79 @@ | ||
return _(this).proxy; | ||
} // copy element methods | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.slice | ||
*/ | ||
slice(...opts) { | ||
assertFloat16Array(this); | ||
const uint16 = new Uint16Array(this.buffer, this.byteOffset, this.length); | ||
const float16bits = uint16.slice(...opts); | ||
const Constructor = (0, _spec.SpeciesConstructor)(this, Float16Array); | ||
const proxy = new Constructor(float16bits.buffer); | ||
assertFloat16Array(proxy); | ||
return proxy; | ||
assertFloat16ArrayBits(this); | ||
const Constructor = (0, _spec.SpeciesConstructor)(this, Float16Array); // for optimization | ||
if (Constructor === Float16Array) { | ||
const uint16 = new Uint16Array(this.buffer, this.byteOffset, this.length); | ||
const float16bits = uint16.slice(...opts); | ||
const proxy = new Float16Array(float16bits.buffer); | ||
return proxy; | ||
} | ||
const length = this.length; | ||
const start = (0, _spec.ToIntegerOrInfinity)(opts[0]); | ||
const end = opts[1] === undefined ? length : (0, _spec.ToIntegerOrInfinity)(opts[1]); | ||
let k; | ||
if (start === -Infinity) { | ||
k = 0; | ||
} else if (start < 0) { | ||
k = length + start > 0 ? length + start : 0; | ||
} else { | ||
k = length < start ? length : start; | ||
} | ||
let final; | ||
if (end === -Infinity) { | ||
final = 0; | ||
} else if (end < 0) { | ||
final = length + end > 0 ? length + end : 0; | ||
} else { | ||
final = length < end ? length : end; | ||
} | ||
const count = final - k > 0 ? final - k : 0; | ||
const array = new Constructor(count); | ||
if (count <= 0) { | ||
return array; | ||
} | ||
let n = 0; | ||
while (k < final) { | ||
array[n] = (0, _lib.convertToNumber)(this[k]); | ||
++k; | ||
++n; | ||
} | ||
return array; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.subarray | ||
*/ | ||
subarray(...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const uint16 = new Uint16Array(this.buffer, this.byteOffset, this.length); | ||
const float16bits = uint16.subarray(...opts); | ||
const Constructor = (0, _spec.SpeciesConstructor)(this, Float16Array); | ||
const proxy = new Constructor(float16bits.buffer, float16bits.byteOffset, float16bits.length); | ||
assertFloat16Array(proxy); | ||
return proxy; | ||
} // contains methods | ||
const array = new Constructor(float16bits.buffer, float16bits.byteOffset, float16bits.length); | ||
return array; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.indexof | ||
*/ | ||
indexOf(element, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const length = this.length; | ||
@@ -538,5 +709,9 @@ let from = (0, _spec.ToIntegerOrInfinity)(opts[0]); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.lastindexof | ||
*/ | ||
lastIndexOf(element, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const length = this.length; | ||
@@ -563,5 +738,9 @@ let from = opts.length >= 1 ? (0, _spec.ToIntegerOrInfinity)(opts[0]) : length - 1; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.includes | ||
*/ | ||
includes(element, ...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const length = this.length; | ||
@@ -597,19 +776,30 @@ let from = (0, _spec.ToIntegerOrInfinity)(opts[0]); | ||
return false; | ||
} // string methods | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.join | ||
*/ | ||
join(...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const array = copyToArray(this); | ||
return array.join(...opts); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.tolocalestring | ||
*/ | ||
toLocaleString(...opts) { | ||
assertFloat16Array(this); | ||
assertFloat16ArrayBits(this); | ||
const array = copyToArray(this); | ||
return array.toLocaleString(...opts); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-get-%typedarray%.prototype-@@tostringtag | ||
*/ | ||
get [Symbol.toStringTag]() { | ||
if (isFloat16Array(this)) { | ||
if (isFloat16ArrayBits(this)) { | ||
return "Float16Array"; | ||
@@ -622,7 +812,16 @@ } | ||
exports.default = Float16Array; | ||
const Float16Array$prototype = Float16Array.prototype; | ||
const Float16ArrayPrototype = Float16Array.prototype; | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype-@@iterator | ||
*/ | ||
Object.defineProperty(Float16ArrayPrototype, Symbol.iterator, { | ||
value: Float16ArrayPrototype.values, | ||
writable: true, | ||
configurable: true | ||
}); | ||
const defaultFloat16ArrayMethods = new WeakSet(); | ||
for (const key of Reflect.ownKeys(Float16Array$prototype)) { | ||
const val = Float16Array$prototype[key]; | ||
for (const key of Reflect.ownKeys(Float16ArrayPrototype)) { | ||
const val = Float16ArrayPrototype[key]; | ||
@@ -629,0 +828,0 @@ if (typeof val === "function") { |
@@ -7,2 +7,4 @@ "use strict"; | ||
exports.isDataView = isDataView; | ||
exports.isSharedArrayBuffer = isSharedArrayBuffer; | ||
exports.isIterable = isIterable; | ||
exports.isCanonicalIntegerIndexString = isCanonicalIntegerIndexString; | ||
@@ -21,19 +23,45 @@ Object.defineProperty(exports, "isObject", { | ||
}); | ||
Object.defineProperty(exports, "isTypedArray", { | ||
enumerable: true, | ||
get: function () { | ||
return _isTypedArray.default; | ||
} | ||
}); | ||
var _isObject = _interopRequireDefault(require("lodash/isObject.js")); | ||
var _spec = require("./spec.js"); | ||
var _isObject = _interopRequireDefault(require("lodash/isObject.js")); | ||
var _isArrayBuffer = _interopRequireDefault(require("lodash/isArrayBuffer.js")); | ||
var _isTypedArray = _interopRequireDefault(require("lodash/isTypedArray.js")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* @param {unknown} view | ||
* @param {unknown} value | ||
* @returns {boolean} | ||
*/ | ||
function isDataView(view) { | ||
return ArrayBuffer.isView(view) && Object.prototype.toString.call(view) === "[object DataView]"; | ||
function isDataView(value) { | ||
return ArrayBuffer.isView(value) && Object.prototype.toString.call(value) === "[object DataView]"; | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {boolean} | ||
*/ | ||
function isSharedArrayBuffer(value) { | ||
return Object.prototype.toString.call(value) === "[object SharedArrayBuffer]"; | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {boolean} | ||
*/ | ||
function isIterable(value) { | ||
return (0, _isObject.default)(value) && typeof value[Symbol.iterator] === "function"; | ||
} | ||
/** | ||
* @param {unknown} key | ||
@@ -40,0 +68,0 @@ * @returns {boolean} |
@@ -19,25 +19,25 @@ "use strict"; | ||
if (e < -27) { | ||
baseTable[i | 0x000] = 0x0000; | ||
baseTable[i] = 0x0000; | ||
baseTable[i | 0x100] = 0x8000; | ||
shiftTable[i | 0x000] = 24; | ||
shiftTable[i] = 24; | ||
shiftTable[i | 0x100] = 24; // small number (denorm) | ||
} else if (e < -14) { | ||
baseTable[i | 0x000] = 0x0400 >> -e - 14; | ||
baseTable[i] = 0x0400 >> -e - 14; | ||
baseTable[i | 0x100] = 0x0400 >> -e - 14 | 0x8000; | ||
shiftTable[i | 0x000] = -e - 1; | ||
shiftTable[i] = -e - 1; | ||
shiftTable[i | 0x100] = -e - 1; // normal number | ||
} else if (e <= 15) { | ||
baseTable[i | 0x000] = e + 15 << 10; | ||
baseTable[i] = e + 15 << 10; | ||
baseTable[i | 0x100] = e + 15 << 10 | 0x8000; | ||
shiftTable[i | 0x000] = 13; | ||
shiftTable[i] = 13; | ||
shiftTable[i | 0x100] = 13; // large number (Infinity, -Infinity) | ||
} else if (e < 128) { | ||
baseTable[i | 0x000] = 0x7c00; | ||
baseTable[i] = 0x7c00; | ||
baseTable[i | 0x100] = 0xfc00; | ||
shiftTable[i | 0x000] = 24; | ||
shiftTable[i] = 24; | ||
shiftTable[i | 0x100] = 24; // stay (NaN, Infinity, -Infinity) | ||
} else { | ||
baseTable[i | 0x000] = 0x7c00; | ||
baseTable[i] = 0x7c00; | ||
baseTable[i | 0x100] = 0xfc00; | ||
shiftTable[i | 0x000] = 13; | ||
shiftTable[i] = 13; | ||
shiftTable[i | 0x100] = 13; | ||
@@ -44,0 +44,0 @@ } |
@@ -7,2 +7,3 @@ "use strict"; | ||
exports.ToIntegerOrInfinity = ToIntegerOrInfinity; | ||
exports.LengthOfArrayLike = LengthOfArrayLike; | ||
exports.SpeciesConstructor = SpeciesConstructor; | ||
@@ -36,2 +37,30 @@ exports.defaultCompareFunction = defaultCompareFunction; | ||
* @param {unknown} target | ||
* @returns {number} | ||
*/ | ||
function ToLength(target) { | ||
const length = ToIntegerOrInfinity(target); | ||
if (length < 0) { | ||
return 0; | ||
} | ||
return length < Number.MAX_SAFE_INTEGER ? length : Number.MAX_SAFE_INTEGER; | ||
} | ||
/** | ||
* @param {object} arrayLike | ||
* @returns {number} | ||
*/ | ||
function LengthOfArrayLike(arrayLike) { | ||
if (!(0, _is.isObject)(arrayLike)) { | ||
throw TypeError("this is not a object"); | ||
} | ||
return ToLength(arrayLike.length); | ||
} | ||
/** | ||
* @param {object} target | ||
* @param {Function} defaultConstructor | ||
@@ -38,0 +67,0 @@ * @returns {Function} |
{ | ||
"name": "@petamoriken/float16", | ||
"description": "half precision floating point for JavaScript", | ||
"version": "3.2.2", | ||
"version": "3.2.3", | ||
"main": "./lib/index.js", | ||
@@ -6,0 +6,0 @@ "module": "./src/index.mjs", |
# <a href="https://git.io/float16" target="_blank">@petamoriken/float16</a> | ||
half precision floating point for JavaScript | ||
see [ES Discuss Float16Array topic](https://esdiscuss.org/topic/float16array) | ||
See [ES Discuss Float16Array topic](https://esdiscuss.org/topic/float16array) | ||
@@ -75,3 +75,3 @@ [![npm](https://img.shields.io/npm/v/@petamoriken/float16.svg?style=flat-square)](https://www.npmjs.com/package/@petamoriken/float16) | ||
const array = new Float16Array([1.0, 1.1, 1.2]); | ||
for(const val of array) { | ||
for (const val of array) { | ||
console.log(val); // => 1, 1.099609375, 1.19921875 | ||
@@ -125,5 +125,5 @@ } | ||
Built-in `TypedArray` objects uses "internal slots" for built-in methods. Some limitations exist because the `Proxy` object can't trap internal slots ([explanation](https://javascript.info/proxy#built-in-objects-internal-slots)). | ||
### Built-in functions | ||
### Built-in Functions | ||
Built-in `TypedArray` objects use "internal slots" for built-in methods. Some limitations exist because the `Proxy` object can't trap internal slots ([explanation](https://javascript.info/proxy#built-in-objects-internal-slots)). | ||
@@ -139,5 +139,9 @@ This library isn't polyfill, in other words, it doesn't change native global functions and static/prototype methods. | ||
### `Float16Array` prototype methods | ||
Due to implementation reasons, some details of `Float16Array` prototype methods may differ from the ECMAScript specification. See JSDoc comments in `src/Float16Array.mjs`. | ||
### WebGL | ||
WebGL requires `Uint16Array` for buffer or texture data whose type are `gl.HALF_FLOAT` (WebGL 2) or `ext.HALF_FLOAT_OES` (WebGL 1 extension). Do not apply the `Float16Array` object directly to `gl.bufferData` or `gl.texImage2D` etc. | ||
WebGL requires `Uint16Array` for buffer or texture data whose types are `gl.HALF_FLOAT` (WebGL 2) or `ext.HALF_FLOAT_OES` (WebGL 1 extension). Do not apply the `Float16Array` object directly to `gl.bufferData` or `gl.texImage2D` etc. | ||
@@ -148,5 +152,5 @@ ```js | ||
const vertices = new Float16Array([ | ||
-0.5, -0.5, 0, | ||
0.5, -0.5, 0, | ||
0.5, 0.5, 0, | ||
-0.5, -0.5, 0, | ||
0.5, -0.5, 0, | ||
0.5, 0.5, 0, | ||
]); | ||
@@ -153,0 +157,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
145120
4029
207