Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class String | ||
# Get a new string with non-ASCII characters removed. | ||
# | ||
# alt - String to replace non-ASCII characters with. | ||
# Defaults to a blank string (`''`). | ||
# | ||
# Examples | ||
# | ||
# 'abc'.ascii_only #=> 'abc' | ||
# '中文123'.ascii_only #=> '123' | ||
# | ||
# Returns a copy of [String] with ASCII characters only. | ||
# | ||
# CREDIT: Nathan Long | ||
# | ||
# SEE: http://stackoverflow.com/questions/1268289 | ||
def ascii_only(alt='') | ||
encoding_options = { | ||
:invalid => :replace, # Replace invalid byte sequences | ||
:undef => :replace, # Replace anything not defined in ASCII | ||
:replace => alt, # Use a blank for those replacements | ||
:UNIVERSAL_NEWLINE_DECORATOR => true # Always break lines with \n | ||
} | ||
self.encode(Encoding.find('ASCII'), encoding_options) | ||
end | ||
|
||
# Modify string keeping only ASCII characters. | ||
# | ||
# alt - String to replace non-ASCII characters with. | ||
# Defaults to a blank string (`''`). | ||
# | ||
# Examples | ||
# | ||
# 'abc'.ascii_only! #=> 'abc' | ||
# '中文123'.ascii_only! #=> '123' | ||
# | ||
# Returns [String] | ||
# | ||
# CREDIT: Nathan Long | ||
# | ||
# SEE: http://stackoverflow.com/questions/1268289 | ||
def ascii_only!(alt='') | ||
encoding_options = { | ||
:invalid => :replace, # Replace invalid byte sequences | ||
:undef => :replace, # Replace anything not defined in ASCII | ||
:replace => alt, # Use a blank for those replacements | ||
:UNIVERSAL_NEWLINE_DECORATOR => true # Always break lines with \n | ||
} | ||
self.encode!(Encoding.find('ASCII'), encoding_options) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
covers 'facets/string/ascii_only' | ||
|
||
test_case String do | ||
|
||
method :ascii_only do | ||
test 'without any non-ascii' do | ||
'abc'.ascii_only.assert == 'abc' | ||
end | ||
|
||
test 'with non-ascii' do | ||
'中文123'.ascii_only.assert == '123' | ||
end | ||
end | ||
|
||
method :ascii_only! do | ||
test 'without any non-ascii' do | ||
s = 'abc' | ||
s.ascii_only! | ||
s.assert == 'abc' | ||
end | ||
|
||
test 'with non-ascii' do | ||
s = '中文123' | ||
s.ascii_only! | ||
s.assert == '123' | ||
end | ||
end | ||
|
||
end | ||
|