File tree 5 files changed +54
-0
lines changed
5 files changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -112,6 +112,26 @@ export interface Options {
112
112
```
113
113
*/
114
114
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 ;
115
135
}
116
136
117
137
/**
Original file line number Diff line number Diff line change @@ -31,10 +31,12 @@ export default function slugify(string, options) {
31
31
decamelize : true ,
32
32
customReplacements : [ ] ,
33
33
preserveLeadingUnderscore : false ,
34
+ preserveTrailingDash : false ,
34
35
...options
35
36
} ;
36
37
37
38
const shouldPrependUnderscore = options . preserveLeadingUnderscore && string . startsWith ( '_' ) ;
39
+ const shouldAppendDash = options . preserveTrailingDash && string . endsWith ( '-' ) ;
38
40
39
41
const customReplacements = new Map ( [
40
42
...builtinOverridableReplacements ,
@@ -64,6 +66,10 @@ export default function slugify(string, options) {
64
66
string = `_${ string } ` ;
65
67
}
66
68
69
+ if ( shouldAppendDash ) {
70
+ string = `${ string } -` ;
71
+ }
72
+
67
73
return string ;
68
74
}
69
75
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ expectType<string>(slugify('Déjà Vu!', {lowercase: false}));
7
7
expectType < string > ( slugify ( 'fooBar' , { decamelize : false } ) ) ;
8
8
expectType < string > ( slugify ( 'I ♥ 🦄 & 🐶' , { customReplacements : [ [ '🐶' , 'dog' ] ] } ) ) ;
9
9
expectType < string > ( slugify ( '_foo_bar' , { preserveLeadingUnderscore : true } ) ) ;
10
+ expectType < string > ( slugify ( 'foo-bar-' , { preserveTrailingDash : true } ) ) ;
10
11
11
12
// Counter
12
13
expectType < string > ( slugifyWithCounter ( ) ( 'I ♥ Dogs' ) ) ;
Original file line number Diff line number Diff line change @@ -167,6 +167,25 @@ slugify('_foo_bar', {preserveLeadingUnderscore: true});
167
167
// => '_foo-bar'
168
168
```
169
169
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
+
170
189
### slugifyWithCounter()
171
190
172
191
Returns a new instance of ` slugify(string, options?) ` with a counter to handle multiple occurrences of the same string.
Original file line number Diff line number Diff line change @@ -133,6 +133,14 @@ test('leading underscore', t => {
133
133
t . is ( slugify ( '____-___foo__bar' , { preserveLeadingUnderscore : true } ) , '_foo-bar' ) ;
134
134
} ) ;
135
135
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
+
136
144
test ( 'counter' , t => {
137
145
const slugify = slugifyWithCounter ( ) ;
138
146
t . is ( slugify ( 'foo bar' ) , 'foo-bar' ) ;
You can’t perform that action at this time.
0 commit comments