Skip to content

Commit 2aa5659

Browse files
authoredMay 13, 2021
Add preserveTrailingDash option (#57)
1 parent 1a3336c commit 2aa5659

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed
 

‎index.d.ts

+20
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,26 @@ export interface Options {
112112
```
113113
*/
114114
readonly preserveLeadingUnderscore?: boolean;
115+
116+
/**
117+
If your string ends with a dash, it will be preserved in the slugified string.
118+
119+
For example, using slugify on an input field would allow for validation while not preventing the user from writing a slug.
120+
121+
@default false
122+
123+
@example
124+
```
125+
import slugify from '@sindresorhus/slugify';
126+
127+
slugify('foo-bar-');
128+
//=> 'foo-bar'
129+
130+
slugify('foo-bar-', {preserveTrailingDash: true});
131+
//=> 'foo-bar-'
132+
```
133+
*/
134+
readonly preserveTrailingDash?: boolean;
115135
}
116136

117137
/**

‎index.js

+6
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ export default function slugify(string, options) {
3131
decamelize: true,
3232
customReplacements: [],
3333
preserveLeadingUnderscore: false,
34+
preserveTrailingDash: false,
3435
...options
3536
};
3637

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

3941
const customReplacements = new Map([
4042
...builtinOverridableReplacements,
@@ -64,6 +66,10 @@ export default function slugify(string, options) {
6466
string = `_${string}`;
6567
}
6668

69+
if (shouldAppendDash) {
70+
string = `${string}-`;
71+
}
72+
6773
return string;
6874
}
6975

‎index.test-d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ expectType<string>(slugify('Déjà Vu!', {lowercase: false}));
77
expectType<string>(slugify('fooBar', {decamelize: false}));
88
expectType<string>(slugify('I ♥ 🦄 & 🐶', {customReplacements: [['🐶', 'dog']]}));
99
expectType<string>(slugify('_foo_bar', {preserveLeadingUnderscore: true}));
10+
expectType<string>(slugify('foo-bar-', {preserveTrailingDash: true}));
1011

1112
// Counter
1213
expectType<string>(slugifyWithCounter()('I ♥ Dogs'));

‎readme.md

+19
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,25 @@ slugify('_foo_bar', {preserveLeadingUnderscore: true});
167167
//=> '_foo-bar'
168168
```
169169

170+
##### preserveTrailingDash
171+
172+
Type: `boolean`\
173+
Default: `false`
174+
175+
If your string ends with a dash, it will be preserved in the slugified string.
176+
177+
For example, using slugify on an input field would allow for validation while not preventing the user from writing a slug.
178+
179+
```js
180+
import slugify from '@sindresorhus/slugify';
181+
182+
slugify('foo-bar-');
183+
//=> 'foo-bar'
184+
185+
slugify('foo-bar-', {preserveTrailingDash: true});
186+
//=> 'foo-bar-'
187+
```
188+
170189
### slugifyWithCounter()
171190

172191
Returns a new instance of `slugify(string, options?)` with a counter to handle multiple occurrences of the same string.

‎test.js

+8
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ test('leading underscore', t => {
133133
t.is(slugify('____-___foo__bar', {preserveLeadingUnderscore: true}), '_foo-bar');
134134
});
135135

136+
test('trailing dash', t => {
137+
t.is(slugify('foo bar-', {preserveTrailingDash: true}), 'foo-bar-');
138+
t.is(slugify('foo-bar--', {preserveTrailingDash: true}), 'foo-bar-');
139+
t.is(slugify('foo-bar -', {preserveTrailingDash: true}), 'foo-bar-');
140+
t.is(slugify('foo-bar - ', {preserveTrailingDash: true}), 'foo-bar');
141+
t.is(slugify('foo-bar ', {preserveTrailingDash: true}), 'foo-bar');
142+
});
143+
136144
test('counter', t => {
137145
const slugify = slugifyWithCounter();
138146
t.is(slugify('foo bar'), 'foo-bar');

0 commit comments

Comments
 (0)
Please sign in to comment.