Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
url-search-params-polyfill
Advanced tools
Readme
This is a polyfill library for JavaScript's URLSearchParams
class.
URLSearchParams
and extend itThis can also be installed with npm
.
$ npm install url-search-params-polyfill --save
For Babel and ES2015+, make sure to import the file:
import 'url-search-params-polyfill';
For ES5:
require('url-search-params-polyfill');
For browser, copy the index.js
file to your project, and add a script
tag in your html:
<script src="index.js"></script>
Use URLSearchParams
directly. You can instantiate a new instance of URLSearchParams
from a string or an object.
// new an empty object
var search1 = new URLSearchParams();
// from a string
var search2 = new URLSearchParams("id=1&from=home");
// from an object
var search3 = new URLSearchParams({ id: 1, from: "home" });
// from location.search, will remove first "?" automatically
var search4 = new URLSearchParams(window.location.search);
// from anther URLSearchParams object
var search5 = new URLSearchParams(search2);
// from a sequence
var search6 = new URLSearchParams([["foo", 1], ["bar", 2]]);
var search = new URLSearchParams();
search.append("id", 1);
search.delete("id");
search.get("id");
search.getAll("id");
search.has("id");
search.set("id", 2);
search.toString();
search.sort();
search.forEach(function (item) {
console.log(item);
});
for (var key of search.keys()) {
console.log(key);
}
for (var value of search.values()) {
console.log(value);
}
for (var item of search) {
console.log('key: ' + item[0] + ', ' + 'value: ' + item[1]);
}
console.log(search.size)
Via fetch spec, when passing a URLSearchParams
object as a request body, the request should add a header with Content-Type: application/x-www-form-urlencoded; charset=UTF-8
, but browsers which have fetch
support and not URLSearchParams
support do not have this behavior.
Via the data of caniuse, there are many browsers which support fetch
but not URLSearchParams
:
Edge | Chrome | Opera | Samsung Internet | Baidu | |
---|---|---|---|---|---|
14 - 16 | 40 - 48 | 27 - 35 | 4 | 1.2 | 7.12 |
If you want to be compatible with these browsers, you should add a Content-Type
header manually:
function myFetch(url, { headers = {}, body }) {
headers = headers instanceof Headers ? headers : new Headers(headers);
if (body instanceof URLSearchParams) {
headers.set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
}
fetch(url, {
headers,
body
});
}
MIT license
FAQs
a simple polyfill for javascript URLSearchParams
The npm package url-search-params-polyfill receives a total of 428,534 weekly downloads. As such, url-search-params-polyfill popularity was classified as popular.
We found that url-search-params-polyfill demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.