UNPKG

4.46 kBJavaScriptView Raw
1/*
2 * Copyright 2015 the original author or authors
3 * @license MIT, see LICENSE.txt for details
4 *
5 * @author Scott Andrews
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 * Decode URL encoded strings
78 *
79 * @param {string} URL encoded string
80 * @returns {string} URL decoded string
81 */
82 decode: decode,
83
84 /*
85 * URL encode a string
86 *
87 * All but alpha-numerics and a very limited set of punctuation - . _ ~ are
88 * encoded.
89 *
90 * @param {string} string to encode
91 * @returns {string} URL encoded string
92 */
93 encode: makeEncoder(),
94
95 /*
96 * URL encode a URL
97 *
98 * All character permitted anywhere in a URL are left unencoded even
99 * if that character is not permitted in that portion of a URL.
100 *
101 * Note: This method is typically not what you want.
102 *
103 * @param {string} string to encode
104 * @returns {string} URL encoded string
105 */
106 encodeURL: makeEncoder(charMap.url),
107
108 /*
109 * URL encode the scheme portion of a URL
110 *
111 * @param {string} string to encode
112 * @returns {string} URL encoded string
113 */
114 encodeScheme: makeEncoder(charMap.scheme),
115
116 /*
117 * URL encode the user info portion of a URL
118 *
119 * @param {string} string to encode
120 * @returns {string} URL encoded string
121 */
122 encodeUserInfo: makeEncoder(charMap.userinfo),
123
124 /*
125 * URL encode the host portion of a URL
126 *
127 * @param {string} string to encode
128 * @returns {string} URL encoded string
129 */
130 encodeHost: makeEncoder(charMap.host),
131
132 /*
133 * URL encode the port portion of a URL
134 *
135 * @param {string} string to encode
136 * @returns {string} URL encoded string
137 */
138 encodePort: makeEncoder(charMap.port),
139
140 /*
141 * URL encode a path segment portion of a URL
142 *
143 * @param {string} string to encode
144 * @returns {string} URL encoded string
145 */
146 encodePathSegment: makeEncoder(charMap.segment),
147
148 /*
149 * URL encode the path portion of a URL
150 *
151 * @param {string} string to encode
152 * @returns {string} URL encoded string
153 */
154 encodePath: makeEncoder(charMap.path),
155
156 /*
157 * URL encode the query portion of a URL
158 *
159 * @param {string} string to encode
160 * @returns {string} URL encoded string
161 */
162 encodeQuery: makeEncoder(charMap.query),
163
164 /*
165 * URL encode the fragment portion of a URL
166 *
167 * @param {string} string to encode
168 * @returns {string} URL encoded string
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 // Boilerplate for AMD and Node
179));