Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@petamoriken/float16

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@petamoriken/float16 - npm Package Compare versions

Comparing version 3.0.1 to 3.1.0

86

browser/float16.js
/**
* @petamoriken/float16 7d9f74d | MIT License - https://git.io/float16
* @petamoriken/float16 v3.1.0 | MIT License - https://git.io/float16
*

@@ -958,9 +958,17 @@ * @license

*/
function ToInteger(target) {
let number = typeof target !== "number" ? Number(target) : target;
function ToIntegerOrInfinity(target) {
const number = Number(target);
if (Number.isNaN(number)) {
number = 0;
if (Number.isNaN(number) || number === 0) {
return 0;
}
if (number === Infinity) {
return Infinity;
}
if (number === -Infinity) {
return -Infinity;
}
return Math.trunc(number);

@@ -1130,4 +1138,4 @@ }

function isStringNumberKey(key) {
return typeof key === "string" && key === ToInteger(key) + "";
function isCanonicalIntegerIndexString(key) {
return typeof key === "string" && key === ToIntegerOrInfinity(key) + "";
}

@@ -1217,3 +1225,3 @@

get(target, key) {
if (isStringNumberKey(key)) {
if (isCanonicalIntegerIndexString(key)) {
return Reflect.has(target, key) ? convertToNumber(Reflect.get(target, key)) : undefined;

@@ -1239,3 +1247,3 @@ } else {

set(target, key, value) {
if (isStringNumberKey(key)) {
if (isCanonicalIntegerIndexString(key)) {
return Reflect.set(target, key, roundToFloat16Bits(value));

@@ -1336,2 +1344,15 @@ } else {

}
}
at(index) {
assertFloat16Array(this);
const length = this.length;
const relativeIndex = ToIntegerOrInfinity(index);
const k = relativeIndex >= 0 ? relativeIndex : length + relativeIndex;
if (k < 0 || k >= length) {
return;
}
return convertToNumber(this[k]);
} // functional methods

@@ -1447,2 +1468,30 @@ // @ts-ignore

findLast(callback, ...opts) {
assertFloat16Array(this);
const thisArg = opts[0];
for (let i = this.length - 1; i >= 0; --i) {
const value = convertToNumber(this[i]);
if (callback.call(thisArg, value, i, _(this).proxy)) {
return value;
}
}
}
findLastIndex(callback, ...opts) {
assertFloat16Array(this);
const thisArg = opts[0];
for (let i = this.length - 1; i >= 0; --i) {
const value = convertToNumber(this[i]);
if (callback.call(thisArg, value, i, _(this).proxy)) {
return i;
}
}
return -1;
}
every(callback, ...opts) {

@@ -1572,4 +1621,8 @@ assertFloat16Array(this);

const length = this.length;
let from = ToInteger(opts[0]);
let from = ToIntegerOrInfinity(opts[0]);
if (from === Infinity) {
return -1;
}
if (from < 0) {

@@ -1584,3 +1637,3 @@ from += length;

for (let i = from, l = length; i < l; ++i) {
if (convertToNumber(this[i]) === element) {
if (Object.prototype.hasOwnProperty.call(this, i) && convertToNumber(this[i]) === element) {
return i;

@@ -1596,3 +1649,8 @@ }

const length = this.length;
let from = ToInteger(opts[0]);
let from = ToIntegerOrInfinity(opts[0]);
if (from === -Infinity) {
return -1;
}
from = from === 0 ? length : from + 1;

@@ -1607,3 +1665,3 @@

for (let i = from; i--;) {
if (convertToNumber(this[i]) === element) {
if (Object.prototype.hasOwnProperty.call(this, i) && convertToNumber(this[i]) === element) {
return i;

@@ -1619,3 +1677,3 @@ }

const length = this.length;
let from = ToInteger(opts[0]);
let from = ToIntegerOrInfinity(opts[0]);

@@ -1622,0 +1680,0 @@ if (from < 0) {

@@ -44,2 +44,9 @@ /**

/**
* Returns a value in the array
* @param index If index is negative, it is treated as length+index where length is the
* length of the array.
*/
at(index: number): number;
/**
* Returns the this object after copying a section of the array identified by start and end

@@ -107,2 +114,24 @@ * to the same array starting at position target

/**
* Returns the value of the last element in the array where predicate is true, and undefined
* otherwise.
* @param predicate find calls predicate once for each element of the array, in descending
* order, until it finds one where predicate returns true. If such an element is found, find
* immediately returns that element value. Otherwise, find returns undefined.
* @param thisArg If provided, it will be used as the this value for each invocation of
* predicate. If it is not provided, undefined is used instead.
*/
findLast(predicate: (value: number, index: number, obj: Float16Array) => boolean, thisArg?: any): number | undefined;
/**
* Returns the index of the last element in the array where predicate is true, and -1
* otherwise.
* @param predicate find calls predicate once for each element of the array, in descending
* order, until it finds one where predicate returns true. If such an element is found,
* findIndex immediately returns that element index. Otherwise, findIndex returns -1.
* @param thisArg If provided, it will be used as the this value for each invocation of
* predicate. If it is not provided, undefined is used instead.
*/
findLastIndex(predicate: (value: number, index: number, obj: Float16Array) => boolean, thisArg?: any): number;
/**
* Performs the specified action for each element in an array.

@@ -109,0 +138,0 @@ * @param callbackfn A function that accepts up to three arguments. forEach calls the

@@ -84,3 +84,3 @@ "use strict";

get(target, key) {
if ((0, _is.isStringNumberKey)(key)) {
if ((0, _is.isCanonicalIntegerIndexString)(key)) {
return Reflect.has(target, key) ? (0, _lib.convertToNumber)(Reflect.get(target, key)) : undefined;

@@ -106,3 +106,3 @@ } else {

set(target, key, value) {
if ((0, _is.isStringNumberKey)(key)) {
if ((0, _is.isCanonicalIntegerIndexString)(key)) {
return Reflect.set(target, key, (0, _lib.roundToFloat16Bits)(value));

@@ -204,2 +204,15 @@ } else {

}
}
at(index) {
assertFloat16Array(this);
const length = this.length;
const relativeIndex = (0, _spec.ToIntegerOrInfinity)(index);
const k = relativeIndex >= 0 ? relativeIndex : length + relativeIndex;
if (k < 0 || k >= length) {
return;
}
return (0, _lib.convertToNumber)(this[k]);
} // functional methods

@@ -315,2 +328,30 @@ // @ts-ignore

findLast(callback, ...opts) {
assertFloat16Array(this);
const thisArg = opts[0];
for (let i = this.length - 1; i >= 0; --i) {
const value = (0, _lib.convertToNumber)(this[i]);
if (callback.call(thisArg, value, i, _(this).proxy)) {
return value;
}
}
}
findLastIndex(callback, ...opts) {
assertFloat16Array(this);
const thisArg = opts[0];
for (let i = this.length - 1; i >= 0; --i) {
const value = (0, _lib.convertToNumber)(this[i]);
if (callback.call(thisArg, value, i, _(this).proxy)) {
return i;
}
}
return -1;
}
every(callback, ...opts) {

@@ -440,4 +481,8 @@ assertFloat16Array(this);

const length = this.length;
let from = (0, _spec.ToInteger)(opts[0]);
let from = (0, _spec.ToIntegerOrInfinity)(opts[0]);
if (from === Infinity) {
return -1;
}
if (from < 0) {

@@ -452,3 +497,3 @@ from += length;

for (let i = from, l = length; i < l; ++i) {
if ((0, _lib.convertToNumber)(this[i]) === element) {
if (Object.prototype.hasOwnProperty.call(this, i) && (0, _lib.convertToNumber)(this[i]) === element) {
return i;

@@ -464,3 +509,8 @@ }

const length = this.length;
let from = (0, _spec.ToInteger)(opts[0]);
let from = (0, _spec.ToIntegerOrInfinity)(opts[0]);
if (from === -Infinity) {
return -1;
}
from = from === 0 ? length : from + 1;

@@ -475,3 +525,3 @@

for (let i = from; i--;) {
if ((0, _lib.convertToNumber)(this[i]) === element) {
if (Object.prototype.hasOwnProperty.call(this, i) && (0, _lib.convertToNumber)(this[i]) === element) {
return i;

@@ -487,3 +537,3 @@ }

const length = this.length;
let from = (0, _spec.ToInteger)(opts[0]);
let from = (0, _spec.ToIntegerOrInfinity)(opts[0]);

@@ -490,0 +540,0 @@ if (from < 0) {

6

lib/is.js

@@ -7,3 +7,3 @@ "use strict";

exports.isDataView = isDataView;
exports.isStringNumberKey = isStringNumberKey;
exports.isCanonicalIntegerIndexString = isCanonicalIntegerIndexString;
Object.defineProperty(exports, "isArrayBuffer", {

@@ -35,4 +35,4 @@ enumerable: true,

function isStringNumberKey(key) {
return typeof key === "string" && key === (0, _spec.ToInteger)(key) + "";
function isCanonicalIntegerIndexString(key) {
return typeof key === "string" && key === (0, _spec.ToIntegerOrInfinity)(key) + "";
}

@@ -6,3 +6,3 @@ "use strict";

});
exports.ToInteger = ToInteger;
exports.ToIntegerOrInfinity = ToIntegerOrInfinity;
exports.defaultCompareFunction = defaultCompareFunction;

@@ -14,9 +14,17 @@

*/
function ToInteger(target) {
let number = typeof target !== "number" ? Number(target) : target;
function ToIntegerOrInfinity(target) {
const number = Number(target);
if (Number.isNaN(number)) {
number = 0;
if (Number.isNaN(number) || number === 0) {
return 0;
}
if (number === Infinity) {
return Infinity;
}
if (number === -Infinity) {
return -Infinity;
}
return Math.trunc(number);

@@ -23,0 +31,0 @@ }

{
"name": "@petamoriken/float16",
"description": "half precision floating point for JavaScript",
"version": "3.0.1",
"version": "3.1.0",
"main": "lib/index.js",

@@ -55,3 +55,3 @@ "module": "src/index.js",

"refresh": "yarn run clean && yarn run build && yarn run docs",
"version": "yarn run refresh && git add -A",
"version": "NPM_VERSION_SCRIPT=1 yarn run refresh && git add -A",
"prepublishOnly": "yarn run lint && yarn test"

@@ -58,0 +58,0 @@ },

import memoize from "lodash-es/memoize";
import { isArrayBuffer, isStringNumberKey } from "./is";
import { isArrayBuffer, isCanonicalIntegerIndexString } from "./is";
import { convertToNumber, roundToFloat16Bits } from "./lib";
import { createPrivateStorage } from "./private";
import { ToInteger, defaultCompareFunction } from "./spec";
import { ToIntegerOrInfinity, defaultCompareFunction } from "./spec";

@@ -55,3 +55,3 @@ const _ = createPrivateStorage();

if (isFloat16Array(thisArg) && isDefaultFloat16ArrayMethods(func)) {
return Reflect.apply(func, _(thisArg).target ,args);
return Reflect.apply(func, _(thisArg).target, args);
}

@@ -66,3 +66,3 @@

get(target, key) {
if (isStringNumberKey(key)) {
if (isCanonicalIntegerIndexString(key)) {
return Reflect.has(target, key) ? convertToNumber(Reflect.get(target, key)) : undefined;

@@ -88,3 +88,3 @@ } else {

set(target, key, value) {
if (isStringNumberKey(key)) {
if (isCanonicalIntegerIndexString(key)) {
return Reflect.set(target, key, roundToFloat16Bits(value));

@@ -195,2 +195,16 @@ } else {

at(index) {
assertFloat16Array(this);
const length = this.length;
const relativeIndex = ToIntegerOrInfinity(index);
const k = relativeIndex >= 0 ? relativeIndex : length + relativeIndex;
if (k < 0 || k >= length) {
return;
}
return convertToNumber(this[k]);
}
// functional methods

@@ -308,2 +322,30 @@ // @ts-ignore

findLast(callback, ...opts) {
assertFloat16Array(this);
const thisArg = opts[0];
for(let i = this.length - 1; i >= 0; --i) {
const value = convertToNumber(this[i]);
if (callback.call(thisArg, value, i, _(this).proxy)) {
return value;
}
}
}
findLastIndex(callback, ...opts) {
assertFloat16Array(this);
const thisArg = opts[0];
for(let i = this.length - 1; i >= 0; --i) {
const value = convertToNumber(this[i]);
if (callback.call(thisArg, value, i, _(this).proxy)) {
return i;
}
}
return -1;
}
every(callback, ...opts) {

@@ -452,3 +494,6 @@ assertFloat16Array(this);

let from = ToInteger(opts[0]);
let from = ToIntegerOrInfinity(opts[0]);
if (from === Infinity) {
return -1;
}

@@ -463,3 +508,3 @@ if (from < 0) {

for(let i = from, l = length; i < l; ++i) {
if (convertToNumber(this[i]) === element) {
if (Object.prototype.hasOwnProperty.call(this, i) && convertToNumber(this[i]) === element) {
return i;

@@ -477,3 +522,6 @@ }

let from = ToInteger(opts[0]);
let from = ToIntegerOrInfinity(opts[0]);
if (from === -Infinity) {
return -1;
}

@@ -489,3 +537,3 @@ from = from === 0 ? length : from + 1;

for(let i = from; i--;) {
if (convertToNumber(this[i]) === element) {
if (Object.prototype.hasOwnProperty.call(this, i) && convertToNumber(this[i]) === element) {
return i;

@@ -503,3 +551,3 @@ }

let from = ToInteger(opts[0]);
let from = ToIntegerOrInfinity(opts[0]);

@@ -506,0 +554,0 @@ if (from < 0) {

@@ -1,2 +0,2 @@

import { ToInteger } from "./spec";
import { ToIntegerOrInfinity } from "./spec";

@@ -17,4 +17,4 @@ export { default as isArrayBuffer } from "lodash-es/isArrayBuffer";

*/
export function isStringNumberKey(key) {
return typeof key === "string" && key === ToInteger(key) + "";
export function isCanonicalIntegerIndexString(key) {
return typeof key === "string" && key === ToIntegerOrInfinity(key) + "";
}

@@ -5,7 +5,17 @@ /**

*/
export function ToInteger(target) {
let number = typeof target !== "number" ? Number(target) : target;
if (Number.isNaN(number)) {
number = 0;
export function ToIntegerOrInfinity(target) {
const number = Number(target);
if (Number.isNaN(number) || number === 0) {
return 0;
}
if (number === Infinity) {
return Infinity;
}
if (number === -Infinity) {
return -Infinity;
}
return Math.trunc(number);

@@ -12,0 +22,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc