Skip to content

Commit c2118b1

Browse files
committed
Explore Cerata.term_length(str)
1 parent f13bb38 commit c2118b1

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

Diff for: lib/cerata.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def table_to_s(a, indent='')
7878
val_widths =
7979
a.inject({}) { |w, h|
8080
h.each { |k, v|
81-
k = k.to_s; w[k] = [ w[k] || 0, v.inspect.length ].max }
81+
k = k.to_s
82+
w[k] = [ w[k] || 0, v.inspect.length ].max }
8283
w }
8384

8485
o = StringIO.new
@@ -106,6 +107,15 @@ def table_to_s(a, indent='')
106107

107108
o.string
108109
end
110+
111+
#def term_length(s)
112+
# s
113+
# .chars
114+
# .inject(0) { |r, c|
115+
# c.match?(/[\p{Han}\p{Hiragana}\p{Katakana}]/) ? r + 2 :
116+
# r + 1 }
117+
#end
118+
#alias tl term_length
109119
end
110120
end
111121

Diff for: test/cerata_test.rb

+24
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@
5353
{:name=>"Castafiore", :fun=>"Diva", :age=>59, :id=>123, :height=>163},
5454
{:name=>"Milou", :fun=>"Dog", :age=>3, :id=>12, :height=>20}])
5555
end
56+
57+
_test 'formats ok with Kanji' do
58+
59+
ah = [
60+
{ name: 'Haddock', fun: '船長', age: 52 },
61+
{ name: 'ティンティン', fun: 'Reporter', age: 31, weight: 60 } ]
62+
ahs = Cerata.table_to_s(ah)
63+
puts ahs
64+
end
65+
end
66+
67+
group '.term_length' do
68+
69+
{ '' => 0,
70+
'012345' => 6,
71+
'定石 b' => 6,
72+
73+
}.each do |k, v|
74+
75+
_test "returns #{v} for #{k.inspect}" do
76+
77+
assert_equal Cerata.term_length(k), v
78+
end
79+
end
5680
end
5781
end
5882

0 commit comments

Comments
 (0)