Skip to content

Commit

Permalink
+ Add String#ascii_only & #ascii_only!
Browse files Browse the repository at this point in the history
  • Loading branch information
PikachuEXE committed Sep 16, 2013
1 parent d1ec550 commit a4f35e8
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,9 @@

## master (unreleased)

* Added `String#ascii_only`
* Added `String#ascii_only!`

## 0.0.8 (13/09/2013)

### New features
Expand Down
1 change: 1 addition & 0 deletions lib/powerpack/string.rb
@@ -1,3 +1,4 @@
require_relative 'string/ascii_only'
require_relative 'string/blank'
require_relative 'string/format'
require_relative 'string/strip_indent'
Expand Down
42 changes: 42 additions & 0 deletions lib/powerpack/string/ascii_only.rb
@@ -0,0 +1,42 @@
# encoding: utf-8

class String
# Return a copy of string with ASCII characters only
# Source: http://stackoverflow.com/questions/1268289/how-to-get-rid-of-non-ascii-characters-in-ruby
#
# @return [String] a copy of string with ASCII characters only
#
# @example
# 'abc'.ascii_only #=> 'abc'
#
# @example
# '中文123'.ascii_only #=> '123'
unless String.method_defined? :ascii_only
def ascii_only
dup.ascii_only!
end
end

# Modify self and keep ASCII characters only
# Returns the string even if no changes were made.
# Source: http://stackoverflow.com/questions/1268289/how-to-get-rid-of-non-ascii-characters-in-ruby
#
# @return [String] The result string
#
# @example
# 'abc'.ascii_only! #=> 'abc'
#
# @example
# '中文123'.ascii_only! #=> '123'
unless String.method_defined? :ascii_only!
def ascii_only!
encoding_options = {
:invalid => :replace, # Replace invalid byte sequences
:undef => :replace, # Replace anything not defined in ASCII
:replace => '', # Use a blank for those replacements
:UNIVERSAL_NEWLINE_DECORATOR => true # Always break lines with \n
}
self.encode! Encoding.find('ASCII'), encoding_options
end
end
end
43 changes: 43 additions & 0 deletions spec/powerpack/string/ascii_only_spec.rb
@@ -0,0 +1,43 @@
# encoding: utf-8

require 'spec_helper'

describe 'String#ascii_only' do
it 'returns same value for string with ASCII chars only' do
expect('abc'.ascii_only).to eq 'abc'
end

it 'returns string without non-ASCII chars' do
expect("abc\u{6666}".force_encoding("UTF-8").ascii_only).to eq 'abc'
end

it 'returns string without non-ASCII chars and with ASCII chars' do
expect("\u{6666}".force_encoding("UTF-8").ascii_only).to eq ''
end

it 'does not change the original string' do
string = "abc\u{6666}".force_encoding("UTF-8")
string.ascii_only
expect(string).to eq "abc\u{6666}".force_encoding("UTF-8")
end
end

describe 'String#ascii_only!' do
it 'returns same value for string with ASCII chars only' do
expect('abc'.ascii_only!).to eq 'abc'
end

it 'returns string without non-ASCII chars' do
expect("abc\u{6666}".force_encoding("UTF-8").ascii_only!).to eq 'abc'
end

it 'returns string without non-ASCII chars and with ASCII chars' do
expect("\u{6666}".force_encoding("UTF-8").ascii_only!).to eq ''
end

it 'changes the original string' do
string = "abc\u{6666}".force_encoding("UTF-8")
string.ascii_only!
expect(string).to eq 'abc'
end
end

0 comments on commit a4f35e8

Please sign in to comment.