web3-utils
Advanced tools
Comparing version 4.2.4-dev.e29deea.0 to 4.2.4-dev.ebbbf1e.0
@@ -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(''); |
@@ -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.4-dev.e29deea.0+e29deea", | ||
"version": "4.2.4-dev.ebbbf1e.0+ebbbf1e", | ||
"description": "Collection of utility functions used in web3.js.", | ||
@@ -68,7 +68,7 @@ "main": "./lib/commonjs/index.js", | ||
"eventemitter3": "^5.0.1", | ||
"web3-errors": "1.1.5-dev.e29deea.0+e29deea", | ||
"web3-types": "1.6.1-dev.e29deea.0+e29deea", | ||
"web3-validator": "2.0.6-dev.e29deea.0+e29deea" | ||
"web3-errors": "1.1.5-dev.ebbbf1e.0+ebbbf1e", | ||
"web3-types": "1.6.1-dev.ebbbf1e.0+ebbbf1e", | ||
"web3-validator": "2.0.6-dev.ebbbf1e.0+ebbbf1e" | ||
}, | ||
"gitHead": "e29deeac2bb9705d70c839cd825bc34a93cde1b6" | ||
"gitHead": "ebbbf1e8bb2562708f89de431fc630ea7d665624" | ||
} |
@@ -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
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
527070
9978