StringMagic
Summary
StringMagic is a Ruby gem that enhances Ruby's String
class with powerful methods for text formatting, manipulation, and analysis. It simplifies common string operations while introducing advanced features for developers.
Features
- Text Analysis: Extract emails, URLs, dates, and more from text.
- String Transformation: Convert between cases (snake, kebab, camel, Pascal) or manipulate words.
- Formatting Utilities: Highlight phrases, truncate text, and mask sensitive information.
- Core Operations: Palindrome detection, word count, and anagram checks.
Installation
To include StringMagic in your project, add it to your Gemfile:
gem 'string_magic'
Then run:
bundle install
Or install it globally with:
gem install string_magic
Usage
Require the gem in your code:
require 'string_magic'
# Core Operations
puts StringMagic.palindrome?("A man a plan a canal Panama")
# => true
puts StringMagic.word_count("Hello world!")
# => 2
puts StringMagic.to_pig_latin("hello")
# => "ellohay"
# Text Analysis
text = "Contact support@example.com or visit https://example.com"
puts StringMagic.extract_entities(text)
# => {
# emails: ["support@example.com"],
# urls: ["https://example.com"],
# phone_numbers: [],
# dates: [],
# hashtags: [],
# mentions: []
# }
# Formatting Utilities
puts StringMagic.highlight("Hello world", "world", tag: "strong")
# => "Hello <strong>world</strong>"
puts StringMagic.truncate_words("Hello world how are you", 2)
# => "Hello world..."
# Security Features
card_text = "My card is 4111-1111-1111-1111"
puts StringMagic.mask_sensitive_data(card_text)
# => "My card is ************1111"
# Case Conversions
puts StringMagic.to_snake_case("HelloWorld")
# => "hello_world"
puts StringMagic.to_kebab_case("HelloWorld")
# => "hello-world"
puts StringMagic.camel_case("hello world")
# => "HelloWorld"
# Text Manipulation
puts StringMagic.remove_duplicates("hello")
# => "helo"
puts StringMagic.alternating_case("hello")
# => "HeLlO"
puts StringMagic.reverse_words("hello world")
# => "world hello"
Available Methods
Core Operations
-
palindrome?(text)
: Determines if the given text is a palindrome. -
word_count(text)
: Counts words in the text. -
anagram?(text1, text2)
: Checks if two texts are anagrams.
Text Analysis
-
readability_score(text)
: Calculates text complexity using the Flesch-Kincaid formula. -
extract_entities(text)
: Extracts entities like emails, URLs, phone numbers, dates, hashtags, and mentions.
String Transformation
-
to_snake_case(text)
: Converts text tosnake_case
. -
to_kebab_case(text)
: Converts text tokebab-case
. -
to_pascal_case(text)
: Converts text toPascalCase
. -
camel_case(text)
: Converts text tocamelCase
.
Formatting Utilities
-
highlight(text, phrases, options)
: Highlights text with HTML tags. -
truncate_words(text, count, options)
: Truncates text to a specified number of words. -
truncate_sentences(text, count, options)
: Truncates text to a specified number of sentences.
Security Features
-
mask_sensitive_data(text, options)
: Masks sensitive information like credit card numbers and emails.
Text Manipulation
-
remove_duplicates(text)
: Removes duplicate characters from text. -
alternating_case(text)
: Alternates character case in text. -
reverse_words(text)
: Reverses the order of words in text.
Contributing
I welcome contributions! Here's how you can help:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Write tests for your changes
- Commit your changes (
git commit -am 'feat: add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Please make sure to update tests and follow coding standards.
License
This gem is open source and available under the MIT License.
Code of Conduct
Interactions in the project's codebases, issue trackers, and other channels must adhere to the Code of Conduct.