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
18 changes: 18 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ declare namespace slugify {
```
*/
readonly customReplacements?: ReadonlyArray<[string, string]>;

/**
It preserves leading underscore.

@default false

@example
```
import slugify = require('@sindresorhus/slugify');

slugify('_foo_bar');
//=> 'foo-bar'

slugify('_foo_bar', {preserveLeadingUnderscore: true});
//=> '_foo-bar'
```
*/
readonly preserveLeadingUnderscore?: boolean;
}
}

Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ const slugify = (string, options) => {

options = {
separator: '-',
leadingUnderscore: false,
lowercase: true,
decamelize: true,
customReplacements: [],
preserveLeadingUnderscore: false,
...options
};

Expand All @@ -65,7 +65,7 @@ const slugify = (string, options) => {
patternSlug = /[^a-z\d]+/g;
}

if (options.leadingUnderscore) {
if (options.preserveLeadingUnderscore) {
let patternSlugString = patternSlug.source;
patternSlugString = `(?!^)_?${patternSlugString}`;
patternSlug = new RegExp(patternSlugString, 'g');
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ expectType<string>(slugify('fooBar', {decamelize: false}));
expectType<string>(
slugify('I ♥ 🦄 & 🐶', {customReplacements: [['🐶', 'dog']]})
);
expectType<string>(slugify('_foo_bar', {preserveLeadingUnderscore: true}));
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,20 @@ slugify('foo@unicorn', {
//=> 'foo-at-unicorn'
```

##### customReplacements
##### preserveLeadingUnderscore

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.


```js
const slugify = require('@sindresorhus/slugify');

slugify('_foo_bar');
//=> 'foo-bar'

slugify('_foo_bar', {leadingUnderscore: true});
slugify('_foo_bar', {preserveLeadingUnderscore: true});
//=> '_foo-bar'
```

Expand Down
5 changes: 3 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ test('supports Armenian', t => {
});

test('leading underscore', t => {
t.is(slugify('_foo bar', {leadingUnderscore: true}), '_foo-bar');
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.

});