-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding.js
98 lines (89 loc) · 2.54 KB
/
encoding.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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const chunk = (str, size) => {
const numChunks = Math.ceil(str.length / size);
const chunks = new Array(numChunks);
for (let i = 0, o = 0; i < numChunks; ++i, o += size) {
chunks[i] = str.substr(o, size);
}
return chunks;
};
function encodeUTFAlphabet(data) {
const value = data instanceof Object ? JSON.stringify(data) : String(data);
return value
.replace(/ |\t|\s|\r|\n/gi, "")
.split("")
.map((character) => {
// matches only non Latin alphabet characters
if (!character.match(/[a-zA-Z]/) && character.match(/\p{L}/u)) {
const hex = character.charCodeAt().toString(16);
return "\\u" + "0000".substring(0, 4 - hex.length) + hex;
}
return character;
})
.join("");
}
function percentEncoding(value) {
return String(value)
.split("")
.map((character) => {
if (character.match(/ /)) {
return "+";
}
if (!character.match(/\p{L}|\p{N}/u)) {
let hex = Buffer.from(character).toString("hex");
if (hex.length > 2) {
hex = chunk(hex, 2).join("%");
}
return `%${hex}`.toUpperCase();
}
return character;
})
.join("");
}
function encodeURIRequest(request) {
function encodeValue(value, isNestedValue = false, addComma = false) {
if (value instanceof Object) {
let newValue = "";
const keys = Object.keys(value);
for (let i = 0; i < keys.length; i++) {
if (i === 0) {
newValue += Array.isArray(value) ? "[" : "{";
}
const currentKey = keys[i];
const nestedValue = value[currentKey];
if (Array.isArray(value)) {
newValue += `${encodeValue(nestedValue, true, i < keys.length - 1)}`;
} else {
newValue += `"${currentKey}":${encodeValue(
nestedValue,
true,
i < keys.length - 1
)}`;
}
}
newValue += Array.isArray(value) ? "]" : "}";
return newValue;
}
let processedValue = value;
if (isNestedValue && typeof value === "string") {
processedValue = `"${value}"`;
}
if (addComma) {
processedValue += ",";
}
return processedValue;
}
let encodedRequest = "";
for (const [key, value] of Object.entries(request)) {
if (encodedRequest.length > 0) {
encodedRequest += "&";
}
const newValue = percentEncoding(encodeValue(value));
encodedRequest += `${key}=${newValue}`;
}
return encodeUTFAlphabet(encodedRequest);
}
module.exports = {
encodeUTFAlphabet,
encodeURIRequest,
percentEncoding,
};