UNPKG

@kodex-data/prototypes

Version:

A library of TypeScript prototypes that extend built-in types in JavaScript, providing additional functionality to make development easier and more efficient. This library includes prototypes for working with big numbers, Ethereum Virtual Machine (EVM) fu

213 lines (212 loc) 10.1 kB
import type { Fraction, MinMax, ToBnInputs, ToBnOptions } from './types'; import { BigNumber as BigNumberJS } from 'bignumber.js'; import { BigNumber } from '@ethersproject/bignumber'; import BN from 'bn.js'; export declare const EVM_NUMBER: RegExp; export declare const EVM_HEX: RegExp; export declare const EVM_EMPTY_ADDR: RegExp; export declare const EVM_ADDRESS: RegExp; export declare const EVM_HASH: RegExp; export declare const TWITTER_REGEX: RegExp; export declare function isTwitterUrl(url: string): boolean; export declare function getTwitterUsername(url: string): string | undefined; export declare const saveParse: <T = any>(data: string, errorHandler?: Function) => T | undefined; export declare function toSha256Hex(str: string): string; /** * Checks if a piece of code is valid JSON * @date 5/24/2022 - 12:20:00 AM * * @export * @param {string} code containing a piece of JSON code * @returns {code is string} */ export declare function isValidJSON(code: string): boolean; export declare class ToJSON_WithSpaces_Options { insideObjectBraces: boolean; insideArrayBrackets: boolean; betweenPropsOrItems: boolean; betweenPropNameAndValue: boolean; } export declare function ToJSON_WithSpaces(obj: any, options?: Partial<ToJSON_WithSpaces_Options>): string; export declare function formatJSON(input: string, options?: Partial<ToJSON_WithSpaces_Options>): string; export declare const genRanHex: (size: number) => string; export declare function isError(candidate: any): candidate is Error; export declare function createError(message: string | Error, ...metaData: any[]): Error; export declare function compare(candidate: string, comparator: string, checkAddr?: boolean): boolean; export declare function findHex(str: string, _type?: 'hex' | 'address' | 'keccak', match?: RegExp): string[]; export declare function isEtherAddress(query: string): boolean; export declare function isKeccakHash(query: string): boolean; export declare function isAddress(query: string): boolean; export declare function toHEX(input: string): string; export declare function fromHEX(input: string): string; export declare function isHEX(input: string): boolean; export declare function toUint(inputValue: number | string): string; export declare function parseUint(inputValue: number | string | BigNumber, add0x?: boolean): string; export declare function toSHA3(data: any): string; export declare function isNumber(query: string): boolean; export declare function strip0x(input: string): string; /** * Calculates the checksum address of an EVM address. * @date 11.7.2023 - 20:50:02 * * @export * @param {string} address - The EVM address (with or without '0x' prefix). * @returns {string} The checksum address. */ export declare function toChecksumAddress(address: string): string; /** * Returns the Unix timestamp (number of seconds since January 1, 1970) rounded up to the nearest second. * @date 8.3.2023 - 09:53:47 * * @export * @returns {number} - The Unix timestamp in seconds. */ export declare function timestamp(): number; /** * Returns a promise that resolves after a given number of milliseconds. * @date 8.3.2023 - 09:54:22 * * @export * @param {number} ms - The number of milliseconds to wait before resolving the promise. * @returns {Promise<void>} - A promise that resolves after the specified number of milliseconds */ export declare function sleep(ms: number): Promise<void>; /** * Converts a Unix timestamp to a date string in the format "MM/DD/YYYY HH:MM:SS". * @date 8.3.2023 - 09:55:03 * * @export * @param {number} ts - The Unix timestamp in seconds. * @returns {string} - The date string in the format "MM/DD/YYYY HH:MM:SS". */ export declare function ts2date(ts: number): string; /** * Formats a date object as a string in the format "YYYY-MM-DD". * @date 8.3.2023 - 09:56:08 * * @export * @param {Date} date - The date object to format. * @returns {string} The formatted date string in the format "YYYY-MM-DD". */ export declare function formatMMDDYYYY(date: Date): string; /** * Converts a number of seconds to a human-readable string in the format "X units". * @date 8.3.2023 - 09:56:41 * * @export * @param {number} seconds - The number of seconds to convert. * @param {?string} [tags=''] - (optional) The HTML tags to wrap around the unit name. Defaults to an empty string. * @returns {string} The human-readable string in the format "X units". */ export declare function sec2read(seconds: number, tags?: string): string; /** * Returns the time part of a Date object in the format "HH:MM:SS" or "HH:MM:SS:MS" if addMS is true. * @date 8.3.2023 - 09:57:48 * * @export * @param {Date} date - The Date object to format. * @param {?boolean} [addMS] - Optional. Whether to include the milliseconds part in the returned string. * @returns {string} - The time part of the Date object in the specified format. */ export declare function formatHHMMSS(date: Date, addMS?: boolean): string; /** * Returns an array of strings representing the days of the specified month and year. * Each string in the array has the format "DD-dayName", where "DD" is the day of the month and "dayName" is the name of the day (e.g. "01-Sun"). * @date 8.3.2023 - 09:59:43 * * @export * @param {number} year - The year of the month to get days for. * @param {number} month - The month to get days for (1-based). * @returns {string[]} - An array of strings representing the days of the specified month and year. */ export declare function getDaysArray(year: number, month: number): string[]; /** * Converts the specified input into a big number object. * @date 8.3.2023 - 08:25:00 * * @export * @param {ToBnInputs} input The input to convert. * @param {ToBnOptions} [options={}] Options for the conversion. * @param {?BnTypes} options.type - The library to use for big number conversion. Defaults to 'bignumber.js'. * @param {?number} options.base - The base to use when converting from a string. Only used for 'bignumber.js'. * @returns {BN | BigNumber | BigNumberJS | bigint} The input as a big number object. */ export declare function toBN(input: ToBnInputs, options?: ToBnOptions): BN | BigNumber | BigNumberJS | bigint; /** * Converts a number to a pretty string with thousands separators. * @date 8.3.2023 - 08:30:06 * * @export * @param {(string | number | BigNumber | BigNumberJS)} input The input number as a string, number, BigNumber, or BigNumberJS. * @returns {string} The input number as a pretty string. */ export declare function prettyNum(input: string | number | BigNumber | BigNumberJS): string; /** * Computes the sum of an array of numbers or BigNumbers. * @date 8.3.2023 - 08:30:17 * * @export * @param {(string | number | BigNumber | BigNumberJS)[]} inputs The array of numbers or BigNumbers to sum. * @returns {BigNumberJS} The sum of the input array as a BigNumberJS. */ export declare function sum(inputs: (string | number | BigNumber | BigNumberJS)[]): BigNumberJS; /** * Computes the mean of an array of numbers or BigNumbers. * @date 8.3.2023 - 08:30:11 * * @export * @param {(string | number | BigNumber | BigNumberJS)[]} inputs The array of numbers or BigNumbers to compute the mean of. * @returns {BigNumberJS} The mean of the input array as a BigNumberJS. */ export declare function mean(inputs: (string | number | BigNumber | BigNumberJS)[]): BigNumberJS; /** * Computes the minimum and maximum values of an array of numbers or BigNumbers. * @date 8.3.2023 - 08:30:50 * * @export * @param {(string | number | BigNumber | BigNumberJS)[]} items The array of numbers or BigNumbers to compute the minimum and maximum of. * @returns {MinMax} An object containing the minimum and maximum values as BigNumberJS instances. */ export declare function minMax(items: (string | number | BigNumber | BigNumberJS)[]): MinMax; /** * Computes the minimum value of an array of numbers or BigNumbers. * @date 8.3.2023 - 08:30:56 * * @export * @param {(string | number | BigNumber | BigNumberJS)[]} items The array of numbers or BigNumbers to compute the minimum of. * @returns {(BigNumberJS)} The minimum value of the input array as a BigNumberJS. */ export declare function min(items: (string | number | BigNumber | BigNumberJS)[]): BigNumberJS; /** * Computes the maximum value of an array of numbers or BigNumbers. * @date 8.3.2023 - 08:31:01 * * @export * @param {(string | number | BigNumber | BigNumberJS)[]} items The array of numbers or BigNumbers to compute the maximum of. * @returns {(BigNumberJS)} The maximum value of the input array as a BigNumberJS. */ export declare function max(items: (string | number | BigNumber | BigNumberJS)[]): BigNumberJS; /** * Converts a number or BigNumber to a fraction represented as two strings mul and div. * @date 8.3.2023 - 08:31:06 * * @export * @param {(string | number | BigNumber | BigNumberJS)} input The input number as a string, number, BigNumber, or BigNumberJS. * @param {?boolean} [asBigInt] - Whether to return the result as a Fraction<BigNumberJS> or Fraction<string>. * @returns {Fraction<string>} The input number as a fraction represented as two strings `mul` and `div`. */ export declare function toFraction(input: string | number | BigNumber | BigNumberJS): Fraction<string>; export declare function toFraction(input: string | number | BigNumber | BigNumberJS, asBigInt: true): Fraction<BigNumberJS>; export declare function toFraction(input: string | number | BigNumber | BigNumberJS, asBigInt?: boolean): Fraction<string | BigNumberJS>; /** * Shifts the decimal point of a given `bigint` by the specified number of positions. * @returns {bigint} * @date 7.7.2023 - 23:40:36 * * @export * @param {bigint} input - The `bigint` number to shift. * @param {number} n - The number of positions to shift the decimal point. * A positive value shifts the decimal point to the right, and a negative value to the left. * @returns {bigint} The `bigint` with the decimal point shifted by `n` positions. */ export declare function shiftedBy(input: bigint, n: number): bigint;