Skip to content

Commit

Permalink
Add preserveTrailingDash option (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
vhpoet authored May 13, 2021
1 parent 1a3336c commit 2aa5659
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
20 changes: 20 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@ export interface Options {
```
*/
readonly preserveLeadingUnderscore?: boolean;

/**
If your string ends with a dash, it will be preserved in the slugified string.
For example, using slugify on an input field would allow for validation while not preventing the user from writing a slug.
@default false
@example
```
import slugify from '@sindresorhus/slugify';
slugify('foo-bar-');
//=> 'foo-bar'
slugify('foo-bar-', {preserveTrailingDash: true});
//=> 'foo-bar-'
```
*/
readonly preserveTrailingDash?: boolean;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ export default function slugify(string, options) {
decamelize: true,
customReplacements: [],
preserveLeadingUnderscore: false,
preserveTrailingDash: false,
...options
};

const shouldPrependUnderscore = options.preserveLeadingUnderscore && string.startsWith('_');
const shouldAppendDash = options.preserveTrailingDash && string.endsWith('-');

const customReplacements = new Map([
...builtinOverridableReplacements,
Expand Down Expand Up @@ -64,6 +66,10 @@ export default function slugify(string, options) {
string = `_${string}`;
}

if (shouldAppendDash) {
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 @@ -7,6 +7,7 @@ expectType<string>(slugify('Déjà Vu!', {lowercase: false}));
expectType<string>(slugify('fooBar', {decamelize: false}));
expectType<string>(slugify('I ♥ 🦄 & 🐶', {customReplacements: [['🐶', 'dog']]}));
expectType<string>(slugify('_foo_bar', {preserveLeadingUnderscore: true}));
expectType<string>(slugify('foo-bar-', {preserveTrailingDash: true}));

// Counter
expectType<string>(slugifyWithCounter()('I ♥ Dogs'));
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,25 @@ slugify('_foo_bar', {preserveLeadingUnderscore: true});
//=> '_foo-bar'
```

##### preserveTrailingDash

Type: `boolean`\
Default: `false`

If your string ends with a dash, it will be preserved in the slugified string.

For example, using slugify on an input field would allow for validation while not preventing the user from writing a slug.

```js
import slugify from '@sindresorhus/slugify';

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

slugify('foo-bar-', {preserveTrailingDash: true});
//=> 'foo-bar-'
```

### slugifyWithCounter()

Returns a new instance of `slugify(string, options?)` with a counter to handle multiple occurrences of the same string.
Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ test('leading underscore', t => {
t.is(slugify('____-___foo__bar', {preserveLeadingUnderscore: true}), '_foo-bar');
});

test('trailing dash', t => {
t.is(slugify('foo bar-', {preserveTrailingDash: true}), 'foo-bar-');
t.is(slugify('foo-bar--', {preserveTrailingDash: true}), 'foo-bar-');
t.is(slugify('foo-bar -', {preserveTrailingDash: true}), 'foo-bar-');
t.is(slugify('foo-bar - ', {preserveTrailingDash: true}), 'foo-bar');
t.is(slugify('foo-bar ', {preserveTrailingDash: true}), 'foo-bar');
});

test('counter', t => {
const slugify = slugifyWithCounter();
t.is(slugify('foo bar'), 'foo-bar');
Expand Down

0 comments on commit 2aa5659

Please sign in to comment.