String Master
String Master
is a powerful JavaScript/TypeScript library designed to simplify a wide range of string operations and manipulations. With a comprehensive set of utility classes, it provides tools for transforming text, converting between formats, compressing data, encoding and decoding strings, sanitizing input, searching efficiently, and analyzing content. Each utility class is designed to address specific needs, making String Master
an all-in-one solution for developers handling string data.
Table of Contents
- Installation
- Usage
- CasingMaster: Convert strings to various cases, like Camel Case, Snake Case, and more.
- ConversionMaster: Convert strings between different types, such as hexadecimal, integers, or float.
- CompressionMaster: Compress and decompress strings.
- EmojiMaster: Manipulate, find, or replace emojis in strings.
- EntropyMaster: Calculate the entropy of a string for cryptographic analysis.
- FrequencyMaster: Analyze the frequency of characters or words in strings.
- InflectionMaster: Handle pluralization and singularization of words.
- InitialsMaster: Extract initials from names or phrases.
- ManipulationMaster: Reverse, shuffle, or manipulate strings in various ways.
- MaskingMaster: Mask sensitive parts of strings, like credit card numbers or emails.
- ObfuscationMaster: Obfuscate and deobfuscate strings for privacy or security.
- SanitizationMaster: Sanitize strings by removing unwanted characters or formatting.
- SearchMaster: Search within strings using fuzzy search, proximity search, and more.
- SimilarityMaster: Calculate similarity between strings using various algorithms.
- SlugMaster: Convert strings to URL-friendly slugs.
- TemplateMaster: Perform string interpolation and templating.
- TrimmingMaster: Trim whitespace or specific characters from strings.
- ValidationMaster: Validate strings against various rules, like email or URL formats.
- Contributing
- License
- Contact
1. Installation
To install the string-master
package, use npm or yarn:
npm install string-master
yarn add string-master
2. Usage
1. CasingMaster
The CasingMaster class provides methods to convert strings to various cases, such as Camel Case, Snake Case, Pascal Case, and more.
import { CasingMaster } from 'string-master';
console.log(CasingMaster.toCamelCase('hello world'));
console.log(CasingMaster.toSnakeCase('hello world'));
console.log(CasingMaster.toPascalCase('hello world'));
console.log(CasingMaster.toKebabCase('hello world'));
console.log(CasingMaster.toScreamingSnakeCase('hello world'));
console.log(CasingMaster.toSentenceCase('HELLO WORLD'));
console.log(CasingMaster.toTitleCase('hello world'));
console.log(CasingMaster.smartCaseConvert('hello world', 'pascal'));
console.log(CasingMaster.smartCaseConvert('hello world', 'kebab'));
console.log(CasingMaster.smartCaseConvert('HELLO WORLD', 'sentence'));
2. ConversionMaster
The ConversionMaster class provides methods to convert strings to different types, such as from a string to hexadecimal, integer, or binary.
import { ConversionMaster } from 'string-master';
console.log(ConversionMaster.toBoolean('yes'));
console.log(ConversionMaster.toBoolean('on'));
console.log(ConversionMaster.toBoolean('0'));
console.log(ConversionMaster.toBoolean('true'));
console.log(ConversionMaster.toFloat('123.456', 2));
console.log(ConversionMaster.toFloat('123.456'));
console.log(ConversionMaster.toInt('42'));
console.log(ConversionMaster.toInt('0x1A'));
console.log(ConversionMaster.toJson('{"name": "John"}'));
console.log(ConversionMaster.fromJson({ name: 'John' }));
console.log(ConversionMaster.toBase64('Hello World!'));
console.log(ConversionMaster.fromBase64('SGVsbG8gV29ybGQh'));
console.log(ConversionMaster.toHex('Hello'));
console.log(ConversionMaster.fromHex('48656c6c6f'));
3. CompressionMaster
The CompressionMaster class handles string compression and decompression.
import { CompressionMaster } from 'string-master';
const compressed = CompressionMaster.compress('Hello, world!');
console.log(compressed);
const decompressed = CompressionMaster.decompress(compressed);
console.log(decompressed);
4. EmojiMaster
The EmojiMaster class helps find, replace, or manipulate emojis in strings.
import { EmojiMaster } from 'string-master';
console.log(EmojiMaster.findEmojis('Hello 👋 World 🌍!'));
console.log(EmojiMaster.countEmojis('Hello 👋 World 🌍!'));
console.log(EmojiMaster.containsEmojis('Hello 👋 World!'));
console.log(EmojiMaster.extractEmojiSequences('Hello 👋 World 🌍! 🎉'));
console.log(EmojiMaster.replaceEmojis('Hello 👋 World 🌍!', '[emoji]'));
const replacements = { happy: '😊', sad: '😢' };
const modifiedTextWithEmojis = EmojiMaster.replaceWordsWithEmojis('I am very happy and a bit sad.', replacements);
console.log(modifiedTextWithEmojis);
5. EntropyMaster
The EntropyMaster class calculates the entropy of a string, useful for cryptography or information theory.
import { EntropyMaster } from 'string-master';
console.log(EntropyMaster.calculateEntropy('password123'));
console.log(EntropyMaster.getStrength('password123'));
6. FrequencyMaster
The FrequencyMaster class analyzes the frequency of characters or words in a string.
import { FrequencyMaster } from 'string-master';
const text = 'hello world hello';
console.log(FrequencyMaster.countWordFrequency(text));
console.log(FrequencyMaster.countCharacterFrequency(text));
console.log(FrequencyMaster.findMostFrequentWords(text, 1));
console.log(FrequencyMaster.findLeastFrequentWords(text, 1));
console.log(FrequencyMaster.getTopNCharacters(text, 2));
7. InflectionMaster
The InflectionMaster class handles pluralization and singularization of words.
Examples:
import { InflectionMaster } from 'string-master';
const pluralPerson = InflectionMaster.pluralize('person');
console.log(pluralPerson);
const pluralOctopus = InflectionMaster.pluralize('octopus');
console.log(pluralOctopus);
const pluralHat = InflectionMaster.pluralize('Hat');
console.log(pluralHat);
const singularPeople = InflectionMaster.singularize('people');
console.log(singularPeople);
const singularOctopuses = InflectionMaster.singularize('octopuses');
console.log(singularOctopuses);
const singularHats = InflectionMaster.singularize('Hats');
console.log(singularHats);
const inflectPerson1 = InflectionMaster.inflect('people', 1);
console.log(inflectPerson1);
const inflectOctopuses1 = InflectionMaster.inflect('octopuses', 1);
console.log(inflectOctopuses1);
const inflectHats1 = InflectionMaster.inflect('Hats', 1);
console.log(inflectHats1);
const inflectGuys1 = InflectionMaster.inflect('guys', 1);
console.log(inflectGuys1);
const inflectInches15 = InflectionMaster.inflect('inches', 1.5);
console.log(inflectInches15);
const inflectPerson2 = InflectionMaster.inflect('person', 2);
console.log(inflectPerson2);
const inflectOctopus2 = InflectionMaster.inflect('octopus', 2);
console.log(inflectOctopus2);
const inflectHat2 = InflectionMaster.inflect('Hat', 2);
console.log(inflectHat2);
8. InitialsMaster
The InitialsMaster class extracts initials from a string.
Examples:
import { InitialsMaster } from 'string-master';
const initials1 = InitialsMaster.extractInitials('John Doe');
console.log(initials1);
const initials2 = InitialsMaster.extractInitials(['John Doe', 'Jane Smith']);
console.log(initials2);
const initials3 = InitialsMaster.extractInitials('John Doe', { length: 3 });
console.log(initials3);
const nameWithInitials1 = InitialsMaster.addInitialsTo('John Doe');
console.log(nameWithInitials1);
const namesWithInitials2 = InitialsMaster.addInitialsTo(['John Doe', 'Jane Smith']);
console.log(namesWithInitials2);
const nameWithInitials3 = InitialsMaster.addInitialsTo('John Doe', { length: 3 });
console.log(nameWithInitials3);
const nameWithInitials4 = InitialsMaster.addInitialsTo('John Doe', { existing: { 'John Doe': 'JDoe' } });
console.log(nameWithInitials4);
const parsedName1 = InitialsMaster.parse('John Doe');
console.log(parsedName1);
const parsedName2 = InitialsMaster.parse('John Doe <john.doe@example.com>');
console.log(parsedName2);
const parsedNames3 = InitialsMaster.parse(['John Doe <john.doe@example.com>', 'Jane Smith']);
console.log(parsedNames3);
const initialsPhrase = InitialsMaster.createInitialsFromPhrase('The Quick Brown Fox', ['the', 'of']);
console.log(initialsPhrase);
const abbreviatedName = InitialsMaster.abbreviateName('John Michael Smith');
console.log(abbreviatedName);
const acronymASAP = InitialsMaster.generateAcronym('As Soon As Possible');
console.log(acronymASAP);
const firstLetters = InitialsMaster.getFirstLetterOfEachWord('Hello World Example');
console.log(firstLetters);
const customAcronym = InitialsMaster.generateCustomAcronym('For Your Information', ['for', 'your']);
console.log(customAcronym);
9. ManipulationMaster
The ManipulationMaster class provides methods to perform various manipulations on strings, like reversing or shuffling.
Examples:
import { ManipulationMaster } from 'string-master';
const betweenResult = ManipulationMaster.between('Hello [World]!', '[', ']');
console.log(betweenResult);
const chompLeftResult = ManipulationMaster.chompLeft('HelloWorld', 'Hello');
console.log(chompLeftResult);
const chompRightResult = ManipulationMaster.chompRight('HelloWorld', 'World');
console.log(chompRightResult);
const removeNonAlphaNumericResult = ManipulationMaster.removeNonAlphaNumeric('Hello, World! 123');
console.log(removeNonAlphaNumericResult);
const truncateResult = ManipulationMaster.truncate('This is a long string', 10);
console.log(truncateResult);
const replaceAllResult = ManipulationMaster.replaceAll('foo bar foo', 'foo', 'baz');
console.log(replaceAllResult);
const reverseResult = ManipulationMaster.reverse('abc');
console.log(reverseResult);
const getLeftResult = ManipulationMaster.getLeft('HelloWorld', 5);
console.log(getLeftResult);
const getRightResult = ManipulationMaster.getRight('HelloWorld', -5);
console.log(getRightResult);
const removeAllResult = ManipulationMaster.removeAll('Hello World', ' ', 'l');
console.log(removeAllResult);
const linesResult = ManipulationMaster.lines('Hello\r\nWorld\r\n!');
console.log(linesResult);
const stripPunctuationResult = ManipulationMaster.stripPunctuation('Hello, world!');
console.log(stripPunctuationResult);
const ensureLeftResult = ManipulationMaster.ensureLeft('world', 'hello ');
console.log(ensureLeftResult);
const ensureRightResult = ManipulationMaster.ensureRight('hello', ' world');
console.log(ensureRightResult);
const latiniseResult = ManipulationMaster.latinise('Olá, mundo!');
console.log(latiniseResult);
const ordinalizeResult = ManipulationMaster.ordinalize('the 1 pitch and 22 jumps');
console.log(ordinalizeResult);
10. MaskingMaster
The MaskingMaster class masks parts of a string, such as credit card numbers or emails.
Examples:
import { MaskingMaster } from 'string-master';
const maskedCreditCard = MaskingMaster.maskCreditCard('1234567812345678');
console.log(maskedCreditCard);
const maskedEmail = MaskingMaster.maskEmail('john.doe@example.com');
console.log(maskedEmail);
const maskedSSN = MaskingMaster.maskSSN('123-45-6789');
console.log(maskedSSN);
const maskedString = MaskingMaster.maskString('SensitiveData', 2, 2);
console.log(maskedString);
11. ObfuscationMaster
The ObfuscationMaster class obfuscates and deobfuscates strings for privacy or security purposes.
Examples:
import { ObfuscationMaster } from 'string-master';
const obfuscated = ObfuscationMaster.obfuscate('Hello World', 3);
console.log(obfuscated);
const deobfuscated = ObfuscationMaster.deobfuscate('Khoor#Zruog', 3);
console.log(deobfuscated);
const obfuscatedXOR = ObfuscationMaster.obfuscateXOR('Hello World', 'K');
console.log(obfuscatedXOR);
const original = ObfuscationMaster.deobfuscateXOR(obfuscatedXOR, 'K');
console.log(original);
12. SanitizationMaster
The SanitizationMaster class provides methods to sanitize strings, removing unwanted characters or formatting.
Examples:
import { SanitizationMaster } from 'string-master';
const escapedHtml = SanitizationMaster.escapeHtml('<div>Hello & welcome!</div>');
console.log(escapedHtml);
const cleanedString = SanitizationMaster.removeControlCharacters('Hello\u0001World\u0002');
console.log(cleanedString);
const sanitizedString = SanitizationMaster.sanitizeForDisplay('<div>Hello\u0001World</div>');
console.log(sanitizedString);
13. SearchMaster
The SearchMaster class provides methods for searching within strings, including fuzzy search and proximity search.
Examples:
import { SearchMaster } from 'string-master';
const firstIndex = SearchMaster.indexOf('Hello, world!', 'world');
console.log(firstIndex);
const lastIndex = SearchMaster.lastIndexOf('Hello, world! world!', 'world');
console.log(lastIndex);
const nthIndex = SearchMaster.nthIndexOf('abcabcabc', 'abc', 2);
console.log(nthIndex);
const matches = SearchMaster.matchAll('Hello 123, world 456!', /\d+/g);
console.log(matches);
const firstMatch = SearchMaster.findFirst('Hello 123 world', /\d+/);
console.log(firstMatch);
const fuzzyResults = SearchMaster.fuzzySearch('Hello, wrld!', 'world', 1);
console.log(fuzzyResults);
const proximity = SearchMaster.proximitySearch('The quick brown fox jumps over the lazy dog', 'fox', 'dog', 5);
console.log(proximity);
const highlighted = SearchMaster.highlight('Hello, world!', 'world');
console.log(highlighted);
14. SimilarityMaster
The SimilarityMaster class calculates the similarity between two strings using various algorithms like Jaccard, Levenshtein, and Jaro-Winkler.
Examples:
import { SimilarityMaster } from 'string-master';
const sm = new SimilarityMaster(['apple', 'banana', 'grape']);
const hasSimilar = sm.hasSimilarString('apples', 0.8);
console.log(hasSimilar);
const scores = sm.getSimilarStrings('apple', 'dice');
console.log(scores);
const dicsScore = SimilarityMaster.calculateSimilarity('hello', 'hallo', 'dice');
console.log(dicsScore);
const levenshteinScore = SimilarityMaster.calculateSimilarity('hello', 'hallo', 'levenshtein');
console.log(levenshteinScore);
const jaroWinklerScore = SimilarityMaster.calculateSimilarity('hello', 'hallo', 'jaro-winkler');
console.log(jaroWinklerScore);
const jaccardScore = SimilarityMaster.calculateSimilarity('hello', 'hallo', 'jaccard');
console.log(jaccardScore);
const comparison = SimilarityMaster.compareMultipleStrings('apple', ['apples', 'banana', 'grape'], 'jaccard');
console.log(comparison.bestMatch);
console.log(comparison.bestMatchIndex);
15. SlugMaster
The SlugMaster class converts strings to URL-friendly slugs.
Examples:
import { SlugMaster } from 'string-master';
const slug1 = SlugMaster.slugify('Hello World!');
console.log(slug1);
const slug2 = SlugMaster.slugify('JavaScript Mastery');
console.log(slug2);
const slugWithOptions1 = SlugMaster.slugifyWithOptions('Hello World!', { separator: '_', lowercase: false });
console.log(slugWithOptions1);
SlugMaster.setCustomRules([{ rule: /and/g, replacement: 'n' }]);
const slugWithRules = SlugMaster.slugify('Rock and Roll');
console.log(slugWithRules);
16. TemplateMaster
The TemplateMaster class allows for simple templating and string interpolation.
Examples:
import { TemplateMaster } from 'string-master';
const result1 = TemplateMaster.fill('Hello, {name}!', { name: 'John' });
console.log(result1);
const result2 = TemplateMaster.fillWithDelimiter('Hello, [name]!', { name: 'John' }, ['[', ']']);
console.log(result2);
const result3 = TemplateMaster.escape('Hello, {name}! <script>alert("XSS")</script>');
console.log(result3);
const templateFunc = TemplateMaster.toFunction('Hello, {name}!');
const result4 = templateFunc({ name: 'John' });
console.log(result4);
17. TrimmingMaster
The TrimmingMaster class provides various methods for trimming whitespace or characters from strings.
Examples:
import { TrimmingMaster } from 'string-master';
const result1 = TrimmingMaster.trimStart(' Hello', ' ');
console.log(result1);
const result2 = TrimmingMaster.trimEnd('Hello ', ' ');
console.log(result2);
const result3 = TrimmingMaster.collapseWhitespace('Hello World! ');
console.log(result3);
18. ValidationMaster
The ValidationMaster class validates strings against different rules, such as email or URL formats.
Examples:
import { ValidationMaster } from 'string-master';
console.log(ValidationMaster.isAlpha('abc'));
console.log(ValidationMaster.isAlpha('abc123'));
console.log(ValidationMaster.isAlphaNumeric('abc123'));
console.log(ValidationMaster.isAlphaNumeric('abc!123'));
console.log(ValidationMaster.isEmpty(''));
console.log(ValidationMaster.isEmpty(' '));
console.log(ValidationMaster.isEmpty('abc'));
console.log(ValidationMaster.isLower('abc'));
console.log(ValidationMaster.isLower('Abc'));
console.log(ValidationMaster.isNumeric('123'));
console.log(ValidationMaster.isNumeric('123a'));
console.log(ValidationMaster.isUpper('ABC'));
console.log(ValidationMaster.isUpper('Abc'));
console.log(ValidationMaster.startsWith('Hello, world!', 'Hello'));
console.log(ValidationMaster.endsWith('hello', 'lo', 'world'));
console.log(ValidationMaster.endsWith('hello', 'hi'));
console.log(ValidationMaster.equalsIgnoreCase('Hello', 'hello'));
console.log(ValidationMaster.contains('hello world', 'world'));
console.log(ValidationMaster.isAnagram('listen', 'silent'));
console.log(ValidationMaster.isPalindrome('A man, a plan, a canal, Panama'));
3. Contributing
I welcome contributions from the community to help improve the library. Please follow the guidelines below to get started.
4. License
String Master is licensed under the MIT License. See the LICENSE file for more information.
5. Contact
If you have any questions, suggestions, or would like to collaborate, please feel free to reach out:
I look forward to hearing from you!