StrMasker
A simple and flexible Ruby gem for masking portions of strings with customizable characters.
Installation
Add this line to your application's Gemfile:
gem 'str_masker'And then execute:
$ bundle installOr install it yourself as:
$ gem install str_maskerUsage
StrMasker provides a simple interface for masking strings with asterisks or any custom character.
Basic Usage
require 'str_masker'
# Mask entire string (default behavior)
StrMasker.mask_string("password")
#=> "********"
# Mask middle characters, keeping first and last
StrMasker.mask_string("password", from: 1, to: -2)
#=> "p******d"
# Mask from beginning, keeping last character
StrMasker.mask_string("secret", from: 0, to: -2)
#=> "*****t"
# Mask from second character to end
StrMasker.mask_string("confidential", from: 1, to: -1)
#=> "c***********"Custom Masking Character
# Use a different character for masking
StrMasker.mask_string("password", from: 1, to: -2, char: '#')
#=> "p######d"
StrMasker.mask_string("email@example.com", from: 2, to: -5, char: 'X')
#=> "emXXXXXXXXX.com"Edge Cases
# Returns nil for nil input
StrMasker.mask_string(nil)
#=> nil
# Returns original string if no masking is needed
StrMasker.mask_string("ab", from: 1, to: -2)
#=> "ab"
# Empty string
StrMasker.mask_string("")
#=> ""API Reference
mask_string(val, from: 0, to: -1, char: '*')
Masks a portion of a string with the specified character.
Parameters
-
val(String, nil): The string to mask. Returnsnilif input isnil. -
from(Integer, optional): Starting position for masking (inclusive). Must be non-negative. Default:0. -
to(Integer, optional): Ending position for masking (must be negative, counts from end). Default:-1. -
char(String, optional): The character to use for masking. Must be a single character. Default:'*'.
Returns
-
Stringornil: The masked string, ornilif input wasnil.
Raises
-
ArgumentError: Iffromis negative,tois non-negative, orcharis not a single character.
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/MichalZaporski/str_masker.
License
The gem is available as open source under the terms of the MIT License.