-
Notifications
You must be signed in to change notification settings - Fork 43
/
encoding.ts
176 lines (162 loc) Β· 4.35 KB
/
encoding.ts
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { toASCII } from "./punycode";
import { QueryValue } from "./query";
// Utils used from https://github.com/vuejs/vue-router-next/blob/master/src/encoding.ts (Author @posva)
const HASH_RE = /#/g; // %23
const AMPERSAND_RE = /&/g; // %26
const SLASH_RE = /\//g; // %2F
const EQUAL_RE = /=/g; // %3D
const IM_RE = /\?/g; // %3F
const PLUS_RE = /\+/g; // %2B
const ENC_CARET_RE = /%5e/gi; // ^
const ENC_BACKTICK_RE = /%60/gi; // `
const ENC_CURLY_OPEN_RE = /%7b/gi; // {
const ENC_PIPE_RE = /%7c/gi; // |
const ENC_CURLY_CLOSE_RE = /%7d/gi; // }
const ENC_SPACE_RE = /%20/gi;
const ENC_SLASH_RE = /%2f/gi;
const ENC_ENC_SLASH_RE = /%252f/gi;
/**
* Encode characters that need to be encoded on the path, search and hash
* sections of the URL.
*
* @group encoding_utils
*
* @param text - string to encode
* @returns encoded string
*/
export function encode(text: string | number): string {
return encodeURI("" + text).replace(ENC_PIPE_RE, "|");
}
/**
* Encode characters that need to be encoded on the hash section of the URL.
*
* @group encoding_utils
*
* @param text - string to encode
* @returns encoded string
*/
export function encodeHash(text: string): string {
return encode(text)
.replace(ENC_CURLY_OPEN_RE, "{")
.replace(ENC_CURLY_CLOSE_RE, "}")
.replace(ENC_CARET_RE, "^");
}
/**
* Encode characters that need to be encoded query values on the query
* section of the URL.
*
* @group encoding_utils
*
* @param input - string to encode
* @returns encoded string
*/
export function encodeQueryValue(input: QueryValue): string {
return (
encode(typeof input === "string" ? input : JSON.stringify(input))
// Encode the space as +, encode the + to differentiate it from the space
.replace(PLUS_RE, "%2B")
.replace(ENC_SPACE_RE, "+")
.replace(HASH_RE, "%23")
.replace(AMPERSAND_RE, "%26")
.replace(ENC_BACKTICK_RE, "`")
.replace(ENC_CARET_RE, "^")
.replace(SLASH_RE, "%2F")
);
}
/**
* Encode characters that need to be encoded query values on the query
* section of the URL and also encodes the `=` character.
*
* @group encoding_utils
*
* @param text - string to encode
*/
export function encodeQueryKey(text: string | number): string {
return encodeQueryValue(text).replace(EQUAL_RE, "%3D");
}
/**
* Encode characters that need to be encoded on the path section of the URL.
*
* @group encoding_utils
*
* @param text - string to encode
* @returns encoded string
*/
export function encodePath(text: string | number): string {
return encode(text)
.replace(HASH_RE, "%23")
.replace(IM_RE, "%3F")
.replace(ENC_ENC_SLASH_RE, "%2F")
.replace(AMPERSAND_RE, "%26")
.replace(PLUS_RE, "%2B");
}
/**
* Encode characters that need to be encoded on the path section of the URL as a
* param. This function encodes everything `encodePath` does plus the
* slash (`/`) character.
*
* @group encoding_utils
*
* @param text - string to encode
* @returns encoded string
*/
export function encodeParam(text: string | number): string {
return encodePath(text).replace(SLASH_RE, "%2F");
}
/**
* Decode text using `decodeURIComponent`. Returns the original text if it
* fails.
*
* @group encoding_utils
*
* @param text - string to decode
* @returns decoded string
*/
export function decode(text: string | number = ""): string {
try {
return decodeURIComponent("" + text);
} catch {
return "" + text;
}
}
/**
* Decode path section of URL (consistent with encodePath for slash encoding).
*
* @group encoding_utils
*
* @param text - string to decode
* @returns decoded string
*/
export function decodePath(text: string): string {
return decode(text.replace(ENC_SLASH_RE, "%252F"));
}
/**
* Decodes query key (consistent with `encodeQueryKey` for plus encoding).
*
* @group encoding_utils
*
* @param text - string to decode
* @returns decoded string
*/
export function decodeQueryKey(text: string): string {
return decode(text.replace(PLUS_RE, " "));
}
/**
* Decode query value (consistent with encodeQueryValue for plus encoding).
*
* @group encoding_utils
*
* @param text - string to decode
* @returns decoded string
*/
export function decodeQueryValue(text: string): string {
return decode(text.replace(PLUS_RE, " "));
}
/**
* Encodes hostname with punycode encoding.
*
* @group encoding_utils
*/
export function encodeHost(name = "") {
return toASCII(name);
}