-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
34 lines (31 loc) · 794 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"use strict";
const encode = function(str) {
return encodeURIComponent(str + '')
.replace(/[!'()]/g, escape)
.replace(/\*/g, "%2A")
.replace(/\%20/g, "+")
.replace(/~/g, '%7E');
};
const decode = function(str) {
return decodeURIComponent(
(str + '')
.replace(/%(?![\da-f]{2})/gi, '%25')
.replace(/\+/g, '%20')
);
};
const definePrototypes = function () {
if (!String.prototype.urlencode) {
global.String.prototype.urlencode = function () {
return encode(this);
};
}
if (!String.prototype.urldecode) {
global.String.prototype.urldecode = function () {
return decode(this);
};
}
};
module.exports = encode;
module.exports.encode = encode;
module.exports.decode = decode;
module.exports.definePrototypes = definePrototypes;