Skip to content

Commit

Permalink
Change method name to stay consistent and handle string number input
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Dec 17, 2019
1 parent 28ffe2b commit 719d21a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
19 changes: 15 additions & 4 deletions lib/strings/numeral.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def cardinalize(num, **options)
# @api public
def ordinalize(num, **options)
if options[:short]
num.to_s + short_ordinalize(num)
num.to_s + ordinalize_short(num)
else
decimals = (num.to_i.abs != num.to_f.abs)
sentence = convert_numeral(num, **options)
Expand Down Expand Up @@ -227,13 +227,24 @@ def ordinalize(num, **options)
alias :ordinalise :ordinalize
module_function :ordinalise

def short_ordinalize(num, short: false)
num_abs = num.abs
# Convert a number to a short ordinal form
#
# @example
# ordinalize_short(123) # => 123rd
#
# @param [Numeric, String] num
# the number to convert
#
# @return [String]
#
# @api private
def ordinalize_short(num)
num_abs = num.to_i.abs

CARDINAL_TO_SHORT_ORDINAL[num_abs % 100] ||
CARDINAL_TO_SHORT_ORDINAL[num_abs % 10]
end
module_function :short_ordinalize
module_function :ordinalize_short

# Convert a number into a numeral
#
Expand Down
3 changes: 2 additions & 1 deletion spec/unit/ordinalize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
123 => "123rd",
167 => "167th",
457 => "457th",
-23 => "-23rd"
-23 => "-23rd",
"125" => "125th"
}.each do |num, word|
it "ordinalizes #{num.inspect} to short #{word.inspect}" do
expect(Strings::Numeral.ordinalize(num, short: true)).to eq(word)
Expand Down

0 comments on commit 719d21a

Please sign in to comment.