Skip to content

Commit

Permalink
Merge branch 'E1337Kat-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
infertux committed Sep 27, 2023
2 parents c4f173f + 4edf594 commit a65e7d4
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 163 deletions.
21 changes: 3 additions & 18 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,17 @@ AllCops:
TargetRubyVersion: 3.0
NewCops: enable

Gemspec/DevelopmentDependencies:
Enabled: false

Metrics:
Enabled: false

Layout/AccessModifierIndentation:
EnforcedStyle: outdent

Layout/CaseIndentation:
Enabled: false

Layout/EndAlignment:
Enabled: false

Layout/EmptyLinesAroundArguments:
Enabled: false

Layout/LineLength:
Enabled: false

Style/Alias:
EnforcedStyle: prefer_alias_method

Style/Documentation:
Enabled: false

Style/StringLiterals:
EnforcedStyle: double_quotes

Style/SpecialGlobalVars:
Enabled: false
9 changes: 1 addition & 8 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,4 @@ end
require "rubocop/rake_task"
RuboCop::RakeTask.new

require "cane/rake_task"
Cane::RakeTask.new do |t|
t.no_doc = true
t.style_measure = 120
t.abc_max = 20
end

task default: %i[spec rubocop cane]
task default: %i[spec rubocop]
133 changes: 102 additions & 31 deletions lib/ordinalize_full.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "i18n"

# Main module
module OrdinalizeFull
I18n.load_path += Dir[File.join(__dir__, "ordinalize_full/locales/*.yml")]

Expand Down Expand Up @@ -35,7 +36,11 @@ def ordinalize_in_full(gender: :masculine, plurality: :singular)

value
else
I18n.t("ordinalize_full.n_#{self}", throw: true)
begin
integer_to_long_form_ordinal
rescue ArgumentError
I18n.t("ordinalize_full.n_#{self}", throw: true)
end
end
rescue ArgumentError
raise NotImplementedError, "Unknown locale #{I18n.locale}"
Expand All @@ -47,40 +52,106 @@ def ordinalize_in_full(gender: :masculine, plurality: :singular)

def ordinalize_in_short(gender: :masculine, plurality: :singular)
abs_number = to_i.abs
suffix = case I18n.locale
when :en
if (11..13).cover?(abs_number % 100)
"th"
else
case abs_number % 10
when 1 then "st"
when 2 then "nd"
when 3 then "rd"
else "th"
suffix = \
case I18n.locale
when :en
if (11..13).cover?(abs_number % 100)
"th"
else
case abs_number % 10
when 1 then "st"
when 2 then "nd"
when 3 then "rd"
else "th"
end
end
when :fr
self == 1 ? "er" : "ème"
when :it
"°"
when :nl
[8, 1, 0].include?(self % 100) || self % 100 > 19 ? "ste" : "de"
when :es
value = ordinalize_in_full(gender: gender, plurality: plurality)

if value.end_with?("er")
".ᵉʳ"
elsif value.end_with?("a")
".ᵃ"
elsif value.end_with?("o")
".ᵒ"
elsif value.end_with?("os")
".ᵒˢ"
elsif value.end_with?("as")
".ᵃˢ"
end
end
when :fr
self == 1 ? "er" : "ème"
when :it
"°"
when :nl
[8, 1, 0].include?(self % 100) || self % 100 > 19 ? "ste" : "de"
when :es
value = ordinalize_in_full(gender: gender, plurality: plurality)

if value.end_with?("er")
".ᵉʳ"
elsif value.end_with?("a")
".ᵃ"
elsif value.end_with?("o")
".ᵒ"
elsif value.end_with?("os")
".ᵒˢ"
elsif value.end_with?("as")
".ᵃˢ"

[self, suffix].join
end

# Build the long form of a given number. In this context, it is used for every digit except the last two.
# @param number [Integer] - A number to write in long form
# @return [String] - The long form of a number
def number_to_word(number)
if number.zero?
I18n.t("long_form.simple.ones.n_0", throw: true)
elsif number < 20
I18n.t("long_form.simple.ones.n_#{number}", throw: true)
elsif number < 100
[
I18n.t("long_form.simple.tens.n_#{number / 10}", throw: true),
(number % 10).zero? ? "" : "-#{I18n.t("long_form.simple.ones.n_#{number % 10}", throw: true)}"
].join
elsif number < 1000
[
I18n.t("long_form.simple.ones.n_#{number / 100}", throw: true),
" ",
I18n.t("long_form.simple.hundred", throw: true),
(number % 100).zero? ? "" : " and #{number_to_word(number % 100)}"
].join
else
i = 0
meow = number
while meow >= 1000
meow /= 1000
i += 1
end
the_one_tenth = number % ((10**(3 * i)))
[
number_to_word(meow),
" ",
i.positive? ? I18n.t("long_form.simple.larger.n_#{i}", throw: true) : "",
the_one_tenth.zero? ? "" : " #{number_to_word(the_one_tenth)}"
].join
end
end

[self, suffix].join
# Builds the ordinalized version of a number. Specific to english. Really only the two least significant digits are
# ordinalized, and the rest are just in long format
# @param number [Integer] - A number to ordinalize in long form
# @return [String] - The long form of a number as an ordinal
def number_to_ordinal_word(number)
if number.zero?
I18n.t("long_form.ordinal.ones.n_0", throw: true)
elsif number < 20
I18n.t("long_form.ordinal.ones.n_#{number}", throw: true)
elsif number < 100 && (number % 10).zero?
I18n.t("long_form.ordinal.tens.n_#{number / 10}", throw: true)
elsif number < 100
"#{number_to_word(number - (number % 10))}-#{I18n.t("long_form.ordinal.ones.n_#{number % 10}", throw: true)}"
elsif number >= 100 && (number % 100).zero?
"#{number_to_word(number - (number % 100))}th"
else
"#{number_to_word(number - (number % 100))} and #{number_to_ordinal_word((number % 100))}"
end
end

# Builds the long form ordinal of an integer
# @return [String] - The long form ordinal of an integer (including negative)
def integer_to_long_form_ordinal
return "#{I18n.t('long_form.negative', throw: true)} #{number_to_ordinal_word(-self)}" if negative?

number_to_ordinal_word(self)
end
end
172 changes: 71 additions & 101 deletions lib/ordinalize_full/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,102 +1,72 @@
en:
ordinalize_full:
n_1: first
n_2: second
n_3: third
n_4: fourth
n_5: fifth
n_6: sixth
n_7: seventh
n_8: eighth
n_9: ninth
n_10: tenth
n_11: eleventh
n_12: twelfth
n_13: thirteenth
n_14: fourteenth
n_15: fifteenth
n_16: sixteenth
n_17: seventeenth
n_18: eighteenth
n_19: nineteenth
n_20: twentieth
n_21: twenty first
n_22: twenty second
n_23: twenty third
n_24: twenty fourth
n_25: twenty fifth
n_26: twenty sixth
n_27: twenty seventh
n_28: twenty eighth
n_29: twenty ninth
n_30: thirtieth
n_31: thirty first
n_32: thirty second
n_33: thirty third
n_34: thirty fourth
n_35: thirty fifth
n_36: thirty sixth
n_37: thirty seventh
n_38: thirty eighth
n_39: thirty ninth
n_40: fortieth
n_41: forty first
n_42: forty second
n_43: forty third
n_44: forty fourth
n_45: forty fifth
n_46: forty sixth
n_47: forty seventh
n_48: forty eighth
n_49: forty ninth
n_50: fiftieth
n_51: fifty first
n_52: fifty second
n_53: fifty third
n_54: fifty fourth
n_55: fifty fifth
n_56: fifty sixth
n_57: fifty seventh
n_58: fifty eighth
n_59: fifty ninth
n_60: sixtieth
n_61: sixty first
n_62: sixty second
n_63: sixty third
n_64: sixty fourth
n_65: sixty fifth
n_66: sixty sixth
n_67: sixty seventh
n_68: sixty eighth
n_69: sixty ninth
n_70: seventieth
n_71: seventy first
n_72: seventy second
n_73: seventy third
n_74: seventy fourth
n_75: seventy fifth
n_76: seventy sixth
n_77: seventy seventh
n_78: seventy eighth
n_79: seventy ninth
n_80: eightieth
n_81: eighty first
n_82: eighty second
n_83: eighty third
n_84: eighty fourth
n_85: eighty fifth
n_86: eighty sixth
n_87: eighty seventh
n_88: eighty eighth
n_89: eighty ninth
n_90: ninetieth
n_91: ninety first
n_92: ninety second
n_93: ninety third
n_94: ninety fourth
n_95: ninety fifth
n_96: ninety sixth
n_97: ninety seventh
n_98: ninety eighth
n_99: ninety ninth
n_100: one hundredth
long_form:
negative: negative
simple:
ones:
n_0: zero
n_1: one
n_2: two
n_3: three
n_4: four
n_5: five
n_6: six
n_7: seven
n_8: eight
n_9: nine
n_10: ten
n_11: eleven
n_12: twelve
n_13: thirteen
n_14: fourteen
n_15: fifteen
n_16: sixteen
n_17: seventeen
n_18: eighteen
n_19: nineteen
tens:
n_2: twenty
n_3: thirty
n_4: forty
n_5: fifty
n_6: sixty
n_7: seventy
n_8: eighty
n_9: ninety
hundred: hundred
larger:
n_0: ""
n_1: thousand
n_2: million
n_3: billion
n_4: trillion
ordinal:
ones:
n_0: zeroth
n_1: first
n_2: second
n_3: third
n_4: fourth
n_5: fifth
n_6: sixth
n_7: seventh
n_8: eighth
n_9: ninth
n_10: tenth
n_11: eleventh
n_12: twelfth
n_13: thirteenth
n_14: fourteenth
n_15: fifteenth
n_16: sixteenth
n_17: seventeenth
n_18: eigthteenth
n_19: nineteenth
tens:
n_2: twentieth
n_3: thirtieth
n_4: fortieth
n_5: fiftieth
n_6: sixtieth
n_7: seventieth
n_8: eightieth
n_9: ninetieth
3 changes: 1 addition & 2 deletions ordinalize_full.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
spec.homepage = "https://github.com/infertux/ordinalize_full"
spec.license = "MIT"

spec.files = `git ls-files`.split($/)
spec.files = `git ls-files`.split($/) # rubocop:disable Style/SpecialGlobalVars
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

Expand All @@ -22,7 +22,6 @@ Gem::Specification.new do |spec|
spec.add_dependency "i18n", "~> 1.8"

spec.add_development_dependency "bundler", ">= 1.5"
spec.add_development_dependency "cane"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec", "~> 3"
spec.add_development_dependency "rubocop"
Expand Down
Loading

0 comments on commit a65e7d4

Please sign in to comment.