source: imaps-frontend/node_modules/core-js/modules/web.url-search-params.constructor.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 16.7 KB
Line 
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');
4require('../modules/es.string.from-code-point');
5var $ = require('../internals/export');
6var globalThis = require('../internals/global-this');
7var safeGetBuiltIn = require('../internals/safe-get-built-in');
8var getBuiltIn = require('../internals/get-built-in');
9var call = require('../internals/function-call');
10var uncurryThis = require('../internals/function-uncurry-this');
11var DESCRIPTORS = require('../internals/descriptors');
12var USE_NATIVE_URL = require('../internals/url-constructor-detection');
13var defineBuiltIn = require('../internals/define-built-in');
14var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
15var defineBuiltIns = require('../internals/define-built-ins');
16var setToStringTag = require('../internals/set-to-string-tag');
17var createIteratorConstructor = require('../internals/iterator-create-constructor');
18var InternalStateModule = require('../internals/internal-state');
19var anInstance = require('../internals/an-instance');
20var isCallable = require('../internals/is-callable');
21var hasOwn = require('../internals/has-own-property');
22var bind = require('../internals/function-bind-context');
23var classof = require('../internals/classof');
24var anObject = require('../internals/an-object');
25var isObject = require('../internals/is-object');
26var $toString = require('../internals/to-string');
27var create = require('../internals/object-create');
28var createPropertyDescriptor = require('../internals/create-property-descriptor');
29var getIterator = require('../internals/get-iterator');
30var getIteratorMethod = require('../internals/get-iterator-method');
31var createIterResultObject = require('../internals/create-iter-result-object');
32var validateArgumentsLength = require('../internals/validate-arguments-length');
33var wellKnownSymbol = require('../internals/well-known-symbol');
34var arraySort = require('../internals/array-sort');
35
36var ITERATOR = wellKnownSymbol('iterator');
37var URL_SEARCH_PARAMS = 'URLSearchParams';
38var URL_SEARCH_PARAMS_ITERATOR = URL_SEARCH_PARAMS + 'Iterator';
39var setInternalState = InternalStateModule.set;
40var getInternalParamsState = InternalStateModule.getterFor(URL_SEARCH_PARAMS);
41var getInternalIteratorState = InternalStateModule.getterFor(URL_SEARCH_PARAMS_ITERATOR);
42
43var nativeFetch = safeGetBuiltIn('fetch');
44var NativeRequest = safeGetBuiltIn('Request');
45var Headers = safeGetBuiltIn('Headers');
46var RequestPrototype = NativeRequest && NativeRequest.prototype;
47var HeadersPrototype = Headers && Headers.prototype;
48var TypeError = globalThis.TypeError;
49var encodeURIComponent = globalThis.encodeURIComponent;
50var fromCharCode = String.fromCharCode;
51var fromCodePoint = getBuiltIn('String', 'fromCodePoint');
52var $parseInt = parseInt;
53var charAt = uncurryThis(''.charAt);
54var join = uncurryThis([].join);
55var push = uncurryThis([].push);
56var replace = uncurryThis(''.replace);
57var shift = uncurryThis([].shift);
58var splice = uncurryThis([].splice);
59var split = uncurryThis(''.split);
60var stringSlice = uncurryThis(''.slice);
61var exec = uncurryThis(/./.exec);
62
63var plus = /\+/g;
64var FALLBACK_REPLACER = '\uFFFD';
65var VALID_HEX = /^[0-9a-f]+$/i;
66
67var parseHexOctet = function (string, start) {
68 var substr = stringSlice(string, start, start + 2);
69 if (!exec(VALID_HEX, substr)) return NaN;
70
71 return $parseInt(substr, 16);
72};
73
74var getLeadingOnes = function (octet) {
75 var count = 0;
76 for (var mask = 0x80; mask > 0 && (octet & mask) !== 0; mask >>= 1) {
77 count++;
78 }
79 return count;
80};
81
82var utf8Decode = function (octets) {
83 var codePoint = null;
84
85 switch (octets.length) {
86 case 1:
87 codePoint = octets[0];
88 break;
89 case 2:
90 codePoint = (octets[0] & 0x1F) << 6 | (octets[1] & 0x3F);
91 break;
92 case 3:
93 codePoint = (octets[0] & 0x0F) << 12 | (octets[1] & 0x3F) << 6 | (octets[2] & 0x3F);
94 break;
95 case 4:
96 codePoint = (octets[0] & 0x07) << 18 | (octets[1] & 0x3F) << 12 | (octets[2] & 0x3F) << 6 | (octets[3] & 0x3F);
97 break;
98 }
99
100 return codePoint > 0x10FFFF ? null : codePoint;
101};
102
103var decode = function (input) {
104 input = replace(input, plus, ' ');
105 var length = input.length;
106 var result = '';
107 var i = 0;
108
109 while (i < length) {
110 var decodedChar = charAt(input, i);
111
112 if (decodedChar === '%') {
113 if (charAt(input, i + 1) === '%' || i + 3 > length) {
114 result += '%';
115 i++;
116 continue;
117 }
118
119 var octet = parseHexOctet(input, i + 1);
120
121 // eslint-disable-next-line no-self-compare -- NaN check
122 if (octet !== octet) {
123 result += decodedChar;
124 i++;
125 continue;
126 }
127
128 i += 2;
129 var byteSequenceLength = getLeadingOnes(octet);
130
131 if (byteSequenceLength === 0) {
132 decodedChar = fromCharCode(octet);
133 } else {
134 if (byteSequenceLength === 1 || byteSequenceLength > 4) {
135 result += FALLBACK_REPLACER;
136 i++;
137 continue;
138 }
139
140 var octets = [octet];
141 var sequenceIndex = 1;
142
143 while (sequenceIndex < byteSequenceLength) {
144 i++;
145 if (i + 3 > length || charAt(input, i) !== '%') break;
146
147 var nextByte = parseHexOctet(input, i + 1);
148
149 // eslint-disable-next-line no-self-compare -- NaN check
150 if (nextByte !== nextByte) {
151 i += 3;
152 break;
153 }
154 if (nextByte > 191 || nextByte < 128) break;
155
156 push(octets, nextByte);
157 i += 2;
158 sequenceIndex++;
159 }
160
161 if (octets.length !== byteSequenceLength) {
162 result += FALLBACK_REPLACER;
163 continue;
164 }
165
166 var codePoint = utf8Decode(octets);
167 if (codePoint === null) {
168 result += FALLBACK_REPLACER;
169 } else {
170 decodedChar = fromCodePoint(codePoint);
171 }
172 }
173 }
174
175 result += decodedChar;
176 i++;
177 }
178
179 return result;
180};
181
182var find = /[!'()~]|%20/g;
183
184var replacements = {
185 '!': '%21',
186 "'": '%27',
187 '(': '%28',
188 ')': '%29',
189 '~': '%7E',
190 '%20': '+'
191};
192
193var replacer = function (match) {
194 return replacements[match];
195};
196
197var serialize = function (it) {
198 return replace(encodeURIComponent(it), find, replacer);
199};
200
201var URLSearchParamsIterator = createIteratorConstructor(function Iterator(params, kind) {
202 setInternalState(this, {
203 type: URL_SEARCH_PARAMS_ITERATOR,
204 target: getInternalParamsState(params).entries,
205 index: 0,
206 kind: kind
207 });
208}, URL_SEARCH_PARAMS, function next() {
209 var state = getInternalIteratorState(this);
210 var target = state.target;
211 var index = state.index++;
212 if (!target || index >= target.length) {
213 state.target = null;
214 return createIterResultObject(undefined, true);
215 }
216 var entry = target[index];
217 switch (state.kind) {
218 case 'keys': return createIterResultObject(entry.key, false);
219 case 'values': return createIterResultObject(entry.value, false);
220 } return createIterResultObject([entry.key, entry.value], false);
221}, true);
222
223var URLSearchParamsState = function (init) {
224 this.entries = [];
225 this.url = null;
226
227 if (init !== undefined) {
228 if (isObject(init)) this.parseObject(init);
229 else this.parseQuery(typeof init == 'string' ? charAt(init, 0) === '?' ? stringSlice(init, 1) : init : $toString(init));
230 }
231};
232
233URLSearchParamsState.prototype = {
234 type: URL_SEARCH_PARAMS,
235 bindURL: function (url) {
236 this.url = url;
237 this.update();
238 },
239 parseObject: function (object) {
240 var entries = this.entries;
241 var iteratorMethod = getIteratorMethod(object);
242 var iterator, next, step, entryIterator, entryNext, first, second;
243
244 if (iteratorMethod) {
245 iterator = getIterator(object, iteratorMethod);
246 next = iterator.next;
247 while (!(step = call(next, iterator)).done) {
248 entryIterator = getIterator(anObject(step.value));
249 entryNext = entryIterator.next;
250 if (
251 (first = call(entryNext, entryIterator)).done ||
252 (second = call(entryNext, entryIterator)).done ||
253 !call(entryNext, entryIterator).done
254 ) throw new TypeError('Expected sequence with length 2');
255 push(entries, { key: $toString(first.value), value: $toString(second.value) });
256 }
257 } else for (var key in object) if (hasOwn(object, key)) {
258 push(entries, { key: key, value: $toString(object[key]) });
259 }
260 },
261 parseQuery: function (query) {
262 if (query) {
263 var entries = this.entries;
264 var attributes = split(query, '&');
265 var index = 0;
266 var attribute, entry;
267 while (index < attributes.length) {
268 attribute = attributes[index++];
269 if (attribute.length) {
270 entry = split(attribute, '=');
271 push(entries, {
272 key: decode(shift(entry)),
273 value: decode(join(entry, '='))
274 });
275 }
276 }
277 }
278 },
279 serialize: function () {
280 var entries = this.entries;
281 var result = [];
282 var index = 0;
283 var entry;
284 while (index < entries.length) {
285 entry = entries[index++];
286 push(result, serialize(entry.key) + '=' + serialize(entry.value));
287 } return join(result, '&');
288 },
289 update: function () {
290 this.entries.length = 0;
291 this.parseQuery(this.url.query);
292 },
293 updateURL: function () {
294 if (this.url) this.url.update();
295 }
296};
297
298// `URLSearchParams` constructor
299// https://url.spec.whatwg.org/#interface-urlsearchparams
300var URLSearchParamsConstructor = function URLSearchParams(/* init */) {
301 anInstance(this, URLSearchParamsPrototype);
302 var init = arguments.length > 0 ? arguments[0] : undefined;
303 var state = setInternalState(this, new URLSearchParamsState(init));
304 if (!DESCRIPTORS) this.size = state.entries.length;
305};
306
307var URLSearchParamsPrototype = URLSearchParamsConstructor.prototype;
308
309defineBuiltIns(URLSearchParamsPrototype, {
310 // `URLSearchParams.prototype.append` method
311 // https://url.spec.whatwg.org/#dom-urlsearchparams-append
312 append: function append(name, value) {
313 var state = getInternalParamsState(this);
314 validateArgumentsLength(arguments.length, 2);
315 push(state.entries, { key: $toString(name), value: $toString(value) });
316 if (!DESCRIPTORS) this.length++;
317 state.updateURL();
318 },
319 // `URLSearchParams.prototype.delete` method
320 // https://url.spec.whatwg.org/#dom-urlsearchparams-delete
321 'delete': function (name /* , value */) {
322 var state = getInternalParamsState(this);
323 var length = validateArgumentsLength(arguments.length, 1);
324 var entries = state.entries;
325 var key = $toString(name);
326 var $value = length < 2 ? undefined : arguments[1];
327 var value = $value === undefined ? $value : $toString($value);
328 var index = 0;
329 while (index < entries.length) {
330 var entry = entries[index];
331 if (entry.key === key && (value === undefined || entry.value === value)) {
332 splice(entries, index, 1);
333 if (value !== undefined) break;
334 } else index++;
335 }
336 if (!DESCRIPTORS) this.size = entries.length;
337 state.updateURL();
338 },
339 // `URLSearchParams.prototype.get` method
340 // https://url.spec.whatwg.org/#dom-urlsearchparams-get
341 get: function get(name) {
342 var entries = getInternalParamsState(this).entries;
343 validateArgumentsLength(arguments.length, 1);
344 var key = $toString(name);
345 var index = 0;
346 for (; index < entries.length; index++) {
347 if (entries[index].key === key) return entries[index].value;
348 }
349 return null;
350 },
351 // `URLSearchParams.prototype.getAll` method
352 // https://url.spec.whatwg.org/#dom-urlsearchparams-getall
353 getAll: function getAll(name) {
354 var entries = getInternalParamsState(this).entries;
355 validateArgumentsLength(arguments.length, 1);
356 var key = $toString(name);
357 var result = [];
358 var index = 0;
359 for (; index < entries.length; index++) {
360 if (entries[index].key === key) push(result, entries[index].value);
361 }
362 return result;
363 },
364 // `URLSearchParams.prototype.has` method
365 // https://url.spec.whatwg.org/#dom-urlsearchparams-has
366 has: function has(name /* , value */) {
367 var entries = getInternalParamsState(this).entries;
368 var length = validateArgumentsLength(arguments.length, 1);
369 var key = $toString(name);
370 var $value = length < 2 ? undefined : arguments[1];
371 var value = $value === undefined ? $value : $toString($value);
372 var index = 0;
373 while (index < entries.length) {
374 var entry = entries[index++];
375 if (entry.key === key && (value === undefined || entry.value === value)) return true;
376 }
377 return false;
378 },
379 // `URLSearchParams.prototype.set` method
380 // https://url.spec.whatwg.org/#dom-urlsearchparams-set
381 set: function set(name, value) {
382 var state = getInternalParamsState(this);
383 validateArgumentsLength(arguments.length, 1);
384 var entries = state.entries;
385 var found = false;
386 var key = $toString(name);
387 var val = $toString(value);
388 var index = 0;
389 var entry;
390 for (; index < entries.length; index++) {
391 entry = entries[index];
392 if (entry.key === key) {
393 if (found) splice(entries, index--, 1);
394 else {
395 found = true;
396 entry.value = val;
397 }
398 }
399 }
400 if (!found) push(entries, { key: key, value: val });
401 if (!DESCRIPTORS) this.size = entries.length;
402 state.updateURL();
403 },
404 // `URLSearchParams.prototype.sort` method
405 // https://url.spec.whatwg.org/#dom-urlsearchparams-sort
406 sort: function sort() {
407 var state = getInternalParamsState(this);
408 arraySort(state.entries, function (a, b) {
409 return a.key > b.key ? 1 : -1;
410 });
411 state.updateURL();
412 },
413 // `URLSearchParams.prototype.forEach` method
414 forEach: function forEach(callback /* , thisArg */) {
415 var entries = getInternalParamsState(this).entries;
416 var boundFunction = bind(callback, arguments.length > 1 ? arguments[1] : undefined);
417 var index = 0;
418 var entry;
419 while (index < entries.length) {
420 entry = entries[index++];
421 boundFunction(entry.value, entry.key, this);
422 }
423 },
424 // `URLSearchParams.prototype.keys` method
425 keys: function keys() {
426 return new URLSearchParamsIterator(this, 'keys');
427 },
428 // `URLSearchParams.prototype.values` method
429 values: function values() {
430 return new URLSearchParamsIterator(this, 'values');
431 },
432 // `URLSearchParams.prototype.entries` method
433 entries: function entries() {
434 return new URLSearchParamsIterator(this, 'entries');
435 }
436}, { enumerable: true });
437
438// `URLSearchParams.prototype[@@iterator]` method
439defineBuiltIn(URLSearchParamsPrototype, ITERATOR, URLSearchParamsPrototype.entries, { name: 'entries' });
440
441// `URLSearchParams.prototype.toString` method
442// https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
443defineBuiltIn(URLSearchParamsPrototype, 'toString', function toString() {
444 return getInternalParamsState(this).serialize();
445}, { enumerable: true });
446
447// `URLSearchParams.prototype.size` getter
448// https://github.com/whatwg/url/pull/734
449if (DESCRIPTORS) defineBuiltInAccessor(URLSearchParamsPrototype, 'size', {
450 get: function size() {
451 return getInternalParamsState(this).entries.length;
452 },
453 configurable: true,
454 enumerable: true
455});
456
457setToStringTag(URLSearchParamsConstructor, URL_SEARCH_PARAMS);
458
459$({ global: true, constructor: true, forced: !USE_NATIVE_URL }, {
460 URLSearchParams: URLSearchParamsConstructor
461});
462
463// Wrap `fetch` and `Request` for correct work with polyfilled `URLSearchParams`
464if (!USE_NATIVE_URL && isCallable(Headers)) {
465 var headersHas = uncurryThis(HeadersPrototype.has);
466 var headersSet = uncurryThis(HeadersPrototype.set);
467
468 var wrapRequestOptions = function (init) {
469 if (isObject(init)) {
470 var body = init.body;
471 var headers;
472 if (classof(body) === URL_SEARCH_PARAMS) {
473 headers = init.headers ? new Headers(init.headers) : new Headers();
474 if (!headersHas(headers, 'content-type')) {
475 headersSet(headers, 'content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
476 }
477 return create(init, {
478 body: createPropertyDescriptor(0, $toString(body)),
479 headers: createPropertyDescriptor(0, headers)
480 });
481 }
482 } return init;
483 };
484
485 if (isCallable(nativeFetch)) {
486 $({ global: true, enumerable: true, dontCallGetSet: true, forced: true }, {
487 fetch: function fetch(input /* , init */) {
488 return nativeFetch(input, arguments.length > 1 ? wrapRequestOptions(arguments[1]) : {});
489 }
490 });
491 }
492
493 if (isCallable(NativeRequest)) {
494 var RequestConstructor = function Request(input /* , init */) {
495 anInstance(this, RequestPrototype);
496 return new NativeRequest(input, arguments.length > 1 ? wrapRequestOptions(arguments[1]) : {});
497 };
498
499 RequestPrototype.constructor = RequestConstructor;
500 RequestConstructor.prototype = RequestPrototype;
501
502 $({ global: true, constructor: true, dontCallGetSet: true, forced: true }, {
503 Request: RequestConstructor
504 });
505 }
506}
507
508module.exports = {
509 URLSearchParams: URLSearchParamsConstructor,
510 getState: getInternalParamsState
511};
Note: See TracBrowser for help on using the repository browser.