strings-inflection icon indicating copy to clipboard operation
strings-inflection copied to clipboard

singularize Returns Incorrect Values

Open sshaw opened this issue 5 years ago • 2 comments
trafficstars

irb [2.3.7] (class2)$ Strings::Inflection.singularize("a")  # expect "a"
=> "on"
irb [2.3.7] (class2)$ Strings::Inflection.singularize("gas")  # expect "gas"
=> "ga"
irb [2.3.7] (class2)$ Strings::Inflection.singularize("gas", term: :verb) # expect "gas"
=> "gasses"
irb [2.3.7] (class2)$ Strings::Inflection.singularize("la")   # expect "la"
=> "lon"
irb [2.3.7] (class2)$ Strings::Inflection.singularize("ala")   # expect "ala"
=> "alon"
irb [2.3.7] (class2)$ Strings::Inflection.singularize("ss")  # expect "s"
=> "s"

Using a lot of .* for regexes. Well at least in the case of "a" this is certainly the problem.

Also not sure if this is a feature or bug:

irb [2.3.7] (class2)$ Strings::Inflection.singularize("cars\ntrucks\nbikes")
=> "car\ntrucks\nbikes"

I think bug but saw you were using \A and $ instead of \A and \z so just asking.

sshaw avatar Apr 12 '20 08:04 sshaw

Thanks for sharing these!

The gem could do with 'tightening up' some regexes and rules to keep such edge cases at bay. I guess the single characters can be dealt with relatively easily. With others, I don't know, tweaking the rules is rather hard. It may be that some of the rules could do with replacing .* for .+. Do you have time to contribute a PR?

Just curious how did you come about these? Are you actively using the gem in your project? Always keen to get feedback.

The intention was for the methods to accept a single word.

The methods could be extended to accept a variable number of nouns/verbs but I would be less inclined to support chunks of text with newline delimiters and such. So this would be my preferred approach:

Strings::Inflection.singularize("cars", "trucks", "bikes")

piotrmurach avatar Apr 14 '20 11:04 piotrmurach

Do you have time to contribute a PR?

I'm not opposed to a good ol' fashioned PR. Feel free to remind me in 2 months if I don't do it!

Just curious how did you come about these? Are you actively using the gem in your project?

Trying, to use it in my project: class2. Currently it uses ActiveSupport for these sorts of things and I'd like to add the word "lightweight" to class2's docs but this would be a programming crime considering it's using ActiveSupport!

The branch with your gems is here.

The intention was for the methods to accept a single word.

I don't want more than 1 I just noticed that your regex was matching on end of line so wasn't sure if it was a bug or a feature.

sshaw avatar Apr 17 '20 04:04 sshaw