A utility library for string manipulation
npm install node-string-utilssh
npm install node-string-utils
`
π§ Usage
Import the package in your project:
`javascript
import { toCamelCase, toSnakeCase, reverseString, wordCount } from "node-string-utils";
// Convert to camelCase
console.log(toCamelCase("hello world")); // "helloWorld"
// Convert to snake_case
console.log(toSnakeCase("helloWorld")); // "hello_world"
// Reverse a string
console.log(reverseString("hello")); // "olleh"
// Count words
console.log(wordCount("Hello world!")); // 2
`
π API Reference
$3
| Function | Description | Example |
|-----------------|------------------------------------------|---------|
| toCamelCase(str) | Converts to camelCase | "hello world" β "helloWorld" |
| toSnakeCase(str) | Converts to snake_case | "helloWorld" β "hello_world" |
| toKebabCase(str) | Converts to kebab-case | "hello world" β "hello-world" |
| toPascalCase(str) | Converts to PascalCase | "hello world" β "HelloWorld" |
| toUpperSnakeCase(str) | Converts to UPPER_SNAKE_CASE | "hello world" β "HELLO_WORLD" |
$3
| Function | Description | Example |
|------------------------|------------------------------------------|---------|
| reverseString(str) | Reverses a string | "hello" β "olleh" |
| capitalizeWords(str) | Capitalizes each word | "hello world" β "Hello World" |
| trimString(str) | Trims whitespace | " hello " β "hello" |
| truncateString(str, maxLength) | Truncates a string and adds "..." if too long | ("hello world", 5) β "hello..." |
| slugify(str) | Converts to URL-friendly slug | "Hello World!" β "hello-world" |
| generateRandomString(length) | Generates a random string | 10 β "aZ3xY9wLpQ" |
| countCharacter(str, char) | Counts occurrences of a character | ("hello", "l") β 2 |
| isAlpha(str) | Checks if string contains only letters | "hello" β true |
| uncapitalize(str) | Converts first letter to lowercase | "Hello" β "hello" |
| removeVowels(str) | Removes vowels from string | "hello" β "hll" |
| wordFrequency(str) | Counts occurrences of each word | "hi hi hello" β { hi: 2, hello: 1 } |
| reverseWords(str) | Reverses words in a sentence | "hello world" β "world hello" |
| toTitleCase(str) | Converts to Title Case | "hello world" β "Hello World" |
| extractNumbers(str) | Extracts numbers from string | "abc123xyz" β [123] |
| isEmptyOrWhitespace(str) | Checks if string is empty/whitespace | " " β true |
| swapCase(str) | Swaps case of each letter | "Hello" β "hELLO" |
| insertAt(str, subStr, index) | Inserts substring at index | ("hello", "X", 2) β "heXllo" |
| removeSubstring(str, subStr) | Removes all occurrences of substring | ("hello world", "world") β "hello " |
| shuffleString(str) | Randomly shuffles characters | "hello" β "loleh" |
| stringToBinary(str) | Converts string to binary | "hi" β "01101000 01101001" |
| binaryToString(binary) | Converts binary to string | "01101000 01101001" β "hi" |
| toAlternatingCase(str) | Converts to alternating case | "hello" β "hElLo" |
| removeDuplicateCharacters(str) | Removes duplicate characters | "hello" β "helo" |
| longestWord(str) | Finds longest word in a string | "hi hello" β "hello" |
| toHashtag(str) | Converts string to hashtag format | "hello world" β "#HelloWorld" |
| uniqueWords(str) | Extracts unique words | "hi hello hi" β "hi hello" |
| charFrequency(str) | Counts occurrences of each character | "hello" β { h:1, e:1, l:2, o:1 } |
$3
| Function | Description | Example |
|-------------------------|----------------------------------------|---------|
| removeNonAlphaNumeric(str) | Removes non-alphanumeric characters | "Hello@World!" β "HelloWorld" |
| normalizeWhitespace(str) | Converts multiple spaces into a single space | "Hello World" β "Hello World" |
| trimPunctuation(str) | Removes leading/trailing punctuation | "!Hello!" β "Hello" |
| toAscii(str) | Converts non-ASCII characters to ASCII equivalent | "CafΓ©" β "Cafe" |
| maskString(str, start, end) | Masks part of a string with asterisks | ("password", 2, -2) β "pa**rd" |
| isNumeric(str) | Checks if a string contains only digits | "12345" β true |
| removeDuplicateWords(str) | Removes duplicate words from a string | "hi hi hello" β "hi hello" |
| normalizeDiacritics(str) | Normalizes diacritics (Γ© β e, Γ± β n) | "CafΓ©" β "Cafe" |
| removeExtraPunctuation(str) | Removes extra punctuation marks | "Hello!!" β "Hello!" |
| convertQuotes(str) | Converts curly quotes to straight quotes | βHelloβ β "Hello" |
$3
| Function | Description | Example |
|------------------------|----------------------------------|---------|
| readabilityScore(str) | Computes readability score based on word/sentence length | "Hello world. This is a test." β 80.3 |
| mostCommonWord(str) | Finds the most common word in a string | "hi hi hello" β "hi" |
| sentimentAnalysis(str) | Analyzes sentiment as positive, negative, or neutral | "I love this!" β "positive" |
$3
| Function | Description | Example |
| --------------------- | ------------------------------------------------ | ---------------------------------------------- |
| base64Encode(str) | Encodes a string in Base64 | base64Encode("Hello") β "SGVsbG8=" |
| base64Decode(str) | Decodes a Base64-encoded string | base64Decode("SGVsbG8=") β "Hello" |
| urlEncode(str) | Encodes a string for safe use in URLs | urlEncode("Hello World!") β "Hello%20World%21" |
| urlDecode(str) | Decodes a URL-encoded string | urlDecode("Hello%20World%21") β "Hello World!" |
| rot13(str) | Applies ROT13 cipher to shift letters | rot13("Hello") β "Uryyb" |
$3
| Function | Description | Example |
| --------------------- | ------------------------------------------------ | ---------------------------------------------- |
| isValidEmail(str) | Checks if a string is a valid email | isValidEmail("test@example.com") β true |
| isValidURL(str) | Checks if a string is a valid URL | isValidURL("https://example.com") β true |
| isValidPhoneNumber(str) | Checks if a string is a valid phone number | isValidPhoneNumber("+1234567890") β true |
| isAlpha(str) | Checks if a string contains only alphabetic characters | isAlpha("Hello") β true |
| isAlphanumeric(str) | Checks if a string contains only alphanumeric characters | isAlphanumeric("Hello123") β true |
| isLowercase(str) | Checks if a string contains only lowercase letters | isLowercase("hello") β true |
| isUppercase(str) | Checks if a string contains only uppercase letters | isUppercase("HELLO") β true |
| isValidHexColor(str) | Checks if a string is a valid hexadecimal color code | isValidHexColor("#ff5733") β true |
| isValidJSON(str) | Checks if a string is a valid JSON | isValidJSON('{"key": "value"}') β true |
$3
| Function | Description | Example |
|------------------------|-----------------------------------|---------|
| tokenizeWords(str) | Splits into words | "hello world" β ["hello", "world"] |
| tokenizeSentences(str) | Splits into sentences | "Hello! How are you?" β ["Hello!", "How are you?"] |
| tokenizeCharacters(str) | Splits into characters | "hi" β ["h", "i"] |
$3
| Function | Description | Example |
|------------------------|------------------------------------|---------|
| wrapText(str, maxLength) | Wraps text into lines of a given max length | wrapText("This is a long text", 10) β "This is a\nlong text" |
| randomWord(words) | Selects a random word from an array | randomWord(["apple", "banana", "cherry"]) β "banana" |
| toOrdinal(number) | Converts a number to its ordinal representation | toOrdinal(21) β "21st" |
| isTitleCase(str) | Checks if a string is in Title Case | isTitleCase("Hello World") β true |
π Development
Clone the repository:
`sh
git clone https://github.com/ianfyan/node-string-utils.git
cd node-string-utils
npm install
`
$3
To test all functions:
`sh
npm test
``