sat-utils
Version:
158 lines (157 loc) • 6.57 kB
TypeScript
type TcamelCase = {
firstWordUpperCase?: boolean;
allUpperCase?: boolean;
joinWords?: string;
};
/**
* Converts a value or an array of values to an array.
*
* @template T
* @param {T | T[]} anyArgument - The value or array to convert.
* @param {T | T[]} undefinedIsFine - Return arr with undefined member is argument is undefined
* @returns {T[]} An array containing the input value(s).
*/
declare function toArray<T>(anyArgument: T | T[], undefinedIsFine?: boolean): T[];
/**
* Splits an array into multiple smaller arrays.
*
* @template T
* @param {T[]} arr - The input array.
* @param {number} chunksAmount - The number of smaller arrays to create.
* @param {boolean} [followIndex=false] - If true, chunks will have approximately equal lengths; otherwise, they will be balanced.
* @returns {Array<T[]>} An array of smaller arrays.
* @throws {TypeError} If the input is not an array or the chunk size is not a number.
*/
declare function chunkArr<T>(arr: T[], chunksAmount: number, followIndex?: boolean): Array<T[]>;
/**
* Shuffles an array in place.
*
* @template T
* @param {T[]} arr - The array to shuffle.
* @throws {TypeError} If the input is not an array.
*/
declare function shuffleArrMutable<T>(arr: T[]): void;
/**
* Shuffles a copy of an array.
*
* @template T
* @param {T[]} arr - The input array.
* @returns {T[]} A shuffled copy of the input array.
* @throws {TypeError} If the input is not an array.
*/
declare function shuffleArr<T>(arr: T[]): T[];
/**
* Options for the prettifyCamelCase function.
*
* @typedef {Object} TcamelCase
* @property {boolean} [firstWordUpperCase=false] - If true, the first word will start with an uppercase letter.
* @property {boolean} [allUpperCase=false] - If true, the entire string will be in uppercase.
* @property {string} [joinWords=' '] - The string to use for joining words.
*/
/**
* Prettifies a camelCase string.
*
* @param {string} camelCaseString - The camelCase string to prettify.
* @param {TcamelCase} [_opts] - Options for prettifying the string.
* @returns {string} The prettified string.
* @throws {TypeError} If the input is not a string or the options are not valid.
*/
declare function prettifyCamelCase(camelCaseString: string, _opts?: TcamelCase): string;
/**
* Checks if a mathematical expression is true for a given number.
*
* @param {string} expression - The mathematical expression to evaluate.
* @param {number} numberArg - The number to use in the expression.
* @returns {boolean} True if the expression is true for the given number; otherwise, false.
* @throws {TypeError} If the expression is not a string or the number is not a number.
*/
declare function execNumberExpression(expression: string, numberArg: number): boolean;
/**
* Converts a string to camelCase.
*
* @param {string} str - The input string.
* @returns {string} The string in camelCase.
* @throws {TypeError} If the input is not a string.
*/
declare function camelize(str: string): string;
/**
* Safely converts an object to a JSON string.
*
* @param {*} data - The data to stringify.
* @param {boolean} [inline=false] - If true, the resulting JSON will be on a single line.
* @param {*} [returnIfError=''] - The value to return if an error occurs during stringification.
* @returns {string} The JSON string or the specified return value if an error occurs.
*/
declare function safeJSONstringify(data: any, inline?: boolean, returnIfError?: string): string;
/**
* Safely parses a JSON string.
*
* @param {*} data - The JSON string to parse.
* @param {*} [returnIfError={}] - The value to return if an error occurs during parsing.
* @returns {*} The parsed JSON object or the specified return value if an error occurs.
*/
declare function safeJSONparse(data: any, returnIfError?: {}): any;
/**
* Checks if an object has a specified property.
*
* @param {*} item - The object to check.
* @param {string} key - The property key to check for.
* @returns {boolean} True if the object has the property; otherwise, false.
* @throws {TypeError} If the key is not a string.
*/
declare function safeHasOwnPropery(item: any, key: string | symbol): boolean;
/**
* Generates an array of indexes from 0 to a specified length.
*
* @param {number} length - The length of the resulting array.
* @returns {number[]} An array of indexes.
* @throws {TypeError} If the length is not a number.
*/
declare function lengthToIndexesArray(length: number): number[];
/**
* Generates a random number within a specified range.
*
* @param {number} min - The minimum value of the range (inclusive).
* @param {number} max - The maximum value of the range (exclusive).
* @returns {number} A random number within the specified range.
* @throws {TypeError} If the min or max values are not numbers.
*/
declare function getRandomNumberFromRange(min: number, max: number): number;
/**
* Checks if an object can be safely converted to a JSON string.
*
* @param {*} item - The object to check.
* @returns {boolean} True if the object can be converted to JSON; otherwise, false.
*/
declare function canBeStringified(item?: any): boolean;
/**
* Filters and returns an object with only stringifiable values.
*
* @param {*} data - The input data.
* @returns {*} An object with only stringifiable values or an empty string if none are found.
*/
declare function getStringifyReadyData(data: any): any;
type TstringifyDataConfig = {
ignoreFunctions?: boolean;
};
/**
* Converts an object to a string.
*
* @param {*} obj - The object to convert.
* @param {TstringifyDataConfig} [config] - Configuration options.
* @returns {string} The string representation of the object.
*/
declare function stringifyData(obj: unknown, config?: TstringifyDataConfig): string;
type TgetStringEqualtyPersentage = {
ignoreSpaces?: boolean;
toLowerCase?: boolean;
ignorePunctuation?: boolean;
};
/**
* @param {string} str string to check percentage of the equality
* @param {string} inStr string to check how many pecent is in str
* @param {object} opts strings modification options
* @returns {number} percentage of the equality
*/
declare function getStringEqualtyPersentage(str: string, inStr: string, opts?: TgetStringEqualtyPersentage): number;
export { toArray, prettifyCamelCase, execNumberExpression, camelize, safeJSONstringify, shuffleArrMutable, safeHasOwnPropery, shuffleArr, chunkArr, lengthToIndexesArray, getRandomNumberFromRange, getStringifyReadyData, canBeStringified, safeJSONparse, stringifyData, getStringEqualtyPersentage, };