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.
- data/lib/content_truncate/version.rb +1 -1
- data/lib/content_truncate.rb +7 -8
- data/test/content_truncate_test.rb +6 -1
- metadata +1 -1
data/lib/content_truncate.rb
CHANGED
@@ -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
|
-
|
19
|
-
if
|
20
|
-
while
|
21
|
-
prev_index,
|
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
|
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
|