mirror of https://github.com/jkjoy/sunpeiwen.git
22 lines
490 B
JavaScript
22 lines
490 B
JavaScript
'use strict';
|
|
|
|
const { parse, format } = require('url');
|
|
const { unescape } = require('querystring');
|
|
|
|
const encodeURL = str => {
|
|
if (parse(str).protocol) {
|
|
const parsed = new URL(str);
|
|
|
|
// Exit if input is a data url
|
|
if (parsed.origin === 'null') return str;
|
|
|
|
parsed.search = encodeURI(unescape(parsed.search));
|
|
// preserve IDN
|
|
return format(parsed, { unicode: true });
|
|
}
|
|
|
|
return encodeURI(unescape(str));
|
|
};
|
|
|
|
module.exports = encodeURL;
|