craftix
Version:
This package helps you in game development and HTML design.
253 lines (252 loc) • 9.12 kB
TypeScript
/**
* Checks if the code is running in a web browser environment.
* @returns {boolean} True if running in a web browser, false otherwise.
*/
export declare const isUsingWebBrowser: boolean;
/**
* Generates a random integer between the specified minimum and maximum values, inclusive.
* @param {number} min - The minimum value of the range.
* @param {number} max - The maximum value of the range.
*/
export declare const getRandomInteger: (min: number, max: number) => number;
/**
* Generates a random boolean value (true/false) with equal probability.
*/
export declare const getRandomBoolean: () => boolean;
/**
* Checks if the current device is a mobile device based on the user agent string.
*/
export declare const isMobileDevice: boolean;
/**
* Checks if the application is running locally (on localhost).
*/
export declare const isRunningLocally: boolean;
/**
* Checks if the current device supports touch events.
*/
export declare const isTouchDevice: boolean | 0;
/**
* Formats a number into a currency string using the en-US locale.
* @param {number} value - The number to be formatted as currency.
* @example
* ```typescript
* const formattedCurrency = formatToCurrency(123456.78);
* console.log(formattedCurrency); // Prints: $123,456.78
* ```
*/
export declare const formatToCurrency: (value: number) => string;
/**
* Pauses execution for the specified number of milliseconds asynchronously.
* @param {number} ms - The number of milliseconds to wait.
* @example
* ```typescript
* await wait(1000); // Waits for 1 second
* ```
*/
export declare const wait: (ms: number) => Promise<void>;
/**
* Calculates the distance between two points in one-dimensional space.
* @param {number} p1 - The first point.
* @param {number} p2 - The second point.
* @example
* ```typescript
* const distance = calculateVectorDistance(-5, 5);
* console.log(distance); // Prints: 10
* ```
*/
export declare const calculateVectorDistance: (p1: number, p2: number) => number;
/**
* Calculates the Euclidean distance between two points in a 2D plane.
* @param {number} x1 - The x-coordinate of the first point.
* @param {number} y1 - The y-coordinate of the first point.
* @param {number} x2 - The x-coordinate of the second point.
* @param {number} y2 - The y-coordinate of the second point.
* @example
* ```typescript
* const distance = calculateDistance(0, 0, 3, 4);
* console.log(distance); // Prints: 5
* ```
*/
export declare const calculateDistance: (x1: number, y1: number, x2: number, y2: number) => number;
/**
* Calculates the percentage of a number.
* @param {number} number - The number to calculate the percentage of.
* @param {number} percent - The percentage to calculate.
* @example:
* ```typescript
* const percentage = calculatePercentage(50, 20);
* console.log(percentage); // Prints: 10
* ```
*/
export declare const calculatePercentage: (number: number, percent: number) => number;
/**
* Calculates the percentage of a value relative to a maximum value.
* @param {number} value - The value to calculate the percentage of.
* @param {number} max - The maximum value.
* @example
* ```typescript
* const relativePercentage = calculateRelativePercentage(25, 100);
* console.log(relativePercentage); // Prints: 25
* ```
*/
export declare const calculateRelativePercentage: (value: number, max: number) => number;
/**
* Checks if a text contains right-to-left (RTL) characters.
* ```typescript
* const hasRTL = hasRTLCharacters('سلام');
* console.log(hasRTL); // Prints: true
* ```
*/
export declare const hasRTLCharacters: (text: string) => boolean;
/**
* Decodes HTML encoded text.
* @example
* ```typescript
* const decodedText = decodeHTMLEncodedText('★ Hello ★');
* console.log(decodedText); // Prints: ★ Hello ★
* ```
*/
export declare const decodeHTMLEncodedText: (text: string) => string;
/**
* Clamps a value within a specified range.
* @param {number} min - The minimum value of the range.
* @param {number} value - The value to be clamped.
* @param {number} max - The maximum value of the range.
* @example
* ```typescript
* const clampedValue = clamp(0, 10, 20);
* console.log(clampedValue); // Prints: 10
* ```
*/
export declare const clamp: (min: number, value: number, max: number) => number;
/**
* Converts RGB values to a hexadecimal color code.
* @param {number} r - The red component (0-255).
* @param {number} g - The green component (0-255).
* @param {number} b - The blue component (0-255).
* @example
* ```typescript
* const hexColor = convertRGBToHex(255, 0, 0);
* console.log(hexColor); // Prints: #ff0000
* ```
*/
export declare const convertRGBToHex: (r: number, g: number, b: number) => string;
/**
* Converts a hexadecimal color code to its RGB representation.
* @param {string} hex - The hexadecimal color code.
* @example
* ```typescript
* const rgbArray = convertHexToRGB('#00ff00');
* console.log(rgbArray); // Prints: [0, 255, 0]
* ```
*/
export declare const convertHexToRGB: (hex: string) => number[] | null;
/**
* Converts a hexadecimal color code to its RGBA representation.
* @param {string} hex - The hexadecimal color code.
* @example
* ```typescript
* const rgbaArray = convertHexToRGBA('#ff0000');
* console.log(rgbaArray); // Prints: [255, 0, 0, 1]
* ```
*/
export declare const convertHexToRGBA: (hex: string) => (number | string)[];
/**
* Converts RGBA values to a hexadecimal color code with alpha.
* @example
* ```typescript
* const hexWithAlpha = convertRGBAToHexWithAlpha(255, 0, 0, 0.5);
* console.log(hexWithAlpha); // Prints: #ff000080
* ```
*/
export declare function convertRGBAToHexWithAlpha(r: number, g: number, b: number, a: number): string;
/**
* Linearly interpolates between two colors.
* @param {number} a - The first color.
* @param {number} b - The second color.
* @param {number} amount - The interpolation amount (0-1).
* @example
* ```typescript
* const interpolatedColor = interpolateColor(0xff0000, 0x00ff00, 0.5);
* console.log(interpolatedColor); // Prints: #ffff00
* ```
*/
export declare function interpolateColor(a: number, b: number, amount: number): string;
/**
* Sets the opacity of a hexadecimal color code.
* @param {string} color - The hexadecimal color code.
* @param {number} opacity - The opacity value (0-1).
* @example
* ```typescript
* const modifiedColor = setHexOpacity('#ff0000', 0.5);
* console.log(modifiedColor); // Prints: #ff000080
* ```
*/
export declare const setHexOpacity: (color: string, opacity: number) => string;
/**
* Formats a number with appropriate suffixes (K, M, B, T) based on its magnitude.
*
* @param {number} number - The number to be formatted.
* @example
* ```typescript
* const formattedNumber = formatNumberWithSuffix(1234567);
* console.log(formattedNumber); // Prints: 1.23M
* ```
*/
export declare function formatNumberWithSuffix(number: number): string;
/**
* Constructs a query string by appending key-value pairs from the given data object to the provided URL.
*
* @param {string} url - The base URL to which the query string will be appended.
* @param {object} data - The data object containing key-value pairs to be converted into query parameters.
* @example
* ```typescript
* const url = dataToQuery('https://example.com/api', { page: 1, limit: 10 });
* console.log(url); // Prints: https://example.com/api?page=1&limit=10
* ```
*/
export declare const dataToQuery: (url: string, data: Record<string, string>) => string;
/**
* Generates a random GUID (Globally Unique Identifier).
*
* @param {number} [length=10] - The length of the generated GUID. Default is 10.
* @example
* ```typescript
* const generatedGUID = guid();
* console.log(generatedGUID); // Prints: "a8b4e95d12"
* ```
*/
export declare const guid: (length?: number) => string;
/**
* Adds a new script element to the document head with the provided script.
*
* @param {string} script - The JavaScript script to be executed by the added script element.
* @example
* ```typescript
* const script = "console.log('Hello, world!');";
* addScriptByElement(script);
* ```
*/
export declare function addScriptByElement(script: string): void;
/**
* Checks if an element is visible in the viewport.
*
* @param {Element} element - The element to check visibility for.
* @param {boolean} [partiallyVisible=false] - Whether the element is considered visible if only partially visible. Default is false.
*/
export declare const isElementVisibleInViewport: (element: Element, partiallyVisible?: boolean) => boolean;
/**
* Measures the elapsed time taken by an asynchronous function to execute.
*
* @param {Function} func - The asynchronous function to measure the elapsed time for.
* @example
* ```typescript
* async function exampleAsyncFunction() {
* await wait(1000);
* console.log('Async function executed!');
* }
* const elapsedTime = await elapsed(exampleAsyncFunction);
* console.log(elapsedTime); // Prints: Time taken in seconds
* ```
*/
export declare function elapsed(func: Function): Promise<number>;