Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@c_s_v_s_subrahmanyam/string-utils

A comprehensive utility library for advanced string operations.

string, utilities, validation, transformation, analytics, email check, captcha check, password check, password strength, password hashing, random string, random number, extract email, extract urls, string-utilities, c_s_v_s_subrahmanyam, sorting, searching, reversing, substring, encryption, decryption, encoding, decoding, compression, decompression, formatting, parsing, validation, captcha generation, count characters, is palindrome, remove prefix, remove suffix, password suggestions, password generation, password validation, unique string, indexing and slicing, string manipulation

readme

@c_s_v_s_subrahmanyam/string-utils

A comprehensive set of string manipulation utilities implemented in JavaScript. This package offers a variety of custom functions for string analysis, transformation, checks, and more.

All string operations are implemented manually without relying on inbuilt functions for educational purposes.

Installation

You can install the package via npm:

npm install @c_s_v_s_subrahmanyam/string-utils

Usage

Once installed, you can use the string utilities in your JavaScript code like so:

const stringUtils = require('@c_s_v_s_subrahmanyam/string-utils');

// Translate string using a mapping
const mapping = { 'a': '1', 'b': '2', 'c': '3' };
console.log(stringUtils.translate('abc', mapping)); // 123

// Expand tabs with a specified tab size
console.log(stringUtils.expandTabs('Hello\tWorld', 4)); // Hello    World

// Iterate over characters of a string
console.log(stringUtils.iterateCharacters('Hello')); // ['H', 'e', 'l', 'l', 'o']

// Get length of string
console.log(stringUtils.getLength('Hello World')); // 11

// Count words in a string
console.log(stringUtils.countWords('Hello World! This is a test.')); // 6

// Reverse a string
console.log(stringUtils.reverse('abcd')); // dcba

// Convert string to uppercase
console.log(stringUtils.toUpperCase('hello')); // HELLO

The remaining functions' sample code is available in the test.js file

Available Functions

String Translation

  • translate(str, mapping): Translates characters in the string according to the provided mapping.

    String Expansion and Iteration

  • expandTabs(str, tabSize): Expands tabs in the string with a specified tab size (default 4).
  • iterateCharacters(str): Returns an array of characters in the string.

    Indexing and Slicing

  • sliceString(str, start, end): Slices the string from start to end indices.
  • getIndex(str, index): Returns the character at the specified index in the string.

    String Partitioning

  • splitAt(str, index): Splits the string at the specified index into two parts.
  • partition(str, delimiter): Partitions the string at the first occurrence of the delimiter.
  • rpartition(str, delimiter): Partitions the string at the last occurrence of the delimiter.

    Encoding and Decoding

  • encodeToBytes(str): Encodes the string into a byte array (UTF-8).
  • decodeFromBytes(bytes): Decodes a byte array into a string.
  • encodeBase64(str): Encodes the string to Base64.
  • decodeBase64(str): Decodes a Base64 string back to its original format.
  • encodeURI(str): Encodes the string as a URI.
  • decodeURI(str: Decodes a URI-encoded string.

    String Analytics

  • countCharacters(str): Returns the number of characters in the string.
  • characterFrequency(str): Returns the frequency of each character in the string.
  • startsWith(str, search): Checks if the string starts with the specified substring.
  • endsWith(str, search): Checks if the string ends with the specified substring.
  • substring(str, start, end): Returns a substring from start to end indices.
  • split(str, delimiter): Splits the string by the specified delimiter.
  • join(array, delimiter): Joins an array of strings into one string using the specified delimiter.

    Case Folding and Normalization

  • caseFold(str): Converts the string to lowercase.
  • normalize(str, form): Normalizes the string to a specific Unicode form (default: 'NFC').

    String Comparison Utilities

  • equalsIgnoreCase(str1, str2): Compares two strings ignoring case.
  • compareLexicographically(str1, str2): Compares two strings lexicographically.
  • startsWith(str, prefix): Checks if the string starts with the specified prefix.
  • endsWith(str, suffix): Checks if the string ends with the specified suffix.
  • containsSubstring(str, substring): Checks if the string contains the specified substring.

    Prefix and Suffix Utilities

  • removePrefix(str, prefix): Removes the specified prefix from the string.
  • removeSuffix(str, suffix): Removes the specified suffix from the string.

    Regex Utilities

  • match(str, regex): Returns an array of matches found using the regex.
  • replaceRegex(str, regex, replacement): Replaces matched substrings with the replacement string.
  • testRegex(str, regex): Tests if the string matches the given regex pattern.
  • extractEmails(str): Extracts all emails from the string.
  • extractURLs(str): Extracts all URLs from the string.

    String Repetition

  • repeatString(str, times): Repeats the string the specified number of times.
  • generateCaptcha(length): Generates a random captcha string of the specified length (default 6).

    Sorting Utilities

  • sortCharacters(str, order): Sorts the characters in the string in ascending or descending order.
  • sortWords(str, order): Sorts the words in the string in ascending or descending order.
  • uniqueCharacters(str): Returns a string with unique characters from the original string.

    String Transformations

  • toUpperCase(str): Converts the string to uppercase.
  • toLowerCase(str): Converts the string to lowercase.
  • reverse(str): Reverses the string.
  • concatenate(...args): Concatenates multiple strings into one.
  • replace(str, search, replace): Replaces all occurrences of search in the string with replace.
  • trim(str): Trims leading and trailing whitespace from the string.
  • padStart(str, length, char): Pads the start of the string to the specified length with the given character.
  • padEnd(str, length, char): Pads the end of the string to the specified length with the given character.
  • caseConversion(str, targetCase): Converts the string to camelCase, snake_case, or kebab-case.

    Unicode Operations

  • charAt(str, index): Returns the character at the specified index.
  • charCodeAt(str, index): Returns the Unicode value of the character at the specified index.
  • fromCharCode(...codes): Returns a string created from the given Unicode character codes.
  • toUnicode(str): Converts the string to Unicode escape sequences.
  • fromUnicode(unicodeStr): Converts a Unicode escape sequence string to a regular string.

    Validators for Strings

  • isEmail(str): Checks if the string is a valid email address.
  • isURL(str): Checks if the string is a valid URL.
  • isPalindrome(str): Checks if the string is a palindrome (ignoring non-alphanumeric characters).
  • isAlphanumeric(str): Checks if the string contains only alphanumeric characters.
  • isDigit(str): Checks if the string is a valid number.
  • isSpace(str): Checks if the string contains only whitespace characters.
  • passwordStrength(password): Returns the strength of the password ('Weak', 'Medium', 'Strong').

    Unique Words and Word Analysis

  • findUniqueWords(str): Extracts and returns an array of unique words from the input string.
  • getShortestWord(str): Returns the shortest word in the string.
  • getLongestWord(str): Returns the longest word in the string.

    Sentence and Line Counting

  • countSentences(str): Counts the number of sentences in the string.
  • countLines(str): Counts the number of lines in the string.

    Word Filtering

  • getWordsStartingWithConsonants(str): Returns an array of words starting with consonants.
  • getWordsStartingWithVowels(str): Returns an array of words starting with vowels.

    String Comparison and Substrings

  • isAnagram(str1, str2): Checks if two strings are anagrams of each other.
  • isPangram(str): Checks if the string contains all letters of the alphabet.
  • reverseSubstring(str, substring): Reverses the specified substring in the string.
  • isSubstring(str, substring): Checks if the substring exists within the string.

    Random String and Substring Generation

  • generateRandomSubstring(str, length): Generates a random substring of the specified length from the string.
  • generateRandomString(length): Generates a random string of the specified length using alphanumeric characters.

    Password Suggestions

  • generatePasswordSuggestions(base): Generates an array of password suggestions based on the input base string.

    License

    This package is open-source and available under the ISC License.