1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | (function (define) {
|
9 | 'use strict';
|
10 |
|
11 | define(function (/* require */) {
|
12 |
|
13 | var charMap;
|
14 |
|
15 | charMap = (function () {
|
16 | var strings = {
|
17 | alpha: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
|
18 | digit: '0123456789'
|
19 | };
|
20 |
|
21 | strings.genDelims = ':/?#[]@';
|
22 | strings.subDelims = '!$&\'()*+,;=';
|
23 | strings.reserved = strings.genDelims + strings.subDelims;
|
24 | strings.unreserved = strings.alpha + strings.digit + '-._~';
|
25 | strings.url = strings.reserved + strings.unreserved;
|
26 | strings.scheme = strings.alpha + strings.digit + '+-.';
|
27 | strings.userinfo = strings.unreserved + strings.subDelims + ':';
|
28 | strings.host = strings.unreserved + strings.subDelims;
|
29 | strings.port = strings.digit;
|
30 | strings.pchar = strings.unreserved + strings.subDelims + ':@';
|
31 | strings.segment = strings.pchar;
|
32 | strings.path = strings.segment + '/';
|
33 | strings.query = strings.pchar + '/?';
|
34 | strings.fragment = strings.pchar + '/?';
|
35 |
|
36 | return Object.keys(strings).reduce(function (charMap, set) {
|
37 | charMap[set] = strings[set].split('').reduce(function (chars, myChar) {
|
38 | chars[myChar] = true;
|
39 | return chars;
|
40 | }, {});
|
41 | return charMap;
|
42 | }, {});
|
43 | }());
|
44 |
|
45 | function encode(str, allowed) {
|
46 | if (typeof str !== 'string') {
|
47 | throw new Error('String required for URL encoding');
|
48 | }
|
49 | return str.split('').map(function (myChar) {
|
50 | if (allowed.hasOwnProperty(myChar)) {
|
51 | return myChar;
|
52 | }
|
53 | var code = myChar.charCodeAt(0);
|
54 | if (code <= 127) {
|
55 | return '%' + code.toString(16).toUpperCase();
|
56 | }
|
57 | else {
|
58 | return encodeURIComponent(myChar).toUpperCase();
|
59 | }
|
60 | }).join('');
|
61 | }
|
62 |
|
63 | function makeEncoder(allowed) {
|
64 | allowed = allowed || charMap.unreserved;
|
65 | return function (str) {
|
66 | return encode(str, allowed);
|
67 | };
|
68 | }
|
69 |
|
70 | function decode(str) {
|
71 | return decodeURIComponent(str);
|
72 | }
|
73 |
|
74 | return {
|
75 |
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | decode: decode,
|
83 |
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | encode: makeEncoder(),
|
94 |
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | encodeURL: makeEncoder(charMap.url),
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | encodeScheme: makeEncoder(charMap.scheme),
|
115 |
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | encodeUserInfo: makeEncoder(charMap.userinfo),
|
123 |
|
124 | |
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | encodeHost: makeEncoder(charMap.host),
|
131 |
|
132 | |
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | encodePort: makeEncoder(charMap.port),
|
139 |
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | encodePathSegment: makeEncoder(charMap.segment),
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | encodePath: makeEncoder(charMap.path),
|
155 |
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | encodeQuery: makeEncoder(charMap.query),
|
163 |
|
164 | |
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | encodeFragment: makeEncoder(charMap.fragment)
|
171 |
|
172 | };
|
173 |
|
174 | });
|
175 |
|
176 | }(
|
177 | typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
|
178 |
|
179 | ));
|