@petamoriken/float16
Advanced tools
Comparing version 3.4.4 to 3.4.5
@@ -1,2 +0,2 @@ | ||
/*! @petamoriken/float16 v3.4.4 | MIT License - https://git.io/float16 */ | ||
/*! @petamoriken/float16 v3.4.5 | MIT License - https://git.io/float16 */ | ||
@@ -286,2 +286,19 @@ var float16 = (function (exports) { | ||
* @param {unknown} value | ||
* @returns {value is any[]} | ||
*/ | ||
function isOrdinaryTypedArray(value) { | ||
if (!isTypedArray(value)) { | ||
return false; | ||
} | ||
const iterator = value[Symbol.iterator](); | ||
if (toString.call(iterator) !== "[object Array Iterator]") { | ||
return false; | ||
} | ||
return true; | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {value is string} | ||
@@ -591,3 +608,5 @@ */ | ||
} else if (isObject(input) && !isArrayBuffer(input)) { | ||
/** @type {ArrayLike<number>} */ | ||
let list; | ||
/** @type {number} */ | ||
let length; | ||
@@ -673,18 +692,65 @@ | ||
static from(src, ...opts) { | ||
const Constructor = this; | ||
if (!Reflect.has(Constructor, brand)) { | ||
throw TypeError("This constructor is not a subclass of Float16Array"); | ||
} | ||
// for optimization | ||
if (isFloat16Array(src) && opts.length === 0) { | ||
const uint16 = new Uint16Array(src.buffer, src.byteOffset, src.length); | ||
return new Float16Array(uint16.slice().buffer); | ||
if (Constructor === Float16Array) { | ||
if (isFloat16Array(src) && opts.length === 0) { | ||
const uint16 = new Uint16Array(src.buffer, src.byteOffset, src.length); | ||
return new Float16Array(uint16.slice().buffer); | ||
} | ||
if (opts.length === 0) { | ||
return new Float16Array(Uint16Array.from(src, roundToFloat16Bits).buffer); | ||
} | ||
const mapFunc = opts[0]; | ||
const thisArg = opts[1]; | ||
return new Float16Array(Uint16Array.from(src, function (val, ...args) { | ||
return roundToFloat16Bits(mapFunc.call(this, val, ...args)); | ||
}, thisArg).buffer); | ||
} | ||
/** @type {ArrayLike<number>} */ | ||
let list; | ||
/** @type {number} */ | ||
let length; | ||
// Iterable (TypedArray, Array) | ||
if (isIterable(src)) { | ||
// for optimization | ||
if (isOrdinaryArray(src) || isOrdinaryTypedArray(src)) { | ||
list = src; | ||
length = src.length; | ||
} else { | ||
list = [...src]; | ||
length = list.length; | ||
} | ||
// ArrayLike | ||
} else { | ||
list = src; | ||
length = LengthOfArrayLike(src); | ||
} | ||
const array = new Constructor(length); | ||
if (opts.length === 0) { | ||
return new Float16Array(Uint16Array.from(src, roundToFloat16Bits).buffer); | ||
for (let i = 0; i < length; ++i) { | ||
array[i] = list[i]; | ||
} | ||
} else { | ||
const mapFunc = opts[0]; | ||
const thisArg = opts[1]; | ||
for (let i = 0; i < length; ++i) { | ||
array[i] = mapFunc.call(thisArg, list[i], i); | ||
} | ||
} | ||
const mapFunc = opts[0]; | ||
const thisArg = opts[1]; | ||
return new Float16Array(Uint16Array.from(src, function (val, ...args) { | ||
return roundToFloat16Bits(mapFunc.call(this, val, ...args)); | ||
}, thisArg).buffer); | ||
return array; | ||
} | ||
@@ -697,12 +763,29 @@ | ||
static of(...items) { | ||
const Constructor = this; | ||
if (!Reflect.has(Constructor, brand)) { | ||
throw TypeError("This constructor is not a subclass of Float16Array"); | ||
} | ||
const length = items.length; | ||
const proxy = new Float16Array(length); | ||
const float16bitsArray = getFloat16BitsArrayFromFloat16Array(proxy); | ||
// for optimization | ||
if (Constructor === Float16Array) { | ||
const proxy = new Float16Array(length); | ||
const float16bitsArray = getFloat16BitsArrayFromFloat16Array(proxy); | ||
for (let i = 0; i < length; ++i) { | ||
float16bitsArray[i] = roundToFloat16Bits(items[i]); | ||
} | ||
return proxy; | ||
} | ||
const array = new Constructor(length); | ||
for (let i = 0; i < length; ++i) { | ||
float16bitsArray[i] = roundToFloat16Bits(items[i]); | ||
array[i] = items[i]; | ||
} | ||
return proxy; | ||
return array; | ||
} | ||
@@ -709,0 +792,0 @@ |
@@ -185,3 +185,6 @@ "use strict"; | ||
} else if ((0, _is.isObject)(input) && !(0, _is.isArrayBuffer)(input)) { | ||
/** @type {ArrayLike<number>} */ | ||
let list; | ||
/** @type {number} */ | ||
let length; // TypedArray | ||
@@ -259,17 +262,64 @@ | ||
static from(src, ...opts) { | ||
// for optimization | ||
if (isFloat16Array(src) && opts.length === 0) { | ||
const uint16 = new Uint16Array(src.buffer, src.byteOffset, src.length); | ||
return new Float16Array(uint16.slice().buffer); | ||
const Constructor = this; | ||
if (!Reflect.has(Constructor, brand)) { | ||
throw TypeError("This constructor is not a subclass of Float16Array"); | ||
} // for optimization | ||
if (Constructor === Float16Array) { | ||
if (isFloat16Array(src) && opts.length === 0) { | ||
const uint16 = new Uint16Array(src.buffer, src.byteOffset, src.length); | ||
return new Float16Array(uint16.slice().buffer); | ||
} | ||
if (opts.length === 0) { | ||
return new Float16Array(Uint16Array.from(src, _converter.roundToFloat16Bits).buffer); | ||
} | ||
const mapFunc = opts[0]; | ||
const thisArg = opts[1]; | ||
return new Float16Array(Uint16Array.from(src, function (val, ...args) { | ||
return (0, _converter.roundToFloat16Bits)(mapFunc.call(this, val, ...args)); | ||
}, thisArg).buffer); | ||
} | ||
/** @type {ArrayLike<number>} */ | ||
let list; | ||
/** @type {number} */ | ||
let length; // Iterable (TypedArray, Array) | ||
if ((0, _is.isIterable)(src)) { | ||
// for optimization | ||
if ((0, _is.isOrdinaryArray)(src) || (0, _is.isOrdinaryTypedArray)(src)) { | ||
list = src; | ||
length = src.length; | ||
} else { | ||
list = [...src]; | ||
length = list.length; | ||
} // ArrayLike | ||
} else { | ||
list = src; | ||
length = (0, _spec.LengthOfArrayLike)(src); | ||
} | ||
const array = new Constructor(length); | ||
if (opts.length === 0) { | ||
return new Float16Array(Uint16Array.from(src, _converter.roundToFloat16Bits).buffer); | ||
for (let i = 0; i < length; ++i) { | ||
array[i] = list[i]; | ||
} | ||
} else { | ||
const mapFunc = opts[0]; | ||
const thisArg = opts[1]; | ||
for (let i = 0; i < length; ++i) { | ||
array[i] = mapFunc.call(thisArg, list[i], i); | ||
} | ||
} | ||
const mapFunc = opts[0]; | ||
const thisArg = opts[1]; | ||
return new Float16Array(Uint16Array.from(src, function (val, ...args) { | ||
return (0, _converter.roundToFloat16Bits)(mapFunc.call(this, val, ...args)); | ||
}, thisArg).buffer); | ||
return array; | ||
} | ||
@@ -283,11 +333,28 @@ /** | ||
static of(...items) { | ||
const length = items.length; | ||
const proxy = new Float16Array(length); | ||
const float16bitsArray = getFloat16BitsArrayFromFloat16Array(proxy); | ||
const Constructor = this; | ||
if (!Reflect.has(Constructor, brand)) { | ||
throw TypeError("This constructor is not a subclass of Float16Array"); | ||
} | ||
const length = items.length; // for optimization | ||
if (Constructor === Float16Array) { | ||
const proxy = new Float16Array(length); | ||
const float16bitsArray = getFloat16BitsArrayFromFloat16Array(proxy); | ||
for (let i = 0; i < length; ++i) { | ||
float16bitsArray[i] = (0, _converter.roundToFloat16Bits)(items[i]); | ||
} | ||
return proxy; | ||
} | ||
const array = new Constructor(length); | ||
for (let i = 0; i < length; ++i) { | ||
float16bitsArray[i] = (0, _converter.roundToFloat16Bits)(items[i]); | ||
array[i] = items[i]; | ||
} | ||
return proxy; | ||
return array; | ||
} | ||
@@ -294,0 +361,0 @@ /** |
@@ -15,2 +15,3 @@ "use strict"; | ||
exports.isOrdinaryArray = isOrdinaryArray; | ||
exports.isOrdinaryTypedArray = isOrdinaryTypedArray; | ||
exports.isCanonicalIntegerIndexString = isCanonicalIntegerIndexString; | ||
@@ -125,2 +126,21 @@ | ||
* @param {unknown} value | ||
* @returns {value is any[]} | ||
*/ | ||
function isOrdinaryTypedArray(value) { | ||
if (!isTypedArray(value)) { | ||
return false; | ||
} | ||
const iterator = value[Symbol.iterator](); | ||
if (toString.call(iterator) !== "[object Array Iterator]") { | ||
return false; | ||
} | ||
return true; | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {value is string} | ||
@@ -127,0 +147,0 @@ */ |
{ | ||
"name": "@petamoriken/float16", | ||
"description": "half precision floating point for JavaScript", | ||
"version": "3.4.4", | ||
"version": "3.4.5", | ||
"main": "./lib/index.js", | ||
@@ -66,3 +66,3 @@ "module": "./src/index.mjs", | ||
"babel-plugin-replace-import-extension": "^1.1.1", | ||
"browserslist": "^4.16.7", | ||
"browserslist": "^4.17.0", | ||
"eslint": "^7.4.0", | ||
@@ -80,4 +80,4 @@ "eslint-plugin-import": "^2.20.2", | ||
"rollup": "^2.6.0", | ||
"source-map-support": "^0.5.19" | ||
"source-map-support": "^0.5.20" | ||
} | ||
} |
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
168383
4921