Skip to content

Commit 9a78d8f

Browse files
dgaussrsindresorhus
andauthoredFeb 11, 2020
Add leading underscore option (#43)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 8a67581 commit 9a78d8f

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed
 

‎index.d.ts

+19
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,25 @@ declare namespace slugify {
8080
```
8181
*/
8282
readonly customReplacements?: ReadonlyArray<[string, string]>;
83+
84+
/**
85+
If your string starts with an underscore, it will be preserved in the slugified string.
86+
Sometimes leading underscores are intentional, for example, filenames representing hidden paths on a website.
87+
88+
@default false
89+
90+
@example
91+
```
92+
import slugify = require('@sindresorhus/slugify');
93+
94+
slugify('_foo_bar');
95+
//=> 'foo-bar'
96+
97+
slugify('_foo_bar', {preserveLeadingUnderscore: true});
98+
//=> '_foo-bar'
99+
```
100+
*/
101+
readonly preserveLeadingUnderscore?: boolean;
83102
}
84103
}
85104

‎index.js

+7
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ const slugify = (string, options) => {
3838
lowercase: true,
3939
decamelize: true,
4040
customReplacements: [],
41+
preserveLeadingUnderscore: false,
4142
...options
4243
};
4344

45+
const shouldPrependUnderscore = options.preserveLeadingUnderscore && string.startsWith('_');
46+
4447
const separator = escapeStringRegexp(options.separator);
4548

4649
const customReplacements = new Map([
@@ -68,6 +71,10 @@ const slugify = (string, options) => {
6871
string = string.replace(/\\/g, '');
6972
string = removeMootSeparators(string, separator);
7073

74+
if (shouldPrependUnderscore) {
75+
string = `_${string}`;
76+
}
77+
7178
return string;
7279
};
7380

‎index.test-d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ expectType<string>(slugify('fooBar', {decamelize: false}));
88
expectType<string>(
99
slugify('I ♥ 🦄 & 🐶', {customReplacements: [['🐶', 'dog']]})
1010
);
11+
expectType<string>(slugify('_foo_bar', {preserveLeadingUnderscore: true}));

‎readme.md

+19
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,25 @@ slugify('foo@unicorn', {
132132
//=> 'foo-at-unicorn'
133133
```
134134

135+
##### preserveLeadingUnderscore
136+
137+
Type: `boolean`\
138+
Default: `false`
139+
140+
If your string starts with an underscore, it will be preserved in the slugified string.
141+
142+
Sometimes leading underscores are intentional, for example, filenames representing hidden paths on a website.
143+
144+
```js
145+
const slugify = require('@sindresorhus/slugify');
146+
147+
slugify('_foo_bar');
148+
//=> 'foo-bar'
149+
150+
slugify('_foo_bar', {preserveLeadingUnderscore: true});
151+
//=> '_foo-bar'
152+
```
153+
135154
## Related
136155

137156
- [slugify-cli](https://github.com/sindresorhus/slugify-cli) - CLI for this module

‎test.js

+7
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,10 @@ test('supports Turkish', t => {
124124
test('supports Armenian', t => {
125125
t.is(slugify('Ե ր ե ւ ա ն', {lowercase: false, separator: ' '}), 're ye v a n');
126126
});
127+
128+
test('leading underscore', t => {
129+
t.is(slugify('_foo bar', {preserveLeadingUnderscore: true}), '_foo-bar');
130+
t.is(slugify('_foo_bar', {preserveLeadingUnderscore: true}), '_foo-bar');
131+
t.is(slugify('__foo__bar', {preserveLeadingUnderscore: true}), '_foo-bar');
132+
t.is(slugify('____-___foo__bar', {preserveLeadingUnderscore: true}), '_foo-bar');
133+
});

0 commit comments

Comments
 (0)
Please sign in to comment.