UNPKG

11.9 kBJavaScriptView Raw
1'use strict';
2// TODO: in core-js@4, move /modules/ dependencies to public entries for better optimization by tools like `preset-env`
3require('../modules/es.array.iterator');
4var $ = require('../internals/export');
5var getBuiltIn = require('../internals/get-built-in');
6var USE_NATIVE_URL = require('../internals/native-url');
7var redefine = require('../internals/redefine');
8var redefineAll = require('../internals/redefine-all');
9var setToStringTag = require('../internals/set-to-string-tag');
10var createIteratorConstructor = require('../internals/create-iterator-constructor');
11var InternalStateModule = require('../internals/internal-state');
12var anInstance = require('../internals/an-instance');
13var hasOwn = require('../internals/has');
14var bind = require('../internals/function-bind-context');
15var classof = require('../internals/classof');
16var anObject = require('../internals/an-object');
17var isObject = require('../internals/is-object');
18var create = require('../internals/object-create');
19var createPropertyDescriptor = require('../internals/create-property-descriptor');
20var getIterator = require('../internals/get-iterator');
21var getIteratorMethod = require('../internals/get-iterator-method');
22var wellKnownSymbol = require('../internals/well-known-symbol');
23
24var $fetch = getBuiltIn('fetch');
25var Headers = getBuiltIn('Headers');
26var ITERATOR = wellKnownSymbol('iterator');
27var URL_SEARCH_PARAMS = 'URLSearchParams';
28var URL_SEARCH_PARAMS_ITERATOR = URL_SEARCH_PARAMS + 'Iterator';
29var setInternalState = InternalStateModule.set;
30var getInternalParamsState = InternalStateModule.getterFor(URL_SEARCH_PARAMS);
31var getInternalIteratorState = InternalStateModule.getterFor(URL_SEARCH_PARAMS_ITERATOR);
32
33var plus = /\+/g;
34var sequences = Array(4);
35
36var percentSequence = function (bytes) {
37 return sequences[bytes - 1] || (sequences[bytes - 1] = RegExp('((?:%[\\da-f]{2}){' + bytes + '})', 'gi'));
38};
39
40var percentDecode = function (sequence) {
41 try {
42 return decodeURIComponent(sequence);
43 } catch (error) {
44 return sequence;
45 }
46};
47
48var deserialize = function (it) {
49 var result = it.replace(plus, ' ');
50 var bytes = 4;
51 try {
52 return decodeURIComponent(result);
53 } catch (error) {
54 while (bytes) {
55 result = result.replace(percentSequence(bytes--), percentDecode);
56 }
57 return result;
58 }
59};
60
61var find = /[!'()~]|%20/g;
62
63var replace = {
64 '!': '%21',
65 "'": '%27',
66 '(': '%28',
67 ')': '%29',
68 '~': '%7E',
69 '%20': '+'
70};
71
72var replacer = function (match) {
73 return replace[match];
74};
75
76var serialize = function (it) {
77 return encodeURIComponent(it).replace(find, replacer);
78};
79
80var parseSearchParams = function (result, query) {
81 if (query) {
82 var attributes = query.split('&');
83 var index = 0;
84 var attribute, entry;
85 while (index < attributes.length) {
86 attribute = attributes[index++];
87 if (attribute.length) {
88 entry = attribute.split('=');
89 result.push({
90 key: deserialize(entry.shift()),
91 value: deserialize(entry.join('='))
92 });
93 }
94 }
95 }
96};
97
98var updateSearchParams = function (query) {
99 this.entries.length = 0;
100 parseSearchParams(this.entries, query);
101};
102
103var validateArgumentsLength = function (passed, required) {
104 if (passed < required) throw TypeError('Not enough arguments');
105};
106
107var URLSearchParamsIterator = createIteratorConstructor(function Iterator(params, kind) {
108 setInternalState(this, {
109 type: URL_SEARCH_PARAMS_ITERATOR,
110 iterator: getIterator(getInternalParamsState(params).entries),
111 kind: kind
112 });
113}, 'Iterator', function next() {
114 var state = getInternalIteratorState(this);
115 var kind = state.kind;
116 var step = state.iterator.next();
117 var entry = step.value;
118 if (!step.done) {
119 step.value = kind === 'keys' ? entry.key : kind === 'values' ? entry.value : [entry.key, entry.value];
120 } return step;
121});
122
123// `URLSearchParams` constructor
124// https://url.spec.whatwg.org/#interface-urlsearchparams
125var URLSearchParamsConstructor = function URLSearchParams(/* init */) {
126 anInstance(this, URLSearchParamsConstructor, URL_SEARCH_PARAMS);
127 var init = arguments.length > 0 ? arguments[0] : undefined;
128 var that = this;
129 var entries = [];
130 var iteratorMethod, iterator, next, step, entryIterator, entryNext, first, second, key;
131
132 setInternalState(that, {
133 type: URL_SEARCH_PARAMS,
134 entries: entries,
135 updateURL: function () { /* empty */ },
136 updateSearchParams: updateSearchParams
137 });
138
139 if (init !== undefined) {
140 if (isObject(init)) {
141 iteratorMethod = getIteratorMethod(init);
142 if (typeof iteratorMethod === 'function') {
143 iterator = iteratorMethod.call(init);
144 next = iterator.next;
145 while (!(step = next.call(iterator)).done) {
146 entryIterator = getIterator(anObject(step.value));
147 entryNext = entryIterator.next;
148 if (
149 (first = entryNext.call(entryIterator)).done ||
150 (second = entryNext.call(entryIterator)).done ||
151 !entryNext.call(entryIterator).done
152 ) throw TypeError('Expected sequence with length 2');
153 entries.push({ key: first.value + '', value: second.value + '' });
154 }
155 } else for (key in init) if (hasOwn(init, key)) entries.push({ key: key, value: init[key] + '' });
156 } else {
157 parseSearchParams(entries, typeof init === 'string' ? init.charAt(0) === '?' ? init.slice(1) : init : init + '');
158 }
159 }
160};
161
162var URLSearchParamsPrototype = URLSearchParamsConstructor.prototype;
163
164redefineAll(URLSearchParamsPrototype, {
165 // `URLSearchParams.prototype.append` method
166 // https://url.spec.whatwg.org/#dom-urlsearchparams-append
167 append: function append(name, value) {
168 validateArgumentsLength(arguments.length, 2);
169 var state = getInternalParamsState(this);
170 state.entries.push({ key: name + '', value: value + '' });
171 state.updateURL();
172 },
173 // `URLSearchParams.prototype.delete` method
174 // https://url.spec.whatwg.org/#dom-urlsearchparams-delete
175 'delete': function (name) {
176 validateArgumentsLength(arguments.length, 1);
177 var state = getInternalParamsState(this);
178 var entries = state.entries;
179 var key = name + '';
180 var index = 0;
181 while (index < entries.length) {
182 if (entries[index].key === key) entries.splice(index, 1);
183 else index++;
184 }
185 state.updateURL();
186 },
187 // `URLSearchParams.prototype.get` method
188 // https://url.spec.whatwg.org/#dom-urlsearchparams-get
189 get: function get(name) {
190 validateArgumentsLength(arguments.length, 1);
191 var entries = getInternalParamsState(this).entries;
192 var key = name + '';
193 var index = 0;
194 for (; index < entries.length; index++) {
195 if (entries[index].key === key) return entries[index].value;
196 }
197 return null;
198 },
199 // `URLSearchParams.prototype.getAll` method
200 // https://url.spec.whatwg.org/#dom-urlsearchparams-getall
201 getAll: function getAll(name) {
202 validateArgumentsLength(arguments.length, 1);
203 var entries = getInternalParamsState(this).entries;
204 var key = name + '';
205 var result = [];
206 var index = 0;
207 for (; index < entries.length; index++) {
208 if (entries[index].key === key) result.push(entries[index].value);
209 }
210 return result;
211 },
212 // `URLSearchParams.prototype.has` method
213 // https://url.spec.whatwg.org/#dom-urlsearchparams-has
214 has: function has(name) {
215 validateArgumentsLength(arguments.length, 1);
216 var entries = getInternalParamsState(this).entries;
217 var key = name + '';
218 var index = 0;
219 while (index < entries.length) {
220 if (entries[index++].key === key) return true;
221 }
222 return false;
223 },
224 // `URLSearchParams.prototype.set` method
225 // https://url.spec.whatwg.org/#dom-urlsearchparams-set
226 set: function set(name, value) {
227 validateArgumentsLength(arguments.length, 1);
228 var state = getInternalParamsState(this);
229 var entries = state.entries;
230 var found = false;
231 var key = name + '';
232 var val = value + '';
233 var index = 0;
234 var entry;
235 for (; index < entries.length; index++) {
236 entry = entries[index];
237 if (entry.key === key) {
238 if (found) entries.splice(index--, 1);
239 else {
240 found = true;
241 entry.value = val;
242 }
243 }
244 }
245 if (!found) entries.push({ key: key, value: val });
246 state.updateURL();
247 },
248 // `URLSearchParams.prototype.sort` method
249 // https://url.spec.whatwg.org/#dom-urlsearchparams-sort
250 sort: function sort() {
251 var state = getInternalParamsState(this);
252 var entries = state.entries;
253 // Array#sort is not stable in some engines
254 var slice = entries.slice();
255 var entry, entriesIndex, sliceIndex;
256 entries.length = 0;
257 for (sliceIndex = 0; sliceIndex < slice.length; sliceIndex++) {
258 entry = slice[sliceIndex];
259 for (entriesIndex = 0; entriesIndex < sliceIndex; entriesIndex++) {
260 if (entries[entriesIndex].key > entry.key) {
261 entries.splice(entriesIndex, 0, entry);
262 break;
263 }
264 }
265 if (entriesIndex === sliceIndex) entries.push(entry);
266 }
267 state.updateURL();
268 },
269 // `URLSearchParams.prototype.forEach` method
270 forEach: function forEach(callback /* , thisArg */) {
271 var entries = getInternalParamsState(this).entries;
272 var boundFunction = bind(callback, arguments.length > 1 ? arguments[1] : undefined, 3);
273 var index = 0;
274 var entry;
275 while (index < entries.length) {
276 entry = entries[index++];
277 boundFunction(entry.value, entry.key, this);
278 }
279 },
280 // `URLSearchParams.prototype.keys` method
281 keys: function keys() {
282 return new URLSearchParamsIterator(this, 'keys');
283 },
284 // `URLSearchParams.prototype.values` method
285 values: function values() {
286 return new URLSearchParamsIterator(this, 'values');
287 },
288 // `URLSearchParams.prototype.entries` method
289 entries: function entries() {
290 return new URLSearchParamsIterator(this, 'entries');
291 }
292}, { enumerable: true });
293
294// `URLSearchParams.prototype[@@iterator]` method
295redefine(URLSearchParamsPrototype, ITERATOR, URLSearchParamsPrototype.entries);
296
297// `URLSearchParams.prototype.toString` method
298// https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
299redefine(URLSearchParamsPrototype, 'toString', function toString() {
300 var entries = getInternalParamsState(this).entries;
301 var result = [];
302 var index = 0;
303 var entry;
304 while (index < entries.length) {
305 entry = entries[index++];
306 result.push(serialize(entry.key) + '=' + serialize(entry.value));
307 } return result.join('&');
308}, { enumerable: true });
309
310setToStringTag(URLSearchParamsConstructor, URL_SEARCH_PARAMS);
311
312$({ global: true, forced: !USE_NATIVE_URL }, {
313 URLSearchParams: URLSearchParamsConstructor
314});
315
316// Wrap `fetch` for correct work with polyfilled `URLSearchParams`
317// https://github.com/zloirock/core-js/issues/674
318if (!USE_NATIVE_URL && typeof $fetch == 'function' && typeof Headers == 'function') {
319 $({ global: true, enumerable: true, forced: true }, {
320 fetch: function fetch(input /* , init */) {
321 var args = [input];
322 var init, body, headers;
323 if (arguments.length > 1) {
324 init = arguments[1];
325 if (isObject(init)) {
326 body = init.body;
327 if (classof(body) === URL_SEARCH_PARAMS) {
328 headers = init.headers ? new Headers(init.headers) : new Headers();
329 if (!headers.has('content-type')) {
330 headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
331 }
332 init = create(init, {
333 body: createPropertyDescriptor(0, String(body)),
334 headers: createPropertyDescriptor(0, headers)
335 });
336 }
337 }
338 args.push(init);
339 } return $fetch.apply(this, args);
340 }
341 });
342}
343
344module.exports = {
345 URLSearchParams: URLSearchParamsConstructor,
346 getState: getInternalParamsState
347};