ICanBoogie/Inflector

Hyphenate expectations

Closed this issue · 4 comments

I don't want to consider this a bug, but more an initial talking point for discussion. Given that hyphenate is described as a combination of underscore and dasherize, this may affect other expectations.

Firstly, it would seem to me that hyphenate should do more than simply insert hyphens on case differences, which is all it appears to do at current. If I want to hyphenate the string "I expect more than 2 hyphens here", I'd think the result should be more like "i-expect-more-than-2-hyphens-here" -- what really happens is essentially nothing without some initial prep work.

For starters, I need to ucwords. Secondly, I need to then strip spaces (and perhaps many more characters). Lastly, even once I do that, I still won't get a hyphen between the "than" and the "2" because 2 doesn't seem to be recognized in the same way a capital letter would be.

I'd be happy to submit a pull request to improve this with respect to English, but I wanted to confirm that the idea as to what "hyphenate" should be is shared before wasting my time and yours.

Hi Mattsah,

I open to improving hyphenate(), its current behavior is inherited from https://github.com/davidcelis/inflections. What do you think about these examples?

String Hyphenate
Aaa Bbb aaa-bbb
Aaa-Bbb aaa-bbb
Aaa_Bbb aaa-bbb
AaaBbb aaa-bbb
Aaa123 aaa123
Aaa 123 aaa-123

That would certainly be more in line with my expectations.

I think my bigger concern though is that the README says hyphenate is a combinaton of underscore and dasherize -- does this mean underscore has a similar pattern as current? Should that change... can it change, or are there backwards compatibility breaks to be concerned with here?

With the current implementation I get the following results:

hyphenate:
aaa bbb
aaa-bbb
aaa-bbb
aaa-bbb
aaa123
aaa 123

underscore:
aaa bbb
aaa_bbb
aaa_bbb
aaa_bbb
aaa123
aaa 123

I think an update on underscore() to change spaces into underscores is in order. Looks like a bug to me. What do you think?

I've gone ahead and made underscore handle spaces. Additionally, I made it look for 1 or more repeating instances of a space (or the original dash) to consolidate them. See Pull Request #23