content_truncate 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module ContentTruncate
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -6,7 +6,7 @@ module ContentTruncate
6
6
  sub_string = self.dup
7
7
  sep = separators.shift
8
8
  if sep
9
- sub_string.send(:smart_truncate, limit_length, sep, separators)
9
+ sub_string.send(:smart_truncate, limit_length, sep, *separators)
10
10
  else
11
11
  sub_string.truncate(limit_length, separator: sep)
12
12
  end
@@ -15,15 +15,14 @@ module ContentTruncate
15
15
  private
16
16
  def smart_truncate limit_length, sep, *separators
17
17
  while sep
18
- index = self.index(sep)
19
- if index <= limit_length
20
- while index && index <= limit_length
21
- prev_index, index = index, self.index(sep, index+1)
18
+ position = self.index(sep)||limit_length+1
19
+ if position <= limit_length
20
+ while position && position <= limit_length
21
+ prev_index, position = position, self.index(sep, position+1)
22
22
  end
23
- return self[0...prev_index].strip
24
- else
25
- sep = separators.shift
23
+ return self[0..(prev_index+sep.length)].strip
26
24
  end
25
+ sep = separators.shift
27
26
  end
28
27
  end
29
28
  end
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'test/unit'
2
3
  require_relative '../lib/content_truncate'
3
4
 
@@ -12,10 +13,14 @@ class ContentTruncateTest < Test::Unit::TestCase
12
13
  end
13
14
 
14
15
  def test_content_truncate_for_one_separator
15
- assert_equal "Truncate a string down to \n x characters.", mock_string.content_truncate(100, "<br/>")
16
+ assert_equal "Truncate a string down to \n x characters. <br/>", mock_string.content_truncate(100, "<br/>")
16
17
  end
17
18
 
18
19
  def test_content_truncate_for_separators
19
20
  assert_equal "Truncate a string down to", mock_string.content_truncate(60, "\n", "<br/>", " ")
20
21
  end
22
+
23
+ def test_content_truncate_for_missing_char
24
+ assert_equal "Truncate a string down to \n x characters. <br/>", mock_string.content_truncate(60, "。", "<br/>")
25
+ end
21
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: content_truncate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: