0.0.2 β€’ Published 6 months ago

node-string-utils v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

node-string-utils πŸ“

A lightweight and powerful string utility library for JavaScript and Node.js, providing formatting, manipulation, sanitization, analysis, encoding, validation, and tokenization functions.

πŸš€ Features

  • βœ… Convert between different case formats (camelCase, snake_case, PascalCase, etc.)
  • πŸ”„ String manipulations (reverse, capitalize, trim, truncate, etc.)
  • 🧺 Sanitize text (remove special characters, normalize spaces, trim punctuation, convert quotes)
  • πŸ“Š Analyze text (word count, readability score, sentiment analysis)
  • πŸ“‚ Encode and decode text (Base64, URL encoding, ROT13)
  • πŸ›‘οΈ Validate strings (email, URL, phone number, JSON, etc.)
  • βœ‚οΈ Tokenize text into words, sentences, or characters
  • 🎲 Utility functions (ordinal numbers, random word selection, text wrapping)

πŸ“¦ Installation

Install via npm:

npm install node-string-utils

πŸ”§ Usage

Import the package in your project:

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

πŸ› Case Formatters

FunctionDescriptionExample
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"

πŸ”„ String Manipulation

FunctionDescriptionExample
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 string10 β†’ "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 }

🧺 Text Sanitization

FunctionDescriptionExample
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"

πŸ“Š Text Analysis

FunctionDescriptionExample
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"

πŸ“‚ Encoding and Decoding

FunctionDescriptionExample
base64Encode(str)Encodes a string in Base64base64Encode("Hello") β†’ "SGVsbG8="
base64Decode(str)Decodes a Base64-encoded stringbase64Decode("SGVsbG8=") β†’ "Hello"
urlEncode(str)Encodes a string for safe use in URLsurlEncode("Hello World!") β†’ "Hello%20World%21"
urlDecode(str)Decodes a URL-encoded stringurlDecode("Hello%20World%21") β†’ "Hello World!"
rot13(str)Applies ROT13 cipher to shift lettersrot13("Hello") β†’ "Uryyb"

πŸ›‘οΈ Validation

FunctionDescriptionExample
isValidEmail(str)Checks if a string is a valid emailisValidEmail("test@example.com") β†’ true
isValidURL(str)Checks if a string is a valid URLisValidURL("https://example.com") β†’ true
isValidPhoneNumber(str)Checks if a string is a valid phone numberisValidPhoneNumber("+1234567890") β†’ true
isAlpha(str)Checks if a string contains only alphabetic charactersisAlpha("Hello") β†’ true
isAlphanumeric(str)Checks if a string contains only alphanumeric charactersisAlphanumeric("Hello123") β†’ true
isLowercase(str)Checks if a string contains only lowercase lettersisLowercase("hello") β†’ true
isUppercase(str)Checks if a string contains only uppercase lettersisUppercase("HELLO") β†’ true
isValidHexColor(str)Checks if a string is a valid hexadecimal color codeisValidHexColor("#ff5733") β†’ true
isValidJSON(str)Checks if a string is a valid JSONisValidJSON('{"key": "value"}') β†’ true

βœ‚οΈ Tokenization

FunctionDescriptionExample
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"]

🎲 Utilities

FunctionDescriptionExample
wrapText(str, maxLength)Wraps text into lines of a given max lengthwrapText("This is a long text", 10) β†’ "This is a\nlong text"
randomWord(words)Selects a random word from an arrayrandomWord(["apple", "banana", "cherry"]) β†’ "banana"
toOrdinal(number)Converts a number to its ordinal representationtoOrdinal(21) β†’ "21st"
isTitleCase(str)Checks if a string is in Title CaseisTitleCase("Hello World") β†’ true

πŸ›  Development

Clone the repository:

git clone https://github.com/ianfyan/node-string-utils.git
cd node-string-utils
npm install

πŸ’š Run Tests

To test all functions:

npm test

πŸ“„ License

MIT License - Free to use and modify.


With node-string-utils, handling strings in JavaScript & Node.js is easier than ever! πŸš€
Got feature requests? Feel free to contribute! πŸŽ‰

0.0.2

6 months ago

0.0.1

6 months ago