normalize-url 5.3.0 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -103,6 +103,30 @@ declare namespace normalizeUrl {
103
103
  */
104
104
  readonly stripProtocol?: boolean;
105
105
 
106
+ /**
107
+ Strip the [text fragment](https://web.dev/text-fragments/) part of the URL
108
+
109
+ __Note:__ The text fragment will always be removed if the `stripHash` option is set to `true`, as the hash contains the text fragment.
110
+
111
+ @default true
112
+
113
+ @example
114
+ ```
115
+ normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello');
116
+ //=> 'http://sindresorhus.com/about.html#'
117
+
118
+ normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello');
119
+ //=> 'http://sindresorhus.com/about.html#section'
120
+
121
+ normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello', {stripTextFragment: false});
122
+ //=> 'http://sindresorhus.com/about.html#:~:text=hello'
123
+
124
+ normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello', {stripTextFragment: false});
125
+ //=> 'http://sindresorhus.com/about.html#section:~:text=hello'
126
+ ```
127
+ */
128
+ readonly stripTextFragment?: boolean;
129
+
106
130
  /**
107
131
  Removes `www.` from the URL.
108
132
 
@@ -131,8 +155,26 @@ declare namespace normalizeUrl {
131
155
  });
132
156
  //=> 'http://sindresorhus.com/?foo=bar'
133
157
  ```
158
+
159
+ If a boolean is provided, `true` will remove all the query parameters.
160
+
161
+ ```
162
+ normalizeUrl('www.sindresorhus.com?foo=bar', {
163
+ removeQueryParameters: true
164
+ });
165
+ //=> 'http://sindresorhus.com'
166
+ ```
167
+
168
+ `false` will not remove any query parameter.
169
+
170
+ ```
171
+ normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
172
+ removeQueryParameters: false
173
+ });
174
+ //=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test'
175
+ ```
134
176
  */
135
- readonly removeQueryParameters?: ReadonlyArray<RegExp | string>;
177
+ readonly removeQueryParameters?: ReadonlyArray<RegExp | string> | boolean;
136
178
 
137
179
  /**
138
180
  Removes trailing slash.
package/index.js CHANGED
@@ -9,7 +9,7 @@ const testParameter = (name, filters) => {
9
9
  };
10
10
 
11
11
  const normalizeDataURL = (urlString, {stripHash}) => {
12
- const match = /^data:(?<type>.*?),(?<data>.*?)(?:#(?<hash>.*))?$/.exec(urlString);
12
+ const match = /^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(urlString);
13
13
 
14
14
  if (!match) {
15
15
  throw new Error(`Invalid URL: ${urlString}`);
@@ -67,6 +67,7 @@ const normalizeUrl = (urlString, options) => {
67
67
  forceHttps: false,
68
68
  stripAuthentication: true,
69
69
  stripHash: false,
70
+ stripTextFragment: true,
70
71
  stripWWW: true,
71
72
  removeQueryParameters: [/^utm_\w+/i],
72
73
  removeTrailingSlash: true,
@@ -118,6 +119,8 @@ const normalizeUrl = (urlString, options) => {
118
119
  // Remove hash
119
120
  if (options.stripHash) {
120
121
  urlObj.hash = '';
122
+ } else if (options.stripTextFragment) {
123
+ urlObj.hash = urlObj.hash.replace(/#?:~:text.*?$/i, '');
121
124
  }
122
125
 
123
126
  // Remove duplicate slashes if not preceded by a protocol
@@ -170,6 +173,10 @@ const normalizeUrl = (urlString, options) => {
170
173
  }
171
174
  }
172
175
 
176
+ if (options.removeQueryParameters === true) {
177
+ urlObj.search = '';
178
+ }
179
+
173
180
  // Sort query parameters
174
181
  if (options.sortQueryParameters) {
175
182
  urlObj.searchParams.sort();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "normalize-url",
3
- "version": "5.3.0",
3
+ "version": "6.1.0",
4
4
  "description": "Normalize a URL",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/normalize-url",
@@ -8,7 +8,7 @@
8
8
  "author": {
9
9
  "name": "Sindre Sorhus",
10
10
  "email": "sindresorhus@gmail.com",
11
- "url": "sindresorhus.com"
11
+ "url": "https://sindresorhus.com"
12
12
  },
13
13
  "engines": {
14
14
  "node": ">=10"
@@ -37,9 +37,14 @@
37
37
  ],
38
38
  "devDependencies": {
39
39
  "ava": "^2.4.0",
40
- "coveralls": "^3.0.6",
41
40
  "nyc": "^15.0.0",
42
41
  "tsd": "^0.11.0",
43
42
  "xo": "^0.25.3"
43
+ },
44
+ "nyc": {
45
+ "reporter": [
46
+ "text",
47
+ "lcov"
48
+ ]
44
49
  }
45
50
  }
package/readme.md CHANGED
@@ -1,4 +1,4 @@
1
- # normalize-url [![Build Status](https://travis-ci.org/sindresorhus/normalize-url.svg?branch=master)](https://travis-ci.org/sindresorhus/normalize-url) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/normalize-url/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/normalize-url?branch=master)
1
+ # normalize-url [![Coverage Status](https://codecov.io/gh/sindresorhus/normalize-url/branch/main/graph/badge.svg)](https://codecov.io/gh/sindresorhus/normalize-url)
2
2
 
3
3
  > [Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL
4
4
 
@@ -135,6 +135,29 @@ normalizeUrl('https://sindresorhus.com', {stripProtocol: true});
135
135
  //=> 'sindresorhus.com'
136
136
  ```
137
137
 
138
+ ##### stripTextFragment
139
+
140
+ Type: `boolean`\
141
+ Default: `true`
142
+
143
+ Strip the [text fragment](https://web.dev/text-fragments/) part of the URL.
144
+
145
+ **Note:** The text fragment will always be removed if the `stripHash` option is set to `true`, as the hash contains the text fragment.
146
+
147
+ ```js
148
+ normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello');
149
+ //=> 'http://sindresorhus.com/about.html#'
150
+
151
+ normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello');
152
+ //=> 'http://sindresorhus.com/about.html#section'
153
+
154
+ normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello', {stripTextFragment: false});
155
+ //=> 'http://sindresorhus.com/about.html#:~:text=hello'
156
+
157
+ normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello', {stripTextFragment: false});
158
+ //=> 'http://sindresorhus.com/about.html#section:~:text=hello'
159
+ ```
160
+
138
161
  ##### stripWWW
139
162
 
140
163
  Type: `boolean`\
@@ -152,7 +175,7 @@ normalizeUrl('http://www.sindresorhus.com', {stripWWW: false});
152
175
 
153
176
  ##### removeQueryParameters
154
177
 
155
- Type: `Array<RegExp | string>`\
178
+ Type: `Array<RegExp | string> | boolean`\
156
179
  Default: `[/^utm_\w+/i]`
157
180
 
158
181
  Remove query parameters that matches any of the provided strings or regexes.
@@ -164,6 +187,24 @@ normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
164
187
  //=> 'http://sindresorhus.com/?foo=bar'
165
188
  ```
166
189
 
190
+ If a boolean is provided, `true` will remove all the query parameters.
191
+
192
+ ```js
193
+ normalizeUrl('www.sindresorhus.com?foo=bar', {
194
+ removeQueryParameters: true
195
+ });
196
+ //=> 'http://sindresorhus.com'
197
+ ```
198
+
199
+ `false` will not remove any query parameter.
200
+
201
+ ```js
202
+ normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
203
+ removeQueryParameters: false
204
+ });
205
+ //=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test'
206
+ ```
207
+
167
208
  ##### removeTrailingSlash
168
209
 
169
210
  Type: `boolean`\
@@ -232,7 +273,6 @@ normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
232
273
 
233
274
  - [compare-urls](https://github.com/sindresorhus/compare-urls) - Compare URLs by first normalizing them
234
275
 
235
-
236
276
  ---
237
277
 
238
278
  <div align="center">