UNPKG

hardhat

Version:

Hardhat is an extensible developer tool that helps smart contract developers increase productivity by reliably bringing together the tools they want.

43 lines 1.64 kB
import { isObject } from "@nomicfoundation/hardhat-utils/lang"; /** * Creates a successful Result with the given value. * * @param value The value to include in the result. * @returns A Result with success: true and the given value. */ export function successfulResult(...args) { /* eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Cast needed because TS loses the conditional rest parameter context */ return { success: true, value: args[0] }; } /** * Creates a failed Result with the given error. * * @param error The error to include in the result. * @returns A Result with success: false and the given error. */ export function errorResult(...args) { /* eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Cast needed because TS loses the conditional rest parameter context */ return { success: false, error: args[0] }; } /** * A type guard that checks if a value is a Result. * * Optionally validates the value and error fields using type guard functions. * * @param value The value to check. * @param isValue Optional type guard for the value field. * @param isError Optional type guard for the error field. * @returns true if the value is a Result. */ export function isResult(value, isValue, isError) { if (!isObject(value) || typeof value.success !== "boolean") { return false; } if (value.success === true) { return "value" in value && (isValue === undefined || isValue(value.value)); } return "error" in value && (isError === undefined || isError(value.error)); } //# sourceMappingURL=result.js.map