Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite9d4dbf

Browse files
committed
Add flags for covnerting count to word numerals
1 parentb58cf98 commite9d4dbf

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

‎lib/strings/inflection/parser.rb‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,32 @@ def parse_verb
6565
# @api private
6666
defparse_count
6767
if@scanner.scan(/\{\{#([^\}]*?):([^\}]+?)\}\}/)
68+
require"strings-numeral"
6869
option=@scanner[1].to_s.tr(" ","").downcase
69-
ifoption =~/[^f]/
70+
ifoption =~/[^fwo\d]/
7071
raise"Unknown option '#{option}' in {{#:...}} tag"
7172
end
72-
amount=caseoption
73-
when"f"
74-
fuzzy_count(@count)
75-
else
76-
@count
77-
end
78-
@value <<amount
73+
@value <<parse_count_flags(option)
74+
end
75+
end
76+
77+
# @api private
78+
defparse_count_flags(option)
79+
caseoption
80+
when->(opt){opt =~/w/ &&opt =~/o/}
81+
Strings::Numeral.ordinalize(@count)
82+
when/w(\d*)/
83+
if $1.empty? ||@count <= $1.to_i
84+
Strings::Numeral.cardinalize(@count)
85+
else
86+
@count
87+
end
88+
when"o"
89+
Strings::Numeral.ordinalize(@count,short:true)
90+
when"f"
91+
fuzzy_count(@count)
92+
else
93+
@count
7994
end
8095
end
8196

‎spec/unit/parser_spec.rb‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,33 @@
5757
Strings::Inflection::Parser.parse(template,2)
5858
}.toraise_error("Unknown option 'u' in {{#:...}} tag")
5959
end
60+
61+
it"counts with number as cardinal words"do
62+
expect(Strings::Inflection::Parser.parse("{{#w: count}}",12)).toeq("twelve")
63+
end
64+
65+
it"counts with number as cardinal words up to the limit"do
66+
expect(Strings::Inflection::Parser.parse("{{#w10: count}}",12)).toeq("12")
67+
end
68+
69+
it"counts with a short ordinal"do
70+
expect(Strings::Inflection::Parser.parse("{{#o: count}}",12)).toeq("12th")
71+
end
72+
73+
it"counts with an ordinal"do
74+
expect(Strings::Inflection::Parser.parse("{{# o w : count}}",12)).toeq("twelfth")
75+
end
6076
end
6177

6278
context"count fuzzy"do
6379
it"allows for space between separator"do
6480
expect(Strings::Inflection::Parser.parse("{{# f : count }}",0)).toeq("no")
6581
end
6682

83+
it"allows for capital letter"do
84+
expect(Strings::Inflection::Parser.parse("{{#F:count}}",0)).toeq("no")
85+
end
86+
6787
it"changes 0 count to no "do
6888
expect(Strings::Inflection::Parser.parse("{{#f:count}}",0)).toeq("no")
6989
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp