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 preserveTrailingDash option #57

Merged
merged 3 commits into from May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions index.d.ts
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
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
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
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 occurences of the same string.
Expand Down
8 changes: 8 additions & 0 deletions test.js
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-');
Copy link
Owner

Choose a reason for hiding this comment

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

Add some more assertions. For example, foo-bar -, foo-bar , etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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