Skip to content

kajvans/utils4strings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Utils4strings - Enhance Your String Operations

Description

Utils4strings is a comprehensive library designed to simplify string manipulation tasks. From case conversions and validation to trimming, padding, and encoding, this library provides a set of powerful functions to perform common string operations with ease.

The library includes utility methods for string formatting, character counting, string manipulation, and more. It can be used in both JavaScript and TypeScript projects.

Installation

To install utils4strings, simply run the following command using npm:

npm i utils4strings

Features

String Case Utilities

  • Capitalize: Capitalize the first letter of a string.
  • LowerCase: Convert a string to lowercase.
  • TitleCase: Convert a string to title case (capitalize the first letter of each word).
  • CapitalizeSentences: Capitalize the first letter of each sentence.
  • CamelCase: Convert a string to camel case (first word lowercase, subsequent words capitalized).
  • PascalCase: Convert a string to Pascal case (capitalized first letter of each word).
  • SnakeCase: Convert a string to snake case (words separated by underscores).
  • KebabCase: Convert a string to kebab case (words separated by hyphens).

String Trimming and Removal Utilities

  • truncate: Truncate a string to a specified length and append ellipsis.
  • removeWhitespace: Remove all whitespace characters from a string.
  • removeNonAlphaNumeric: Remove all non-alphanumeric characters.
  • removeNonNumeric: Remove all non-numeric characters.
  • removeTrailingWhitespace: Remove trailing whitespace from a string.
  • removeLeadingWhitespace: Remove leading whitespace from a string.

Padding Utilities

  • padStart: Pad a string from the start with a specified character until it reaches a desired length.
  • padEnd: Pad a string from the end with a specified character until it reaches a desired length.

Repetition and Reversal

  • repeat: Repeat a string a specified number of times.
  • reverse: Reverse the characters in a string.

Replacement Utilities

  • replace: Replace the first occurrence of a substring in a string.
  • replaceAll: Replace all occurrences of a substring in a string.

Splitting and Joining Utilities

  • split: Split a string into an array of substrings based on a separator.
  • join: Join an array of substrings into a single string using a separator.

Validation Utilities

  • isEmpty: Check if a string is empty or contains only whitespace.
  • isAlpha: Check if a string contains only alphabetic characters.
  • isAlphaNumeric: Check if a string contains only alphanumeric characters.
  • isNumeric: Check if a string contains only numeric characters.
  • isLowerCase: Check if a string is in lowercase.
  • isUpperCase: Check if a string is in uppercase.
  • isPalindrome: Check if a string is a palindrome.
  • isAnagram: Check if two strings are anagrams of each other.

Counting Utilities

  • countWords: Count the number of words in a string.
  • countCharacters: Count the number of characters in a string.
  • countOccurrences: Count the occurrences of a substring in a string.
  • countAlpha: Count the number of alphabetic characters in a string.

Miscellaneous Utilities

  • stripTags: Remove HTML tags from a string.
  • splitWords: Split a string into words by removing non-alphabetic characters.
  • slugify: Create a URL-friendly slug from a string.

Encoding and Decoding Utilities

  • toBase64: Encode a string to Base64 format.
  • fromBase64: Decode a Base64-encoded string.
  • toBase64Url: Encode a string to Base64 URL-safe format.
  • fromBase64Url: Decode a Base64 URL-safe encoded string.

HTML Utilities

  • escapeHTML: Escape special HTML characters in a string.
  • unescapeHTML: Unescape HTML-encoded characters in a string.

Miscellaneous Utilities

  • insertAt: Insert a substring at a specific index in a string.
  • extractBetween: Extract a substring between two given substrings.
  • hashString: Hash a string using a specified algorithm (default is sha256).
  • compareIgnoreCase: Compare two strings ignoring case.
  • generateRandomString: Generate a random string of a given length.
  • matchPattern: Check if a string matches a specified regular expression pattern.
  • padWithCustomString: Pad a string with a custom string until it reaches a desired length.
  • removeDuplicates: Remove duplicate characters from a string.
  • CountMostCommonCharacters: Count the occurrences of each character in a string.
  • countVowels: Count the number of vowels in a string.
  • countConsonants: Count the number of consonants in a string.
  • countLines: Count the number of lines in a string.
  • countSentences: Count the number of sentences in a string.
  • countParagraphs: Count the number of paragraphs in a string.
  • countSpaces: Count the number of spaces in a string.
  • countTabs: Count the number of tabs in a string.
  • countSpecialCharacters: Count the number of special characters in a string.
  • countMostUsedWords: Count the occurrences of each word in a string.

Usage/Examples

const {
    Capitalize,
    LowerCase,
    titleCase,
    truncate,
    hashString,
    toBase64,
    generateRandomString,
} = require('utils4strings');

// Capitalize the first letter of a string
console.log(Capitalize('hello world')); // "Hello world"

// Convert string to lowercase
console.log(LowerCase('HELLO WORLD')); // "hello world"

// Convert string to title case
console.log(titleCase('hello world example')); // "Hello World Example"

// Truncate a string with ellipsis
console.log(truncate('This is a very long sentence.', 10)); // "This is a..."

// Hash a string using SHA-256
console.log(hashString('hello world')); // "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda6f6f5c5b5e6a98a1c4c"

// Encode a string to Base64
console.log(toBase64('hello world')); // "aGVsbG8gd29ybGQ="

// Generate a random string of length 10
console.log(generateRandomString(10)); // Example: "aXx8A2dHhJ"

Notes

  • String Utilities provides a wide range of functions for manipulating strings, making it ideal for general-purpose string processing tasks.
  • All functions are simple to use and return the expected results in various scenarios.
  • Most functions in this library are synchronous, making them suitable for immediate use in your code.

Authors

License

MIT License

String Utilities - Simplify your string operations!


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published