-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.urlencode.js
More file actions
64 lines (53 loc) · 1.37 KB
/
string.urlencode.js
File metadata and controls
64 lines (53 loc) · 1.37 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* @title: url encode string
* @description: replace ' ' (space) with %20
* @author: Thorsten Kober
* @email: [email protected]
*/
function urlify(str) {
// str = str.trim();
while (str.substr(0, 1) === ' ') {
str = str.substr(1);
}
while (str.substr(str.length - 1) === ' ') {
str = str.substr(0, str.length - 1);
}
let spaceCount = 0;
const replacement = '%20';
const strLength = str.length - 1;
for (let i = 0; i < str.length; i++) {
if (str[i] === ' ') {
spaceCount++;
}
}
const newLength = spaceCount * replacement.length - 1 + strLength;
let index = newLength - 1;
const chars = [];
for (let i = str.length - 1; i >= 0; i--) {
if (str[i] === ' ') {
chars[index] = '0';
chars[index - 1] = '2';
chars[index - 2] = '%';
index -= 3;
} else {
chars[index] = str[i];
index--;
}
}
return chars.join('');
}
// recursive
function urlify2(str) {
if (str.length === 0) return str;
if (str.substr(0, 1) === ' ') {
return '%20' + urlify2(str.substr(1)); // eslint-disable-line
}
return str.substr(0, 1) + urlify2(str.substr(1));
}
// npx jest algorithms/string/string.urlencode.js
test('urlify()', () => {
expect(urlify('Mr John Smith')).toEqual('Mr%20John%20Smith');
});
test('urlify2()', () => {
expect(urlify2('Mr John Smith')).toEqual('Mr%20John%20Smith');
});