web3-utils
Advanced tools
Comparing version 4.3.2-dev.75df267.0 to 4.3.2-dev.76c468a.0
@@ -22,2 +22,23 @@ 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; | ||
/** | ||
* Given data that can be interpreted according to the provided schema, returns equivalent data that has been formatted | ||
* according to the provided return format. | ||
* | ||
* @param schema - how to interpret the data | ||
* @param data - data to be formatted | ||
* @param returnFormat - how to format the data | ||
* @returns - formatted data | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* import { FMT_NUMBER, utils } from "web3"; | ||
* | ||
* console.log( | ||
* utils.format({ format: "uint" }, "221", { number: FMT_NUMBER.HEX }), | ||
* ); | ||
* // 0xdd | ||
* ``` | ||
* | ||
*/ | ||
export declare const format: <DataType extends unknown, ReturnType_1 extends DataFormat>(schema: ValidationSchemaInput | JsonSchema, data: DataType, returnFormat?: ReturnType_1) => FormatType<DataType, ReturnType_1>; |
@@ -265,5 +265,5 @@ "use strict"; | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call | ||
if (((schemaProp === null || schemaProp === void 0 ? void 0 : schemaProp.format) === undefined) && ((schemaProp === null || schemaProp === void 0 ? void 0 : schemaProp.oneOf) !== undefined)) { | ||
if ((schemaProp === null || schemaProp === void 0 ? void 0 : schemaProp.format) === undefined && (schemaProp === null || schemaProp === void 0 ? void 0 : schemaProp.oneOf) !== undefined) { | ||
for (const [_index, oneOfSchemaProp] of schemaProp.oneOf.entries()) { | ||
if (((oneOfSchemaProp === null || oneOfSchemaProp === void 0 ? void 0 : oneOfSchemaProp.format) !== undefined)) { | ||
if ((oneOfSchemaProp === null || oneOfSchemaProp === void 0 ? void 0 : oneOfSchemaProp.format) !== undefined) { | ||
schemaProp = oneOfSchemaProp; | ||
@@ -273,3 +273,2 @@ break; | ||
} | ||
; | ||
} | ||
@@ -283,2 +282,23 @@ object[key] = (0, exports.convertScalarValue)(value, schemaProp.format, format); | ||
exports.convert = convert; | ||
/** | ||
* Given data that can be interpreted according to the provided schema, returns equivalent data that has been formatted | ||
* according to the provided return format. | ||
* | ||
* @param schema - how to interpret the data | ||
* @param data - data to be formatted | ||
* @param returnFormat - how to format the data | ||
* @returns - formatted data | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* import { FMT_NUMBER, utils } from "web3"; | ||
* | ||
* console.log( | ||
* utils.format({ format: "uint" }, "221", { number: FMT_NUMBER.HEX }), | ||
* ); | ||
* // 0xdd | ||
* ``` | ||
* | ||
*/ | ||
const format = (schema, data, returnFormat = web3_types_1.DEFAULT_RETURN_FORMAT) => { | ||
@@ -285,0 +305,0 @@ let dataToParse; |
@@ -70,5 +70,5 @@ "use strict"; | ||
} | ||
web3_validator_1.validator.validate(['int'], [value]); | ||
const hexString = typeof value === 'string' && (0, web3_validator_1.isHexStrict)(value) ? value : (0, converters_js_1.numberToHex)(value); | ||
const prefixLength = hexString.startsWith('-') ? 3 : 2; | ||
web3_validator_1.validator.validate([hexString.startsWith('-') ? 'int' : 'uint'], [value]); | ||
return hexString.padEnd(characterAmount + prefixLength, sign); | ||
@@ -75,0 +75,0 @@ }; |
@@ -260,5 +260,5 @@ /* | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call | ||
if (((schemaProp === null || schemaProp === void 0 ? void 0 : schemaProp.format) === undefined) && ((schemaProp === null || schemaProp === void 0 ? void 0 : schemaProp.oneOf) !== undefined)) { | ||
if ((schemaProp === null || schemaProp === void 0 ? void 0 : schemaProp.format) === undefined && (schemaProp === null || schemaProp === void 0 ? void 0 : schemaProp.oneOf) !== undefined) { | ||
for (const [_index, oneOfSchemaProp] of schemaProp.oneOf.entries()) { | ||
if (((oneOfSchemaProp === null || oneOfSchemaProp === void 0 ? void 0 : oneOfSchemaProp.format) !== undefined)) { | ||
if ((oneOfSchemaProp === null || oneOfSchemaProp === void 0 ? void 0 : oneOfSchemaProp.format) !== undefined) { | ||
schemaProp = oneOfSchemaProp; | ||
@@ -268,3 +268,2 @@ break; | ||
} | ||
; | ||
} | ||
@@ -277,2 +276,23 @@ object[key] = convertScalarValue(value, schemaProp.format, format); | ||
}; | ||
/** | ||
* Given data that can be interpreted according to the provided schema, returns equivalent data that has been formatted | ||
* according to the provided return format. | ||
* | ||
* @param schema - how to interpret the data | ||
* @param data - data to be formatted | ||
* @param returnFormat - how to format the data | ||
* @returns - formatted data | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* import { FMT_NUMBER, utils } from "web3"; | ||
* | ||
* console.log( | ||
* utils.format({ format: "uint" }, "221", { number: FMT_NUMBER.HEX }), | ||
* ); | ||
* // 0xdd | ||
* ``` | ||
* | ||
*/ | ||
export const format = (schema, data, returnFormat = DEFAULT_RETURN_FORMAT) => { | ||
@@ -279,0 +299,0 @@ let dataToParse; |
@@ -66,5 +66,5 @@ /* | ||
} | ||
validator.validate(['int'], [value]); | ||
const hexString = typeof value === 'string' && isHexStrict(value) ? value : numberToHex(value); | ||
const prefixLength = hexString.startsWith('-') ? 3 : 2; | ||
validator.validate([hexString.startsWith('-') ? 'int' : 'uint'], [value]); | ||
return hexString.padEnd(characterAmount + prefixLength, sign); | ||
@@ -71,0 +71,0 @@ }; |
@@ -22,3 +22,24 @@ 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; | ||
/** | ||
* Given data that can be interpreted according to the provided schema, returns equivalent data that has been formatted | ||
* according to the provided return format. | ||
* | ||
* @param schema - how to interpret the data | ||
* @param data - data to be formatted | ||
* @param returnFormat - how to format the data | ||
* @returns - formatted data | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* import { FMT_NUMBER, utils } from "web3"; | ||
* | ||
* console.log( | ||
* utils.format({ format: "uint" }, "221", { number: FMT_NUMBER.HEX }), | ||
* ); | ||
* // 0xdd | ||
* ``` | ||
* | ||
*/ | ||
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.3.2-dev.75df267.0+75df267", | ||
"version": "4.3.2-dev.76c468a.0+76c468a", | ||
"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.3.1-dev.75df267.0+75df267", | ||
"web3-types": "1.7.1-dev.75df267.0+75df267", | ||
"web3-validator": "2.0.7-dev.75df267.0+75df267" | ||
"web3-errors": "1.3.1-dev.76c468a.0+76c468a", | ||
"web3-types": "1.8.1-dev.76c468a.0+76c468a", | ||
"web3-validator": "2.0.7-dev.76c468a.0+76c468a" | ||
}, | ||
"gitHead": "75df2677caa955ed3d62eac18e3c6a1fad2103b7" | ||
"gitHead": "76c468ad490ba7a50229acb12690e0239870d8fb" | ||
} |
@@ -136,5 +136,4 @@ /* | ||
if (baseType === 'string') { | ||
return String(value); | ||
} | ||
return String(value); | ||
} | ||
} catch (error) { | ||
@@ -332,14 +331,14 @@ // If someone didn't use `eth` keyword we can return original value | ||
// The following code is basically saying: | ||
// if the schema specifies oneOf, then we are to loop | ||
// over each possible schema and check if they type of the schema specifies format | ||
// and if so we use the oneOfSchemaProp as the schema for formatting | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call | ||
if ((schemaProp?.format === undefined) && (schemaProp?.oneOf !== undefined)) { | ||
for (const [_index, oneOfSchemaProp] of schemaProp.oneOf.entries()) { | ||
if ((oneOfSchemaProp?.format !== undefined)) { | ||
schemaProp = oneOfSchemaProp; | ||
break; | ||
} | ||
}; | ||
} | ||
// if the schema specifies oneOf, then we are to loop | ||
// over each possible schema and check if they type of the schema specifies format | ||
// and if so we use the oneOfSchemaProp as the schema for formatting | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call | ||
if (schemaProp?.format === undefined && schemaProp?.oneOf !== undefined) { | ||
for (const [_index, oneOfSchemaProp] of schemaProp.oneOf.entries()) { | ||
if (oneOfSchemaProp?.format !== undefined) { | ||
schemaProp = oneOfSchemaProp; | ||
break; | ||
} | ||
} | ||
} | ||
@@ -355,2 +354,23 @@ object[key] = convertScalarValue(value, schemaProp.format as string, format); | ||
/** | ||
* Given data that can be interpreted according to the provided schema, returns equivalent data that has been formatted | ||
* according to the provided return format. | ||
* | ||
* @param schema - how to interpret the data | ||
* @param data - data to be formatted | ||
* @param returnFormat - how to format the data | ||
* @returns - formatted data | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* import { FMT_NUMBER, utils } from "web3"; | ||
* | ||
* console.log( | ||
* utils.format({ format: "uint" }, "221", { number: FMT_NUMBER.HEX }), | ||
* ); | ||
* // 0xdd | ||
* ``` | ||
* | ||
*/ | ||
export const format = < | ||
@@ -357,0 +377,0 @@ DataType extends Record<string, unknown> | unknown[] | unknown, |
@@ -78,7 +78,7 @@ /* | ||
validator.validate(['int'], [value]); | ||
const hexString = typeof value === 'string' && isHexStrict(value) ? value : numberToHex(value); | ||
const prefixLength = hexString.startsWith('-') ? 3 : 2; | ||
const prefixLength = hexString.startsWith('-') ? 3 : 2; | ||
validator.validate([hexString.startsWith('-') ? 'int' : 'uint'], [value]); | ||
return hexString.padEnd(characterAmount + prefixLength, sign); | ||
@@ -85,0 +85,0 @@ }; |
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
544050
10370