This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
## String#acronym
|
||||
|
||||
require 'facets/string/acronym'
|
||||
|
||||
Transform a string into an acronym.
|
||||
|
||||
'abc'.acronym.assert == 'a'
|
||||
'abc def'.acronym.assert == 'ad'
|
||||
'abc def ghi'.acronym.assert == 'adg'
|
||||
@@ -0,0 +1,34 @@
|
||||
## String#align
|
||||
|
||||
require 'facets/string/align'
|
||||
|
||||
Align a string to a given position. This method simply delegates
|
||||
to the other three more specific align methods.
|
||||
|
||||
"xxx".align(:right, 9).assert == " xxx"
|
||||
"xxx".align(:left, 9).assert == "xxx "
|
||||
|
||||
## String#align_right
|
||||
|
||||
Align a string to the right. The default alignment separation is
|
||||
a newline ("\n"). This can be changed as can be the padding string
|
||||
which defaults to a single space (' ').
|
||||
|
||||
"xxx".align_right(9).assert == " xxx"
|
||||
|
||||
## String#align_left
|
||||
|
||||
Align a string to the left. The default alignment separation is a
|
||||
newline ("\n"). This can be changed as can be the padding string
|
||||
which defaults to a single space (' ').
|
||||
|
||||
"xxx".align_left(9).assert == "xxx "
|
||||
|
||||
## String#align_center
|
||||
|
||||
Centers each line of a string. The default alignment separation is
|
||||
a new line ("\n"). This can be changed as can be the padding string
|
||||
which defaults to a single space (' ').
|
||||
|
||||
"xxx".align_center(9).assert == " xxx "
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
## String#ascii_only
|
||||
|
||||
require 'facets/string/ascii_only'
|
||||
|
||||
Return a new string with non-ASCII characters removed.
|
||||
|
||||
'abc'.ascii_only.assert == 'abc'
|
||||
'中文123'.ascii_only.assert == '123'
|
||||
|
||||
Return a new string with non-ASCII characters replaced.
|
||||
|
||||
'中文123'.ascii_only('!').assert == '!!123'
|
||||
|
||||
## String#ascii_only!
|
||||
|
||||
Modifiy string with non-ASCII chracters removed.
|
||||
|
||||
s = 'abc'
|
||||
s.ascii_only!
|
||||
s.assert == 'abc'
|
||||
|
||||
s = '中文123'
|
||||
s.ascii_only!
|
||||
s.assert == '123'
|
||||
|
||||
|
||||
Modifiy string with non-ASCII chracters replaced.
|
||||
|
||||
s = 'abc'
|
||||
s.ascii_only!('!')
|
||||
s.assert == 'abc'
|
||||
|
||||
s = '中文123'
|
||||
s.ascii_only!('!')
|
||||
s.assert == '!!123'
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
## String#bracket
|
||||
|
||||
require 'facets/string/bracket'
|
||||
|
||||
'X'.bracket('#').assert == '#X#'
|
||||
'X'.bracket('x','!').assert == 'xX!'
|
||||
'X'.bracket('{','}').assert == '{X}'
|
||||
'X'.bracket('<').assert == '<X>'
|
||||
'X'.bracket('(').assert == '(X)'
|
||||
'X'.bracket('[').assert == '[X]'
|
||||
'X'.bracket('{').assert == '{X}'
|
||||
|
||||
## String#bracket!
|
||||
|
||||
a = 'X' ; a.bracket!('#')
|
||||
a.assert == '#X#'
|
||||
|
||||
a = 'X' ; a.bracket!('x','!')
|
||||
a.assert == 'xX!'
|
||||
|
||||
a = 'X' ; a.bracket!('{','}')
|
||||
a.assert == '{X}'
|
||||
|
||||
a = 'X' ; a.bracket!('<')
|
||||
a.assert == '<X>'
|
||||
|
||||
a = 'X' ; a.bracket!('(')
|
||||
a.assert == '(X)'
|
||||
|
||||
a = 'X' ; a.bracket!('[')
|
||||
a.assert == '[X]'
|
||||
|
||||
a = 'X' ; a.bracket!('{')
|
||||
a.assert == '{X}'
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
## String#Camelcase
|
||||
|
||||
require 'facets/string/camelcase'
|
||||
|
||||
The `camelcase` method converts snakecase strings into camelcase strings.
|
||||
|
||||
"abc_xyz".camelcase.assert == "abcXyz"
|
||||
|
||||
Continious underscore characters are treated as if a single underscore
|
||||
character.
|
||||
|
||||
"abc____xyz".camelcase.assert == "abcXyz"
|
||||
|
||||
Without an argument the first character is left alone.
|
||||
|
||||
"Camel_case".camelcase.assert == "CamelCase"
|
||||
|
||||
Passing +:upper+ or +true+ to #camelcase captializes the first letter.
|
||||
This is known as upper-camelcase.
|
||||
|
||||
"Abc_xyz".camelcase(true).assert == "AbcXyz"
|
||||
"Abc____xyz".camelcase(:upper).assert == "AbcXyz"
|
||||
|
||||
Where as passing +false+ or +:lower+ downcases the first character,
|
||||
known as lower-camelcase.
|
||||
|
||||
"abc_xyz".camelcase(false).assert == "abcXyz"
|
||||
"abc____xyz".camelcase(false).assert == "abcXyz"
|
||||
"Abc_xyz".camelcase(:lower).assert == "abcXyz"
|
||||
"Abc____xyz".camelcase(:lower).assert == "abcXyz"
|
||||
|
||||
By passing a match string to the method we can make +camelcase+
|
||||
apply to each word in a string.
|
||||
|
||||
"abc xyz".camelcase(' ').assert == "abcXyz"
|
||||
"abc xyz".camelcase(/\s/).assert == "abcXyz"
|
||||
"abc\txyz".camelcase(/\s/).assert == "abcXyz"
|
||||
"abc\nxyz".camelcase(/\s/).assert == "abcXyz"
|
||||
|
||||
## String#lower_camelcase
|
||||
|
||||
"abc_xyz".lower_camelcase.assert == "abcXyz"
|
||||
|
||||
## String#upper_camelcase
|
||||
|
||||
"abc_xyz".upper_camelcase.assert == "AbcXyz"
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
## String#capitalized?
|
||||
|
||||
String#capitalized? return true if a string begins with a capitalized
|
||||
letter, false otherwise.
|
||||
|
||||
require 'facets/string/capitalized'
|
||||
|
||||
'Abc'.assert.capitalized?
|
||||
|
||||
## String#downcase?
|
||||
|
||||
In addition String#downcase? is provided which checks to see if the
|
||||
whole string is composed of lowercase letters.
|
||||
|
||||
'abc'.assert.downcase?
|
||||
|
||||
#'abc'.assert.lowercase?
|
||||
|
||||
## String#upcase?
|
||||
|
||||
And String#upcase? which checks to see if the whole string is composed
|
||||
of uppercase letters.
|
||||
|
||||
'ABC'.assert.upcase?
|
||||
|
||||
#'ABC'.assert.uppercase?
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
## String#characters
|
||||
|
||||
The String#chars methods simply splits a string into
|
||||
an Array of character strings.
|
||||
|
||||
require 'facets/string/characters'
|
||||
|
||||
"abc".characters.assert == ["a","b","c"]
|
||||
|
||||
"ab\nc".characters.assert == ["a","b","\n","c"]
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
## String#cleanlines
|
||||
|
||||
Thre String#cleanlines method works essentially the same as String#lines,
|
||||
returning an Array of line strings, but it also removes trailing whitespace
|
||||
from each line.
|
||||
|
||||
require 'facets/string/cleanlines'
|
||||
|
||||
"a \n b \nc".cleanlines.to_a.assert == ["a","b","c"]
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
## String#cleave
|
||||
|
||||
require 'facets/string/cleave'
|
||||
|
||||
no spaces short word
|
||||
|
||||
s, x = 'whole', ['whole', '']
|
||||
s.cleave.assert == x
|
||||
|
||||
no spaces long word
|
||||
|
||||
s, x = 'Supercalifragilisticexpialidocious' , ['Supercalifragilisticexpialidocious', '']
|
||||
s.cleave.assert == x
|
||||
|
||||
exact middle two words
|
||||
|
||||
s, x = 'fancy split', ['fancy', 'split']
|
||||
s.cleave.assert == x
|
||||
|
||||
exact middle many words
|
||||
|
||||
s, x = 'All good Rubyists know how to party', ['All good Rubyists', 'know how to party']
|
||||
s.cleave.assert == x
|
||||
|
||||
closer to start
|
||||
|
||||
s, x = 'short splitter', ['short', 'splitter']
|
||||
s.cleave.assert == x
|
||||
|
||||
closer to start
|
||||
|
||||
s, x = 'Four score and seven years ago...', ['Four score and', 'seven years ago...']
|
||||
s.cleave.assert == x
|
||||
|
||||
closer to start
|
||||
|
||||
s, x = 'abc def ghijklm nop', ['abc def', 'ghijklm nop']
|
||||
s.cleave.assert == x
|
||||
|
||||
closer to end
|
||||
|
||||
s, x = 'extended split', ['extended', 'split']
|
||||
s.cleave.assert == x
|
||||
|
||||
closer to end
|
||||
|
||||
s, x = 'abc defghi jklm nop', [ 'abc defghi', 'jklm nop']
|
||||
s.cleave.assert == x
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
## String#compress_lines
|
||||
|
||||
require 'facets/string/compress_lines'
|
||||
|
||||
Matches any whitespace (including newline) at beginning and end of lines,
|
||||
and replaces with a single space.
|
||||
|
||||
string = <<-QUERY.compress_lines
|
||||
SELECT name
|
||||
FROM users
|
||||
QUERY
|
||||
string.assert == "SELECT name FROM users"
|
||||
|
||||
Matches any whitespace (including newline) at beginning and end of lines,
|
||||
and removes it.
|
||||
|
||||
string = <<-QUERY.compress_lines(false)
|
||||
SELECT name
|
||||
FROM users
|
||||
QUERY
|
||||
string.assert == "SELECT nameFROM users"
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
## String#crypt
|
||||
|
||||
require 'facets/string/crypt'
|
||||
|
||||
Adds a default salt to the built-in crypt method.
|
||||
|
||||
NOTE: This method is not a common core extension and is not
|
||||
loaded automatically when using <code>require 'facets'</code>.
|
||||
|
||||
"abc".crypt('ZZ').assert.start_with?('ZZ')
|
||||
|
||||
encrypted = "abc".crypt
|
||||
salt = encrypted[0..1]
|
||||
"abc".crypt(salt).assert == encrypted
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
## String#divide
|
||||
|
||||
require 'facets/string/divide'
|
||||
|
||||
s = "<p>This<b>is</b>a test.</p>"
|
||||
d = s.divide(/<.*?>/)
|
||||
e = ["<p>This", "<b>is", "</b>a test.", "</p>"]
|
||||
d.assert == e
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
## String#each_word
|
||||
|
||||
require 'facets/string/each_word'
|
||||
|
||||
a = []
|
||||
i = "this is a test"
|
||||
|
||||
i.each_word{ |w| a << w }
|
||||
|
||||
a.assert == ['this', 'is', 'a', 'test']
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
## String#edit_distance
|
||||
|
||||
require 'facets/string/edit_distance'
|
||||
|
||||
Levenshtein distance algorithm implementation for Ruby, with UTF-8 support.
|
||||
|
||||
The Levenshtein distance is a measure of how similar two strings s and t are,
|
||||
calculated as the number of deletions/insertions/substitutions needed to
|
||||
transform s into t. The greater the distance, the more the strings differ.
|
||||
|
||||
d = "pretzel".edit_distance('pretzil')
|
||||
d.assert = 1
|
||||
|
||||
d = "zretzel".edit_distance('pretzil')
|
||||
d.assert = 2
|
||||
@@ -0,0 +1,15 @@
|
||||
## String#exclude?
|
||||
|
||||
require 'facets/string/exclude'
|
||||
|
||||
The inverse of include?.
|
||||
|
||||
"aaaa".assert.exclude?('bb')
|
||||
"aaab".assert.exclude?('bb')
|
||||
"aaba".assert.exclude?('bb')
|
||||
"baaa".assert.exclude?('bb')
|
||||
|
||||
"abba".refute.exclude?('bb')
|
||||
"aabb".refute.exclude?('bb')
|
||||
"bbaa".refute.exclude?('bb')
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
## String#expand_tab
|
||||
|
||||
require 'facets/string/expand_tab'
|
||||
|
||||
We will use this string as a basis for demonstration.
|
||||
|
||||
tabs = <<-EOF.gsub(/^\s+\|/, '')
|
||||
|
|
||||
|\tOne tab
|
||||
| \tOne space and one tab
|
||||
| \t Six spaces, a tab, and a space
|
||||
EOF
|
||||
|
||||
Expanding tabs 0.
|
||||
|
||||
expected = <<-EOF.gsub(/^\s+\|/, '')
|
||||
|
|
||||
|One tab
|
||||
| One space and one tab
|
||||
| Six spaces, a tab, and a space
|
||||
EOF
|
||||
|
||||
tabs.expand_tabs(0).assert == expected
|
||||
|
||||
Expanding tabs 1.
|
||||
|
||||
expected = <<-EOF.gsub(/^\s+\|/, '')
|
||||
|
|
||||
| One tab
|
||||
| One space and one tab
|
||||
| Six spaces, a tab, and a space
|
||||
EOF
|
||||
|
||||
tabs.expand_tabs(1).assert == expected
|
||||
|
||||
Expanding tabs 4.
|
||||
|
||||
expected = <<-EOF.gsub(/^\s+\|/, '')
|
||||
|
|
||||
| One tab
|
||||
| One space and one tab
|
||||
| Six spaces, a tab, and a space
|
||||
EOF
|
||||
|
||||
tabs.expand_tabs(4).assert == expected
|
||||
|
||||
Expanding tabs 8.
|
||||
|
||||
expected = <<-EOF.gsub(/^\s+\|/, '')
|
||||
|
|
||||
| One tab
|
||||
| One space and one tab
|
||||
| Six spaces, a tab, and a space
|
||||
EOF
|
||||
|
||||
tabs.expand_tabs.assert == expected
|
||||
tabs.expand_tabs(8).assert == expected
|
||||
|
||||
Expanding tabs 16.
|
||||
|
||||
expected = <<-EOF.gsub(/^\s+\|/, '')
|
||||
|
|
||||
| One tab
|
||||
| One space and one tab
|
||||
| Six spaces, a tab, and a space
|
||||
EOF
|
||||
|
||||
tabs.expand_tabs(16).assert == expected
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
## String#fold
|
||||
|
||||
require 'facets/string/fold'
|
||||
|
||||
s = "This is\na test.\n\nIt clumps\nlines of text."
|
||||
o = "This is a test.\n\nIt clumps lines of text."
|
||||
s.fold.assert == o
|
||||
|
||||
leave spaces
|
||||
|
||||
s = "This is\na test.\n\n This is pre.\n Leave alone.\n\nIt clumps\nlines of text."
|
||||
o = "This is a test.\n\n This is pre.\n Leave alone.\n\nIt clumps lines of text."
|
||||
s.fold(true).assert == o
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
## String#indent
|
||||
|
||||
require 'facets/string/indent'
|
||||
|
||||
positive
|
||||
|
||||
"xyz".indent(4).assert == ' xyz'
|
||||
" xyz".indent(2).assert == ' xyz'
|
||||
|
||||
multiline positive
|
||||
|
||||
"abc\nxyz".indent(2).assert == " abc\n xyz"
|
||||
|
||||
zero
|
||||
|
||||
'xyz'.indent(0).assert == 'xyz'
|
||||
|
||||
negative
|
||||
|
||||
' xyz'.indent(-2).assert == ' xyz'
|
||||
' xyz'. indent(-2).assert == 'xyz'
|
||||
|
||||
multiline negative
|
||||
|
||||
" abc\n xyz".indent(-2).assert == " abc\n xyz"
|
||||
|
||||
negative more than is possible
|
||||
|
||||
' xyz'.indent(-3).assert == 'xyz'
|
||||
|
||||
non-space positive
|
||||
|
||||
"xyz".indent(4, '-').assert == '----xyz'
|
||||
|
||||
non-space zero
|
||||
|
||||
'xyz'.indent(0, '-').assert == 'xyz'
|
||||
|
||||
non-space negative non-matching character
|
||||
|
||||
' xyz'.indent(-2, '-').assert == ' xyz'
|
||||
' xyz'. indent(-2, '-').assert == ' xyz'
|
||||
|
||||
non-space negative
|
||||
|
||||
'----xyz'.indent(-2, '-').assert == '--xyz'
|
||||
'--xyz'.indent(-2, '-').assert == 'xyz'
|
||||
|
||||
Special regular expresion characters are escaped.
|
||||
|
||||
Ensure '.' is treated literally and not as wildcard
|
||||
|
||||
' xyz'.indent(-2, '.').assert == ' xyz'
|
||||
'..xyz'.indent(-2, '.').assert == 'xyz'
|
||||
|
||||
Ensure '*' is treated literally and not as wildcard
|
||||
|
||||
' xyz'.indent(-2, '*').assert == ' xyz'
|
||||
'**xyz'.indent(-2, '*').assert == 'xyz'
|
||||
|
||||
Ensure '^' is treated literally and not as line start
|
||||
|
||||
' xyz'.indent(-2, '^').assert == ' xyz'
|
||||
'^^xyz'.indent(-2, '^').assert == 'xyz'
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
## String#index_all
|
||||
|
||||
require 'facets/string/index_all'
|
||||
|
||||
Returns an array of all index locations. the reuse flag allows the trailing
|
||||
portion of a match to be resued for subsequent matches.
|
||||
|
||||
"abcabcabc".index_all('a') #=> [0,3,6]
|
||||
|
||||
"a123a567a9".index_all(/a/) #=> [0,4,8]
|
||||
|
||||
"bbb".index_all('bb', false) #=> [0]
|
||||
"bbb".index_all('bb', true) #=> [0,1]
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
## String#interpolate
|
||||
|
||||
require 'facets/string/interpolate'
|
||||
|
||||
a = 1
|
||||
|
||||
String.interpolate{ 'this is #{a}' }.assert == 'this is 1'
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
## String#lchomp
|
||||
|
||||
require 'facets/string/lchomp'
|
||||
|
||||
s = "xxboo"
|
||||
r = s.lchomp("xx")
|
||||
r.assert == "boo"
|
||||
|
||||
## String#lchomp!
|
||||
|
||||
s = "xxboo"
|
||||
s.lchomp!("xx")
|
||||
s.assert == "boo"
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
## String#line_wrap
|
||||
|
||||
require 'facets/string/line_wrap'
|
||||
|
||||
"abc123".line_wrap(3).assert == "abc\n123\n"
|
||||
"abcd123".line_wrap(4).assert == "abcd\n123\n"
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
## String#lowercase
|
||||
|
||||
require 'facets/string/lowercase'
|
||||
|
||||
Downcase the first letter.
|
||||
|
||||
s = 'Abc'
|
||||
s.lowercase.assert == 'abc'
|
||||
|
||||
s = 'ABC'
|
||||
s.lowercase.assert == 'aBC'
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
## String#methodize
|
||||
|
||||
require 'facets/string/methodize'
|
||||
|
||||
module name
|
||||
|
||||
a = "FooBase"
|
||||
r = "foo_base"
|
||||
a.methodize.assert == r
|
||||
|
||||
path name
|
||||
|
||||
a = "foo/base"
|
||||
r = "foo__base"
|
||||
a.methodize.assert == r
|
||||
|
||||
name space
|
||||
|
||||
a = "Foo::Base"
|
||||
r = "foo__base"
|
||||
a.methodize.assert == r
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
## String#modulize
|
||||
|
||||
require 'facets/string/modulize'
|
||||
|
||||
snakecase
|
||||
|
||||
a = "foo_bar"
|
||||
r = "FooBar"
|
||||
a.modulize.assert == r
|
||||
|
||||
path name
|
||||
|
||||
a = "foo/bar"
|
||||
r = "Foo::Bar"
|
||||
a.modulize.assert == r
|
||||
|
||||
on methodized string
|
||||
|
||||
a = "foo__bar"
|
||||
r = "Foo::Bar"
|
||||
a.modulize.assert == r
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
## String#mscan
|
||||
|
||||
require 'facets/string/mscan'
|
||||
|
||||
r = 'abc,def,gh'.mscan(/[,]/)
|
||||
assert( r.all?{ |md| MatchData === md } )
|
||||
r.to_a.length.assert == 2
|
||||
r[0][0].assert == ','
|
||||
r[1][0].assert == ','
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
## String#natcmp
|
||||
|
||||
require 'facets/string/natcmp'
|
||||
|
||||
"my_prog_v1.1.0".natcmp( "my_prog_v1.2.0").assert == -1
|
||||
"my_prog_v1.2.0".natcmp("my_prog_v1.10.0").assert == -1
|
||||
"my_prog_v1.2.0".natcmp( "my_prog_v1.1.0").assert == 1
|
||||
"my_prog_v1.10.0".natcmp("my_prog_v1.2.0").assert == 1
|
||||
"my_prog_v1.0.0".natcmp( "my_prog_v1.0.0").assert == 0
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
## String#nchar
|
||||
|
||||
require 'facets/string/nchar'
|
||||
|
||||
"abcxyz".nchar(3).assert == "abc"
|
||||
"abcxyz".nchar(-3).assert == "xyz"
|
||||
"abcxyz".nchar(3, 'HI').assert == "HIxyz"
|
||||
"abcxyz".nchar(-3, 'HI').assert == "abcHI"
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
## String#newlines
|
||||
|
||||
Since Ruby 1.9 defined #lines to keep the newline character,
|
||||
we have defined #newlines to leave them out.
|
||||
|
||||
require 'facets/string/newlines'
|
||||
|
||||
"a\nb\nc".newlines.to_a.assert == ["a","b","c"]
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
## String#number?
|
||||
|
||||
require 'facets/string/number'
|
||||
|
||||
Returns true if its a decimal number
|
||||
|
||||
x = "1.23\n"
|
||||
x.number?.assert == true
|
||||
|
||||
x = "123_456_789_123_456_789.123_456_000_111\n"
|
||||
x.number?.assert == true
|
||||
|
||||
x = ".123\n"
|
||||
x.number?.assert == false
|
||||
|
||||
x = "123_\n"
|
||||
x.number?.assert == false
|
||||
|
||||
x = "_123\n"
|
||||
x.number?.assert == false
|
||||
|
||||
x = "23_456_789_123_456_.245\n"
|
||||
x.number?.assert == false
|
||||
|
||||
x = "23_456_789_123_456._245\n"
|
||||
x.number?.assert == false
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
## String#op_div
|
||||
|
||||
require 'facets/string/op_div'
|
||||
|
||||
Treats +self+ and +path+ as representations of pathnames, joining thme together
|
||||
as a single path.
|
||||
|
||||
('merb' / 'string').assert == "merb/string"
|
||||
('merb' / :symbol).assert == "merb/symbol"
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
## String#-
|
||||
|
||||
require 'facets/string/op_sub'
|
||||
|
||||
s = "xxbooxxfoo"
|
||||
(s - "xx").assert == "boofoo"
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
## String#pathize
|
||||
|
||||
require 'facets/string/pathize'
|
||||
|
||||
module name
|
||||
|
||||
a = "Foo::Base"
|
||||
x = "foo/base"
|
||||
a.pathize.assert == x
|
||||
|
||||
path name
|
||||
|
||||
a = "foo/base"
|
||||
x = "foo/base"
|
||||
a.pathize.assert == x
|
||||
|
||||
name space
|
||||
|
||||
a = "foo__base"
|
||||
x = "foo/base"
|
||||
a.pathize.assert == x
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
## String#quote
|
||||
|
||||
require 'facets/string/quote'
|
||||
|
||||
The default is single quotes.
|
||||
|
||||
'hi'.quote.assert == %{"hi"}
|
||||
|
||||
The type of quotes can be specified by name.
|
||||
|
||||
'hi'.quote(:single).assert == %{'hi'}
|
||||
'hi'.quote(:double).assert == %{"hi"}
|
||||
'hi'.quote(:back).assert == %{`hi`}
|
||||
|
||||
They can also be specified by the first character of the name.
|
||||
|
||||
'hi'.quote(:s).assert == %{'hi'}
|
||||
'hi'.quote(:d).assert == %{"hi"}
|
||||
'hi'.quote(:b).assert == %{`hi`}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
## String#random
|
||||
|
||||
require 'facets/string/random'
|
||||
|
||||
Generates a random string of given length, using given character set
|
||||
|
||||
s = String.random
|
||||
# "D9DxFIaqR3dr8Ct1AfmFxHxqGsmA4Oz3"
|
||||
s.assert =~ /^[0-9a-zA-Z]{32}$/
|
||||
|
||||
s = String.random(10)
|
||||
# "t8BIna341S"
|
||||
s.assert =~ /^[0-9a-zA-Z]{10}$/
|
||||
|
||||
s = String.random(10, ['a'..'z'])
|
||||
# "nstpvixfri"
|
||||
s.assert =~ /^[a-z]{10}$/
|
||||
|
||||
s = String.random(10, ['0'..'9'] )
|
||||
# "0982541042"
|
||||
s.assert =~ /^[0-9]{10}$/
|
||||
|
||||
s = String.random(10, ['0'..'9','A'..'F'] )
|
||||
# "3EBF48AD3D"
|
||||
s.assert =~ /^[0-9A-F]{10}$/
|
||||
@@ -0,0 +1,19 @@
|
||||
## String#range
|
||||
|
||||
require 'facets/string/range'
|
||||
|
||||
"a123a567a9".range(/123/).assert == (1..3)
|
||||
"a123a567a9".range(/a/).assert == (0..0)
|
||||
|
||||
## String#range_all
|
||||
|
||||
"a123a123a9".range_all(/123/).assert == [ (1..3), (5..7) ]
|
||||
"a123a567a9".range_all(/a/).assert == [ (0..0), (4..4), (8..8) ]
|
||||
|
||||
## String#range_of_line
|
||||
|
||||
a = "0123\n456\n78"
|
||||
x = [0..4, 5..8, 9..10]
|
||||
r = a.range_of_line
|
||||
r.assert == x
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
## String#remove
|
||||
|
||||
require 'facets/string/remove'
|
||||
|
||||
Removes all occurances of a string or pattern from a string.
|
||||
|
||||
s = 'xxbooxxfoo'
|
||||
s.remove('xx').assert == 'boofoo'
|
||||
|
||||
s = 'xxbooxxfoo'
|
||||
s.remove(/x{1,2}/).assert == 'boofoo'
|
||||
|
||||
## String#remove!
|
||||
|
||||
s = 'xxbooxxfoo'
|
||||
s.remove!('xx')
|
||||
s.assert == 'boofoo'
|
||||
|
||||
s = 'xxbooxxfoo'
|
||||
s.remove!(/x{1,2}/)
|
||||
s.assert == 'boofoo'
|
||||
|
||||
|
||||
## String#op_minus (-)
|
||||
|
||||
s = 'xxbooxxfoo'
|
||||
(s - 'xx').assert == 'boofoo'
|
||||
|
||||
s = 'xxbooxxfoo'
|
||||
(s - /x{1,2}/).assert == 'boofoo'
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
## String#rewrite
|
||||
|
||||
require 'facets/string/rewrite'
|
||||
|
||||
s = "HELLO TOMMY!"
|
||||
rules = [[ /TOMMY/, 'MAN' ]]
|
||||
r = s.rewrite(rules)
|
||||
r.assert == "HELLO MAN!"
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
## String#shatter
|
||||
|
||||
require 'facets/string/shatter'
|
||||
|
||||
s = "<p>This<b>is</b>a test.</p>"
|
||||
r = s.shatter( /<.*?>/ )
|
||||
x = ["<p>", "This", "<b>", "is", "</b>", "a test.", "</p>"]
|
||||
r.assert == x
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
## String#similarity
|
||||
|
||||
require 'facets/string/similarity'
|
||||
|
||||
A fuzzy matching mechanism. Returns a score from 0-1, based on the number of
|
||||
shared edges. To be effective, the strings must be of length 2 or greater.
|
||||
|
||||
"Alexsander".similarity("Aleksander").assert == 0.9
|
||||
|
||||
"Alexander".similarity("Alexander").assert == 1.0
|
||||
|
||||
"Alexander".similarity("").assert == 0.0
|
||||
@@ -0,0 +1,14 @@
|
||||
## String#snakecase
|
||||
|
||||
require 'facets/string/snakecase'
|
||||
|
||||
Underscore a string such that camelcase, dashes and spaces are
|
||||
replaced by underscores. This is the reverse of {#camelcase},
|
||||
albeit not an exact inverse.
|
||||
|
||||
'my_name'.snakecase.assert == 'my_name'
|
||||
|
||||
'MyName'.snakecase.assert == 'my_name'
|
||||
|
||||
'URI'.snakecase.assert == 'uri'
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
## String#splice
|
||||
|
||||
require 'facets/string/splice'
|
||||
|
||||
a = "HELLO"
|
||||
a.splice(1).assert == "E"
|
||||
a.assert == "HLLO"
|
||||
|
||||
range
|
||||
|
||||
a = "HELLO"
|
||||
a.splice(1..2).assert == "EL"
|
||||
a.assert == "HLO"
|
||||
|
||||
store
|
||||
|
||||
a = "HELLO"
|
||||
a.splice(1, "X")
|
||||
a.assert == "HXLLO"
|
||||
|
||||
This could be done if class of 2nd arg is checked.
|
||||
|
||||
#Unit :splice_length
|
||||
# a = "HELLO"
|
||||
# a.splice(1,2).assert == "EL"
|
||||
# a.assert == "HLO"
|
||||
#end
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
## String#squish
|
||||
|
||||
require 'facets/string/squish'
|
||||
|
||||
Returns the string, first removing all whitespace on both ends of the string,
|
||||
and then changing remaining consecutive whitespace groups into one space each.
|
||||
|
||||
r = %{ Multi-line
|
||||
string }.squish
|
||||
r.assert == "Multi-line string"
|
||||
|
||||
r = " foo bar \n \t boo".squish
|
||||
r.assert == "foo bar boo"
|
||||
|
||||
## String#squish!
|
||||
|
||||
Performs a destructive squish. See String#squish.
|
||||
|
||||
r = %{ Multi-line
|
||||
string }
|
||||
r.squish!
|
||||
r.assert == "Multi-line string"
|
||||
|
||||
r = " foo bar \n \t boo"
|
||||
r.squish!
|
||||
r.assert == "foo bar boo"
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
## String#store
|
||||
|
||||
require 'facets/string/store'
|
||||
|
||||
Alias for []=
|
||||
|
||||
a = "HELLO"
|
||||
a.store(1, "X")
|
||||
a.assert == "HXLLO"
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
## String#-
|
||||
|
||||
require 'facets/string/subtract'
|
||||
|
||||
("foobar" - "oo").assert == "fbar"
|
||||
("pizza pizza!" - "zz").assert == "pia pia!"
|
||||
("letters" - /[a-z]+/).assert == ""
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
## String#titlecase
|
||||
|
||||
require 'facets/string/titlecase'
|
||||
|
||||
r = "try this out".titlecase
|
||||
x = "Try This Out"
|
||||
r.assert == x
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
## String#to_re
|
||||
|
||||
require 'facets/string/to_re'
|
||||
|
||||
"abc".to_re.assert == /abc/
|
||||
"a+bc".to_re.assert == /a+bc/
|
||||
"a+bc".to_re(false).assert == /a+bc/
|
||||
"a+bc".to_re(true).assert == /a\+bc/
|
||||
|
||||
escaped characters
|
||||
|
||||
a = "?"
|
||||
b = /#{a.to_rx}/
|
||||
assert( b =~ "?" )
|
||||
|
||||
## String#to_rx
|
||||
|
||||
"abc".to_rx.assert == /abc/
|
||||
"a+bc".to_rx.assert == /a\+bc/
|
||||
"a+bc".to_rx(false).assert == /a+bc/
|
||||
"a+bc".to_rx(true).assert == /a\+bc/
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
## String#trim
|
||||
|
||||
require 'facets/string/trim'
|
||||
|
||||
s = %q{
|
||||
|ABC
|
||||
|123
|
||||
|TEST
|
||||
}.trim
|
||||
s.assert == "ABC\n123\nTEST"
|
||||
|
||||
s = %q{
|
||||
|ABC
|
||||
|123
|
||||
|TEST
|
||||
}.trim
|
||||
s.assert == "ABC\n123\nTEST"
|
||||
|
||||
s = %q{|ABC
|
||||
|123
|
||||
|TEST
|
||||
}.trim
|
||||
s.assert == "ABC\n123\nTEST"
|
||||
|
||||
s = %q{
|
||||
|ABC
|
||||
|123
|
||||
|TEST}.trim
|
||||
s.assert == "ABC\n123\nTEST"
|
||||
|
||||
s = %q{|ABC
|
||||
|123
|
||||
|TEST}.trim
|
||||
s.assert == "ABC\n123\nTEST"
|
||||
|
||||
s = %q{ |ABC
|
||||
|123
|
||||
|TEST}.trim
|
||||
s.assert == "ABC\n123\nTEST"
|
||||
|
||||
s = %q{ABC
|
||||
|123
|
||||
|TEST
|
||||
}.trim
|
||||
s.assert == "ABC\n123\nTEST"
|
||||
|
||||
spacing
|
||||
|
||||
s = %q{
|
||||
| ABC
|
||||
| 123
|
||||
| TEST
|
||||
}.trim
|
||||
s.assert == " ABC\n 123\n TEST"
|
||||
|
||||
s = %q{
|
||||
|ABC
|
||||
|123
|
||||
|TEST
|
||||
}.trim(1)
|
||||
s.assert == " ABC\n 123\n TEST"
|
||||
|
||||
s = %q{
|
||||
|ABC
|
||||
|123
|
||||
|TEST
|
||||
}.trim(2)
|
||||
s.assert == " ABC\n 123\n TEST"
|
||||
|
||||
s = %q{ ABC
|
||||
- 123
|
||||
- TEST
|
||||
}.trim
|
||||
s.assert == " ABC\n 123\n TEST"
|
||||
|
||||
random placement
|
||||
|
||||
@volly = {}
|
||||
100.times{ |n|
|
||||
k = []
|
||||
a = []
|
||||
5.times{ |i|
|
||||
k << ( ( ' ' * Integer(rand*10) ) + '|' + i.to_s )
|
||||
a << ( i.to_s )
|
||||
}
|
||||
@volly[k.join("\n")] = a.join("\n")
|
||||
}
|
||||
|
||||
@volly.each{ |k,v|
|
||||
k.trim.assert == v
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
## String#unbracket
|
||||
|
||||
require 'facets/string/unbracket'
|
||||
|
||||
Remove matched paits of brackets from a string
|
||||
|
||||
s = "{unwrap me}"
|
||||
z = s.unbracket('{')
|
||||
z.assert == "unwrap me"
|
||||
|
||||
s = "--unwrap me!"
|
||||
z = s.unbracket('--','!')
|
||||
z.assert == "unwrap me"
|
||||
|
||||
## String#unbracket!
|
||||
|
||||
s = "{unwrap me}"
|
||||
s.unbracket!('{')
|
||||
s.assert == "unwrap me"
|
||||
|
||||
s = "--unwrap me!"
|
||||
s.unbracket!('--','!')
|
||||
s.assert == "unwrap me"
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
## String#underscore
|
||||
|
||||
require 'facets/string/underscore'
|
||||
|
||||
Underscore a string such that camelcase, dashes and spaces are
|
||||
replaced by underscores. This is the reverse of {#camelcase},
|
||||
albeit not an exact inverse.
|
||||
|
||||
Note, this method no longer converts `::` to `/`, in that case
|
||||
use the {#pathize} method instead.
|
||||
|
||||
'my_name'.underscore.assert == 'my_name'
|
||||
|
||||
'MyName'.underscore.assert == 'my_name'
|
||||
|
||||
'URI'.underscore.assert == 'uri'
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
## String#unfold
|
||||
|
||||
require 'facets/string/unfold'
|
||||
|
||||
str = "This is a test of\nparagraph folding."
|
||||
res = str.unfold
|
||||
exp = "This is a test of paragraph folding."
|
||||
res.assert == exp
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
## String#unindent
|
||||
|
||||
require 'facets/string/unindent'
|
||||
|
||||
We will use these two strings as a common basis for demonstration.
|
||||
|
||||
ex1 = <<-EOF.gsub(/^\s*\|/,'')
|
||||
| I must go down to the seas again
|
||||
| The lonely sea and the sky
|
||||
| And all I want is a tall ship
|
||||
| And a star to steer her by
|
||||
EOF
|
||||
|
||||
ex2 = <<-EOF.gsub(/^\s*\|/,'')
|
||||
| "Eek!"
|
||||
| She cried
|
||||
| As the mouse quietly scurried
|
||||
|by.
|
||||
EOF
|
||||
|
||||
simple examples
|
||||
|
||||
" xyz".unindent(-1).assert == " xyz"
|
||||
" xyz".unindent(0).assert == " xyz"
|
||||
" xyz".unindent(1).assert == " xyz"
|
||||
" xyz".unindent(2).assert == " xyz"
|
||||
" xyz".unindent(3).assert == "xyz"
|
||||
" xyz".unindent(4).assert == "xyz"
|
||||
|
||||
large example unindented one space
|
||||
|
||||
expected = <<-EOF.gsub(/^\s*\|/,'')
|
||||
| I must go down to the seas again
|
||||
| The lonely sea and the sky
|
||||
| And all I want is a tall ship
|
||||
| And a star to steer her by
|
||||
EOF
|
||||
|
||||
actual = ex1.unindent(1)
|
||||
actual.assert == expected
|
||||
|
||||
large example unindented four spaces
|
||||
|
||||
expected = <<-EOF.gsub(/^\s*\|/,'')
|
||||
|I must go down to the seas again
|
||||
| The lonely sea and the sky
|
||||
|And all I want is a tall ship
|
||||
| And a star to steer her by
|
||||
EOF
|
||||
|
||||
actual = ex1.unindent(4)
|
||||
actual.assert == expected
|
||||
|
||||
unindent larger than current indention
|
||||
|
||||
expected = <<-EOF.gsub(/^\s*\|/,'')
|
||||
|"Eek!"
|
||||
|She cried
|
||||
|As the mouse quietly scurried
|
||||
|by.
|
||||
EOF
|
||||
|
||||
actual = ex2.unindent(100)
|
||||
actual.assert == expected
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
## String#unquote
|
||||
|
||||
require 'facets/string/unquote'
|
||||
|
||||
"'this'".unquote.assert == "this"
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
## String#uppercase
|
||||
|
||||
require 'facets/string/uppercase'
|
||||
|
||||
Upcase the first letter.
|
||||
|
||||
s = 'bob'
|
||||
s.uppercase.assert == 'Bob'
|
||||
|
||||
s = 'boB'
|
||||
s.uppercase.assert == 'BoB'
|
||||
|
||||
s = 'BoB'
|
||||
s.uppercase.assert == 'BoB'
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
## String#variablize
|
||||
|
||||
require 'facets/string/variablize'
|
||||
|
||||
s = "a"
|
||||
s.variablize.assert == "@a"
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
## String#word_wrap
|
||||
|
||||
require 'facets/string/word_wrap'
|
||||
|
||||
e = "abcde\n12345\nxyzwu\n"
|
||||
r = "abcde 12345 xyzwu".word_wrap(5)
|
||||
r.assert == e
|
||||
|
||||
e = "abcd\n1234\nxyzw\n"
|
||||
r = "abcd 1234 xyzw".word_wrap(4)
|
||||
r.assert == e
|
||||
|
||||
e = "abc\n123\n"
|
||||
r = "abc 123".word_wrap(4)
|
||||
r.assert == e
|
||||
|
||||
e = "abc \n123\n"
|
||||
r = "abc 123".word_wrap(4)
|
||||
r.assert == e
|
||||
|
||||
e = "abc \n123\n"
|
||||
r = "abc 123".word_wrap(4)
|
||||
r.assert == e
|
||||
|
||||
## String#word_wrap!
|
||||
|
||||
w = "abcde 12345 xyzwu"
|
||||
w.word_wrap!(5)
|
||||
w.assert == "abcde\n12345\nxyzwu\n"
|
||||
|
||||
w = "abcd 1234 xyzw"
|
||||
w.word_wrap!(4)
|
||||
w.assert == "abcd\n1234\nxyzw\n"
|
||||
|
||||
w = "abc 123"
|
||||
w.word_wrap!(4)
|
||||
w.assert == "abc\n123\n"
|
||||
|
||||
w = "abc 123"
|
||||
w.word_wrap!(4)
|
||||
w.assert == "abc \n123\n"
|
||||
|
||||
w = "abc 123"
|
||||
w.word_wrap!(4)
|
||||
w.assert == "abc \n123\n"
|
||||
|
||||
# Unit :word_wrap do
|
||||
# assert_equal "abcde-\n12345-\nxyzwu\n", "abcde12345xyzwu".word_wrap(6,2)
|
||||
# assert_equal "abcd-\n1234-\nxyzw\n", "abcd1234xyzw".word_wrap(5,2)
|
||||
# assert_equal "abc \n123\n", "abc 123".word_wrap(4,2)
|
||||
# assert_equal "abc \n123\n", "abc 123".word_wrap(4,2)
|
||||
# assert_equal "abc \n123\n", "abc 123".word_wrap(4,2)
|
||||
# end
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
## String#words
|
||||
|
||||
require 'facets/string/words'
|
||||
|
||||
x = "a b c\nd e"
|
||||
x.words.assert == ['a','b','c','d','e']
|
||||
|
||||
x = "ab cd\nef"
|
||||
x.words.assert == ['ab','cd','ef']
|
||||
|
||||
x = "ab cd \n ef-gh"
|
||||
x.words.assert == ['ab','cd','ef-gh']
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
## String#^
|
||||
|
||||
require 'facets/string/xor'
|
||||
|
||||
r = "try this out" ^ "puddingsnacks"
|
||||
x = "\004\a\035D\035\006\016\000N\016\026\037"
|
||||
r.assert == x
|
||||
|
||||
Reference in New Issue
Block a user