Skip to content

Commit

Permalink
move some logic to state prototypes
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Nov 28, 2021
1 parent 7f6670f commit c4e8bb3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 77 deletions.
140 changes: 74 additions & 66 deletions packages/core-js/modules/web.url-search-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,29 +99,6 @@ var serialize = function (it) {
return replace(encodeURIComponent(it), find, replacer);
};

var parseSearchParams = function (result, 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(result, {
key: deserialize(shift(entry)),
value: deserialize(join(entry, '='))
});
}
}
}
};

var updateSearchParams = function (query) {
this.entries.length = 0;
parseSearchParams(this.entries, query);
};

var validateArgumentsLength = function (passed, required) {
if (passed < required) throw TypeError('Not enough arguments');
};
Expand All @@ -142,48 +119,86 @@ var URLSearchParamsIterator = createIteratorConstructor(function Iterator(params
} return step;
});

// `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;
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 (iteratorMethod) {
iterator = getIterator(init, 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(entries, { key: $toString(first.value), value: $toString(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;
},
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)) push(entries, { key: key, value: $toString(init[key]) });
} else {
parseSearchParams(
entries,
typeof init == 'string' ? charAt(init, 0) === '?' ? stringSlice(init, 1) : init : $toString(init)
);
}
}
},
toString: 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, {
Expand Down Expand Up @@ -310,14 +325,7 @@ redefine(URLSearchParamsPrototype, ITERATOR, URLSearchParamsPrototype.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++];
push(result, serialize(entry.key) + '=' + serialize(entry.value));
} return join(result, '&');
return getInternalParamsState(this).toString();
}, { enumerable: true });

setToStringTag(URLSearchParamsConstructor, URL_SEARCH_PARAMS);
Expand Down
31 changes: 20 additions & 11 deletions packages/core-js/modules/web.url.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,30 +750,39 @@ var parseURL = function (url, input, stateOverride, base) {
}
};

var URLState = function () {
var searchParams = getInternalSearchParamsState(new URLSearchParams());
this.searchParams = searchParams;
searchParams.bindURL(this);
};

URLState.prototype = {
type: 'URL',
update: function () {
this.query = this.searchParams.toString() || null;
}
};

// `URL` constructor
// https://url.spec.whatwg.org/#url-class
var URLConstructor = function URL(url /* , base */) {
var that = anInstance(this, URLPrototype);
var base = arguments.length > 1 ? arguments[1] : undefined;
var urlString = $toString(url);
var state = setInternalState(that, { type: 'URL' });
var state = setInternalState(that, new URLState());
var baseState, failure;
if (base !== undefined) {
try {
baseState = getInternalURLState(base);
} catch (error) {
failure = parseURL(baseState = {}, $toString(base));
baseState = {};
failure = parseURL(baseState, $toString(base));
if (failure) throw TypeError(failure);
}
}
failure = parseURL(state, urlString, null, baseState);
if (failure) throw TypeError(failure);
var searchParams = state.searchParams = new URLSearchParams();
var searchParamsState = getInternalSearchParamsState(searchParams);
searchParamsState.updateSearchParams(state.query);
searchParamsState.updateURL = function () {
state.query = $toString(searchParams) || null;
};
state.searchParams.update();
if (!DESCRIPTORS) {
that.href = call(serializeURL, that);
that.origin = call(getOrigin, that);
Expand Down Expand Up @@ -873,7 +882,7 @@ var getSearch = function () {
};

var getSearchParams = function () {
return getInternalURLState(this).searchParams;
return getInternalURLState(this).searchParams.facade;
};

var getHash = function () {
Expand All @@ -894,7 +903,7 @@ if (DESCRIPTORS) {
var urlString = $toString(href);
var failure = parseURL(url, urlString);
if (failure) throw TypeError(failure);
getInternalSearchParamsState(url.searchParams).updateSearchParams(url.query);
url.searchParams.update();
}),
// `URL.prototype.origin` getter
// https://url.spec.whatwg.org/#dom-url-origin
Expand Down Expand Up @@ -970,7 +979,7 @@ if (DESCRIPTORS) {
url.query = '';
parseURL(url, search, QUERY);
}
getInternalSearchParamsState(url.searchParams).updateSearchParams(url.query);
url.searchParams.update();
}),
// `URL.prototype.searchParams` getter
// https://url.spec.whatwg.org/#dom-url-searchparams
Expand Down

0 comments on commit c4e8bb3

Please sign in to comment.