Skip to content
Open
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
4 changes: 2 additions & 2 deletions lib/exflect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule Exflect do
match_style = Keyword.get(opts, :match_style, false)

cond do
check && Exflect.Detect.singular?(text) -> text
check && (Exflect.Shared.uncountable?(text) || Exflect.Detect.singular?(text)) -> text
true -> unchecked_singularize(text, match_style)
end
end
Expand Down Expand Up @@ -104,7 +104,7 @@ defmodule Exflect do
match_style = Keyword.get(opts, :match_style, false)

cond do
check && Exflect.Detect.plural?(text) -> text
check && (Exflect.Shared.uncountable?(text) || Exflect.Detect.plural?(text)) -> text
true -> unchecked_pluralize(text, match_style)
end
end
Expand Down
21 changes: 19 additions & 2 deletions lib/exflect/detect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ defmodule Exflect.Detect do
~r/[^aouie]s$/
]

def singular?(word), do: Enum.any?(@sg, &Regex.match?(&1, word)) || !plural?(word)
def plural?(word), do: Enum.any?(@pl, &Regex.match?(&1, word))
@singular_exceptions Exflect.Shared.exceptions(:singularize)
@plural_exceptions Exflect.Shared.exceptions(:pluralize)

for {word, _} <- @plural_exceptions do
def singular?(unquote(word)), do: true
end

def singular?(word) do
Enum.any?(@sg, &Regex.match?(&1, word)) ||
!plural?(word)
end

for {word, _} <- @singular_exceptions do
def plural?(unquote(word)), do: true
end

def plural?(word) do
Enum.any?(@pl, &Regex.match?(&1, word))
end
end
12 changes: 12 additions & 0 deletions test/exflect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ defmodule ExflectTest do
assert(equal?(Exflect.singularize("bus", check: true), "bus"))
end

test "exceptions" do
assert(Exflect.singular?("child"))
refute(Exflect.singular?("children"))
assert(Exflect.plural?("children"))
refute(Exflect.plural?("child"))

assert(equal?(Exflect.pluralize("children", check: true), "children"))
assert(equal?(Exflect.singularize("children", check: true), "child"))
assert(equal?(Exflect.singularize("child", check: true), "child"))
assert(equal?(Exflect.pluralize("child", check: true), "children"))
end

@tag :benchmark
test "benchmark" do
%{
Expand Down