Skip to content

Commit bd530be

Browse files
committed
Bug fixes
Add missing encodings improve decoder ensure input type Add characters to test strings
1 parent 07c5038 commit bd530be

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
"use strict";
2-
function encode(str) {
3-
return encodeURIComponent(str)
2+
const encode = str => {
3+
return encodeURIComponent(str + '')
44
.replace(/[!'()]/g, escape)
55
.replace(/\*/g, "%2A")
6-
.replace(/\%20/g, "+");
7-
}
6+
.replace(/\%20/g, "+")
7+
.replace(/~/g, '%7E');
8+
};
89

9-
function decode(str) {
10-
return decodeURIComponent(str.replace(/\+/g, " ").replace(/\%2A/g, "*"));
11-
}
10+
const decode = str => {
11+
return decodeURIComponent(
12+
(str + '')
13+
.replace(/%(?![\da-f]{2})/gi, () => '%25')
14+
.replace(/\+/g, '%20')
15+
);
16+
};
1217

1318
module.exports = encode;
1419
module.exports.encode = encode;

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "phpurlencode",
33
"description": "Functionally similar to PHP urlencode and urldecode functions",
4-
"version": "1.0.0",
4+
"version": "1.0.1",
55
"main": "index.js",
66
"license": "MIT",
77
"devDependencies": {
@@ -13,5 +13,6 @@
1313
"repository": {
1414
"type": "git",
1515
"url": "https://github.com/luke3butler/node-phpurlencode.git"
16-
}
16+
},
17+
"dependencies": {}
1718
}

test/test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ describe("urlencode", function() {
88
"should encode special characters similar to php's urlencode",
99
function() {
1010
assert.equal(
11-
"%21%40%23%24%25%5E%26%2A%28%29%2B%27",
12-
urlencode("!@#$%^&*()+'")
11+
"%21%40%23%24%25%5E%26%2A%28%29%2B%27%7E%22%3C%3E%2C.",
12+
urlencode("!@#$%^&*()+'~\"<>,.")
1313
);
1414
}
1515
);
@@ -28,8 +28,8 @@ describe("urlencode", function() {
2828
"should decode special characters similar to php's urldecode",
2929
function() {
3030
assert.equal(
31-
"!@#$%^&*()+'",
32-
urlencode.decode("%21%40%23%24%25%5E%26%2A%28%29%2B%27")
31+
"!@#$%^&*()+'~\"<>,.",
32+
urlencode.decode("%21%40%23%24%25%5E%26%2A%28%29%2B%27%7E%22%3C%3E%2C.")
3333
);
3434
}
3535
);

0 commit comments

Comments
 (0)