stringlab_jk
v1.0.3
Published
A lightweight string manipulation utility library for JavaScript and TypeScript
Downloads
15
Maintainers
Readme
🧪 stringlab 🧪
A lightweight utility library providing a comprehensive collection of string manipulation functions for JavaScript and TypeScript projects. 🚀
📦 Features ✨
- 🔄 Convert between different string cases (camelCase, snake_case, PascalCase, etc.)
- 💡 Measure string similarity using Levenshtein distance
- 🧹 Sanitize and transform strings (escape HTML, slugify, truncate, etc.)
- 🛠️ Various utility functions (reverse, pad, extract, and more)
🚀 Installation
To get started, simply install the package with npm:
npm install stringlab📚 Usage
Here's how you can start using stringlab:
import {
toTitleCase,
camelCaseToWords,
stringSimilarity,
truncateToLength,
slugify,
escapeHTML,
capitalizeFirst,
reverseString,
removeExtraSpaces,
isPalindrome,
countOccurrences,
truncateWords,
toSnakeCase,
getSubstring,
isAlphaNumeric,
toPascalCase,
isEmpty,
padEndString,
containsSubstring,
toSpaceWords,
toPascalCaseWithSpaces,
} from 'stringlab';
console.log(toTitleCase('hello world')); // "Hello World"
console.log(camelCaseToWords('camelCaseExample')); // "Camel Case Example"
console.log(stringSimilarity('hello', 'helo')); // 0.8
console.log(truncateToLength('This is a long sentence.', 10)); // "This is a..."
console.log(slugify('Hello World!')); // "hello-world"
console.log(escapeHTML('<div>"Test"</div>')); // "<div>"Test"</div>"📄 API Reference 🧑💻
1. toTitleCase(str: string): string 🎓
Converts a string to title case (capitalizes the first letter of each word).
console.log(toTitleCase('hello world')); // "Hello World"2. camelCaseToWords(str: string): string 🐍
Converts a camelCase string to a space-separated string.
console.log(camelCaseToWords('camelCaseExample')); // "Camel Case Example"3. stringSimilarity(str1: string, str2: string): number 🧮
Calculates the similarity between two strings (Levenshtein distance), returning a value between 0 and 1.
console.log(stringSimilarity('hello', 'helo')); // 0.84. truncateToLength(str: string, length: number): string ✂️
Truncates a string to a specific length and appends '...' if necessary.
console.log(truncateToLength('This is a long sentence.', 10)); // "This is a..."5. slugify(str: string): string 🔗
Converts a string into a URL-friendly slug.
console.log(slugify('Hello World!')); // "hello-world"6. escapeHTML(str: string): string 🧼
Escapes HTML special characters.
console.log(escapeHTML('<div>"Test"</div>'));
// "<div>"Test"</div>"7. capitalizeFirst(str: string): string ✨
Capitalizes the first letter of a string.
console.log(capitalizeFirst('hello')); // "Hello"8. reverseString(str: string): string 🔄
Reverses the input string.
console.log(reverseString('hello')); // "olleh"9. removeExtraSpaces(str: string): string 💨
Removes leading, trailing, and multiple consecutive spaces.
console.log(removeExtraSpaces(' Hello World ')); // "Hello World"10. isPalindrome(str: string): boolean 🧐
Checks if a string is a palindrome (case-insensitive, ignoring non-alphanumeric characters).
console.log(isPalindrome('A man a plan a canal Panama')); // true11. countOccurrences(str: string, substr: string): number 🔢
Counts how many times a substring appears in the string.
console.log(countOccurrences('hello hello', 'hello')); // 212. truncateWords(str: string, numWords: number): string ✂️
Truncates a string to a specified number of words.
console.log(truncateWords('This is a long sentence.', 3)); // "This is a..."13. toSnakeCase(str: string): string 🐍
Converts a string to snake_case.
console.log(toSnakeCase('Hello World!')); // "hello_world"14. getSubstring(str: string, start: number, end?: number): string 📏
Extracts a substring from a string between the specified indices.
console.log(getSubstring('hello world', 0, 5)); // "hello"15. isAlphaNumeric(str: string): boolean 🔠
Checks if a string contains only letters and numbers.
console.log(isAlphaNumeric('abc123')); // true16. toPascalCase(str: string): string 🏰
Converts a string to PascalCase.
console.log(toPascalCase('hello world')); // "HelloWorld"17. isEmpty(str: string): boolean 🧹
Checks if a string is empty or contains only whitespace.
console.log(isEmpty(' ')); // true18. padEndString(str: string, length: number, char?: string): string ⏳
Pads the end of a string to a given length with a specified character.
console.log(padEndString('hello', 10, '-')); // "hello-----"19. containsSubstring(str: string, substr: string): boolean 🔍
Checks if a string contains a specific substring.
console.log(containsSubstring('hello world', 'world')); // true20. toSpaceWords(str: string): string 💬
Converts hyphenated words into space-separated words.
console.log(toSpaceWords('hello-world')); // "hello world"21. toPascalCaseWithSpaces(str: string): string 🏰
Converts a string to PascalCase while keeping spaces between words.
console.log(toPascalCaseWithSpaces('hello world')); // "Hello World"📝 License
ISC License.
📢 Author
Created by jella_komal.
