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

Add leading underscore option #43

Merged
merged 10 commits into from
Feb 11, 2020

Conversation

dgaussr
Copy link
Contributor

@dgaussr dgaussr commented Jan 27, 2020

[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:

slugify("_foo bar", { leadingUnderscore: true })

Output: _foo-bar

@sindresorhus
Copy link
Owner

You need to write in the PR description what issue the PR fixes in the form: Fixes #123.

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');
Copy link
Owner

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).

Copy link
Contributor Author

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
Copy link
Owner

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`.
Copy link
Owner

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.

Copy link
Contributor Author

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});
Copy link
Owner

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:

Suggested change
slugify('_foo_bar', {leadingUnderscore: true});
slugify('_foo_bar', {preserveLeadingUnderscore: true});

index.js Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

You also need to document it in index.d.ts.

@dgaussr dgaussr changed the title Add leading underscore option Add leading underscore option [Fixes ##39] Feb 1, 2020
@dgaussr dgaussr changed the title Add leading underscore option [Fixes ##39] Add leading underscore option [Fixes #39] Feb 1, 2020
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');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should result in:

Suggested change
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.

Copy link
Contributor Author

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?

Copy link
Owner

@sindresorhus sindresorhus Feb 3, 2020

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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`.
Copy link
Owner

@sindresorhus sindresorhus Feb 1, 2020

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:

Suggested change
It preserves leading underscore: `_foo_bar``_foo-bar`.
If your string starts with an underscore, it will be preserved in the slugified string.

@sindresorhus
Copy link
Owner

You need to write in the PR description what issue the PR fixes in the form: Fixes #123.

The description, not title.

@dgaussr dgaussr changed the title Add leading underscore option [Fixes #39] Add leading underscore option. Feb 3, 2020
Type: `boolean`\
Default: `false`

If your string starts with an underscore, it will be preserved in the slugified string.
Copy link
Owner

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.

Copy link
Owner

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.

Copy link
Contributor Author

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

Copy link
Owner

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.

Copy link
Contributor Author

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."

Copy link
Owner

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.

Copy link
Contributor Author

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('_');
Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed! Done.

@sindresorhus sindresorhus changed the title Add leading underscore option. Add leading underscore option Feb 5, 2020
@sindresorhus sindresorhus merged commit 9a78d8f into sindresorhus:master Feb 11, 2020
@sindresorhus
Copy link
Owner

Thanks :)

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

Successfully merging this pull request may close these issues.

Add an option to support leading underscores
2 participants