Skip to content
This repository was archived by the owner on Oct 22, 2020. It is now read-only.

Commit d747da5

Browse files
committed
Add helper method for hexifying strings
1 parent aaf976f commit d747da5

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

lib/wpxf/utility/text.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ def self.rand_email
6868
def self.rand_month
6969
%w[january february march april june july august september october november december].sample
7070
end
71+
72+
# Convert each byte of a string to its hexadecimal value and
73+
# concantenate them together, to provide a hexadecimal string.
74+
# @param value [String] the string to hexify.
75+
# @return [String] the hexadecimal string.
76+
def self.hexify_string(value)
77+
value.each_byte.map { |b| b.to_s(16) }.join
78+
end
7179
end
7280
end
7381
end

spec/utility/text_spec.rb

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,88 +5,88 @@
55
describe Wpxf::Utility::Text do
66
describe '.alpha_ranges' do
77
it 'returns an array containing the a-z range when using :lower' do
8-
expect(Wpxf::Utility::Text.alpha_ranges(:lower)).to eq [*'a'..'z']
8+
expect(described_class.alpha_ranges(:lower)).to eq [*'a'..'z']
99
end
1010

1111
it 'returns an array containing the A-Z range when using :upper' do
12-
expect(Wpxf::Utility::Text.alpha_ranges(:upper)).to eq [*'A'..'Z']
12+
expect(described_class.alpha_ranges(:upper)).to eq [*'A'..'Z']
1313
end
1414

1515
it 'returns an array containing the A-Z and a-z range when using :mixed' do
1616
range = [*'A'..'Z', *'a'..'z']
17-
expect(Wpxf::Utility::Text.alpha_ranges(:mixed)).to eq range
17+
expect(described_class.alpha_ranges(:mixed)).to eq range
1818
end
1919
end
2020

2121
describe '.rand_alpha' do
2222
it 'returns a string of the specified length' do
23-
val = Wpxf::Utility::Text.rand_alpha(10)
23+
val = described_class.rand_alpha(10)
2424
expect(val.length).to eq 10
2525
end
2626

2727
it 'returns a lower case string when :lower is specified as the casing' do
28-
val = Wpxf::Utility::Text.rand_alpha(10, :lower)
28+
val = described_class.rand_alpha(10, :lower)
2929
expect(val).to match(/^[a-z]{10}$/)
3030
end
3131

3232
it 'returns an upper case string when :upper is specified as the casing' do
33-
val = Wpxf::Utility::Text.rand_alpha(10, :upper)
33+
val = described_class.rand_alpha(10, :upper)
3434
expect(val).to match(/^[A-Z]{10}$/)
3535
end
3636

3737
it 'returns a mixed case string when :mixed is specified as the casing' do
38-
val = Wpxf::Utility::Text.rand_alpha(10, :mixed)
38+
val = described_class.rand_alpha(10, :mixed)
3939
expect(val).to match(/^[a-zA-Z]{10}$/)
4040
end
4141

4242
it 'returns a mixed case string when no casing arg is specified' do
43-
val = Wpxf::Utility::Text.rand_alpha(10)
43+
val = described_class.rand_alpha(10)
4444
expect(val).to match(/^[a-zA-Z]{10}$/)
4545
end
4646
end
4747

4848
describe '.rand_alphanumeric' do
4949
it 'returns a string of the specified length' do
50-
val = Wpxf::Utility::Text.rand_alphanumeric(10)
50+
val = described_class.rand_alphanumeric(10)
5151
expect(val.length).to eq 10
5252
end
5353

5454
it 'returns a lower case string when :lower is specified as the casing' do
55-
val = Wpxf::Utility::Text.rand_alphanumeric(10, :lower)
55+
val = described_class.rand_alphanumeric(10, :lower)
5656
expect(val).to match(/^[a-z0-9]{10}$/)
5757
end
5858

5959
it 'returns an upper case string when :upper is specified as the casing' do
60-
val = Wpxf::Utility::Text.rand_alphanumeric(10, :upper)
60+
val = described_class.rand_alphanumeric(10, :upper)
6161
expect(val).to match(/^[A-Z0-9]{10}$/)
6262
end
6363

6464
it 'returns a mixed case string when :mixed is specified as the casing' do
65-
val = Wpxf::Utility::Text.rand_alphanumeric(10, :mixed)
65+
val = described_class.rand_alphanumeric(10, :mixed)
6666
expect(val).to match(/^[a-zA-Z0-9]{10}$/)
6767
end
6868

6969
it 'returns a mixed case string when no casing arg is specified' do
70-
val = Wpxf::Utility::Text.rand_alphanumeric(10)
70+
val = described_class.rand_alphanumeric(10)
7171
expect(val).to match(/^[a-zA-Z0-9]{10}$/)
7272
end
7373
end
7474

7575
describe '.rand_numeric' do
7676
it 'returns a string of the specified length' do
77-
val = Wpxf::Utility::Text.rand_numeric(10)
77+
val = described_class.rand_numeric(10)
7878
expect(val.length).to eq 10
7979
end
8080

8181
it 'returns a numeric string' do
82-
val = Wpxf::Utility::Text.rand_numeric(10)
82+
val = described_class.rand_numeric(10)
8383
expect(val).to match(/^[0-9]{10}$/)
8484
end
8585

8686
context 'when allow_leading_zero is set to false' do
8787
it 'returns a value that does not start with zero' do
8888
1000.times do
89-
val = Wpxf::Utility::Text.rand_numeric(3, false)
89+
val = described_class.rand_numeric(3, false)
9090
expect(val[0]).to_not eq '0'
9191
end
9292
end
@@ -95,20 +95,27 @@
9595

9696
describe '.md5' do
9797
it 'returns a hexadecimal representation of the md5 digest' do
98-
hash = Wpxf::Utility::Text.md5('test')
98+
hash = described_class.md5('test')
9999
expect(hash).to match(/^[a-f0-9]{32}$/i)
100100
end
101101

102102
it 'returns an md5 hash' do
103-
hash = Wpxf::Utility::Text.md5('test')
103+
hash = described_class.md5('test')
104104
expect(hash).to eq '098f6bcd4621d373cade4e832627b4f6'
105105
end
106106
end
107107

108108
describe '.rand_email' do
109109
it 'returns an address for a .com domain' do
110-
email = Wpxf::Utility::Text.rand_email
110+
email = described_class.rand_email
111111
expect(email).to match(/^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.com$/)
112112
end
113113
end
114+
115+
describe '.hexify_string' do
116+
it 'returns a hexadecimal representation of the string' do
117+
hex_value = described_class.hexify_string('test')
118+
expect(hex_value).to eq '74657374'
119+
end
120+
end
114121
end

0 commit comments

Comments
 (0)