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

Camel Case Support #127

Open
FabianoLothor opened this issue Jul 23, 2021 · 4 comments
Open

Camel Case Support #127

FabianoLothor opened this issue Jul 23, 2021 · 4 comments

Comments

@FabianoLothor
Copy link

FabianoLothor commented Jul 23, 2021

Is there a way to convert FooBarBaz to foo-bar-baz?

Would be great if we've this option.

Note that Foo BarBaz should generate foo-bar-baz instead of foo--bar-baz.

@Trott
Copy link
Collaborator

Trott commented Jul 23, 2021

What's the use case for this?

If you only need to worry about /[A-Z]/ and not other alphabets, you can use .replace(/([A-Z])/g, '$&-') on the string like this:

const slugify = require('slugify')
const options = { lower: true }

function getSlug(str) {
  return slugify(str.replace(/[A-Z]/g, '-$&'), options)
}

console.log(getSlug('FooBarBaz')) // 'foo-bar-baz'
console.log(getSlug('Foo BarBaz')) // also 'foo-bar-baz'

@FabianoLothor
Copy link
Author

FabianoLothor commented Jul 23, 2021

I'm doing something similar, my question was to know if the lib already has a solution for it and/or if it's a good feature to have.

What's the use case for this?

Twitter #HashTags can be converted to great slugs for example.

@Trott
Copy link
Collaborator

Trott commented Jul 23, 2021

my question was to know if the lib already has a solution for it

There's not anything like a simple option to set at this time.

and/or if it's a good feature to have.

Perhaps, but I'd personally be cautious about adding such a feature because:

  • For simple use cases involving just a few capital letter possibilities, it seems like something that can be done fairly easily by the user of the library. (See example in my previous comment.)
  • For more complex use cases that involve identifying capital letters across many different alphabets, it seems like it might get problematic very fast. For example, the Arabic alphabet does not have capital letters. But letters at the beginning of words do differ in shape from letters in the middle which in turn differ from letters at the end of words. What's the right behavior there? I don't think there's an obvious answer.
  • For the use case mentioned (Twitter hashtags) there will be lots of potentially unsolvable issues. For example, I see a popular hashtag right now of #COVID19nsw (about the record COVID cases reported in New South Wales). That one is going to end up c-o-v-i-d19nsw which is actually less readable than the hashtag. Which leads to the next point...
  • ...which is that hashtags are already readable, or at least not something whose readability is likely to be improved by slugification. That doesn't mean the use case is necessarily invalid, but it does mean that...
  • ...this use case is a bit of a niche use case. I think this is the first time I've heard it suggested.

Of course, it's possible I'm missing things, like additional use cases that might argue strongly in favor of a feature like this.

@FabianoLothor
Copy link
Author

Sure thing Trott, certainly is something that should be more investigate.

But my proposal isn't that the library use this as default, but as a config that we can enable/disable when/if needed.

In cases like your example #COVID19nsw, My expectation is that "letters that are capitalized together should be ignored", so, the result should be: covid-19-nsw.

About the languages that don't use A-Z alphabet can really be very tricky. I don't know its rules.

They could be ignored initially, until the contributors decide when/how the feature should be applied.

Anyway, I'm fine to use a separate code do handle these cases, but would be great to have this here as an option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants