Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overwriting existing plurals, singulars, and irregulars #18

Merged
merged 2 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions spec/inflector_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ require "./support/inflector_test_cases"
include InflectorTestCases

describe Wordsmith::Inflector do
before_each do
Wordsmith::Inflector.inflections.clear
Wordsmith.load
end

describe "pluralize" do
SingularToPlural.each do |singular, plural|
it "should pluralize #{singular}" do
Expand All @@ -25,6 +30,12 @@ describe Wordsmith::Inflector do
Wordsmith::Inflector.pluralize(plural.capitalize).should eq plural.capitalize
end
end

it "is overwriteable" do
Wordsmith::Inflector.inflections.plural("person", "people")
Wordsmith::Inflector.inflections.plural("person", "persons")
Wordsmith::Inflector.pluralize("person").should eq("persons")
end
end

describe "singular" do
Expand All @@ -41,6 +52,11 @@ describe Wordsmith::Inflector do
Wordsmith::Inflector.singularize(singular.capitalize).should eq singular.capitalize
end
end

it "is overwriteable" do
Wordsmith::Inflector.inflections.singular("persons", "person")
Wordsmith::Inflector.singularize("persons").should eq("person")
end
end

describe "camelize" do
Expand Down Expand Up @@ -277,6 +293,11 @@ describe Wordsmith::Inflector do
Wordsmith::Inflector.singularize(singular).should eq singular
end
end

it "is overwriteable" do
Wordsmith::Inflector.inflections.irregular("person", "persons")
Wordsmith::Inflector.pluralize("person").should eq("persons")
end
end

describe "acronyms" do
Expand Down
116 changes: 60 additions & 56 deletions src/wordsmith/inflections.cr
Original file line number Diff line number Diff line change
@@ -1,63 +1,67 @@
require "./inflector/inflections"

module Wordsmith
Inflector.inflections.plural(/$/, "s")
Inflector.inflections.plural(/s$/i, "s")
Inflector.inflections.plural(/^(ax|test)is$/i, "\\1es")
Inflector.inflections.plural(/(octop|vir)us$/i, "\\1i")
Inflector.inflections.plural(/(octop|vir)i$/i, "\\1i")
Inflector.inflections.plural(/(alias|status)$/i, "\\1es")
Inflector.inflections.plural(/(bu)s$/i, "\\1ses")
Inflector.inflections.plural(/(buffal|tomat)o$/i, "\\1oes")
Inflector.inflections.plural(/([ti])um$/i, "\\1a")
Inflector.inflections.plural(/([ti])a$/i, "\\1a")
Inflector.inflections.plural(/sis$/i, "ses")
Inflector.inflections.plural(/(?:([^f])fe|([lr])f)$/i, "\\1\\2ves")
Inflector.inflections.plural(/(hive)$/i, "\\1s")
Inflector.inflections.plural(/([^aeiouy]|qu)y$/i, "\\1ies")
Inflector.inflections.plural(/(x|ch|ss|sh)$/i, "\\1es")
Inflector.inflections.plural(/(matr|vert|ind)(?:ix|ex)$/i, "\\1ices")
Inflector.inflections.plural(/^(m|l)ouse$/i, "\\1ice")
Inflector.inflections.plural(/^(m|l)ice$/i, "\\1ice")
Inflector.inflections.plural(/^(ox)$/i, "\\1en")
Inflector.inflections.plural(/^(oxen)$/i, "\\1")
Inflector.inflections.plural(/(quiz)$/i, "\\1zes")
def self.load
Inflector.inflections.plural(/$/, "s")
Inflector.inflections.plural(/s$/i, "s")
Inflector.inflections.plural(/^(ax|test)is$/i, "\\1es")
Inflector.inflections.plural(/(octop|vir)us$/i, "\\1i")
Inflector.inflections.plural(/(octop|vir)i$/i, "\\1i")
Inflector.inflections.plural(/(alias|status)$/i, "\\1es")
Inflector.inflections.plural(/(bu)s$/i, "\\1ses")
Inflector.inflections.plural(/(buffal|tomat)o$/i, "\\1oes")
Inflector.inflections.plural(/([ti])um$/i, "\\1a")
Inflector.inflections.plural(/([ti])a$/i, "\\1a")
Inflector.inflections.plural(/sis$/i, "ses")
Inflector.inflections.plural(/(?:([^f])fe|([lr])f)$/i, "\\1\\2ves")
Inflector.inflections.plural(/(hive)$/i, "\\1s")
Inflector.inflections.plural(/([^aeiouy]|qu)y$/i, "\\1ies")
Inflector.inflections.plural(/(x|ch|ss|sh)$/i, "\\1es")
Inflector.inflections.plural(/(matr|vert|ind)(?:ix|ex)$/i, "\\1ices")
Inflector.inflections.plural(/^(m|l)ouse$/i, "\\1ice")
Inflector.inflections.plural(/^(m|l)ice$/i, "\\1ice")
Inflector.inflections.plural(/^(ox)$/i, "\\1en")
Inflector.inflections.plural(/^(oxen)$/i, "\\1")
Inflector.inflections.plural(/(quiz)$/i, "\\1zes")

Inflector.inflections.singular(/s$/i, "")
Inflector.inflections.singular(/(ss)$/i, "\\1")
Inflector.inflections.singular(/(n)ews$/i, "\\1ews")
Inflector.inflections.singular(/([ti])a$/i, "\\1um")
Inflector.inflections.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/i, "\\1sis")
Inflector.inflections.singular(/(^analy)(sis|ses)$/i, "\\1sis")
Inflector.inflections.singular(/([^f])ves$/i, "\\1fe")
Inflector.inflections.singular(/(hive)s$/i, "\\1")
Inflector.inflections.singular(/(tive)s$/i, "\\1")
Inflector.inflections.singular(/([lr])ves$/i, "\\1f")
Inflector.inflections.singular(/([^aeiouy]|qu)ies$/i, "\\1y")
Inflector.inflections.singular(/(s)eries$/i, "\\1eries")
Inflector.inflections.singular(/(m)ovies$/i, "\\1ovie")
Inflector.inflections.singular(/(x|ch|ss|sh)es$/i, "\\1")
Inflector.inflections.singular(/^(m|l)ice$/i, "\\1ouse")
Inflector.inflections.singular(/(bus)(es)?$/i, "\\1")
Inflector.inflections.singular(/(o)es$/i, "\\1")
Inflector.inflections.singular(/(shoe)s$/i, "\\1")
Inflector.inflections.singular(/(cris|test)(is|es)$/i, "\\1is")
Inflector.inflections.singular(/^(a)x[ie]s$/i, "\\1xis")
Inflector.inflections.singular(/(octop|vir)(us|i)$/i, "\\1us")
Inflector.inflections.singular(/(alias|status)(es)?$/i, "\\1")
Inflector.inflections.singular(/^(ox)en/i, "\\1")
Inflector.inflections.singular(/(vert|ind)ices$/i, "\\1ex")
Inflector.inflections.singular(/(matr)ices$/i, "\\1ix")
Inflector.inflections.singular(/(quiz)zes$/i, "\\1")
Inflector.inflections.singular(/(database)s$/i, "\\1")
Inflector.inflections.singular(/s$/i, "")
Inflector.inflections.singular(/(ss)$/i, "\\1")
Inflector.inflections.singular(/(n)ews$/i, "\\1ews")
Inflector.inflections.singular(/([ti])a$/i, "\\1um")
Inflector.inflections.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/i, "\\1sis")
Inflector.inflections.singular(/(^analy)(sis|ses)$/i, "\\1sis")
Inflector.inflections.singular(/([^f])ves$/i, "\\1fe")
Inflector.inflections.singular(/(hive)s$/i, "\\1")
Inflector.inflections.singular(/(tive)s$/i, "\\1")
Inflector.inflections.singular(/([lr])ves$/i, "\\1f")
Inflector.inflections.singular(/([^aeiouy]|qu)ies$/i, "\\1y")
Inflector.inflections.singular(/(s)eries$/i, "\\1eries")
Inflector.inflections.singular(/(m)ovies$/i, "\\1ovie")
Inflector.inflections.singular(/(x|ch|ss|sh)es$/i, "\\1")
Inflector.inflections.singular(/^(m|l)ice$/i, "\\1ouse")
Inflector.inflections.singular(/(bus)(es)?$/i, "\\1")
Inflector.inflections.singular(/(o)es$/i, "\\1")
Inflector.inflections.singular(/(shoe)s$/i, "\\1")
Inflector.inflections.singular(/(cris|test)(is|es)$/i, "\\1is")
Inflector.inflections.singular(/^(a)x[ie]s$/i, "\\1xis")
Inflector.inflections.singular(/(octop|vir)(us|i)$/i, "\\1us")
Inflector.inflections.singular(/(alias|status)(es)?$/i, "\\1")
Inflector.inflections.singular(/^(ox)en/i, "\\1")
Inflector.inflections.singular(/(vert|ind)ices$/i, "\\1ex")
Inflector.inflections.singular(/(matr)ices$/i, "\\1ix")
Inflector.inflections.singular(/(quiz)zes$/i, "\\1")
Inflector.inflections.singular(/(database)s$/i, "\\1")

Inflector.inflections.irregular("person", "people")
Inflector.inflections.irregular("man", "men")
Inflector.inflections.irregular("human", "humans")
Inflector.inflections.irregular("child", "children")
Inflector.inflections.irregular("sex", "sexes")
Inflector.inflections.irregular("move", "moves")
Inflector.inflections.irregular("zombie", "zombies")
Inflector.inflections.irregular("person", "people")
Inflector.inflections.irregular("man", "men")
Inflector.inflections.irregular("human", "humans")
Inflector.inflections.irregular("child", "children")
Inflector.inflections.irregular("sex", "sexes")
Inflector.inflections.irregular("move", "moves")
Inflector.inflections.irregular("zombie", "zombies")

Inflector.inflections.uncountable(%w(equipment information rice money species series fish sheep jeans police))
Inflector.inflections.uncountable(%w(equipment information rice money species series fish sheep jeans police))
end

load
end
4 changes: 2 additions & 2 deletions src/wordsmith/inflector/inflections.cr
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ module Wordsmith
end
@uncountables.delete(replacement)
new_plural = {rule => replacement}
@plurals = new_plural.merge(@plurals)
@plurals = @plurals.merge(new_plural)
end

# Define a new singularization rule, either using a pattern or string.
Expand All @@ -138,7 +138,7 @@ module Wordsmith
end
@uncountables.delete(replacement)
new_singular = {rule => replacement}
@singulars = new_singular.merge(@singulars)
@singulars = @singulars.merge(new_singular)
end

# Define a new irregular `String` with a direct translation between singular and plural form.
Expand Down
2 changes: 1 addition & 1 deletion src/wordsmith/inflector/methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ module Wordsmith
if result.empty? || inflections.uncountables.uncountable?(result)
result
else
rules.each { |rule, replacement|
rules.to_a.reverse.each { |rule, replacement|
if result.index(rule)
result = result.sub(rule, replacement)
break
Expand Down