-
-
Notifications
You must be signed in to change notification settings - Fork 77
Add leading underscore option #43
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
Add leading underscore option #43
Conversation
You need to write in the PR description what issue the PR fixes in the form: |
test.js
Outdated
|
||
test('leading underscore', t => { | ||
t.is(slugify('_foo bar', {leadingUnderscore: true}), '_foo-bar'); | ||
t.is(slugify('_foo_bar', {leadingUnderscore: true}), '_foo-bar'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test for __foo__bar
? (Multiple leading underscores).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok perect
readme.md
Outdated
@@ -132,6 +132,23 @@ slugify('foo@unicorn', { | |||
//=> 'foo-at-unicorn' | |||
``` | |||
|
|||
##### customReplacements |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
readme.md
Outdated
Type: `boolean`\ | ||
Default: `false` | ||
|
||
Allow leading underscore as an escape `_foo_bar` → `_foo-bar`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not really an "escape". I think it would be more correct to say that it preserves leading underscore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm understood gonna change it today!
readme.md
Outdated
slugify('_foo_bar'); | ||
//=> 'foo-bar' | ||
|
||
slugify('_foo_bar', {leadingUnderscore: true}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it's verbose, but I think this would be clearer:
slugify('_foo_bar', {leadingUnderscore: true}); | |
slugify('_foo_bar', {preserveLeadingUnderscore: true}); |
You also need to document it in index.d.ts. |
test.js
Outdated
t.is(slugify('_foo_bar', {leadingUnderscore: true}), '_foo-bar'); | ||
t.is(slugify('_foo bar', {preserveLeadingUnderscore: true}), '_foo-bar'); | ||
t.is(slugify('_foo_bar', {preserveLeadingUnderscore: true}), '_foo-bar'); | ||
t.is(slugify('__foo__bar', {preserveLeadingUnderscore: true}), '_-foo-bar'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should result in:
t.is(slugify('__foo__bar', {preserveLeadingUnderscore: true}), '_-foo-bar'); | |
t.is(slugify('__foo__bar', {preserveLeadingUnderscore: true}), '_foo-bar'); |
Would also be good to add a test for slugify('____-___foo__bar', {preserveLeadingUnderscore: true})
, which should also result in _foo-bar
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was testin and after the
string = string.replace(patternSlug, separator);
in all these examples it became _\-foo\-bar.
So, I thought in just slicing the separator (\-) after it. Something like:
if(string.slice(1,2) == separator){
string = string.slice(0, 1) + string.slice(2, string.length);
}
it works, but what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're unnecessarily complicating the implementation. You could just do:
const shouldPrependUnderscore = options.preserveLeadingUnderscore && string.startsWith('_');
at the start and then prepend a underscore at the end if shouldPrependUnderscore
is true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually thought that way firstly, but i thought would be better to just modify the current regex and got confused afterall
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just committed the changes, sindre.
readme.md
Outdated
|
||
Type: `boolean`\ | ||
Default: `false` | ||
|
||
Allow leading underscore as an escape `_foo_bar` → `_foo-bar`. | ||
It preserves leading underscore: `_foo_bar` → `_foo-bar`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be better written as:
It preserves leading underscore: `_foo_bar` → `_foo-bar`. | |
If your string starts with an underscore, it will be preserved in the slugified string. |
The description, not title. |
Type: `boolean`\ | ||
Default: `false` | ||
|
||
If your string starts with an underscore, it will be preserved in the slugified string. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to keep the readme in sync with the index.d.ts docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would also be useful to include a use-case for this option, as mentioned by the original issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have just added an example as a hidden_filename to give this idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I should have been clearer, by use-case, I meant a written use-case, not a code example. I don't think _hidden-filename
clarifies much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. and if I just update the description to:
"If your string starts with an underscore, it will be preserved in the slugified string. Used in filenames to represent a hidden path."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't think that's very clear.
You could almost just copy-paste the comment from the original issue:
Sometimes leading underscores are intentional, for example, filenames representing hidden paths on a website.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, perfect! done that!
index.js
Outdated
@@ -64,10 +65,16 @@ const slugify = (string, options) => { | |||
patternSlug = /[^a-z\d]+/g; | |||
} | |||
|
|||
const shouldPrependUnderscore = options.preserveLeadingUnderscore && string.startsWith('_'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check should be done before any string transformations are done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed! Done.
Thanks :) |
[Fixes #39]
I created a new leading underscore parameter for the function as false at default. But, when it's true, it changes the current regexp to accept the leading underscore:
Example: