Socket
Socket
Sign inDemoInstall

web3-utils

Package Overview
Dependencies
7
Maintainers
4
Versions
409
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.2.3 to 4.2.4-dev.12c2515.0

18

lib/commonjs/converters.js

@@ -60,2 +60,3 @@ "use strict";

};
const PrecisionLossWarning = 'Warning: Using type `number` with values that are large or contain many decimals may cause loss of precision, it is recommended to use type `string` or `BigInt` when using conversion methods';
/**

@@ -373,2 +374,3 @@ * Convert a value from bytes to Uint8Array

if (value > 1e+20) {
console.warn(PrecisionLossWarning);
// JavaScript converts numbers >= 10^21 to scientific notation when coerced to strings,

@@ -496,5 +498,19 @@ // leading to potential parsing errors and incorrect representations.

}
let parsedNumber = number;
if (typeof parsedNumber === 'number') {
if (parsedNumber < 1e-15) {
console.warn(PrecisionLossWarning);
}
if (parsedNumber > 1e+20) {
console.warn(PrecisionLossWarning);
parsedNumber = BigInt(parsedNumber);
}
else {
// in case there is a decimal point, we need to convert it to string
parsedNumber = parsedNumber.toLocaleString('fullwide', { useGrouping: false, maximumFractionDigits: 20 });
}
}
// if value is decimal e.g. 24.56 extract `integer` and `fraction` part
// to avoid `fraction` to be null use `concat` with empty string
const [integer, fraction] = String(typeof number === 'string' && !(0, web3_validator_1.isHexStrict)(number) ? number : (0, exports.toNumber)(number))
const [integer, fraction] = String(typeof parsedNumber === 'string' && !(0, web3_validator_1.isHexStrict)(parsedNumber) ? parsedNumber : (0, exports.toNumber)(parsedNumber))
.split('.')

@@ -501,0 +517,0 @@ .concat('');

2

lib/commonjs/formatter.d.ts

@@ -22,2 +22,2 @@ import { DataFormat, FormatType } from 'web3-types';

export declare const convert: (data: Record<string, unknown> | unknown[] | unknown, schema: JsonSchema, dataPath: string[], format: DataFormat, oneOfPath?: [string, number][]) => unknown;
export declare const format: <DataType extends unknown, ReturnType_1 extends DataFormat>(schema: ValidationSchemaInput | JsonSchema, data: DataType, returnFormat: ReturnType_1) => FormatType<DataType, ReturnType_1>;
export declare const format: <DataType extends unknown, ReturnType_1 extends DataFormat>(schema: ValidationSchemaInput | JsonSchema, data: DataType, returnFormat?: ReturnType_1) => FormatType<DataType, ReturnType_1>;

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

exports.convert = convert;
const format = (schema, data, returnFormat) => {
const format = (schema, data, returnFormat = web3_types_1.DEFAULT_RETURN_FORMAT) => {
let dataToParse;

@@ -233,0 +233,0 @@ if ((0, web3_validator_1.isObject)(data)) {

@@ -57,2 +57,3 @@ /*

};
const PrecisionLossWarning = 'Warning: Using type `number` with values that are large or contain many decimals may cause loss of precision, it is recommended to use type `string` or `BigInt` when using conversion methods';
/**

@@ -358,2 +359,3 @@ * Convert a value from bytes to Uint8Array

if (value > 1e+20) {
console.warn(PrecisionLossWarning);
// JavaScript converts numbers >= 10^21 to scientific notation when coerced to strings,

@@ -478,5 +480,19 @@ // leading to potential parsing errors and incorrect representations.

}
let parsedNumber = number;
if (typeof parsedNumber === 'number') {
if (parsedNumber < 1e-15) {
console.warn(PrecisionLossWarning);
}
if (parsedNumber > 1e+20) {
console.warn(PrecisionLossWarning);
parsedNumber = BigInt(parsedNumber);
}
else {
// in case there is a decimal point, we need to convert it to string
parsedNumber = parsedNumber.toLocaleString('fullwide', { useGrouping: false, maximumFractionDigits: 20 });
}
}
// if value is decimal e.g. 24.56 extract `integer` and `fraction` part
// to avoid `fraction` to be null use `concat` with empty string
const [integer, fraction] = String(typeof number === 'string' && !isHexStrict(number) ? number : toNumber(number))
const [integer, fraction] = String(typeof parsedNumber === 'string' && !isHexStrict(parsedNumber) ? parsedNumber : toNumber(parsedNumber))
.split('.')

@@ -483,0 +499,0 @@ .concat('');

@@ -18,3 +18,3 @@ /*

import { FormatterError } from 'web3-errors';
import { FMT_BYTES, FMT_NUMBER } from 'web3-types';
import { FMT_BYTES, FMT_NUMBER, DEFAULT_RETURN_FORMAT, } from 'web3-types';
import { isNullish, isObject, utils } from 'web3-validator';

@@ -225,3 +225,3 @@ import { bytesToUint8Array, bytesToHex, numberToHex, toBigInt } from './converters.js';

};
export const format = (schema, data, returnFormat) => {
export const format = (schema, data, returnFormat = DEFAULT_RETURN_FORMAT) => {
let dataToParse;

@@ -228,0 +228,0 @@ if (isObject(data)) {

@@ -22,3 +22,3 @@ import { DataFormat, FormatType } from 'web3-types';

export declare const convert: (data: Record<string, unknown> | unknown[] | unknown, schema: JsonSchema, dataPath: string[], format: DataFormat, oneOfPath?: [string, number][]) => unknown;
export declare const format: <DataType extends unknown, ReturnType_1 extends DataFormat>(schema: ValidationSchemaInput | JsonSchema, data: DataType, returnFormat: ReturnType_1) => FormatType<DataType, ReturnType_1>;
export declare const format: <DataType extends unknown, ReturnType_1 extends DataFormat>(schema: ValidationSchemaInput | JsonSchema, data: DataType, returnFormat?: ReturnType_1) => FormatType<DataType, ReturnType_1>;
//# sourceMappingURL=formatter.d.ts.map
{
"name": "web3-utils",
"sideEffects": false,
"version": "4.2.3",
"version": "4.2.4-dev.12c2515.0+12c2515",
"description": "Collection of utility functions used in web3.js.",

@@ -34,3 +34,3 @@ "main": "./lib/commonjs/index.js",

"build:check": "node -e \"require('./lib')\"",
"lint": "eslint --ext .js,.ts .",
"lint": "eslint --cache --cache-strategy content --ext .ts .",
"lint:fix": "eslint --fix --ext .js,.ts .",

@@ -69,7 +69,7 @@ "format": "prettier --write '**/*'",

"eventemitter3": "^5.0.1",
"web3-errors": "^1.1.4",
"web3-types": "^1.6.0",
"web3-validator": "^2.0.5"
"web3-errors": "1.1.5-dev.12c2515.0+12c2515",
"web3-types": "1.6.1-dev.12c2515.0+12c2515",
"web3-validator": "2.0.6-dev.12c2515.0+12c2515"
},
"gitHead": "93eeccb96d7bcddfa49ed4c4848dbf55b825546c"
"gitHead": "12c251589198129781d24d2327d67ead9f20b3e8"
}

@@ -80,2 +80,4 @@ /*

const PrecisionLossWarning = 'Warning: Using type `number` with values that are large or contain many decimals may cause loss of precision, it is recommended to use type `string` or `BigInt` when using conversion methods';
export type EtherUnits = keyof typeof ethUnitMap;

@@ -419,3 +421,4 @@ /**

if (typeof value === 'number') {
if (value > 1e+20) {
if (value > 1e+20) {
console.warn(PrecisionLossWarning)
// JavaScript converts numbers >= 10^21 to scientific notation when coerced to strings,

@@ -560,7 +563,21 @@ // leading to potential parsing errors and incorrect representations.

}
let parsedNumber = number;
if (typeof parsedNumber === 'number'){
if (parsedNumber < 1e-15){
console.warn(PrecisionLossWarning)
}
if (parsedNumber > 1e+20) {
console.warn(PrecisionLossWarning)
parsedNumber = BigInt(parsedNumber);
} else {
// in case there is a decimal point, we need to convert it to string
parsedNumber = parsedNumber.toLocaleString('fullwide', {useGrouping: false, maximumFractionDigits: 20})
}
}
// if value is decimal e.g. 24.56 extract `integer` and `fraction` part
// to avoid `fraction` to be null use `concat` with empty string
const [integer, fraction] = String(
typeof number === 'string' && !isHexStrict(number) ? number : toNumber(number),
typeof parsedNumber === 'string' && !isHexStrict(parsedNumber) ? parsedNumber : toNumber(parsedNumber),
)

@@ -572,2 +589,3 @@ .split('.')

// 24.56 -> 2456
const value = BigInt(`${integer}${fraction}`);

@@ -574,0 +592,0 @@

@@ -18,3 +18,10 @@ /*

import { FormatterError } from 'web3-errors';
import { Bytes, DataFormat, FMT_BYTES, FMT_NUMBER, FormatType } from 'web3-types';
import {
Bytes,
DataFormat,
FMT_BYTES,
FMT_NUMBER,
FormatType,
DEFAULT_RETURN_FORMAT,
} from 'web3-types';
import { isNullish, isObject, JsonSchema, utils, ValidationSchemaInput } from 'web3-validator';

@@ -281,3 +288,3 @@ import { bytesToUint8Array, bytesToHex, numberToHex, toBigInt } from './converters.js';

data: DataType,
returnFormat: ReturnType,
returnFormat: ReturnType = DEFAULT_RETURN_FORMAT as ReturnType,
): FormatType<DataType, ReturnType> => {

@@ -284,0 +291,0 @@ let dataToParse: Record<string, unknown> | unknown[] | unknown;

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

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc