@petamoriken/float16
Advanced tools
Comparing version 3.4.3 to 3.4.4
@@ -1,2 +0,2 @@ | ||
/*! @petamoriken/float16 v3.4.3 | MIT License - https://git.io/float16 */ | ||
/*! @petamoriken/float16 v3.4.4 | MIT License - https://git.io/float16 */ | ||
@@ -6,6 +6,8 @@ var float16 = (function (exports) { | ||
// algorithm: ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf | ||
// algorithm: http://fox-toolkit.org/ftp/fasthalffloatconversion.pdf | ||
const buffer = new ArrayBuffer(4); | ||
const floatView = new Float32Array(buffer); | ||
const uint32View = new Uint32Array(buffer); | ||
const baseTable = new Uint32Array(512); | ||
@@ -15,31 +17,41 @@ const shiftTable = new Uint32Array(512); | ||
for (let i = 0; i < 256; ++i) { | ||
const e = i - 127; // very small number (0, -0) | ||
const e = i - 127; | ||
// very small number (0, -0) | ||
if (e < -27) { | ||
baseTable[i] = 0x0000; | ||
baseTable[i] = 0x0000; | ||
baseTable[i | 0x100] = 0x8000; | ||
shiftTable[i] = 24; | ||
shiftTable[i | 0x100] = 24; // small number (denorm) | ||
shiftTable[i] = 24; | ||
shiftTable[i | 0x100] = 24; | ||
// small number (denorm) | ||
} else if (e < -14) { | ||
baseTable[i] = 0x0400 >> -e - 14; | ||
baseTable[i | 0x100] = 0x0400 >> -e - 14 | 0x8000; | ||
shiftTable[i] = -e - 1; | ||
shiftTable[i | 0x100] = -e - 1; // normal number | ||
baseTable[i] = 0x0400 >> (-e - 14); | ||
baseTable[i | 0x100] = (0x0400 >> (-e - 14)) | 0x8000; | ||
shiftTable[i] = -e - 1; | ||
shiftTable[i | 0x100] = -e - 1; | ||
// normal number | ||
} else if (e <= 15) { | ||
baseTable[i] = e + 15 << 10; | ||
baseTable[i | 0x100] = e + 15 << 10 | 0x8000; | ||
shiftTable[i] = 13; | ||
shiftTable[i | 0x100] = 13; // large number (Infinity, -Infinity) | ||
baseTable[i] = (e + 15) << 10; | ||
baseTable[i | 0x100] = ((e + 15) << 10) | 0x8000; | ||
shiftTable[i] = 13; | ||
shiftTable[i | 0x100] = 13; | ||
// large number (Infinity, -Infinity) | ||
} else if (e < 128) { | ||
baseTable[i] = 0x7c00; | ||
baseTable[i] = 0x7c00; | ||
baseTable[i | 0x100] = 0xfc00; | ||
shiftTable[i] = 24; | ||
shiftTable[i | 0x100] = 24; // stay (NaN, Infinity, -Infinity) | ||
shiftTable[i] = 24; | ||
shiftTable[i | 0x100] = 24; | ||
// stay (NaN, Infinity, -Infinity) | ||
} else { | ||
baseTable[i] = 0x7c00; | ||
baseTable[i] = 0x7c00; | ||
baseTable[i | 0x100] = 0xfc00; | ||
shiftTable[i] = 13; | ||
shiftTable[i] = 13; | ||
shiftTable[i | 0x100] = 13; | ||
} | ||
} | ||
/** | ||
@@ -50,54 +62,45 @@ * round a number to a half float number bits. | ||
*/ | ||
function roundToFloat16Bits(num) { | ||
floatView[0] = num; | ||
const f = uint32View[0]; | ||
const e = f >> 23 & 0x1ff; | ||
const e = (f >> 23) & 0x1ff; | ||
return baseTable[e] + ((f & 0x007fffff) >> shiftTable[e]); | ||
} | ||
const mantissaTable = new Uint32Array(2048); | ||
const exponentTable = new Uint32Array(64); | ||
const offsetTable = new Uint32Array(64); | ||
mantissaTable[0] = 0; | ||
for (let i = 1; i < 1024; ++i) { | ||
let m = i << 13; // zero pad mantissa bits | ||
let m = i << 13; // zero pad mantissa bits | ||
let e = 0; // zero exponent | ||
let e = 0; // zero exponent | ||
// normalized | ||
while ((m & 0x00800000) === 0) { | ||
e -= 0x00800000; // decrement exponent | ||
while((m & 0x00800000) === 0) { | ||
e -= 0x00800000; // decrement exponent | ||
m <<= 1; | ||
} | ||
m &= ~0x00800000; // clear leading 1 bit | ||
m &= ~0x00800000; // clear leading 1 bit | ||
e += 0x38800000; // adjust bias | ||
e += 0x38800000; // adjust bias | ||
mantissaTable[i] = m | e; | ||
} | ||
for (let i = 1024; i < 2048; ++i) { | ||
mantissaTable[i] = 0x38000000 + (i - 1024 << 13); | ||
mantissaTable[i] = 0x38000000 + ((i - 1024) << 13); | ||
} | ||
exponentTable[0] = 0; | ||
for (let i = 1; i < 31; ++i) { | ||
exponentTable[i] = i << 23; | ||
} | ||
exponentTable[31] = 0x47800000; | ||
exponentTable[32] = 0x80000000; | ||
for (let i = 33; i < 63; ++i) { | ||
exponentTable[i] = 0x80000000 + (i - 32 << 23); | ||
exponentTable[i] = 0x80000000 + ((i - 32) << 23); | ||
} | ||
exponentTable[63] = 0xc7800000; | ||
exponentTable[63] = 0xc7800000; | ||
offsetTable[0] = 0; | ||
for (let i = 1; i < 64; ++i) { | ||
@@ -110,2 +113,3 @@ if (i === 32) { | ||
} | ||
/** | ||
@@ -116,4 +120,2 @@ * convert a half float number bits to a number. | ||
*/ | ||
function convertToNumber(float16bits) { | ||
@@ -130,6 +132,6 @@ const m = float16bits >> 10; | ||
*/ | ||
function hfround(num) { | ||
num = Number(num); // for optimization | ||
num = Number(num); | ||
// for optimization | ||
if (!Number.isFinite(num) || num === 0) { | ||
@@ -148,5 +150,4 @@ return num; | ||
const wm = new WeakMap(); | ||
return self => { | ||
return (self) => { | ||
const storage = wm.get(self); | ||
if (storage !== undefined) { | ||
@@ -164,3 +165,4 @@ return storage; | ||
const IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())); | ||
const IteratorPrototype = Reflect.getPrototypeOf(Reflect.getPrototypeOf([][Symbol.iterator]())); | ||
const ArrayIteratorPrototype = Object.create(IteratorPrototype, { | ||
@@ -172,9 +174,11 @@ next: { | ||
writable: true, | ||
configurable: true | ||
configurable: true, | ||
}, | ||
[Symbol.toStringTag]: { | ||
value: "Array Iterator", | ||
configurable: true | ||
} | ||
configurable: true, | ||
}, | ||
}); | ||
/** | ||
@@ -184,3 +188,2 @@ * @param {Iterator<number>} iterator | ||
*/ | ||
function wrapInArrayIterator(iterator) { | ||
@@ -197,4 +200,5 @@ const arrayIterator = Object.create(ArrayIteratorPrototype); | ||
function isObject(value) { | ||
return value !== null && typeof value === "object" || typeof value === "function"; | ||
return (value !== null && typeof value === "object") || typeof value === "function"; | ||
} | ||
/** | ||
@@ -204,9 +208,10 @@ * @param {unknown} value | ||
*/ | ||
function isObjectLike(value) { | ||
return value !== null && typeof value === "object"; | ||
} // Inspired by util.types implementation of Node.js | ||
} | ||
const TypedArrayPrototype = Object.getPrototypeOf(Uint8Array).prototype; | ||
const getTypedArrayPrototypeSybolToStringTag = Object.getOwnPropertyDescriptor(TypedArrayPrototype, Symbol.toStringTag).get; | ||
// Inspired by util.types implementation of Node.js | ||
const TypedArrayPrototype = Reflect.getPrototypeOf(Uint8Array).prototype; | ||
const getTypedArrayPrototypeSybolToStringTag = Reflect.getOwnPropertyDescriptor(TypedArrayPrototype, Symbol.toStringTag).get; | ||
/** | ||
@@ -216,6 +221,6 @@ * @param {unknown} value | ||
*/ | ||
function isTypedArray(value) { | ||
return getTypedArrayPrototypeSybolToStringTag.call(value) !== undefined; | ||
} | ||
/** | ||
@@ -225,7 +230,8 @@ * @param {unknown} value | ||
*/ | ||
function isUint16Array(value) { | ||
return getTypedArrayPrototypeSybolToStringTag.call(value) === "Uint16Array"; | ||
} | ||
const toString = Object.prototype.toString; | ||
/** | ||
@@ -235,3 +241,2 @@ * @param {unknown} value | ||
*/ | ||
function isDataView(value) { | ||
@@ -252,2 +257,3 @@ if (!ArrayBuffer.isView(value)) { | ||
} | ||
/** | ||
@@ -257,6 +263,6 @@ * @param {unknown} value | ||
*/ | ||
function isArrayBuffer(value) { | ||
return isObjectLike(value) && toString.call(value) === "[object ArrayBuffer]"; | ||
} | ||
/** | ||
@@ -266,6 +272,6 @@ * @param {unknown} value | ||
*/ | ||
function isSharedArrayBuffer(value) { | ||
return isObjectLike(value) && toString.call(value) === "[object SharedArrayBuffer]"; | ||
} | ||
/** | ||
@@ -275,6 +281,6 @@ * @param {unknown} value | ||
*/ | ||
function isIterable(value) { | ||
return isObject(value) && typeof value[Symbol.iterator] === "function"; | ||
} | ||
/** | ||
@@ -284,3 +290,2 @@ * @param {unknown} value | ||
*/ | ||
function isOrdinaryArray(value) { | ||
@@ -292,3 +297,2 @@ if (!Array.isArray(value)) { | ||
const iterator = value[Symbol.iterator](); | ||
if (toString.call(iterator) !== "[object Array Iterator]") { | ||
@@ -300,2 +304,3 @@ return false; | ||
} | ||
/** | ||
@@ -305,3 +310,2 @@ * @param {unknown} value | ||
*/ | ||
function isCanonicalIntegerIndexString(value) { | ||
@@ -313,3 +317,2 @@ if (typeof value !== "string") { | ||
const number = Number(value); | ||
if (value !== number + "") { | ||
@@ -331,6 +334,6 @@ return false; | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-tointegerorinfinity | ||
* @param {unknown} target | ||
* @returns {number} | ||
*/ | ||
function ToIntegerOrInfinity(target) { | ||
@@ -353,10 +356,10 @@ const number = Number(target); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-tolength | ||
* @param {unknown} target | ||
* @returns {number} | ||
*/ | ||
function ToLength(target) { | ||
const length = ToIntegerOrInfinity(target); | ||
if (length < 0) { | ||
@@ -368,8 +371,8 @@ return 0; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-lengthofarraylike | ||
* @param {object} arrayLike | ||
* @returns {number} | ||
*/ | ||
function LengthOfArrayLike(arrayLike) { | ||
@@ -382,3 +385,5 @@ if (!isObject(arrayLike)) { | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-speciesconstructor | ||
* @param {object} target | ||
@@ -388,3 +393,2 @@ * @param {Function} defaultConstructor | ||
*/ | ||
function SpeciesConstructor(target, defaultConstructor) { | ||
@@ -396,7 +400,5 @@ if (!isObject(target)) { | ||
const constructor = target.constructor; | ||
if (constructor === undefined) { | ||
return defaultConstructor; | ||
} | ||
if (!isObject(constructor)) { | ||
@@ -407,3 +409,2 @@ throw TypeError("constructor is not a object"); | ||
const species = constructor[Symbol.species]; | ||
if (species == null) { | ||
@@ -415,3 +416,6 @@ return defaultConstructor; | ||
} | ||
/** | ||
* bigint comparisons are not supported | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort | ||
* @param {number} x | ||
@@ -421,5 +425,5 @@ * @param {number} y | ||
*/ | ||
function defaultCompare(x, y) { | ||
const [isNaN_x, isNaN_y] = [Number.isNaN(x), Number.isNaN(y)]; | ||
const isNaN_x = Number.isNaN(x); | ||
const isNaN_y = Number.isNaN(y); | ||
@@ -447,3 +451,4 @@ if (isNaN_x && isNaN_y) { | ||
if (x === 0 && y === 0) { | ||
const [isPlusZero_x, isPlusZero_y] = [Object.is(x, 0), Object.is(y, 0)]; | ||
const isPlusZero_x = Object.is(x, 0); | ||
const isPlusZero_y = Object.is(y, 0); | ||
@@ -465,2 +470,3 @@ if (!isPlusZero_x && isPlusZero_y) { | ||
const _ = createPrivateStorage(); | ||
/** | ||
@@ -470,4 +476,2 @@ * @param {unknown} target | ||
*/ | ||
function hasFloat16ArrayBrand(target) { | ||
@@ -478,8 +482,11 @@ if (!isObjectLike(target)) { | ||
const constructor = target.constructor; | ||
const prototype = Reflect.getPrototypeOf(target); | ||
if (!isObjectLike(prototype)) { | ||
return false; | ||
} | ||
const constructor = prototype.constructor; | ||
if (constructor === undefined) { | ||
return false; | ||
} | ||
if (!isObject(constructor)) { | ||
@@ -491,2 +498,3 @@ throw TypeError("constructor is not a object"); | ||
} | ||
/** | ||
@@ -496,7 +504,6 @@ * @param {unknown} target | ||
*/ | ||
function isFloat16Array(target) { | ||
return hasFloat16ArrayBrand(target) && !isTypedArray(target); | ||
} | ||
/** | ||
@@ -506,6 +513,6 @@ * @param {unknown} target | ||
*/ | ||
function isFloat16BitsArray(target) { | ||
return hasFloat16ArrayBrand(target) && isUint16Array(target); | ||
} | ||
/** | ||
@@ -515,4 +522,2 @@ * @param {unknown} target | ||
*/ | ||
function assertFloat16BitsArray(target) { | ||
@@ -523,2 +528,3 @@ if (!isFloat16BitsArray(target)) { | ||
} | ||
/** | ||
@@ -528,8 +534,6 @@ * @param {Float16Array} float16 | ||
*/ | ||
function getFloat16BitsArrayFromFloat16Array(float16) { | ||
let target = _(float16).target; // from other realms | ||
let target = _(float16).target; | ||
// from other realms | ||
if (target === undefined) { | ||
@@ -542,2 +546,3 @@ const clone = new Float16Array(float16.buffer, float16.byteOffset, float16.length); | ||
} | ||
/** | ||
@@ -547,8 +552,6 @@ * @param {ArrayLike<number>} float16bitsArray | ||
*/ | ||
function copyToArray(float16bitsArray) { | ||
const length = float16bitsArray.length; | ||
const array = []; | ||
for (let i = 0; i < length; ++i) { | ||
@@ -560,2 +563,3 @@ array[i] = convertToNumber(float16bitsArray[i]); | ||
} | ||
/** | ||
@@ -565,10 +569,7 @@ * @param {unknown} target | ||
*/ | ||
function isDefaultFloat16ArrayMethods(target) { | ||
return typeof target === "function" && defaultFloat16ArrayMethods.has(target); | ||
} | ||
/** @type {ProxyHandler<Function>} */ | ||
const applyHandler = Object.freeze({ | ||
@@ -583,8 +584,8 @@ apply(func, thisArg, args) { | ||
return Reflect.apply(func, thisArg, args); | ||
} | ||
}, | ||
}); | ||
}); | ||
const hasOwnProperty = Object.prototype.hasOwnProperty; | ||
/** @type {ProxyHandler<Float16Array>} */ | ||
const handler = Object.freeze({ | ||
@@ -597,8 +598,7 @@ get(target, key) { | ||
const ret = Reflect.get(target, key); | ||
if (!isDefaultFloat16ArrayMethods(ret)) { | ||
return ret; | ||
} // TypedArray methods can't be called by Proxy Object | ||
} | ||
// TypedArray methods can't be called by Proxy Object | ||
let proxy = _(ret).proxy; | ||
@@ -619,10 +619,10 @@ | ||
return Reflect.set(target, key, value); | ||
} | ||
}, | ||
}); | ||
}); | ||
/** | ||
* limitation: see README.md for details | ||
*/ | ||
class Float16Array extends Uint16Array { | ||
class Float16Array extends Uint16Array { | ||
/** | ||
@@ -636,16 +636,21 @@ * @see https://tc39.es/ecma262/#sec-typedarray | ||
const float16bitsArray = getFloat16BitsArrayFromFloat16Array(input); | ||
super(float16bitsArray); // object without ArrayBuffer | ||
super(float16bitsArray); | ||
// object without ArrayBuffer | ||
} else if (isObject(input) && !isArrayBuffer(input)) { | ||
let list; | ||
let length; // TypedArray | ||
let length; | ||
// TypedArray | ||
if (isTypedArray(input)) { | ||
list = input; | ||
length = input.length; | ||
const buffer = input.buffer; | ||
/** @type {ArrayBufferConstructor} */ | ||
const BufferConstructor = !isSharedArrayBuffer(buffer) ? SpeciesConstructor(buffer, ArrayBuffer) : ArrayBuffer; | ||
const data = new BufferConstructor(length * Float16Array.BYTES_PER_ELEMENT); | ||
super(data); // Iterable (Array) | ||
super(data); | ||
// Iterable (Array) | ||
} else if (isIterable(input)) { | ||
@@ -657,2 +662,3 @@ // for optimization | ||
super(length); | ||
} else { | ||
@@ -662,4 +668,5 @@ list = [...input]; | ||
super(length); | ||
} // ArrayLike | ||
} | ||
// ArrayLike | ||
} else { | ||
@@ -669,10 +676,11 @@ list = input; | ||
super(length); | ||
} // set values | ||
} | ||
// set values | ||
for (let i = 0; i < length; ++i) { | ||
// super (Uint16Array) | ||
this[i] = roundToFloat16Bits(list[i]); | ||
} // primitive, ArrayBuffer | ||
} | ||
// primitive, ArrayBuffer | ||
} else { | ||
@@ -701,9 +709,13 @@ switch (arguments.length) { | ||
const proxy = new Proxy(this, handler); // proxy private storage | ||
const proxy = new Proxy(this, handler); | ||
_(proxy).target = this; // this private storage | ||
// proxy private storage | ||
_(proxy).target = this; | ||
// this private storage | ||
_(this).proxy = proxy; | ||
return proxy; | ||
} | ||
/** | ||
@@ -713,4 +725,2 @@ * limitation: `Object.getOwnPropertyNames(Float16Array)` or `Reflect.ownKeys(Float16Array)` include this key | ||
*/ | ||
static from(src, ...opts) { | ||
@@ -729,2 +739,3 @@ // for optimization | ||
const thisArg = opts[1]; | ||
return new Float16Array(Uint16Array.from(src, function (val, ...args) { | ||
@@ -734,2 +745,3 @@ return roundToFloat16Bits(mapFunc.call(this, val, ...args)); | ||
} | ||
/** | ||
@@ -739,6 +751,5 @@ * limitation: `Object.getOwnPropertyNames(Float16Array)` or `Reflect.ownKeys(Float16Array)` include this key | ||
*/ | ||
static of(...items) { | ||
const length = items.length; | ||
const proxy = new Float16Array(length); | ||
@@ -753,11 +764,12 @@ const float16bitsArray = getFloat16BitsArrayFromFloat16Array(proxy); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.keys | ||
*/ | ||
keys() { | ||
assertFloat16BitsArray(this); | ||
return super.keys(); | ||
} | ||
/** | ||
@@ -767,13 +779,13 @@ * limitation: returns a object whose prototype is not `%ArrayIteratorPrototype%` | ||
*/ | ||
values() { | ||
assertFloat16BitsArray(this); | ||
const arrayIterator = super.values(); | ||
return wrapInArrayIterator(function* () { | ||
return wrapInArrayIterator((function* () { | ||
for (const val of arrayIterator) { | ||
yield convertToNumber(val); | ||
} | ||
}()); | ||
})()); | ||
} | ||
/** | ||
@@ -783,20 +795,19 @@ * limitation: returns a object whose prototype is not `%ArrayIteratorPrototype%` | ||
*/ | ||
entries() { | ||
assertFloat16BitsArray(this); | ||
const arrayIterator = super.entries(); | ||
return wrapInArrayIterator(function* () { | ||
return wrapInArrayIterator((function* () { | ||
for (const [i, val] of arrayIterator) { | ||
yield [i, convertToNumber(val)]; | ||
} | ||
}()); | ||
})()); | ||
} | ||
/** | ||
* @see https://tc39.es/proposal-relative-indexing-method/#sec-%typedarray%.prototype.at | ||
*/ | ||
at(index) { | ||
assertFloat16BitsArray(this); | ||
const length = this.length; | ||
@@ -812,13 +823,16 @@ const relativeIndex = ToIntegerOrInfinity(index); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.map | ||
*/ | ||
map(callback, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const thisArg = opts[0]; | ||
const length = this.length; | ||
const Constructor = SpeciesConstructor(this, Float16Array); // for optimization | ||
const Constructor = SpeciesConstructor(this, Float16Array); | ||
// for optimization | ||
if (Constructor === Float16Array) { | ||
@@ -845,15 +859,14 @@ const proxy = new Float16Array(length); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.filter | ||
*/ | ||
filter(callback, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const thisArg = opts[0]; | ||
const kept = []; | ||
for (let i = 0, l = this.length; i < l; ++i) { | ||
const val = convertToNumber(this[i]); | ||
if (callback.call(thisArg, val, i, _(this).proxy)) { | ||
@@ -866,13 +879,13 @@ kept.push(val); | ||
const array = new Constructor(kept); | ||
return array; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduce | ||
*/ | ||
reduce(callback, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const length = this.length; | ||
if (length === 0 && opts.length === 0) { | ||
@@ -883,3 +896,2 @@ throw TypeError("Reduce of empty array with no initial value"); | ||
let accumulator, start; | ||
if (opts.length === 0) { | ||
@@ -899,11 +911,10 @@ accumulator = convertToNumber(this[0]); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduceright | ||
*/ | ||
reduceRight(callback, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const length = this.length; | ||
if (length === 0 && opts.length === 0) { | ||
@@ -914,3 +925,2 @@ throw TypeError("Reduce of empty array with no initial value"); | ||
let accumulator, start; | ||
if (opts.length === 0) { | ||
@@ -930,9 +940,9 @@ accumulator = convertToNumber(this[length - 1]); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.foreach | ||
*/ | ||
forEach(callback, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const thisArg = opts[0]; | ||
@@ -944,9 +954,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.find | ||
*/ | ||
find(callback, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const thisArg = opts[0]; | ||
@@ -956,3 +966,2 @@ | ||
const value = convertToNumber(this[i]); | ||
if (callback.call(thisArg, value, i, _(this).proxy)) { | ||
@@ -963,9 +972,9 @@ return value; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.findindex | ||
*/ | ||
findIndex(callback, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const thisArg = opts[0]; | ||
@@ -975,3 +984,2 @@ | ||
const value = convertToNumber(this[i]); | ||
if (callback.call(thisArg, value, i, _(this).proxy)) { | ||
@@ -984,9 +992,9 @@ return i; | ||
} | ||
/** | ||
* @see https://tc39.es/proposal-array-find-from-last/index.html#sec-%typedarray%.prototype.findlast | ||
*/ | ||
findLast(callback, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const thisArg = opts[0]; | ||
@@ -996,3 +1004,2 @@ | ||
const value = convertToNumber(this[i]); | ||
if (callback.call(thisArg, value, i, _(this).proxy)) { | ||
@@ -1003,9 +1010,9 @@ return value; | ||
} | ||
/** | ||
* @see https://tc39.es/proposal-array-find-from-last/index.html#sec-%typedarray%.prototype.findlastindex | ||
*/ | ||
findLastIndex(callback, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const thisArg = opts[0]; | ||
@@ -1015,3 +1022,2 @@ | ||
const value = convertToNumber(this[i]); | ||
if (callback.call(thisArg, value, i, _(this).proxy)) { | ||
@@ -1024,9 +1030,9 @@ return i; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.every | ||
*/ | ||
every(callback, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const thisArg = opts[0]; | ||
@@ -1042,9 +1048,9 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.some | ||
*/ | ||
some(callback, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const thisArg = opts[0]; | ||
@@ -1060,16 +1066,15 @@ | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.set | ||
*/ | ||
set(input, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const targetOffset = ToIntegerOrInfinity(opts[0]); | ||
if (targetOffset < 0) { | ||
throw RangeError("offset is out of bounds"); | ||
} // for optimization | ||
} | ||
// for optimization | ||
if (isFloat16Array(input)) { | ||
@@ -1083,2 +1088,3 @@ // peel off Proxy | ||
const targetLength = this.length; | ||
const src = Object(input); | ||
@@ -1095,54 +1101,57 @@ const srcLength = LengthOfArrayLike(src); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reverse | ||
*/ | ||
reverse() { | ||
assertFloat16BitsArray(this); | ||
super.reverse(); | ||
return _(this).proxy; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.fill | ||
*/ | ||
fill(value, ...opts) { | ||
assertFloat16BitsArray(this); | ||
super.fill(roundToFloat16Bits(value), ...opts); | ||
return _(this).proxy; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.copywithin | ||
*/ | ||
copyWithin(target, start, ...opts) { | ||
assertFloat16BitsArray(this); | ||
super.copyWithin(target, start, ...opts); | ||
return _(this).proxy; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort | ||
*/ | ||
sort(...opts) { | ||
assertFloat16BitsArray(this); | ||
const compare = opts[0] !== undefined ? opts[0] : defaultCompare; | ||
super.sort((x, y) => { | ||
return compare(convertToNumber(x), convertToNumber(y)); | ||
}); | ||
super.sort((x, y) => { return compare(convertToNumber(x), convertToNumber(y)); }); | ||
return _(this).proxy; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.slice | ||
*/ | ||
slice(...opts) { | ||
assertFloat16BitsArray(this); | ||
const Constructor = SpeciesConstructor(this, Float16Array); // for optimization | ||
const Constructor = SpeciesConstructor(this, Float16Array); | ||
// for optimization | ||
if (Constructor === Float16Array) { | ||
@@ -1157,4 +1166,4 @@ const uint16 = new Uint16Array(this.buffer, this.byteOffset, this.length); | ||
const end = opts[1] === undefined ? length : ToIntegerOrInfinity(opts[1]); | ||
let k; | ||
if (start === -Infinity) { | ||
@@ -1169,3 +1178,2 @@ k = 0; | ||
let final; | ||
if (end === -Infinity) { | ||
@@ -1187,3 +1195,2 @@ final = 0; | ||
let n = 0; | ||
while (k < final) { | ||
@@ -1197,25 +1204,27 @@ array[n] = convertToNumber(this[k]); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.subarray | ||
*/ | ||
subarray(...opts) { | ||
assertFloat16BitsArray(this); | ||
const uint16 = new Uint16Array(this.buffer, this.byteOffset, this.length); | ||
const float16bitsArray = uint16.subarray(...opts); | ||
const Constructor = SpeciesConstructor(this, Float16Array); | ||
const array = new Constructor(float16bitsArray.buffer, float16bitsArray.byteOffset, float16bitsArray.length); | ||
return array; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.indexof | ||
*/ | ||
indexOf(element, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const length = this.length; | ||
let from = ToIntegerOrInfinity(opts[0]); | ||
if (from === Infinity) { | ||
@@ -1227,3 +1236,2 @@ return -1; | ||
from += length; | ||
if (from < 0) { | ||
@@ -1242,12 +1250,12 @@ from = 0; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.lastindexof | ||
*/ | ||
lastIndexOf(element, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const length = this.length; | ||
let from = opts.length >= 1 ? ToIntegerOrInfinity(opts[0]) : length - 1; | ||
if (from === -Infinity) { | ||
@@ -1271,12 +1279,12 @@ return -1; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.includes | ||
*/ | ||
includes(element, ...opts) { | ||
assertFloat16BitsArray(this); | ||
const length = this.length; | ||
let from = ToIntegerOrInfinity(opts[0]); | ||
if (from === Infinity) { | ||
@@ -1288,3 +1296,2 @@ return false; | ||
from += length; | ||
if (from < 0) { | ||
@@ -1296,3 +1303,2 @@ from = 0; | ||
const isNaN = Number.isNaN(element); | ||
for (let i = from, l = length; i < l; ++i) { | ||
@@ -1312,27 +1318,28 @@ const value = convertToNumber(this[i]); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.join | ||
*/ | ||
join(...opts) { | ||
assertFloat16BitsArray(this); | ||
const array = copyToArray(this); | ||
return array.join(...opts); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.tolocalestring | ||
*/ | ||
toLocaleString(...opts) { | ||
assertFloat16BitsArray(this); | ||
const array = copyToArray(this); | ||
return array.toLocaleString(...opts); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-get-%typedarray%.prototype-@@tostringtag | ||
*/ | ||
get [Symbol.toStringTag]() { | ||
@@ -1343,28 +1350,26 @@ if (isFloat16BitsArray(this)) { | ||
} | ||
} | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-typedarray.bytes_per_element | ||
*/ | ||
Object.defineProperty(Float16Array, "BYTES_PER_ELEMENT", { value: Uint16Array.BYTES_PER_ELEMENT }); | ||
Object.defineProperty(Float16Array, "BYTES_PER_ELEMENT", { | ||
value: Uint16Array.BYTES_PER_ELEMENT | ||
}); | ||
/** | ||
* limitation: It is peaked by `Object.getOwnPropertySymbols(Float16Array)` and `Reflect.ownKeys(Float16Array)` | ||
*/ | ||
Object.defineProperty(Float16Array, brand, {}); | ||
Object.defineProperty(Float16Array, brand, {}); | ||
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 | ||
configurable: true, | ||
}); | ||
const defaultFloat16ArrayMethods = new WeakSet(); | ||
for (const key of Reflect.ownKeys(Float16ArrayPrototype)) { | ||
@@ -1377,3 +1382,2 @@ // constructor is not callable | ||
const val = Float16ArrayPrototype[key]; | ||
if (typeof val === "function") { | ||
@@ -1391,3 +1395,2 @@ defaultFloat16ArrayMethods.add(val); | ||
*/ | ||
function getFloat16(dataView, byteOffset, ...opts) { | ||
@@ -1398,4 +1401,5 @@ if (!isDataView(dataView)) { | ||
return convertToNumber(dataView.getUint16(byteOffset, ...opts)); | ||
return convertToNumber( dataView.getUint16(byteOffset, ...opts) ); | ||
} | ||
/** | ||
@@ -1408,3 +1412,2 @@ * stores an unsigned 16-bit float value at the specified byte offset from the start of the DataView. | ||
*/ | ||
function setFloat16(dataView, byteOffset, value, ...opts) { | ||
@@ -1411,0 +1414,0 @@ if (!isDataView(dataView)) { |
@@ -318,17 +318,17 @@ /** | ||
* Creates an array from an array-like or iterable object. | ||
* @param arrayLike An array-like or iterable object to convert to an array. | ||
* @param elements An iterable object to convert to an array. | ||
*/ | ||
from(arrayLike: Iterable<number>): Float16Array; | ||
from(elements: Iterable<number>): Float16Array; | ||
/** | ||
* Creates an array from an array-like or iterable object. | ||
* @param arrayLike An array-like or iterable object to convert to an array. | ||
* @param elements An iterable object to convert to an array. | ||
* @param mapfn A mapping function to call on every element of the array. | ||
* @param thisArg Value of 'this' used to invoke the mapfn. | ||
*/ | ||
from<T>(arrayLike: Iterable<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Float16Array; | ||
from<T>(elements: Iterable<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Float16Array; | ||
/** | ||
* Creates an array from an array-like or iterable object. | ||
* @param arrayLike An array-like or iterable object to convert to an array. | ||
* @param arrayLike An array-like object to convert to an array. | ||
*/ | ||
@@ -339,3 +339,3 @@ from(arrayLike: ArrayLike<number>): Float16Array; | ||
* Creates an array from an array-like or iterable object. | ||
* @param arrayLike An array-like or iterable object to convert to an array. | ||
* @param arrayLike An array-like object to convert to an array. | ||
* @param mapfn A mapping function to call on every element of the array. | ||
@@ -342,0 +342,0 @@ * @param thisArg Value of 'this' used to invoke the mapfn. |
@@ -9,6 +9,6 @@ "use strict"; | ||
var _converter = require("./helper/converter.js"); | ||
var _is = require("./helper/is.js"); | ||
var _lib = require("./helper/lib.js"); | ||
/** | ||
@@ -26,3 +26,3 @@ * returns an unsigned 16-bit float at the specified byte offset from the start of the DataView. | ||
return (0, _lib.convertToNumber)(dataView.getUint16(byteOffset, ...opts)); | ||
return (0, _converter.convertToNumber)(dataView.getUint16(byteOffset, ...opts)); | ||
} | ||
@@ -43,3 +43,3 @@ /** | ||
dataView.setUint16(byteOffset, (0, _lib.roundToFloat16Bits)(value), ...opts); | ||
dataView.setUint16(byteOffset, (0, _converter.roundToFloat16Bits)(value), ...opts); | ||
} |
@@ -10,6 +10,6 @@ "use strict"; | ||
var _converter = require("./helper/converter.js"); | ||
var _is = require("./helper/is.js"); | ||
var _lib = require("./helper/lib.js"); | ||
var _private = require("./helper/private.js"); | ||
@@ -33,4 +33,10 @@ | ||
const constructor = target.constructor; | ||
const prototype = Reflect.getPrototypeOf(target); | ||
if (!(0, _is.isObjectLike)(prototype)) { | ||
return false; | ||
} | ||
const constructor = prototype.constructor; | ||
if (constructor === undefined) { | ||
@@ -103,3 +109,3 @@ return false; | ||
for (let i = 0; i < length; ++i) { | ||
array[i] = (0, _lib.convertToNumber)(float16bitsArray[i]); | ||
array[i] = (0, _converter.convertToNumber)(float16bitsArray[i]); | ||
} | ||
@@ -139,3 +145,3 @@ | ||
if ((0, _is.isCanonicalIntegerIndexString)(key) && hasOwnProperty.call(target, key)) { | ||
return (0, _lib.convertToNumber)(Reflect.get(target, key)); | ||
return (0, _converter.convertToNumber)(Reflect.get(target, key)); | ||
} | ||
@@ -161,3 +167,3 @@ | ||
if ((0, _is.isCanonicalIntegerIndexString)(key) && hasOwnProperty.call(target, key)) { | ||
return Reflect.set(target, key, (0, _lib.roundToFloat16Bits)(value)); | ||
return Reflect.set(target, key, (0, _converter.roundToFloat16Bits)(value)); | ||
} | ||
@@ -217,3 +223,3 @@ | ||
// super (Uint16Array) | ||
this[i] = (0, _lib.roundToFloat16Bits)(list[i]); | ||
this[i] = (0, _converter.roundToFloat16Bits)(list[i]); | ||
} // primitive, ArrayBuffer | ||
@@ -265,3 +271,3 @@ | ||
if (opts.length === 0) { | ||
return new Float16Array(Uint16Array.from(src, _lib.roundToFloat16Bits).buffer); | ||
return new Float16Array(Uint16Array.from(src, _converter.roundToFloat16Bits).buffer); | ||
} | ||
@@ -272,3 +278,3 @@ | ||
return new Float16Array(Uint16Array.from(src, function (val, ...args) { | ||
return (0, _lib.roundToFloat16Bits)(mapFunc.call(this, val, ...args)); | ||
return (0, _converter.roundToFloat16Bits)(mapFunc.call(this, val, ...args)); | ||
}, thisArg).buffer); | ||
@@ -288,3 +294,3 @@ } | ||
for (let i = 0; i < length; ++i) { | ||
float16bitsArray[i] = (0, _lib.roundToFloat16Bits)(items[i]); | ||
float16bitsArray[i] = (0, _converter.roundToFloat16Bits)(items[i]); | ||
} | ||
@@ -314,3 +320,3 @@ | ||
for (const val of arrayIterator) { | ||
yield (0, _lib.convertToNumber)(val); | ||
yield (0, _converter.convertToNumber)(val); | ||
} | ||
@@ -330,3 +336,3 @@ }()); | ||
for (const [i, val] of arrayIterator) { | ||
yield [i, (0, _lib.convertToNumber)(val)]; | ||
yield [i, (0, _converter.convertToNumber)(val)]; | ||
} | ||
@@ -350,3 +356,3 @@ }()); | ||
return (0, _lib.convertToNumber)(this[k]); | ||
return (0, _converter.convertToNumber)(this[k]); | ||
} | ||
@@ -369,4 +375,4 @@ /** | ||
for (let i = 0; i < length; ++i) { | ||
const val = (0, _lib.convertToNumber)(this[i]); | ||
float16bitsArray[i] = (0, _lib.roundToFloat16Bits)(callback.call(thisArg, val, i, _(this).proxy)); | ||
const val = (0, _converter.convertToNumber)(this[i]); | ||
float16bitsArray[i] = (0, _converter.roundToFloat16Bits)(callback.call(thisArg, val, i, _(this).proxy)); | ||
} | ||
@@ -380,3 +386,3 @@ | ||
for (let i = 0; i < length; ++i) { | ||
const val = (0, _lib.convertToNumber)(this[i]); | ||
const val = (0, _converter.convertToNumber)(this[i]); | ||
array[i] = callback.call(thisArg, val, i, _(this).proxy); | ||
@@ -398,3 +404,3 @@ } | ||
for (let i = 0, l = this.length; i < l; ++i) { | ||
const val = (0, _lib.convertToNumber)(this[i]); | ||
const val = (0, _converter.convertToNumber)(this[i]); | ||
@@ -426,3 +432,3 @@ if (callback.call(thisArg, val, i, _(this).proxy)) { | ||
if (opts.length === 0) { | ||
accumulator = (0, _lib.convertToNumber)(this[0]); | ||
accumulator = (0, _converter.convertToNumber)(this[0]); | ||
start = 1; | ||
@@ -435,3 +441,3 @@ } else { | ||
for (let i = start; i < length; ++i) { | ||
accumulator = callback(accumulator, (0, _lib.convertToNumber)(this[i]), i, _(this).proxy); | ||
accumulator = callback(accumulator, (0, _converter.convertToNumber)(this[i]), i, _(this).proxy); | ||
} | ||
@@ -457,3 +463,3 @@ | ||
if (opts.length === 0) { | ||
accumulator = (0, _lib.convertToNumber)(this[length - 1]); | ||
accumulator = (0, _converter.convertToNumber)(this[length - 1]); | ||
start = length - 2; | ||
@@ -466,3 +472,3 @@ } else { | ||
for (let i = start; i >= 0; --i) { | ||
accumulator = callback(accumulator, (0, _lib.convertToNumber)(this[i]), i, _(this).proxy); | ||
accumulator = callback(accumulator, (0, _converter.convertToNumber)(this[i]), i, _(this).proxy); | ||
} | ||
@@ -482,3 +488,3 @@ | ||
for (let i = 0, l = this.length; i < l; ++i) { | ||
callback.call(thisArg, (0, _lib.convertToNumber)(this[i]), i, _(this).proxy); | ||
callback.call(thisArg, (0, _converter.convertToNumber)(this[i]), i, _(this).proxy); | ||
} | ||
@@ -496,3 +502,3 @@ } | ||
for (let i = 0, l = this.length; i < l; ++i) { | ||
const value = (0, _lib.convertToNumber)(this[i]); | ||
const value = (0, _converter.convertToNumber)(this[i]); | ||
@@ -514,3 +520,3 @@ if (callback.call(thisArg, value, i, _(this).proxy)) { | ||
for (let i = 0, l = this.length; i < l; ++i) { | ||
const value = (0, _lib.convertToNumber)(this[i]); | ||
const value = (0, _converter.convertToNumber)(this[i]); | ||
@@ -534,3 +540,3 @@ if (callback.call(thisArg, value, i, _(this).proxy)) { | ||
for (let i = this.length - 1; i >= 0; --i) { | ||
const value = (0, _lib.convertToNumber)(this[i]); | ||
const value = (0, _converter.convertToNumber)(this[i]); | ||
@@ -552,3 +558,3 @@ if (callback.call(thisArg, value, i, _(this).proxy)) { | ||
for (let i = this.length - 1; i >= 0; --i) { | ||
const value = (0, _lib.convertToNumber)(this[i]); | ||
const value = (0, _converter.convertToNumber)(this[i]); | ||
@@ -572,3 +578,3 @@ if (callback.call(thisArg, value, i, _(this).proxy)) { | ||
for (let i = 0, l = this.length; i < l; ++i) { | ||
if (!callback.call(thisArg, (0, _lib.convertToNumber)(this[i]), i, _(this).proxy)) { | ||
if (!callback.call(thisArg, (0, _converter.convertToNumber)(this[i]), i, _(this).proxy)) { | ||
return false; | ||
@@ -590,3 +596,3 @@ } | ||
for (let i = 0, l = this.length; i < l; ++i) { | ||
if (callback.call(thisArg, (0, _lib.convertToNumber)(this[i]), i, _(this).proxy)) { | ||
if (callback.call(thisArg, (0, _converter.convertToNumber)(this[i]), i, _(this).proxy)) { | ||
return true; | ||
@@ -628,3 +634,3 @@ } | ||
for (let i = 0; i < srcLength; ++i) { | ||
this[i + targetOffset] = (0, _lib.roundToFloat16Bits)(src[i]); | ||
this[i + targetOffset] = (0, _converter.roundToFloat16Bits)(src[i]); | ||
} | ||
@@ -649,3 +655,3 @@ } | ||
assertFloat16BitsArray(this); | ||
super.fill((0, _lib.roundToFloat16Bits)(value), ...opts); | ||
super.fill((0, _converter.roundToFloat16Bits)(value), ...opts); | ||
return _(this).proxy; | ||
@@ -672,3 +678,3 @@ } | ||
super.sort((x, y) => { | ||
return compare((0, _lib.convertToNumber)(x), (0, _lib.convertToNumber)(y)); | ||
return compare((0, _converter.convertToNumber)(x), (0, _converter.convertToNumber)(y)); | ||
}); | ||
@@ -725,3 +731,3 @@ return _(this).proxy; | ||
while (k < final) { | ||
array[n] = (0, _lib.convertToNumber)(this[k]); | ||
array[n] = (0, _converter.convertToNumber)(this[k]); | ||
++k; | ||
@@ -769,3 +775,3 @@ ++n; | ||
for (let i = from, l = length; i < l; ++i) { | ||
if (hasOwnProperty.call(this, i) && (0, _lib.convertToNumber)(this[i]) === element) { | ||
if (hasOwnProperty.call(this, i) && (0, _converter.convertToNumber)(this[i]) === element) { | ||
return i; | ||
@@ -798,3 +804,3 @@ } | ||
for (let i = from; i >= 0; --i) { | ||
if (hasOwnProperty.call(this, i) && (0, _lib.convertToNumber)(this[i]) === element) { | ||
if (hasOwnProperty.call(this, i) && (0, _converter.convertToNumber)(this[i]) === element) { | ||
return i; | ||
@@ -831,3 +837,3 @@ } | ||
for (let i = from, l = length; i < l; ++i) { | ||
const value = (0, _lib.convertToNumber)(this[i]); | ||
const value = (0, _converter.convertToNumber)(this[i]); | ||
@@ -834,0 +840,0 @@ if (isNaN && Number.isNaN(value)) { |
@@ -12,3 +12,3 @@ "use strict"; | ||
const IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())); | ||
const IteratorPrototype = Reflect.getPrototypeOf(Reflect.getPrototypeOf([][Symbol.iterator]())); | ||
const ArrayIteratorPrototype = Object.create(IteratorPrototype, { | ||
@@ -15,0 +15,0 @@ next: { |
@@ -35,4 +35,4 @@ "use strict"; | ||
const TypedArrayPrototype = Object.getPrototypeOf(Uint8Array).prototype; | ||
const getTypedArrayPrototypeSybolToStringTag = Object.getOwnPropertyDescriptor(TypedArrayPrototype, Symbol.toStringTag).get; | ||
const TypedArrayPrototype = Reflect.getPrototypeOf(Uint8Array).prototype; | ||
const getTypedArrayPrototypeSybolToStringTag = Reflect.getOwnPropertyDescriptor(TypedArrayPrototype, Symbol.toStringTag).get; | ||
/** | ||
@@ -39,0 +39,0 @@ * @param {unknown} value |
@@ -14,2 +14,3 @@ "use strict"; | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-tointegerorinfinity | ||
* @param {unknown} target | ||
@@ -36,2 +37,3 @@ * @returns {number} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-tolength | ||
* @param {unknown} target | ||
@@ -52,2 +54,3 @@ * @returns {number} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-lengthofarraylike | ||
* @param {object} arrayLike | ||
@@ -66,2 +69,3 @@ * @returns {number} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-speciesconstructor | ||
* @param {object} target | ||
@@ -97,2 +101,4 @@ * @param {Function} defaultConstructor | ||
/** | ||
* bigint comparisons are not supported | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort | ||
* @param {number} x | ||
@@ -105,3 +111,4 @@ * @param {number} y | ||
function defaultCompare(x, y) { | ||
const [isNaN_x, isNaN_y] = [Number.isNaN(x), Number.isNaN(y)]; | ||
const isNaN_x = Number.isNaN(x); | ||
const isNaN_y = Number.isNaN(y); | ||
@@ -129,3 +136,4 @@ if (isNaN_x && isNaN_y) { | ||
if (x === 0 && y === 0) { | ||
const [isPlusZero_x, isPlusZero_y] = [Object.is(x, 0), Object.is(y, 0)]; | ||
const isPlusZero_x = Object.is(x, 0); | ||
const isPlusZero_y = Object.is(y, 0); | ||
@@ -132,0 +140,0 @@ if (!isPlusZero_x && isPlusZero_y) { |
@@ -8,3 +8,3 @@ "use strict"; | ||
var _lib = require("./helper/lib.js"); | ||
var _converter = require("./helper/converter.js"); | ||
@@ -23,4 +23,4 @@ /** | ||
const x16 = (0, _lib.roundToFloat16Bits)(num); | ||
return (0, _lib.convertToNumber)(x16); | ||
const x16 = (0, _converter.roundToFloat16Bits)(num); | ||
return (0, _converter.convertToNumber)(x16); | ||
} |
{ | ||
"name": "@petamoriken/float16", | ||
"description": "half precision floating point for JavaScript", | ||
"version": "3.4.3", | ||
"version": "3.4.4", | ||
"main": "./lib/index.js", | ||
@@ -34,8 +34,11 @@ "module": "./src/index.mjs", | ||
"half-precision", | ||
"ieee754", | ||
"Float16Array", | ||
"TypedArray", | ||
"DataView", | ||
"getFloat16", | ||
"setFloat16", | ||
"DataView", | ||
"hfround" | ||
"hfround", | ||
"ponyfill", | ||
"shim" | ||
], | ||
@@ -62,4 +65,3 @@ "scripts": { | ||
"@babel/core": "^7.0.0", | ||
"@babel/preset-env": "^7.9.5", | ||
"@rollup/plugin-babel": "^5.3.0", | ||
"@babel/plugin-transform-modules-commonjs": "^7.15.4", | ||
"babel-plugin-replace-import-extension": "^1.1.1", | ||
@@ -66,0 +68,0 @@ "browserslist": "^4.16.7", |
# <a href="https://git.io/float16">@petamoriken/float16</a> | ||
<p align="center"> | ||
half precision floating point for JavaScript<br> | ||
See <a href="https://esdiscuss.org/topic/float16array">ES Discuss Float16Array topic</a> | ||
half precision floating point for JavaScript<br> | ||
See <a href="https://esdiscuss.org/topic/float16array">the archive of the ES Discuss Float16Array topic</a> for details | ||
</p> | ||
<p align="center"> | ||
<a href="https://www.npmjs.com/package/@petamoriken/float16"> | ||
<img src="https://img.shields.io/npm/dw/@petamoriken/float16?logo=npm&style=flat-square" alt="npm downloads"> | ||
</a> | ||
<a href="https://www.npmjs.com/package/@petamoriken/float16"> | ||
<img src="https://img.shields.io/npm/v/@petamoriken/float16.svg?label=version&logo=npm&style=flat-square" alt="npm version"> | ||
</a> | ||
<a href="https://deno.land/x/float16"> | ||
<img src="https://img.shields.io/github/v/tag/petamoriken/float16?label=version&logo=deno&style=flat-square" alt="deno version"> | ||
</a> | ||
<a href="https://github.com/petamoriken/float16/blob/master/package.json"> | ||
<img src="https://img.shields.io/david/petamoriken/float16?style=flat-square" alt="dependencies"> | ||
</a> | ||
<a href="https://github.com/petamoriken/float16/blob/master/LICENSE"> | ||
<img src="https://img.shields.io/npm/l/@petamoriken/float16.svg?style=flat-square" alt="license"> | ||
</a> | ||
<a href="https://codecov.io/gh/petamoriken/float16"> | ||
<img src="https://img.shields.io/codecov/c/gh/petamoriken/float16?logo=codecov&style=flat-square" alt="codecov"> | ||
</a> | ||
<a href="https://www.npmjs.com/package/@petamoriken/float16"> | ||
<img src="https://img.shields.io/npm/dw/@petamoriken/float16?logo=npm&style=flat-square" alt="npm downloads"> | ||
</a> | ||
<a href="https://www.npmjs.com/package/@petamoriken/float16"> | ||
<img src="https://img.shields.io/npm/v/@petamoriken/float16.svg?label=version&logo=npm&style=flat-square" alt="npm version"> | ||
</a> | ||
<a href="https://deno.land/x/float16"> | ||
<img src="https://img.shields.io/github/v/tag/petamoriken/float16?label=version&logo=deno&style=flat-square" alt="deno version"> | ||
</a> | ||
<a href="https://github.com/petamoriken/float16/blob/master/package.json"> | ||
<img src="https://img.shields.io/david/petamoriken/float16?style=flat-square" alt="dependencies"> | ||
</a> | ||
<a href="https://github.com/petamoriken/float16/blob/master/LICENSE"> | ||
<img src="https://img.shields.io/npm/l/@petamoriken/float16.svg?style=flat-square" alt="license"> | ||
</a> | ||
<a href="https://codecov.io/gh/petamoriken/float16"> | ||
<img src="https://img.shields.io/codecov/c/gh/petamoriken/float16?logo=codecov&style=flat-square" alt="codecov"> | ||
</a> | ||
</p> | ||
<p align="center"> | ||
<a href="https://saucelabs.com/u/petamoriken"> | ||
<img src="https://saucelabs.com/browser-matrix/petamoriken.svg" alt="Sauce Labs browser matrix"> | ||
</a> | ||
<a href="https://saucelabs.com/u/petamoriken"> | ||
<img src="https://saucelabs.com/browser-matrix/petamoriken.svg" alt="Sauce Labs browser matrix"> | ||
</a> | ||
</p> | ||
@@ -74,3 +74,3 @@ | ||
<script type="module"> | ||
import { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } from "DEST/TO/float16.mjs"; | ||
import { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } from "DEST/TO/float16.mjs"; | ||
</script> | ||
@@ -83,3 +83,3 @@ ``` | ||
<script> | ||
const { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } = float16; | ||
const { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } = float16; | ||
</script> | ||
@@ -93,3 +93,3 @@ ``` | ||
<script type="module"> | ||
import { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } from "https://cdn.jsdelivr.net/npm/@petamoriken/float16/+esm"; | ||
import { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } from "https://cdn.jsdelivr.net/npm/@petamoriken/float16/+esm"; | ||
</script> | ||
@@ -102,3 +102,3 @@ ``` | ||
<script> | ||
const { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } = float16; | ||
const { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } = float16; | ||
</script> | ||
@@ -112,3 +112,3 @@ ``` | ||
<script type="module"> | ||
import { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } from "https://cdn.skypack.dev/@petamoriken/float16?min"; | ||
import { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } from "https://cdn.skypack.dev/@petamoriken/float16?min"; | ||
</script> | ||
@@ -123,6 +123,7 @@ ``` | ||
### Pre-transpiled JavaScript files (CommonJS) | ||
### Pre-transpiled JavaScript files (CommonJS, IIFE) | ||
`lib/` and `browser/` directories in the npm package have JavaScript files already transpiled, whose target are | ||
`lib/` and `browser/` directories in the npm package have JavaScript files already transpiled, and they have been tested automatically in the following environments | ||
* Node.js Active LTS | ||
* Firefox: last 2 versions and ESR | ||
@@ -133,4 +134,2 @@ * Chrome: last 2 versions | ||
If you want to build it yourself using bundler to support older browsers, transpile the JavaScript files in the `src/` directory. | ||
## API | ||
@@ -145,3 +144,3 @@ | ||
for (const val of array) { | ||
console.log(val); // => 1, 1.099609375, 1.19921875 | ||
console.log(val); // => 1, 1.099609375, 1.19921875 | ||
} | ||
@@ -223,6 +222,6 @@ | ||
function isUint16Array(target) { | ||
if (target === null || typeof target !== "object") { | ||
return false; | ||
} | ||
return Object.prototype.toString.call(target) === "[object Uint16Array]"; | ||
if (target === null || typeof target !== "object") { | ||
return false; | ||
} | ||
return Object.prototype.toString.call(target) === "[object Uint16Array]"; | ||
} | ||
@@ -264,5 +263,5 @@ ``` | ||
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, | ||
]); | ||
@@ -269,0 +268,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
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
160940
18
4660
325