daily-toolset
Version:
A lightweight, versatile collection of TypeScript utility functions for everyday development needs. Simplify and streamline your Node.js, React, and Next.js projects with a powerful suite of well-organized helpers for strings, arrays, dates, objects, and
224 lines (223 loc) • 8 kB
TypeScript
/**
* Capitalizes the first letter of a string
*
* @example
* capitalize("hello") // "Hello"
*/
export declare function capitalize(str: string): string;
/**
* Converts a string to a URL slug
*
* @example
* slugify("Hello World") // "hello-world"
* slugify("CamelCaseString") // "camel-case-string"
* slugify("") // ""
*/
export declare function slugify(text: string): string;
/**
* Converts a string to kebab-case
*
* @example
* toKebabCase("Hello World") // "hello-world"
* toKebabCase("CamelCaseString") // "camel-case-string"
* toKebabCase("") // ""
*/
export declare function toKebabCase(str: string): string;
/**
* Converts a string to camelCase.
*
* @example
* toCamelCase("Hello World") // "helloWorld"
* toCamelCase("kebab-case-string") // "kebabCaseString"
* toCamelCase("") // ""
*
* @param str The string to convert to camelCase.
* @returns The camelCase version of the input string.
*/
export declare function toCamelCase(str: string): string;
/**
* Converts a string to Title Case.
*
* @example
* toTitleCase("Hello World") // "Hello World"
* toTitleCase("kebab-case-string") // "Kebab Case String"
* toTitleCase("") // ""
*
* @param str The string to convert to Title Case.
* @returns The Title Case version of the input string.
*/
export declare function toTitleCase(str: string): string;
/**
* Converts a string to Snake Case.
*
* @example
* toSnakeCase("Hello World") // "hello_world"
* toSnakeCase("CamelCaseString") // "camel_case_string"
* toSnakeCase("") // ""
*
* @param str The string to convert to Snake Case.
* @returns The Snake Case version of the input string.
*/
export declare function toSnakeCase(str: string): string;
/**
* Converts a string to Sentence Case.
*
* @example
* toSentenceCase("hello world") // "Hello world"
* toSentenceCase("CamelCaseString") // "Camel case string"
* toSentenceCase("") // ""
*
* @param str The string to convert to Sentence Case.
* @returns The Sentence Case version of the input string.
*/
export declare function toSentenceCase(str: string): string;
/**
* Parses the query string from a URL and returns it as an object.
*
* @param url - The URL containing the query string to parse.
* @returns An object where each key-value pair represents a parameter from the query string.
*/
export declare const parseQueryString: (url: string) => Record<string, string>;
/**
* Builds a query string from a given object, string, or URLSearchParams.
*
* @example
* buildQueryString({ foo: 'bar' }) // "foo=bar"
* buildQueryString("foo=bar") // "foo=bar"
* buildQueryString([["foo", "bar"]]) // "foo=bar"
* buildQueryString(new URLSearchParams("foo=bar")) // "foo=bar"
* buildQueryString(undefined) // ""
* @param {string | Record<string, string> | string[][] | URLSearchParams | undefined} params
* @returns {string}
*/
export declare const buildQueryString: (params: string | Record<string, string> | string[][] | URLSearchParams | undefined) => string;
/**
* Appends the correct ordinal suffix to a number.
*
* @example
* addOrdinal(1) // "1st"
* addOrdinal(2) // "2nd"
* addOrdinal(3) // "3rd"
* addOrdinal(4) // "4th"
* addOrdinal(11) // "11th"
* @param {number} num
* @returns {string}
*/
export declare function addOrdinal(num: number): string;
type ChunkSplitParams = {
groupSize?: number;
separator?: string;
};
/**
* Splits a number or string into chunks of a specified size, separated by a specified separator.
*
* @param {Object} params - The parameters for the function.
* @param {number|string} params.data - The number or string to split into chunks.
* @param {number} [params.groupSize=3] - The size of each chunk (default is 3).
* @param {string} [params.separator=" "] - The string used to separate chunks (default is a space).
* @returns {string} - The resulting string with chunks separated by the specified separator.
*
* @example
* chunkSplit({ data: 123456789, groupSize: 3, separator: "," }) // "123,456,789"
*/
export declare function chunkSplit(data: number | string, { groupSize, separator }?: ChunkSplitParams): string;
type UniqueStringParams = {
length?: number;
isPassword?: boolean;
};
/**
* Generates a unique string of a specified length with optional special characters.
*
* @param {Object} params - The parameters for the function.
* @param {number} [params.length=10] - The length of the unique string to generate (default is 10).
* @param {boolean} [params.isPassword=false] - If true, includes special characters for passwords (default is false).
* @returns {string} - The generated unique string.
*
* @example
* uniqueString({ length: 16, isPassword: true }); // "nC4t@h5Ld^3o9Kv1"
*/
export declare function uniqueString({ length, isPassword, }?: UniqueStringParams): string;
type FormatCurrencyParams = {
amount: number;
currency?: string;
};
/**
* Formats a given number as a currency string.
*
* The function takes an amount and an optional currency symbol.
* It returns the amount formatted with two decimal places and
* a thousands separator, prefixed with the specified currency symbol.
*
* @example
* formatCurrency({ amount: 1234.56, currency: "$" }) // "$1,234.56"
*
* @param {number} amount - The amount to format.
* @param {string} [currency=""] - The currency symbol to prefix.
* @returns {string} The formatted currency string.
*/
export declare function formatCurrency({ amount, currency, }: FormatCurrencyParams): string;
/**
* Converts a file size in bytes to a human-readable string format.
*
* This function takes a file size in bytes and converts it to the
* most appropriate unit (Bytes, KB, MB, or GB) with a specified
* number of decimal places.
*
* @param {number} sizeInBytes - The file size in bytes to be converted.
* @param {number} [decimalPlaces=1] - The number of decimal places for the formatted size.
* @returns {string} A string representing the file size in a human-readable format.
*
* @example
* convertFileSize(1024) // "1.0 KB"
* convertFileSize(1048576, 2) // "1.00 MB"
*/
export declare function convertFileSize(sizeInBytes: number, decimalPlaces?: number): string;
/**
* Converts RGB values to a hexadecimal color string.
*
* @param {number} r - The red component of the color (0-255).
* @param {number} g - The green component of the color (0-255).
* @param {number} b - The blue component of the color (0-255).
* @returns {string} A string in the format `#RRGGBB` representing the color.
*/
export declare function rgbToHex(r: number, g: number, b: number): string;
/**
* Converts a hexadecimal color string to a RGB(A) string.
*
* @param {string} hex - The hexadecimal color string to convert.
* @returns {string} A string in the format `rgb(r, g, b)` or `rgba(r, g, b, a)`
* representing the color.
*
* @example
* hexToRgb("#FF0000") // "rgb(255, 0, 0)"
* hexToRgb("#FF000080") // "rgba(255, 0, 0, 0.5)"
*/
export declare function hexToRgb(hex: string): string;
/**
* Generates a random color string in the specified format.
*
* @param {string} type - The format of the color string to generate.
* One of "hex", "rgb", or "rgba". Defaults to "hex".
* @returns {string} A random color string in the specified format.
* @throws {Error} If the `type` parameter is not one of "hex", "rgb", or "rgba".
*
* @example
* randomColor("hex") // "#FF0000"
* randomColor("rgb") // "rgb(255, 0, 0)"
* randomColor("rgba") // "rgba(255, 0, 0, 0.5)"
*/
export declare function randomColor(type?: "hex" | "rgb" | "rgba"): string;
/**
* Converts an integer to a Roman numeral string.
*
* @param {number} num The number to convert to a Roman numeral.
* @returns {string} The Roman numeral string.
*
* @example
* toRomanNumeral(4) // "IV"
* toRomanNumeral(9) // "IX"
* toRomanNumeral(2023) // "MMXXIII"
*/
export declare function toRomanNumeral(num: number): string;
export declare function stripTags(value: unknown): string;
export {};