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

/**
If your string starts with an underscore, it will be preserved in the slugified string.
Sometimes leading underscores are intentional, for example, filenames representing hidden paths on a website.

@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
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ const slugify = (string, options) => {
lowercase: true,
decamelize: true,
customReplacements: [],
preserveLeadingUnderscore: false,
...options
};

const shouldPrependUnderscore = options.preserveLeadingUnderscore && string.startsWith('_');

const separator = escapeStringRegexp(options.separator);

const customReplacements = new Map([
Expand Down Expand Up @@ -68,6 +71,10 @@ const slugify = (string, options) => {
string = string.replace(/\\/g, '');
string = removeMootSeparators(string, separator);

if (shouldPrependUnderscore) {
string = `_${string}`;
}

return string;
};

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}));
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,25 @@ slugify('foo@unicorn', {
//=> 'foo-at-unicorn'
```

##### preserveLeadingUnderscore

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!


Sometimes leading underscores are intentional, for example, filenames representing hidden paths on a website.

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

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

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

## Related

- [slugify-cli](https://github.com/sindresorhus/slugify-cli) - CLI for this module
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,10 @@ test('supports Turkish', t => {
test('supports Armenian', t => {
t.is(slugify('Ե ր ե ւ ա ն', {lowercase: false, separator: ' '}), 're ye v a n');
});

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