Skip to content

Commit

Permalink
refactor: replace safeDecodeURI with querystring unescape (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh authored Jun 24, 2020
1 parent c92c5e3 commit 14fa817
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
13 changes: 3 additions & 10 deletions lib/decode_url.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
'use strict';

const { parse, format } = require('url');

const safeDecodeURI = str => {
try {
return decodeURI(str);
} catch (err) {
return str;
}
};
const { unescape } = require('querystring');

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

const url = format(parsed, { unicode: true });
return safeDecodeURI(url);
return unescape(url);
}

return safeDecodeURI(str);
return unescape(str);
};

module.exports = decodeURL;
13 changes: 3 additions & 10 deletions lib/encode_url.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
'use strict';

const { parse, format } = require('url');

const safeDecodeURI = str => {
try {
return decodeURI(str);
} catch (err) {
return str;
}
};
const { unescape } = require('querystring');

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

parsed.search = encodeURI(safeDecodeURI(parsed.search));
parsed.search = encodeURI(unescape(parsed.search));
// preserve IDN
return format(parsed, { unicode: true });
}

return encodeURI(safeDecodeURI(str));
return encodeURI(unescape(str));
};

module.exports = encodeURL;

0 comments on commit 14fa817

Please sign in to comment.