web3-utils
Advanced tools
Comparing version 4.2.3 to 4.2.4-dev.12c2515.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.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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
527099
9978
2
1
- Removedavailable-typed-arrays@1.0.7(transitive)
- Removedcall-bind@1.0.7(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedfor-each@0.3.3(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-arguments@1.1.1(transitive)
- Removedis-callable@1.2.7(transitive)
- Removedis-generator-function@1.0.10(transitive)
- Removedis-typed-array@1.1.13(transitive)
- Removedpossible-typed-array-names@1.0.0(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedutil@0.12.5(transitive)
- Removedweb3-errors@1.2.0(transitive)
- Removedweb3-types@1.6.0(transitive)
- Removedweb3-validator@2.0.6(transitive)
- Removedwhich-typed-array@1.1.15(transitive)
- Removedzod@3.23.8(transitive)