nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.
88 lines • 4.9 kB
TypeScript
import type { CaseFormat, MaskOptions } from './types';
/**
* * Converts a string to a specified case format such as `camelCase`, `snake_case`, `kebab-case`, `PascalCase`, `Title Case`, `lowercase`, or `UPPERCASE`.
*
* - This function handles non-alphanumeric characters (e.g., spaces, hyphens, underscores, dots, slashes) as word delimiters. For `Title Case`, prepositions, articles, conjunctions, and auxiliary verbs are not capitalized unless they appear at the start of the title.
* - You can also convert the string to `lowercase` or `UPPERCASE`, but it's recommended to use default string methods like `string.toLowerCase()` and `string.toUpperCase()` for these cases.
*
* @param string The input string to be converted. The string should have words separated by non-alphanumeric characters (e.g., spaces, hyphens, underscores, etc.).
* @param format The format to convert the string to. The available formats are:
* - `'camelCase'`: Converts to camelCase (e.g., `myVariableName`).
* - `'snake_case'`: Converts to snake_case (e.g., `my_variable_name`).
* - `'kebab-case'`: Converts to kebab-case (e.g., `my-variable-name`).
* - `'PascalCase'`: Converts to PascalCase (e.g., `MyVariableName`).
* - `'Title Case'`: Converts to Title Case (e.g., `My Variable Name`), where certain words like `prepositions, articles, conjunctions and auxiliary verbs` are not capitalized unless at the start.
* - `'lowercase'`: Converts the string to all lowercase characters.
* - `'UPPERCASE'`: Converts the string to all uppercase characters.
* @returns The formatted string in the specified case format.
* @example
* convertStringCase('my-example_string', 'camelCase'); // returns 'myExampleString'
* convertStringCase('my-example_string', 'snake_case'); // returns 'my_example_string'
* convertStringCase('my-example_string', 'kebab-case'); // returns 'my-example-string'
* convertStringCase('my example string', 'Title Case'); // returns 'My Example String'
* convertStringCase('my example string', 'lowercase'); // returns 'my example string'
* convertStringCase('my example string', 'UPPERCASE'); // returns 'MY EXAMPLE STRING'
*/
export declare function convertStringCase(string: string, format: CaseFormat): string;
/**
* * Replaces all occurrences of a string or pattern in the given input string.
*
* - If `find` is a string, it is converted into a global regular expression (`/find/g`).
* - If `find` is a `RegExp`, the global (`g`) flag is ensured.
* - Trims the input before performing replacements.
*
* @param input - The string in which replacements should be performed.
* @param find - The substring or regex pattern to search for.
* @param replace - The string to replace matches with.
* @returns The modified/refined string with replacements applied.
*/
export declare const replaceAllInString: (input: string, find: string | RegExp, replace: string) => string;
/**
* * Converts a string into a URL-friendly slug.
* @param input - The string to be converted.
* @returns The slugified string.
*/
export declare const slugifyString: (input: string) => Lowercase<string>;
/**
* * Masks part of a string for privacy.
* @param input - The string to mask.
* @param options - Options for masking a string.
* @returns The masked string.
*/
export declare const maskString: (input: string, options?: MaskOptions) => string;
/**
* * Reverses a given string.
* @param input - The string to reverse.
* @returns The reversed string.
*/
export declare const reverseString: (input: string) => string;
/**
* * Normalizes a string by removing diacritics (accents).
* @param str The input string.
* @returns The normalized string.
*/
export declare function normalizeString(str: string): string;
/**
* * Extracts all email addresses from a string.
* @param str The input string.
* @returns An array of extracted email addresses.
*/
export declare function extractEmails(str: string): string[];
/**
* * Extracts all URLs from a string.
* @param str The input string.
* @returns An array of extracted URLs.
*/
export declare function extractURLs(str: string): string[];
/**
* * Returns a grammatically correct unit string, optionally prefixed with the number.
*
* @remarks For complex and versatile pluralization, please refer to {@link https://nhb-toolbox.vercel.app/docs/utilities/string/pluralizer pluralizer} or {@link https://nhb-toolbox.vercel.app/docs/classes/Pluralizer Pluralizer Class} instead.
*
* @param count The numeric value to determine singular or plural.
* @param unit The unit name (e.g., "day", "hour").
* @param withNumber Whether to prefix the count before the unit. Defaults to `true`.
* @returns Formatted unit string like `"1 day"`, `"2 months"`, or `"hour"`.
*/
export declare function formatUnitWithPlural(count: number, unit: string, withNumber?: boolean): string;
//# sourceMappingURL=convert.d.ts.map