string-utility 2.5.1 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7880b3c35d659af05a90be338b24fcaba8120fea
4
- data.tar.gz: 4dad1d4406a7da5cedd1a92c81f1daadff07b117
3
+ metadata.gz: e3df9b4d5a7705d8d2118adba5a44523e53beb16
4
+ data.tar.gz: 8ba47c57448e59c907f3ed98a46543f3774a89be
5
5
  SHA512:
6
- metadata.gz: c1e532a1139acf27729507682924604722b6964887bc7014fbb9eb8d4f3c913821dd16df9eb7a92493047cd6e3239a1e128d95fdd3d40e2c4cde5ad45714355c
7
- data.tar.gz: c49251f194755f8789342d2b378c354ef99d60050b6fd3ffd265bd25da1860df2d410a7a7bba5c539d8e41fd3348f58113810cce45a598388a06e6e78f784127
6
+ metadata.gz: 461e859617e13218267d078835893f6f8bb9b8a1261f9c004870dd3b2a70f746bc505abd806f172fbb3e1744c137f518e37680efa32c14922db4272e019b554d
7
+ data.tar.gz: 89b404fa66cc1d78b69603adfa0991f5746f38cf40c426293926b95d4c3fa14e688c089a7e915fd3c94a8e364db53df560aa64ca427f758c8c070fb24814680c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
  ## Version 2
3
+ ### Version 2.6.0
4
+ * Improve performance.
5
+ * Add specs.
6
+
3
7
  ### Version 2.5.1
4
8
  * Docs for the new color methods
5
9
 
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 = string.reverse
14
- array = string.scan(/.{1,#{count}}/)
15
- string = array.join(separator)
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
- string = self
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
- string = self
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
- string = self
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
- read = File.readlines(path)
54
- read[rand(read.size)].chomp
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.5.1
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-10-27 00:00:00.000000000 Z
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: