string-extra icon indicating copy to clipboard operation
string-extra copied to clipboard

Why dasherize adds a leading dash?

Open sporto opened this issue 7 years ago • 7 comments

underscore "SomeClassName" == "some_class_name"

Doesn't add _ at the beginning.

dasherize "SomeClassName" == "-some-class-name"

Adds leading _.

What is the logic for this. Seems unexpected.

sporto avatar Oct 17 '17 06:10 sporto

I agree this is weird. However this functionality is based on underscore.string, and they actually specify it to work like this in their docs: http://epeli.github.io/underscore.string/#dasherize-string-gt-string

I can see this being useful in their documented use case (special css properties). However I can also see a usecase for what you describe.

Maybe we should change the default behaviour to do what you describe, and add a flag "addInitialDash" or something like that, to keep the initial dash functionality accessible. This will however be an API break, so we'd have to release a major version for this.

@lorenzo what do you think?

jaapz avatar Oct 19 '17 07:10 jaapz

I see, thanks for answering. The use case they show for css properties is very specific. I don't think this library should to that as is not the general behaviour you would expect. If you want a leading dash then is trivial to add a prefix later.

sporto avatar Oct 19 '17 07:10 sporto

@sporto can you send a PR fixing this?

lorenzo avatar Oct 20 '17 09:10 lorenzo

I would also be okay with just removing the initial dash and have the user add it themselves later, however I think we should make sure to mention that when releasing it as a major version

jaapz avatar Oct 20 '17 12:10 jaapz

One potential approach for versioning: add the new behavior as hyphenate or hyphenated (I prefer the former but the latter is consistent with underscored), deprecate dasherize and then remove it in the next major release. Avoids silently breaking any code (dasherize behavior stays the same until it is removed, at which point anyone still using it will get a compile error), and allows use of the new functionality right away without having to immediately do a major version bump.

ianmackenzie avatar Oct 20 '17 13:10 ianmackenzie

@lorenzo I'll be happy to make a PR.

Re alternative name to avoid major version. Making a major version change seems like a big deal, I struggle with this in my libraries. But since using Elm I'm getting more comfortable with doing major version changes for seemingly small things, but still feels heavy. Up to you.

sporto avatar Oct 21 '17 00:10 sporto

I'm ok with a new major version

lorenzo avatar Oct 23 '17 08:10 lorenzo