Skip to content

Commit

Permalink
Add keepSingleSlash option
Browse files Browse the repository at this point in the history
Decided it's better to have a separate option than to overload a boolean
type with non-boolean values.

Fixes: #119
  • Loading branch information
silverwind committed Sep 28, 2020
1 parent ea4706f commit be04d70
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
16 changes: 16 additions & 0 deletions index.d.ts
Expand Up @@ -155,6 +155,22 @@ declare namespace normalizeUrl {
*/
readonly removeTrailingSlash?: boolean;

/**
Remove a sole `/` pathname in the output. This option is independant of `removeTrailingSlash`.
@default false
@example
```
normalizeUrl('http://sindresorhus.com/');
//=> 'http://sindresorhus.com'
normalizeUrl('http://sindresorhus.com/', {removeSingleSlash: false});
//=> 'http://sindresorhus.com/'
```
*/
readonly removeSingleSlash?: boolean;

/**
Removes the default directory index file from path that matches any of the provided strings or regexes.
When `true`, the regex `/^index\.[a-z]+$/` is used.
Expand Down
5 changes: 3 additions & 2 deletions index.js
Expand Up @@ -70,6 +70,7 @@ const normalizeUrl = (urlString, options) => {
stripWWW: true,
removeQueryParameters: [/^utm_\w+/i],
removeTrailingSlash: true,
removeSingleSlash: true,
removeDirectoryIndex: false,
sortQueryParameters: true,
...options
Expand Down Expand Up @@ -177,8 +178,8 @@ const normalizeUrl = (urlString, options) => {
// Take advantage of many of the Node `url` normalizations
urlString = urlObj.toString();

// Remove ending `/`
if ((options.removeTrailingSlash || urlObj.pathname === '/') && urlObj.hash === '') {
// Remove ending `/` unless removeSingleSlash is false
if ((options.removeTrailingSlash || urlObj.pathname === '/') && urlObj.hash === '' && options.removeSingleSlash) {
urlString = urlString.replace(/\/$/, '');
}

Expand Down
18 changes: 17 additions & 1 deletion readme.md
Expand Up @@ -171,7 +171,7 @@ Default: `true`

Remove trailing slash.

**Note:** Trailing slash is always removed if the URL doesn't have a pathname.
**Note:** Trailing slash is always removed if the URL doesn't have a pathname unless the `removeSingleSlash` option is set to `false`.

```js
normalizeUrl('http://sindresorhus.com/redirect/');
Expand All @@ -184,6 +184,22 @@ normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
//=> 'http://sindresorhus.com'
```

##### removeSingleSlash

Type: `boolean`\
Default: `true`

Remove a sole `/` pathname in the output. This option is independant of `removeTrailingSlash`.

```js
normalizeUrl('http://sindresorhus.com/');
//=> 'http://sindresorhus.com'

normalizeUrl('http://sindresorhus.com/', {removeSingleSlash: false});
//=> 'http://sindresorhus.com/'
```


##### removeDirectoryIndex

Type: `boolean | Array<RegExp | string>`\
Expand Down
18 changes: 18 additions & 0 deletions test.js
Expand Up @@ -124,6 +124,24 @@ test('removeTrailingSlash option', t => {
t.is(normalizeUrl('http://sindresorhus.com/redirect/'), 'http://sindresorhus.com/redirect');
t.is(normalizeUrl('http://sindresorhus.com/redirect/', options), 'http://sindresorhus.com/redirect/');
t.is(normalizeUrl('http://sindresorhus.com/#/', options), 'http://sindresorhus.com/#/');
t.is(normalizeUrl('http://sindresorhus.com/?unicorns=true'), 'http://sindresorhus.com/?unicorns=true');
t.is(normalizeUrl('http://sindresorhus.com/?unicorns=true', options), 'http://sindresorhus.com/?unicorns=true');
});

test('removeSingleSlash option', t => {
const options = {removeSingleSlash: false};
t.is(normalizeUrl('http://sindresorhus.com/', options), 'http://sindresorhus.com/');
t.is(normalizeUrl('http://sindresorhus.com/redirect/', options), 'http://sindresorhus.com/redirect');
t.is(normalizeUrl('http://sindresorhus.com/#/', options), 'http://sindresorhus.com/#/');
t.is(normalizeUrl('http://sindresorhus.com/?unicorns=true', options), 'http://sindresorhus.com/?unicorns=true');
});

test('removeSingleSlash option combined with removeTrailingSlash option', t => {
const options = {removeTrailingSlash: true, removeSingleSlash: false};
t.is(normalizeUrl('http://sindresorhus.com/', options), 'http://sindresorhus.com/');
t.is(normalizeUrl('http://sindresorhus.com/redirect/', options), 'http://sindresorhus.com/redirect');
t.is(normalizeUrl('http://sindresorhus.com/#/', options), 'http://sindresorhus.com/#/');
t.is(normalizeUrl('http://sindresorhus.com/?unicorns=true', options), 'http://sindresorhus.com/?unicorns=true');
});

test('removeDirectoryIndex option', t => {
Expand Down

0 comments on commit be04d70

Please sign in to comment.