url-search-params-delete
Advanced tools
Comparing version
26
index.js
(function () { | ||
if (window.URLSearchParams) { | ||
const originalDeleteFunction = URLSearchParams.prototype.delete; | ||
const deleteWithKey = URLSearchParams.prototype.delete; | ||
const newDeleteFunction = function (key, value) { | ||
const deleteWithKeyAndValue = function (key, value) { | ||
const entriesIterator = URLSearchParams.prototype.entries.call(this); | ||
@@ -13,3 +13,3 @@ const entries = [...entriesIterator]; | ||
const keys = [...keysIterator]; | ||
keys.forEach((k) => originalDeleteFunction.call(this, k)); | ||
keys.forEach((k) => deleteWithKey.call(this, k)); | ||
toBeRestored.forEach(([k, v]) => | ||
@@ -22,8 +22,24 @@ URLSearchParams.prototype.append.call(this, k, v) | ||
if (arguments.length > 1) { | ||
newDeleteFunction.call(this, key, value); | ||
return deleteWithKeyAndValue.call(this, key, value); | ||
} else { | ||
originalDeleteFunction.call(this, key); | ||
return deleteWithKey.call(this, key); | ||
} | ||
}; | ||
const hasWithKey = URLSearchParams.prototype.has; | ||
const hasWithKeyAndValue = function (key, value) { | ||
const entriesIterator = URLSearchParams.prototype.entries.call(this); | ||
const entries = [...entriesIterator]; | ||
return Boolean(entries.find(([k, v]) => k === key && v === value)); | ||
}; | ||
URLSearchParams.prototype.has = function (key, value) { | ||
if (arguments.length > 1) { | ||
return hasWithKeyAndValue.call(this, key, value); | ||
} else { | ||
return hasWithKey.call(this, key); | ||
} | ||
}; | ||
} | ||
})(); |
{ | ||
"name": "url-search-params-delete", | ||
"version": "0.1.2", | ||
"description": "A polyfill adding support for `.delete(key,value)` to `URLSearchParams` as discussed at https://github.com/whatwg/url/issues/335", | ||
"version": "0.2.0", | ||
"description": "A polyfill adding support for .delete(key,value) to URLSearchParams as discussed at https://github.com/whatwg/url/issues/335", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "URLSearchParams", |
@@ -17,1 +17,10 @@ # url-search-params-delete | ||
``` | ||
The polyfill also adds support for `.has(key,value)` (supporting a second argument for `URLSearchParams.has()`). | ||
```js | ||
const paramsString = "foo=1"; | ||
const searchParams = new URLSearchParams(paramsString); | ||
searchParams.has("foo", "1"); // true | ||
searchParams.has("foo", "2"); // false | ||
``` |
4374
116.32%5
66.67%73
192%26
52.94%