web3-utils
Advanced tools
Comparing version 4.3.2-dev.cbcfc18.0 to 4.3.2-dev.cc99825.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>; |
@@ -121,2 +121,5 @@ "use strict"; | ||
} | ||
if (baseType === 'string') { | ||
return String(value); | ||
} | ||
} | ||
@@ -232,3 +235,3 @@ catch (error) { | ||
dataPath.push(key); | ||
const schemaProp = findSchemaByDataPath(schema, dataPath, oneOfPath); | ||
let schemaProp = findSchemaByDataPath(schema, dataPath, oneOfPath); | ||
// If value is a scaler value | ||
@@ -259,2 +262,15 @@ if ((0, web3_validator_1.isNullish)(schemaProp)) { | ||
} | ||
// 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 === 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) { | ||
schemaProp = oneOfSchemaProp; | ||
break; | ||
} | ||
} | ||
} | ||
object[key] = (0, exports.convertScalarValue)(value, schemaProp.format, format); | ||
@@ -267,2 +283,23 @@ dataPath.pop(); | ||
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) => { | ||
@@ -269,0 +306,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 @@ }; |
@@ -117,2 +117,5 @@ /* | ||
} | ||
if (baseType === 'string') { | ||
return String(value); | ||
} | ||
} | ||
@@ -227,3 +230,3 @@ catch (error) { | ||
dataPath.push(key); | ||
const schemaProp = findSchemaByDataPath(schema, dataPath, oneOfPath); | ||
let schemaProp = findSchemaByDataPath(schema, dataPath, oneOfPath); | ||
// If value is a scaler value | ||
@@ -254,2 +257,15 @@ if (isNullish(schemaProp)) { | ||
} | ||
// 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 === 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) { | ||
schemaProp = oneOfSchemaProp; | ||
break; | ||
} | ||
} | ||
} | ||
object[key] = convertScalarValue(value, schemaProp.format, format); | ||
@@ -261,2 +277,23 @@ dataPath.pop(); | ||
}; | ||
/** | ||
* 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) => { | ||
@@ -263,0 +300,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.cbcfc18.0+cbcfc18", | ||
"version": "4.3.2-dev.cc99825.0+cc99825", | ||
"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.2.1-dev.cbcfc18.0+cbcfc18", | ||
"web3-types": "1.7.1-dev.cbcfc18.0+cbcfc18", | ||
"web3-validator": "2.0.7-dev.cbcfc18.0+cbcfc18" | ||
"web3-errors": "1.3.1-dev.cc99825.0+cc99825", | ||
"web3-types": "1.8.1-dev.cc99825.0+cc99825", | ||
"web3-validator": "2.0.7-dev.cc99825.0+cc99825" | ||
}, | ||
"gitHead": "cbcfc1878502008349f805a2e82e9263a02b717e" | ||
"gitHead": "cc99825d31ae4d1d0c6615aa3648486a591a645c" | ||
} |
@@ -134,2 +134,6 @@ /* | ||
} | ||
if (baseType === 'string') { | ||
return String(value); | ||
} | ||
} catch (error) { | ||
@@ -293,3 +297,3 @@ // If someone didn't use `eth` keyword we can return original value | ||
dataPath.push(key); | ||
const schemaProp = findSchemaByDataPath(schema, dataPath, oneOfPath); | ||
let schemaProp = findSchemaByDataPath(schema, dataPath, oneOfPath); | ||
@@ -327,2 +331,16 @@ // If value is a scaler 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; | ||
} | ||
} | ||
} | ||
object[key] = convertScalarValue(value, schemaProp.format as string, format); | ||
@@ -337,2 +355,23 @@ | ||
/** | ||
* 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 = < | ||
@@ -339,0 +378,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