Skip to content

Commit

Permalink
Add leading underscore option (#43)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
dgaussr and sindresorhus committed Feb 11, 2020
1 parent 8a67581 commit 9a78d8f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
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.

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');
});

0 comments on commit 9a78d8f

Please sign in to comment.