UNPKG

string-playground

Version:

String utility functions for clean and consistent text handling in JavaScript.

49 lines (48 loc) 2.85 kB
type ModifyString = { str: string; operation: 'toSlug' | 'capitalizeFirstWord' | 'capitalizeWords' | 'camelCase' | 'countWords' | 'removeNumbers' | 'onlyLetters' | 'removeExtraSpaces' | 'dotCase' | 'removeWords'; blacklist?: string[]; caseSensitive?: boolean; }; /** * * Modifies a string based on the specified operation. * * @param {string} str - The input string to be modified. * @param {string} operation - The operation to perform on the string. * @param {string[]} [blacklist] - An array of words to remove (required for removeWords operation). * @param {boolean} [caseSensitive=false] - Whether the removal of words should be case-sensitive (only for removeWords operation). * * @returns {string | number} - The modified string or the word count, depending on the operation. * * @throws {Error} - Throws an error if the input is not a string, if the operation is invalid, or if blacklist is required but not provided. * * @description * Supported operations: * - toSlug: Converts the string to a URL-friendly slug. * - capitalizeFirstWord: Capitalizes the first word of the string. * - capitalizeWords: Capitalizes the first letter of each word in the string. * - camelCase: Converts the string to camelCase format. * - countWords: Counts the number of words in the string. * - removeNumbers: Removes all numeric characters from the string. * - onlyLetters: Removes all non-letter characters from the string, except spaces. * - removeExtraSpaces: Removes extra spaces, leaving only single spaces between words. * - dotCase: Converts the string to dot.case format. * - removeWords: Removes specified words from the string. Requires a blacklist array and an optional caseSensitive boolean (default is false). * * @example * modifyString({ str: "Hello World!", operation: "toSlug" }); // "hello-world" * modifyString({ str: "hello world", operation: "capitalizeFirstWord" }); // "Hello world" * modifyString({ str: "hello world", operation: "capitalizeWords" }); // "Hello World" * modifyString({ str: "hello world", operation: "camelCase" }); // "helloWorld" * modifyString({ str: "hello world", operation: "countWords" }); // 2 * modifyString({ str: "hello123 world456", operation: "removeNumbers" }); // "hello world" * modifyString({ str: "hello! world#", operation: "onlyLetters" }); // "hello world" * modifyString({ str: "hello world", operation: "removeExtraSpaces" }); // "hello world" * modifyString({ str: "hello world", operation: "dotCase" }); // "hello.world" * modifyString({ str: "This is a test", operation: "removeWords", blacklist: ["is", "a"] }); // "This test" * * @link https://github.com/PatrickNassar0/string-playground */ declare const modifyString: ({ str, operation, blacklist, caseSensitive }: ModifyString) => string | number; export default modifyString;