Skip to content

Commit 6ebea83

Browse files
committed
Add global prototype functions
What's new? Call `.definePrototypes()` to create globally available String prototypes for `.urlencode()` and `.urldecode()`
1 parent 783794e commit 6ebea83

4 files changed

Lines changed: 90 additions & 14 deletions

File tree

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ $ yarn add phpurlencode
1515

1616
## Usage
1717

18-
### phpurlencode(string)
18+
### Include it in your Node.js file
19+
1920
```js
2021
var phpurlencode = require('phpurlencode');
22+
```
23+
24+
### phpurlencode(string)
25+
```js
2126
var encodedString = phpurlencode("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.67 Safari/537.36");
2227
console.log(encodedString);
2328
// => Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_12_2%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2F56.0.2924.67+Safari%2F537.36
@@ -36,3 +41,19 @@ var decodedString = phpurlencode.decode('%21%40%23%24%25%5E%26%2A%28%29-%2B%3D%2
3641
console.log(decodedString);
3742
// => !@#$%^&*()-+='"
3843
```
44+
45+
### phpurlencode.definePrototypes()
46+
#### Globally defines `.urlencode` and `.urldecode` String prototypes
47+
```js
48+
// Register String prototypes.
49+
// Once this function is called, the String prototypes will be available across your entire project
50+
phpurlencode.definePrototypes();
51+
52+
var decodedString = '%21%40%23%24%25%5E%26%2A%28%29-%2B%3D%27%22'.urldecode();
53+
console.log(decodedString);
54+
// => !@#$%^&*()-+='"
55+
56+
var encodedString = decodedString.urlencode();
57+
console.log(encodedString);
58+
// => %21%40%23%24%25%5E%26%2A%28%29-%2B%3D%27%22
59+
```

index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ const decode = function(str) {
1515
);
1616
};
1717

18+
const definePrototypes = function () {
19+
if (!String.prototype.urlencode) {
20+
global.String.prototype.urlencode = function () {
21+
return encode(this);
22+
};
23+
}
24+
if (!String.prototype.urldecode) {
25+
global.String.prototype.urldecode = function () {
26+
return decode(this);
27+
};
28+
}
29+
};
30+
1831
module.exports = encode;
1932
module.exports.encode = encode;
2033
module.exports.decode = decode;
34+
module.exports.definePrototypes = definePrototypes;

package.json

Lines changed: 1 addition & 1 deletion
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.2",
4+
"version": "1.1.0",
55
"main": "index.js",
66
"license": "MIT",
77
"devDependencies": {

test/test.js

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,32 @@
22
const urlencode = require("../index.js");
33
const assert = require("assert");
44

5+
const strings = {
6+
userAgent: {
7+
encoded: "Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_12_2%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2F56.0.2924.67+Safari%2F537.36",
8+
decoded: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.67 Safari/537.36"
9+
},
10+
specialChars: {
11+
encoded: "%21%40%23%24%25%5E%26%2A%28%29%2B%27%7E%22%3C%3E%2C.",
12+
decoded: "!@#$%^&*()+'~\"<>,."
13+
}
14+
};
15+
516
describe("urlencode", function() {
617
describe("#encode()", function() {
718
it(
819
"should encode special characters similar to php's urlencode",
920
function() {
1021
assert.equal(
11-
"%21%40%23%24%25%5E%26%2A%28%29%2B%27%7E%22%3C%3E%2C.",
12-
urlencode("!@#$%^&*()+'~\"<>,.")
22+
strings.specialChars.encoded,
23+
urlencode(strings.specialChars.decoded)
1324
);
1425
}
1526
);
1627
it("should encode a string similar to php's urlencode", function() {
1728
assert.equal(
18-
"Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_12_2%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2F56.0.2924.67+Safari%2F537.36",
19-
urlencode.encode(
20-
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.67 Safari/537.36"
21-
)
29+
strings.userAgent.encoded,
30+
urlencode.encode(strings.userAgent.decoded)
2231
);
2332
});
2433
});
@@ -28,18 +37,50 @@ describe("urlencode", function() {
2837
"should decode special characters similar to php's urldecode",
2938
function() {
3039
assert.equal(
31-
"!@#$%^&*()+'~\"<>,.",
32-
urlencode.decode("%21%40%23%24%25%5E%26%2A%28%29%2B%27%7E%22%3C%3E%2C.")
40+
strings.specialChars.decoded,
41+
urlencode.decode(strings.specialChars.encoded)
3342
);
3443
}
3544
);
3645
it("should decode a string similar to php's urldecode", function() {
3746
assert.equal(
38-
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.67 Safari/537.36",
39-
urlencode.decode(
40-
"Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_12_2%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2F56.0.2924.67+Safari%2F537.36"
41-
)
47+
strings.userAgent.decoded,
48+
urlencode.decode(strings.userAgent.encoded)
4249
);
4350
});
4451
});
4552
});
53+
54+
describe("#definePrototypes()", function () {
55+
it("should register global String prototypes", function () {
56+
urlencode.definePrototypes();
57+
assert.equal(false, !String.prototype.urldecode, 'String.prototype.urldecode exists');
58+
assert.equal(false, !String.prototype.urlencode, 'String.prototype.urlencode exists');
59+
});
60+
61+
it("should correctly encode with String prototype", function () {
62+
assert.equal(
63+
strings.specialChars.decoded.urlencode(),
64+
strings.specialChars.encoded,
65+
'correctly encodes special character test string'
66+
);
67+
assert.equal(
68+
strings.userAgent.decoded.urlencode(),
69+
strings.userAgent.encoded,
70+
'correctly encodes user-agent test string'
71+
);
72+
});
73+
74+
it("should correctly decode with String prototype", function () {
75+
assert.equal(
76+
strings.specialChars.encoded.urldecode(),
77+
strings.specialChars.decoded,
78+
'correctly decodes special character test string'
79+
);
80+
assert.equal(
81+
strings.userAgent.encoded.urldecode(),
82+
strings.userAgent.decoded,
83+
'correctly decodes user-agent test string'
84+
);
85+
});
86+
});

0 commit comments

Comments
 (0)