url-search-params-polyfill
Advanced tools
Comparing version 2.0.3 to 3.0.0
16
index.js
@@ -38,3 +38,2 @@ /** | ||
} | ||
this [__URLSearchParams__] = parseToDict(search); | ||
@@ -267,6 +266,5 @@ } | ||
if (typeof search === "object") { | ||
for (var i in search) { | ||
if (search.hasOwnProperty(i)) { | ||
var str = typeof search [i] === 'string' ? search [i] : JSON.stringify(search [i]); | ||
appendTo(dict, i, str); | ||
for (var key in search) { | ||
if (search.hasOwnProperty(key)) { | ||
appendTo(dict, key, search[key]) | ||
} | ||
@@ -301,6 +299,10 @@ } | ||
function appendTo(dict, name, value) { | ||
var val = typeof value === 'string' ? value : ( | ||
value !== null && typeof value.toString === 'function' ? value.toString() : JSON.stringify(value) | ||
) | ||
if (name in dict) { | ||
dict[name].push('' + value); | ||
dict[name].push(val); | ||
} else { | ||
dict[name] = ['' + value]; | ||
dict[name] = [val]; | ||
} | ||
@@ -307,0 +309,0 @@ } |
{ | ||
"name": "url-search-params-polyfill", | ||
"version": "2.0.3", | ||
"version": "3.0.0", | ||
"description": "a simple polyfill for javascript URLSearchParams", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/jerrybendy/url-search-params-polyfill", |
@@ -1,6 +0,6 @@ | ||
# URLSearchParams polyfill [![](https://img.shields.io/npm/v/url-search-params-polyfill.svg)](https://www.npmjs.com/package/url-search-params-polyfill) | ||
# URLSearchParams Polyfill [![](https://img.shields.io/npm/v/url-search-params-polyfill.svg)](https://www.npmjs.com/package/url-search-params-polyfill) | ||
This is a polyfill library for javascript's URLSearchParams class. This library has implemented all features from [MDN document](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams). | ||
This is a polyfill library for JavaScript's URLSearchParams class. This library has implemented all features from [MDN document](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams). | ||
This library can use for both browsers and nodeJs. | ||
This library can use for both browsers and Node.js. | ||
@@ -18,3 +18,3 @@ Some browsers have native URLSearchParams class support, but not full. The new `2.x` version detects if browsers have full feature support and extends it. | ||
For babel and es2015+, make sure to import the file: | ||
For Babel and ES2015+, make sure to import the file: | ||
@@ -25,3 +25,3 @@ ```javascript | ||
For es5: | ||
For ES5: | ||
@@ -41,19 +41,19 @@ ```javascript | ||
Use `URLSearchParams` directly. You can `new` an object from a string or an object. | ||
Use `URLSearchParams` directly. You can instantiate a new instance of `URLSearchParams` from a string or an object. | ||
```javascript | ||
// new an empty object | ||
var search1 = new URLSearchParams (); | ||
var search1 = new URLSearchParams(); | ||
// from a string | ||
var search2 = new URLSearchParams ("id=1&from=home"); | ||
var search2 = new URLSearchParams("id=1&from=home"); | ||
// from an object | ||
var search3 = new URLSearchParams ({id: 1, from: "home"}); | ||
var search3 = new URLSearchParams({ id: 1, from: "home" }); | ||
// from location.search, will remove first "?" automatically | ||
var search4 = new URLSearchParams (window.location.search); | ||
var search4 = new URLSearchParams(window.location.search); | ||
// from anther URLSearchParams object | ||
var search5 = new URLSearchParams (search2); | ||
var search5 = new URLSearchParams(search2); | ||
``` | ||
@@ -65,3 +65,3 @@ | ||
```javascript | ||
var search = new URLSearchParams (); | ||
var search = new URLSearchParams(); | ||
@@ -132,3 +132,3 @@ search.append("id", 1); | ||
```javascript | ||
for (var value of search.values()){ | ||
for (var value of search.values()) { | ||
console.log(value); | ||
@@ -149,5 +149,5 @@ } | ||
#### Use with fetch ([#18](https://github.com/jerrybendy/url-search-params-polyfill/issues/18)) | ||
Via [fetch spec](https://fetch.spec.whatwg.org/#body-mixin), when passing an `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 but no `URLSearchParams` have no this behavior. | ||
Via [fetch spec](https://fetch.spec.whatwg.org/#body-mixin), 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 ant not `URLSearchParams` support do not have this behavior. | ||
Via the data of [caniuse](https://caniuse.com/#search=fetch), there are many browsers support `fetch` but `URLSearchParams`. They are: | ||
Via the data of [caniuse](https://caniuse.com/#search=fetch), there are many browsers which support `fetch` but not `URLSearchParams`: | ||
@@ -158,6 +158,6 @@ | Edge | Chrome | Opera | Samsung Internet | QQ | Baidu | | ||
If you want to be compatible with these browsers, you should add a `Content-Type` header manually, like below (just an example): | ||
If you want to be compatible with these browsers, you should add a `Content-Type` header manually: | ||
```js | ||
function myFetch(url, {headers = {}, body}) { | ||
function myFetch(url, { headers = {}, body }) { | ||
headers = headers instanceof Headers ? headers : new Headers(headers); | ||
@@ -164,0 +164,0 @@ |
@@ -42,6 +42,10 @@ /** | ||
e: {f: "g"}, | ||
f: "hello" | ||
f: "hello", | ||
g: ['a', '2', false], | ||
h: { | ||
toString: function() {return 'h'} | ||
} | ||
}); | ||
expect(a.toString()).to.be.equal('a=1&b=true&c=null&d=%5B%5D&e=%7B%22f%22%3A%22g%22%7D&f=hello'); | ||
expect(a.toString()).to.be.equal('a=1&b=true&c=null&d=&e=%5Bobject+Object%5D&f=hello&g=a%2C2%2Cfalse&h=h'); | ||
}); | ||
@@ -279,2 +283,2 @@ | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25964
638