UNPKG

@js-utility/string

Version:

A lightweight and powerful collection of string utility functions for Node.js - trimming, casing, formatting, and more.

292 lines (290 loc) 10.8 kB
/** * File: string-utils.ts * Description: Utility functions for string manipulation, including trimming, casing, formatting, and more. * Created by: Pradip Sabhadiya * Created on: 15 April 2025 * Last modified: 15 April 2025 * Usage: This file contains helper functions for handling strings in various ways, such as formatting, * validation, and transformation. These utilities are used across the system designer core module. */ declare const CalcPlaceholderRegex: RegExp; type RandomType = "upper" | "lower" | "alpha" | "number" | "alphanumeric" | "special" | "mix"; /** * Remove whitespace from both ends of a string. * @param str The string to trim. * @returns The trimmed string. */ declare const stringify: (text: any, default_val?: string) => string; /** * Remove whitespace from both ends of a string. * @param str The string to trim. * @returns The trimmed string. */ declare const trim: (str: string) => string; /** * Check if a string is empty. * @param str The string to check. * @returns True if the string is empty, false otherwise. */ declare const isEmptyStr: (str: any) => boolean; /** * Convert a string to uppercase. * @param str The string to convert. * @returns The uppercase version of the string. */ declare const upper: (str: string) => string; /** * Convert a string to lowercase. * @param str The string to convert. * @returns The lowercase version of the string. */ declare const lower: (str: string) => string; /** * Capitalize the first letter of a string. * @param str The string to capitalize. * @returns The string with the first letter capitalized. */ declare const capitalize: (str: string) => string; /** * Capitalize the first letter of each word in a string. * @param str The original string. * @returns The string with the first letter of each word capitalized. */ declare const capitalizeEachWord: (str: string) => string; /** * Reverse a string. * @param str The string to reverse. * @returns The reversed string. */ declare const reverse: (str: string) => string; /** * Truncate a string to a specified length and add an ellipsis if truncated. * @param str The string to truncate. * @param maxLength The maximum length of the string. * @returns The truncated string with an ellipsis if needed. */ declare const truncate: (str: string, maxLength: number) => string; /** * Remove all whitespace from a string. * @param str The string to modify. * @returns The string without any whitespace. */ declare const removeWhitespace: (str: string) => string; /** * Replace multiple spaces with a single space. * @param str The string to modify. * @returns The string with multiple spaces replaced by a single space. */ declare const replaceMultipleSpaces: (str: string) => string; /** * Replace all occurrences of a substring within a string. * @param str The string to modify. * @param search The substring to replace. * @param replacement The replacement string. * @returns The modified string. */ declare const replaceAll: (str: string, search: string, replacement: string) => string; /** * Split a string by a delimiter. * @param str The string to split. * @param delimiter The delimiter to split by. * @returns An array of substrings. */ declare const split: (str: string, delimiter: string) => string[]; /** * Repeat a string a specified number of times. * @param str The string to repeat. * @param count The number of times to repeat. * @returns The repeated string. */ declare const repeat: (str: string, count: number) => string; /** * Get the character at a specific index in a string. * @param str The string to check. * @param index The index to get the character from. * @returns The character at the specified index. */ declare const charAt: (str: string, index: number) => string; /** * Convert a string to an array of characters. * @param str The string to convert. * @returns An array of characters. */ declare const toCharArray: (str: string) => string[]; /** * Check if a string starts with a specific prefix. * @param str The string to check. * @param prefix The prefix to check for. * @returns True if the string starts with the prefix, false otherwise. */ declare const startsWith: (str: string, prefix: string) => boolean; /** * Check if a string ends with a specific suffix. * @param str The string to check. * @param suffix The suffix to check for. * @returns True if the string ends with the suffix, false otherwise. */ declare const endsWith: (str: string, suffix: string) => boolean; /** * Convert a string to camel case. * @param str The string to convert. * @returns The camel case version of the string. */ declare const camelCase: (str: string) => string; /** * Convert a string to kebab case. * @param str The string to convert. * @returns The kebab case version of the string. */ declare const kebabCase: (str: string) => string; /** * Convert a string to snake case. * @param str The string to convert. * @returns The snake case version of the string. */ declare const snakeCase: (str: string) => string; /** * Convert a string to title case. * @param str The string to convert. * @returns The title case version of the string. */ declare const titleCase: (str: string) => string; /** * Remove HTML tags from a string. * @param str The original string containing HTML. * @returns The string with HTML tags removed. */ declare const stripHtmlTags: (str: string) => string; /** * Pad a string on the left side with a specific character up to a specified length. * @param str The string to pad. * @param targetLength The target length of the resulting string. * @param padChar The character to pad with (default is space). * @returns The padded string. */ declare const padLeft: (str: string, targetLength: number, padChar?: string) => string; /** * Pad a string on the right side with a specific character up to a specified length. * @param str The string to pad. * @param targetLength The target length of the resulting string. * @param padChar The character to pad with (default is space). * @returns The padded string. */ declare const padRight: (str: string, targetLength: number, padChar?: string) => string; /** * Pad a string on both sides with a specific character up to a specified total length. * If the target length is odd, the extra padding character will be added to the right. * @param str The string to pad. * @param targetLength The total target length of the resulting string. * @param padChar The character to pad with (default is space). * @returns The padded string. */ declare const padBoth: (str: string, targetLength: number, padChar?: string) => string; /** * Remove duplicate characters from a string. * @param str The string to modify. * @returns The string with duplicate characters removed, keeping the first occurrence. */ declare const removeDuplicateChars: (input: unknown) => string; /** * Remove duplicate words from a string. * @param str The string to modify. * @returns The string with duplicate words removed, keeping the first occurrence. */ declare const removeDuplicateWords: (str: string | null | undefined) => string; /** * Remove consecutive duplicate characters from a string. * @param str The string to modify. * @returns The string with consecutive duplicate characters removed. */ declare const removeConsecutiveDuplicates: (str: string) => string; /** * Remove consecutive duplicate words from a string. * @param str The string to modify. * @returns The string with consecutive duplicate words removed. */ declare const removeConsecutiveDuplicateWords: (str: string) => string; /** * Join multiple strings with a specified separator. * @param separator The separator to use between strings. * @param parts The strings to join. * @returns The concatenated string with the specified separator. */ declare const joinStrings: (separator: string | null | undefined, ...parts: (string | undefined | null)[]) => string; /** * Generate a random string of a specified length and character type. * @param length The length of the generated string. Defaults to 10. * @param _type The type of characters to include in the string. Defaults to "mix". * @returns A random string of the specified length and character type. */ declare function random(length?: number, _type?: RandomType): string; /** * Convert a singular English noun to its plural form. * Handles common cases and irregularities. * @param str The singular noun to pluralize. * @returns The plural form of the noun. */ declare const pluralize: (str: string) => string; /** * Convert a plural English noun to its singular form. * Handles common cases and irregularities. * @param str The plural noun to singularize. * @returns The singular form of the noun. */ declare const singularize: (str: string) => string; /** * Convert a string into a URL-friendly slug. * @param str The original string. * @returns The slugified string. */ declare const slugify: (str: string) => string; /** * Escape special HTML characters in a string. * @param str The string to escape. * @returns The escaped string. */ declare const escapeHtml: (str: string) => string; /** * Unescape special HTML characters in a string. * @param str The string to unescape. * @returns The unescaped string. */ declare const unescapeHtml: (str: string) => string; /** * It creates hash for a string * @param txt * @returns Hashed value for secret fields */ declare function hash(txt: string): string; /** * It compares hashed value with plain text * @param plainText * @param hash * @returns True / False */ declare function compareHash(plainText: string, hashedValue: string): boolean; /** * Encrypt value. * @param data * @returns Encrypted string */ declare function encrypt(data: string): string; /** * Encrypt JSON Object or JSON Array. * @param data * @returns Encrypted string */ declare function encryptJSON(data: any): string; /** * Decrypt value. * @param encryptedData * @returns Decrypted string */ declare function decrypt(encryptedData: string): string; /** * Decrypt JSON. * @param encryptedData * @returns Decrypted JSON Object or Array */ declare function decryptJSON(encryptedData: string): any; export { CalcPlaceholderRegex, camelCase, capitalize, capitalizeEachWord, charAt, compareHash, decrypt, decryptJSON, encrypt, encryptJSON, endsWith, escapeHtml, hash, isEmptyStr, joinStrings, kebabCase, lower, padBoth, padLeft, padRight, pluralize, random, removeConsecutiveDuplicateWords, removeConsecutiveDuplicates, removeDuplicateChars, removeDuplicateWords, removeWhitespace, repeat, replaceAll, replaceMultipleSpaces, reverse, singularize, slugify, snakeCase, split, startsWith, stringify, stripHtmlTags, titleCase, toCharArray, trim, truncate, unescapeHtml, upper };