// TODO: in core-js@4, move /modules/ dependencies to public entries for better optimization by tools like `preset-env`
require('../modules/es.array.iterator');
var $ = require('../internals/export');
+var global = require('../internals/global');
var getBuiltIn = require('../internals/get-built-in');
+var call = require('../internals/function-call');
+var uncurryThis = require('../internals/function-uncurry-this');
var USE_NATIVE_URL = require('../internals/native-url');
var redefine = require('../internals/redefine');
var redefineAll = require('../internals/redefine-all');
var createIteratorConstructor = require('../internals/create-iterator-constructor');
var InternalStateModule = require('../internals/internal-state');
var anInstance = require('../internals/an-instance');
-var hasOwn = require('../internals/has');
+var isCallable = require('../internals/is-callable');
+var hasOwn = require('../internals/has-own-property');
var bind = require('../internals/function-bind-context');
var classof = require('../internals/classof');
var anObject = require('../internals/an-object');
var isObject = require('../internals/is-object');
+var $toString = require('../internals/to-string');
var create = require('../internals/object-create');
var createPropertyDescriptor = require('../internals/create-property-descriptor');
var getIterator = require('../internals/get-iterator');
var getIteratorMethod = require('../internals/get-iterator-method');
var wellKnownSymbol = require('../internals/well-known-symbol');
+var arraySort = require('../internals/array-sort');
-var $fetch = getBuiltIn('fetch');
-var Headers = getBuiltIn('Headers');
var ITERATOR = wellKnownSymbol('iterator');
var URL_SEARCH_PARAMS = 'URLSearchParams';
var URL_SEARCH_PARAMS_ITERATOR = URL_SEARCH_PARAMS + 'Iterator';
var getInternalParamsState = InternalStateModule.getterFor(URL_SEARCH_PARAMS);
var getInternalIteratorState = InternalStateModule.getterFor(URL_SEARCH_PARAMS_ITERATOR);
+var n$Fetch = getBuiltIn('fetch');
+var N$Request = getBuiltIn('Request');
+var Headers = getBuiltIn('Headers');
+var RequestPrototype = N$Request && N$Request.prototype;
+var HeadersPrototype = Headers && Headers.prototype;
+var RegExp = global.RegExp;
+var TypeError = global.TypeError;
+var decodeURIComponent = global.decodeURIComponent;
+var encodeURIComponent = global.encodeURIComponent;
+var charAt = uncurryThis(''.charAt);
+var join = uncurryThis([].join);
+var push = uncurryThis([].push);
+var replace = uncurryThis(''.replace);
+var shift = uncurryThis([].shift);
+var splice = uncurryThis([].splice);
+var split = uncurryThis(''.split);
+var stringSlice = uncurryThis(''.slice);
+
var plus = /\+/g;
var sequences = Array(4);
};
var deserialize = function (it) {
- var result = it.replace(plus, ' ');
+ var result = replace(it, plus, ' ');
var bytes = 4;
try {
return decodeURIComponent(result);
} catch (error) {
while (bytes) {
- result = result.replace(percentSequence(bytes--), percentDecode);
+ result = replace(result, percentSequence(bytes--), percentDecode);
}
return result;
}
var find = /[!'()~]|%20/g;
-var replace = {
+var replacements = {
'!': '%21',
"'": '%27',
'(': '%28',
};
var replacer = function (match) {
- return replace[match];
+ return replacements[match];
};
var serialize = function (it) {
- return encodeURIComponent(it).replace(find, replacer);
-};
-
-var parseSearchParams = function (result, query) {
- if (query) {
- var attributes = query.split('&');
- var index = 0;
- var attribute, entry;
- while (index < attributes.length) {
- attribute = attributes[index++];
- if (attribute.length) {
- entry = attribute.split('=');
- result.push({
- key: deserialize(entry.shift()),
- value: deserialize(entry.join('='))
- });
- }
- }
- }
-};
-
-var updateSearchParams = function (query) {
- this.entries.length = 0;
- parseSearchParams(this.entries, query);
+ return replace(encodeURIComponent(it), find, replacer);
};
var validateArgumentsLength = function (passed, required) {
} return step;
});
-// `URLSearchParams` constructor
-// https://url.spec.whatwg.org/#interface-urlsearchparams
-var URLSearchParamsConstructor = function URLSearchParams(/* init */) {
- anInstance(this, URLSearchParamsConstructor, URL_SEARCH_PARAMS);
- var init = arguments.length > 0 ? arguments[0] : undefined;
- var that = this;
- var entries = [];
- var iteratorMethod, iterator, next, step, entryIterator, entryNext, first, second, key;
-
- setInternalState(that, {
- type: URL_SEARCH_PARAMS,
- entries: entries,
- updateURL: function () { /* empty */ },
- updateSearchParams: updateSearchParams
- });
+var URLSearchParamsState = function (init) {
+ this.entries = [];
+ this.url = null;
if (init !== undefined) {
- if (isObject(init)) {
- iteratorMethod = getIteratorMethod(init);
- if (typeof iteratorMethod === 'function') {
- iterator = iteratorMethod.call(init);
- next = iterator.next;
- while (!(step = next.call(iterator)).done) {
- entryIterator = getIterator(anObject(step.value));
- entryNext = entryIterator.next;
- if (
- (first = entryNext.call(entryIterator)).done ||
- (second = entryNext.call(entryIterator)).done ||
- !entryNext.call(entryIterator).done
- ) throw TypeError('Expected sequence with length 2');
- entries.push({ key: first.value + '', value: second.value + '' });
+ if (isObject(init)) this.parseObject(init);
+ else this.parseQuery(typeof init == 'string' ? charAt(init, 0) === '?' ? stringSlice(init, 1) : init : $toString(init));
+ }
+};
+
+URLSearchParamsState.prototype = {
+ type: URL_SEARCH_PARAMS,
+ bindURL: function (url) {
+ this.url = url;
+ this.update();
+ },
+ parseObject: function (object) {
+ var iteratorMethod = getIteratorMethod(object);
+ var iterator, next, step, entryIterator, entryNext, first, second;
+
+ if (iteratorMethod) {
+ iterator = getIterator(object, iteratorMethod);
+ next = iterator.next;
+ while (!(step = call(next, iterator)).done) {
+ entryIterator = getIterator(anObject(step.value));
+ entryNext = entryIterator.next;
+ if (
+ (first = call(entryNext, entryIterator)).done ||
+ (second = call(entryNext, entryIterator)).done ||
+ !call(entryNext, entryIterator).done
+ ) throw TypeError('Expected sequence with length 2');
+ push(this.entries, { key: $toString(first.value), value: $toString(second.value) });
+ }
+ } else for (var key in object) if (hasOwn(object, key)) {
+ push(this.entries, { key: key, value: $toString(object[key]) });
+ }
+ },
+ parseQuery: function (query) {
+ if (query) {
+ var attributes = split(query, '&');
+ var index = 0;
+ var attribute, entry;
+ while (index < attributes.length) {
+ attribute = attributes[index++];
+ if (attribute.length) {
+ entry = split(attribute, '=');
+ push(this.entries, {
+ key: deserialize(shift(entry)),
+ value: deserialize(join(entry, '='))
+ });
}
- } else for (key in init) if (hasOwn(init, key)) entries.push({ key: key, value: init[key] + '' });
- } else {
- parseSearchParams(entries, typeof init === 'string' ? init.charAt(0) === '?' ? init.slice(1) : init : init + '');
+ }
}
+ },
+ serialize: function () {
+ var entries = this.entries;
+ var result = [];
+ var index = 0;
+ var entry;
+ while (index < entries.length) {
+ entry = entries[index++];
+ push(result, serialize(entry.key) + '=' + serialize(entry.value));
+ } return join(result, '&');
+ },
+ update: function () {
+ this.entries.length = 0;
+ this.parseQuery(this.url.query);
+ },
+ updateURL: function () {
+ if (this.url) this.url.update();
}
};
+// `URLSearchParams` constructor
+// https://url.spec.whatwg.org/#interface-urlsearchparams
+var URLSearchParamsConstructor = function URLSearchParams(/* init */) {
+ anInstance(this, URLSearchParamsPrototype);
+ var init = arguments.length > 0 ? arguments[0] : undefined;
+ setInternalState(this, new URLSearchParamsState(init));
+};
+
var URLSearchParamsPrototype = URLSearchParamsConstructor.prototype;
redefineAll(URLSearchParamsPrototype, {
append: function append(name, value) {
validateArgumentsLength(arguments.length, 2);
var state = getInternalParamsState(this);
- state.entries.push({ key: name + '', value: value + '' });
+ push(state.entries, { key: $toString(name), value: $toString(value) });
state.updateURL();
},
// `URLSearchParams.prototype.delete` method
validateArgumentsLength(arguments.length, 1);
var state = getInternalParamsState(this);
var entries = state.entries;
- var key = name + '';
+ var key = $toString(name);
var index = 0;
while (index < entries.length) {
- if (entries[index].key === key) entries.splice(index, 1);
+ if (entries[index].key === key) splice(entries, index, 1);
else index++;
}
state.updateURL();
get: function get(name) {
validateArgumentsLength(arguments.length, 1);
var entries = getInternalParamsState(this).entries;
- var key = name + '';
+ var key = $toString(name);
var index = 0;
for (; index < entries.length; index++) {
if (entries[index].key === key) return entries[index].value;
getAll: function getAll(name) {
validateArgumentsLength(arguments.length, 1);
var entries = getInternalParamsState(this).entries;
- var key = name + '';
+ var key = $toString(name);
var result = [];
var index = 0;
for (; index < entries.length; index++) {
- if (entries[index].key === key) result.push(entries[index].value);
+ if (entries[index].key === key) push(result, entries[index].value);
}
return result;
},
has: function has(name) {
validateArgumentsLength(arguments.length, 1);
var entries = getInternalParamsState(this).entries;
- var key = name + '';
+ var key = $toString(name);
var index = 0;
while (index < entries.length) {
if (entries[index++].key === key) return true;
var state = getInternalParamsState(this);
var entries = state.entries;
var found = false;
- var key = name + '';
- var val = value + '';
+ var key = $toString(name);
+ var val = $toString(value);
var index = 0;
var entry;
for (; index < entries.length; index++) {
entry = entries[index];
if (entry.key === key) {
- if (found) entries.splice(index--, 1);
+ if (found) splice(entries, index--, 1);
else {
found = true;
entry.value = val;
}
}
}
- if (!found) entries.push({ key: key, value: val });
+ if (!found) push(entries, { key: key, value: val });
state.updateURL();
},
// `URLSearchParams.prototype.sort` method
// https://url.spec.whatwg.org/#dom-urlsearchparams-sort
sort: function sort() {
var state = getInternalParamsState(this);
- var entries = state.entries;
- // Array#sort is not stable in some engines
- var slice = entries.slice();
- var entry, entriesIndex, sliceIndex;
- entries.length = 0;
- for (sliceIndex = 0; sliceIndex < slice.length; sliceIndex++) {
- entry = slice[sliceIndex];
- for (entriesIndex = 0; entriesIndex < sliceIndex; entriesIndex++) {
- if (entries[entriesIndex].key > entry.key) {
- entries.splice(entriesIndex, 0, entry);
- break;
- }
- }
- if (entriesIndex === sliceIndex) entries.push(entry);
- }
+ arraySort(state.entries, function (a, b) {
+ return a.key > b.key ? 1 : -1;
+ });
state.updateURL();
},
// `URLSearchParams.prototype.forEach` method
forEach: function forEach(callback /* , thisArg */) {
var entries = getInternalParamsState(this).entries;
- var boundFunction = bind(callback, arguments.length > 1 ? arguments[1] : undefined, 3);
+ var boundFunction = bind(callback, arguments.length > 1 ? arguments[1] : undefined);
var index = 0;
var entry;
while (index < entries.length) {
}, { enumerable: true });
// `URLSearchParams.prototype[@@iterator]` method
-redefine(URLSearchParamsPrototype, ITERATOR, URLSearchParamsPrototype.entries);
+redefine(URLSearchParamsPrototype, ITERATOR, URLSearchParamsPrototype.entries, { name: 'entries' });
// `URLSearchParams.prototype.toString` method
// https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
redefine(URLSearchParamsPrototype, 'toString', function toString() {
- var entries = getInternalParamsState(this).entries;
- var result = [];
- var index = 0;
- var entry;
- while (index < entries.length) {
- entry = entries[index++];
- result.push(serialize(entry.key) + '=' + serialize(entry.value));
- } return result.join('&');
+ return getInternalParamsState(this).serialize();
}, { enumerable: true });
setToStringTag(URLSearchParamsConstructor, URL_SEARCH_PARAMS);
URLSearchParams: URLSearchParamsConstructor
});
-// Wrap `fetch` for correct work with polyfilled `URLSearchParams`
-// https://github.com/zloirock/core-js/issues/674
-if (!USE_NATIVE_URL && typeof $fetch == 'function' && typeof Headers == 'function') {
- $({ global: true, enumerable: true, forced: true }, {
- fetch: function fetch(input /* , init */) {
- var args = [input];
- var init, body, headers;
- if (arguments.length > 1) {
- init = arguments[1];
- if (isObject(init)) {
- body = init.body;
- if (classof(body) === URL_SEARCH_PARAMS) {
- headers = init.headers ? new Headers(init.headers) : new Headers();
- if (!headers.has('content-type')) {
- headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
- }
- init = create(init, {
- body: createPropertyDescriptor(0, String(body)),
- headers: createPropertyDescriptor(0, headers)
- });
- }
+// Wrap `fetch` and `Request` for correct work with polyfilled `URLSearchParams`
+if (!USE_NATIVE_URL && isCallable(Headers)) {
+ var headersHas = uncurryThis(HeadersPrototype.has);
+ var headersSet = uncurryThis(HeadersPrototype.set);
+
+ var wrapRequestOptions = function (init) {
+ if (isObject(init)) {
+ var body = init.body;
+ var headers;
+ if (classof(body) === URL_SEARCH_PARAMS) {
+ headers = init.headers ? new Headers(init.headers) : new Headers();
+ if (!headersHas(headers, 'content-type')) {
+ headersSet(headers, 'content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
}
- args.push(init);
- } return $fetch.apply(this, args);
- }
- });
+ return create(init, {
+ body: createPropertyDescriptor(0, $toString(body)),
+ headers: createPropertyDescriptor(0, headers)
+ });
+ }
+ } return init;
+ };
+
+ if (isCallable(n$Fetch)) {
+ $({ global: true, enumerable: true, forced: true }, {
+ fetch: function fetch(input /* , init */) {
+ return n$Fetch(input, arguments.length > 1 ? wrapRequestOptions(arguments[1]) : {});
+ }
+ });
+ }
+
+ if (isCallable(N$Request)) {
+ var RequestConstructor = function Request(input /* , init */) {
+ anInstance(this, RequestPrototype);
+ return new N$Request(input, arguments.length > 1 ? wrapRequestOptions(arguments[1]) : {});
+ };
+
+ RequestPrototype.constructor = RequestConstructor;
+ RequestConstructor.prototype = RequestPrototype;
+
+ $({ global: true, forced: true }, {
+ Request: RequestConstructor
+ });
+ }
}
module.exports = {