string-utility 2.5.1 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/utils.rb +13 -17
- data/spec/benchmark.rb +48 -0
- data/spec/testcases.rb +37 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3df9b4d5a7705d8d2118adba5a44523e53beb16
|
4
|
+
data.tar.gz: 8ba47c57448e59c907f3ed98a46543f3774a89be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 461e859617e13218267d078835893f6f8bb9b8a1261f9c004870dd3b2a70f746bc505abd806f172fbb3e1744c137f518e37680efa32c14922db4272e019b554d
|
7
|
+
data.tar.gz: 89b404fa66cc1d78b69603adfa0991f5746f38cf40c426293926b95d4c3fa14e688c089a7e915fd3c94a8e364db53df560aa64ca427f758c8c070fb24814680c
|
data/CHANGELOG.md
CHANGED
data/lib/utils.rb
CHANGED
@@ -10,37 +10,28 @@ module StringUtility
|
|
10
10
|
def separate(count = 3, separator = ',')
|
11
11
|
separator = separator[0] if separator.length > 1
|
12
12
|
string = self
|
13
|
-
string
|
14
|
-
|
15
|
-
string
|
16
|
-
string = string.reverse
|
17
|
-
|
18
|
-
string
|
13
|
+
string.reverse!
|
14
|
+
string = string.scan(/.{1,#{count}}/).join(separator)
|
15
|
+
string.reverse!
|
19
16
|
end
|
20
17
|
|
21
18
|
# Converts a separated string into an integer. This is basically the reverse
|
22
19
|
# of #separate.
|
23
20
|
# @return [Int] The integer version of the separated string.
|
24
21
|
def to_i_separated
|
25
|
-
|
26
|
-
string = string.gsub(/\D/, '')
|
27
|
-
string.to_i
|
22
|
+
self.gsub!(/\D/, '').to_i
|
28
23
|
end
|
29
24
|
|
30
25
|
# Replaces all whitespace with underscores.
|
31
26
|
# @return [String] The string with replaced whitespace.
|
32
27
|
def underscorify
|
33
|
-
|
34
|
-
string = string.gsub(/\s/, '_')
|
35
|
-
string
|
28
|
+
self.gsub!(/\s/, '_')
|
36
29
|
end
|
37
30
|
|
38
31
|
# Replaces all underscores with whitespace.
|
39
32
|
# @return [String] The string with replaced underscores.
|
40
33
|
def spacify
|
41
|
-
|
42
|
-
string = string.tr('_', ' ')
|
43
|
-
string
|
34
|
+
self.tr!('_', ' ')
|
44
35
|
end
|
45
36
|
end
|
46
37
|
|
@@ -50,8 +41,13 @@ module StringUtility
|
|
50
41
|
# @param path [String] The path to the file.
|
51
42
|
# @return [String] A random line in the file.
|
52
43
|
def self.random_line(path)
|
53
|
-
|
54
|
-
|
44
|
+
file = open(path)
|
45
|
+
selected = nil
|
46
|
+
file.each_with_index do |line, num|
|
47
|
+
selected = line if !line.nil? && rand < 1.0 / num
|
48
|
+
end
|
49
|
+
file.close
|
50
|
+
selected
|
55
51
|
end
|
56
52
|
|
57
53
|
# Gets a random three-digit hexidecimal web color string.
|
data/spec/benchmark.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'benchmark'
|
2
|
+
require_relative '../lib/utils'
|
3
|
+
|
4
|
+
TIMES = 100_000
|
5
|
+
using StringUtility
|
6
|
+
Benchmark.bm do |b|
|
7
|
+
b.report('separate') do
|
8
|
+
TIMES.times do
|
9
|
+
'1000'.separate
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
b.report('to_i_separated') do
|
14
|
+
TIMES.times do
|
15
|
+
'1,000'.to_i_separated
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
b.report('underscorify') do
|
20
|
+
TIMES.times do
|
21
|
+
' '.underscorify
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
b.report('spacify') do
|
26
|
+
TIMES.times do
|
27
|
+
'_____'.spacify
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
b.report('random_line') do
|
32
|
+
TIMES.times do
|
33
|
+
StringUtility.random_line("#{Dir.pwd}/README.md")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
b.report('color 3') do
|
38
|
+
TIMES.times do
|
39
|
+
StringUtility.random_color_three
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
b.report('color 6') do
|
44
|
+
TIMES.times do
|
45
|
+
StringUtility.random_color_six
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/spec/testcases.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require_relative '../lib/utils'
|
3
|
+
|
4
|
+
class TestStringUtility < Test::Unit::TestCase
|
5
|
+
using StringUtility
|
6
|
+
def test_separate
|
7
|
+
assert_equal('1,000', '1000'.separate)
|
8
|
+
assert_equal('10,00', '1000'.separate(2))
|
9
|
+
assert_equal('1:000', '1000'.separate(3, ':'))
|
10
|
+
assert_equal('10:00', '1000'.separate(2, ':'))
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_to_i_sparated
|
14
|
+
assert_equal(1000, '1,000'.to_i_separated)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_underscorify
|
18
|
+
assert_equal('_', ' '.underscorify)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_spacify
|
22
|
+
assert_equal(' ', '_'.spacify)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_random_line
|
26
|
+
assert_instance_of(String, StringUtility.random_line("#{Dir.pwd}/" \
|
27
|
+
'spec/testcases.rb'))
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_random_color_three
|
31
|
+
assert_instance_of(String, StringUtility.random_color_three)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_random_color_six
|
35
|
+
assert_instance_of(String, StringUtility.random_color_six)
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: string-utility
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eli Foster
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: " Some simple but handy methods to interact with string objects.\n"
|
14
14
|
email: elifosterwy@gmail.com
|
@@ -19,6 +19,8 @@ files:
|
|
19
19
|
- CHANGELOG.md
|
20
20
|
- lib/string-utility.rb
|
21
21
|
- lib/utils.rb
|
22
|
+
- spec/benchmark.rb
|
23
|
+
- spec/testcases.rb
|
22
24
|
homepage: https://github.com/elifoster/string-utility-ruby
|
23
25
|
licenses:
|
24
26
|
- CC-BY-NC-ND-4.0
|
@@ -45,3 +47,4 @@ signing_key:
|
|
45
47
|
specification_version: 4
|
46
48
|
summary: Provides some basic utilities for interacting with Strings
|
47
49
|
test_files: []
|
50
|
+
has_rdoc:
|