Skip to content

Commit 14fa817

Browse files
authored
refactor: replace safeDecodeURI with querystring unescape (#214)
1 parent c92c5e3 commit 14fa817

File tree

2 files changed

+6
-20
lines changed

2 files changed

+6
-20
lines changed

lib/decode_url.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
'use strict';
22

33
const { parse, format } = require('url');
4-
5-
const safeDecodeURI = str => {
6-
try {
7-
return decodeURI(str);
8-
} catch (err) {
9-
return str;
10-
}
11-
};
4+
const { unescape } = require('querystring');
125

136
const decodeURL = str => {
147
if (parse(str).protocol) {
@@ -18,10 +11,10 @@ const decodeURL = str => {
1811
if (parsed.origin === 'null') return str;
1912

2013
const url = format(parsed, { unicode: true });
21-
return safeDecodeURI(url);
14+
return unescape(url);
2215
}
2316

24-
return safeDecodeURI(str);
17+
return unescape(str);
2518
};
2619

2720
module.exports = decodeURL;

lib/encode_url.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
'use strict';
22

33
const { parse, format } = require('url');
4-
5-
const safeDecodeURI = str => {
6-
try {
7-
return decodeURI(str);
8-
} catch (err) {
9-
return str;
10-
}
11-
};
4+
const { unescape } = require('querystring');
125

136
const encodeURL = str => {
147
if (parse(str).protocol) {
@@ -17,12 +10,12 @@ const encodeURL = str => {
1710
// Exit if input is a data url
1811
if (parsed.origin === 'null') return str;
1912

20-
parsed.search = encodeURI(safeDecodeURI(parsed.search));
13+
parsed.search = encodeURI(unescape(parsed.search));
2114
// preserve IDN
2215
return format(parsed, { unicode: true });
2316
}
2417

25-
return encodeURI(safeDecodeURI(str));
18+
return encodeURI(unescape(str));
2619
};
2720

2821
module.exports = encodeURL;

0 commit comments

Comments
 (0)