url-search-params-polyfill
Advanced tools
Comparing version 1.0.2 to 1.1.0
145
index.js
@@ -18,5 +18,13 @@ /** | ||
var __URLSearchParams__ = "__URLSearchParams__"; | ||
var __URLSearchParams__ = "__URLSearchParams__", | ||
prototype = URLSearchParams.prototype, | ||
iterable = !!(self.Symbol && self.Symbol.iterator); | ||
/** | ||
* Make a URLSearchParams instance | ||
* | ||
* @param {object|string|URLSearchParams} search | ||
* @constructor | ||
*/ | ||
function URLSearchParams (search) { | ||
@@ -27,2 +35,7 @@ search = search || ""; | ||
// support construct object with another URLSearchParams instance | ||
if (search instanceof URLSearchParams) { | ||
search = search.toString(); | ||
} | ||
if (typeof search === "object") { | ||
@@ -59,4 +72,9 @@ for (var i in search) { | ||
URLSearchParams.prototype.append = function(name, value) { | ||
/** | ||
* Appends a specified key/value pair as a new search parameter. | ||
* | ||
* @param {string} name | ||
* @param {string} value | ||
*/ | ||
prototype.append = function(name, value) { | ||
var dict = this [__URLSearchParams__]; | ||
@@ -70,7 +88,19 @@ if (name in dict) { | ||
URLSearchParams.prototype.delete = function (name) { | ||
/** | ||
* Deletes the given search parameter, and its associated value, | ||
* from the list of all search parameters. | ||
* | ||
* @param {string} name | ||
*/ | ||
prototype.delete = function (name) { | ||
delete this [__URLSearchParams__] [name]; | ||
}; | ||
URLSearchParams.prototype.get = function (name) { | ||
/** | ||
* Returns the first value associated to the given search parameter. | ||
* | ||
* @param {string} name | ||
* @returns {string|null} | ||
*/ | ||
prototype.get = function (name) { | ||
var dict = this [__URLSearchParams__]; | ||
@@ -80,3 +110,9 @@ return name in dict ? dict[name][0] : null; | ||
URLSearchParams.prototype.getAll = function (name) { | ||
/** | ||
* Returns all the values association with a given search parameter. | ||
* | ||
* @param {string} name | ||
* @returns {Array} | ||
*/ | ||
prototype.getAll = function (name) { | ||
var dict = this [__URLSearchParams__]; | ||
@@ -86,11 +122,31 @@ return name in dict ? dict [name].slice(0) : []; | ||
URLSearchParams.prototype.has = function (name) { | ||
/** | ||
* Returns a Boolean indicating if such a search parameter exists. | ||
* | ||
* @param {string} name | ||
* @returns {boolean} | ||
*/ | ||
prototype.has = function (name) { | ||
return name in this [__URLSearchParams__]; | ||
}; | ||
URLSearchParams.prototype.set = function set(name, value) { | ||
/** | ||
* Sets the value associated to a given search parameter to | ||
* the given value. If there were several values, delete the | ||
* others. | ||
* | ||
* @param {string} name | ||
* @param {string} value | ||
*/ | ||
prototype.set = function set(name, value) { | ||
this [__URLSearchParams__][name] = ['' + value]; | ||
}; | ||
URLSearchParams.prototype.forEach = function (callback, thisArg) { | ||
/** | ||
* | ||
* | ||
* @param {function} callback | ||
* @param {object} thisArg | ||
*/ | ||
prototype.forEach = function (callback, thisArg) { | ||
var dict = this [__URLSearchParams__]; | ||
@@ -104,3 +160,8 @@ Object.getOwnPropertyNames(dict).forEach(function(name) { | ||
URLSearchParams.prototype.toString = function () { | ||
/** | ||
* Returns a string containg a query string suitable for use in a URL. | ||
* | ||
* @returns {string} | ||
*/ | ||
prototype.toString = function () { | ||
var dict = this[__URLSearchParams__], query = [], i, key, name, value; | ||
@@ -116,3 +177,50 @@ for (key in dict) { | ||
/** | ||
* Returns an iterator allowing to go through all keys of | ||
* the key/value pairs contained in this object. | ||
* | ||
* @returns {function} | ||
*/ | ||
prototype.keys = function () { | ||
var items = []; | ||
this.forEach(function (item, name) { | ||
items.push([name]); | ||
}); | ||
return makeIterator(items); | ||
}; | ||
/** | ||
* Returns an iterator allowing to go through all values of | ||
* the key/value pairs contained in this object. | ||
* | ||
* @returns {function} | ||
*/ | ||
prototype.values = function () { | ||
var items = []; | ||
this.forEach(function (item) { | ||
items.push([item]); | ||
}); | ||
return makeIterator(items); | ||
}; | ||
/** | ||
* Returns an iterator allowing to go through all key/value | ||
* pairs contained in this object. | ||
* | ||
* @returns {function} | ||
*/ | ||
prototype.entries = function () { | ||
var items = []; | ||
this.forEach(function (item, name) { | ||
items.push([name, item]); | ||
}); | ||
return makeIterator(items); | ||
}; | ||
if (iterable) { | ||
prototype[self.Symbol.iterator] = prototype.entries; | ||
} | ||
function encode(str) { | ||
@@ -137,4 +245,19 @@ var replace = { | ||
function makeIterator(arr) { | ||
var iterator = { | ||
next: function () { | ||
var value = arr.shift(); | ||
return {done: value === undefined, value: value}; | ||
} | ||
}; | ||
if (iterable) { | ||
iterator[self.Symbol.iterator] = function () { | ||
return iterator; | ||
}; | ||
} | ||
return iterator; | ||
} | ||
self.URLSearchParams = URLSearchParams; | ||
@@ -145,2 +268,2 @@ | ||
})(typeof self !== 'undefined' ? self : this); | ||
})(typeof global !== 'undefined' ? global : (typeof window !== 'undefined' ? window : this)); |
{ | ||
"name": "url-search-params-polyfill", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "a simple polyfill for javascript URLSearchParams", | ||
@@ -13,2 +13,3 @@ "homepage": "https://github.com/jerrybendy/url-search-params-polyfill", | ||
"test": "mocha test/*.test.js", | ||
"testWatch": "mocha test/*.test.js -w", | ||
"istanbul": "istanbul cover _mocha -- test/*.test.js" | ||
@@ -15,0 +16,0 @@ }, |
@@ -1,4 +0,4 @@ | ||
# URLSearchParams 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 simple polyfill library for javascript's URLSearchParams class. | ||
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). | ||
@@ -21,2 +21,15 @@ | ||
For es5: | ||
```javascript | ||
require('url-search-params-polyfill'); | ||
``` | ||
For browser, copy the `index.js` file to your project, and add a `script` tag in your html: | ||
```html | ||
<script src="index.js"></script> | ||
``` | ||
## Usage | ||
@@ -39,2 +52,4 @@ | ||
// from anther URLSearchParams object | ||
var search5 = new URLSearchParams (search2); | ||
``` | ||
@@ -95,4 +110,31 @@ | ||
### keys | ||
```javascript | ||
for (var key of search.keys()) { | ||
console.log(key); | ||
} | ||
``` | ||
### values | ||
```javascript | ||
for (var value of search.values()){ | ||
console.log(value); | ||
} | ||
``` | ||
### for...of | ||
```javascript | ||
for (var item of search) { | ||
console.log('key: ' + item[0] + ', ' + 'value: ' + item[1]; | ||
} | ||
``` | ||
## LICENSE | ||
MIT license | ||
@@ -8,3 +8,3 @@ /** | ||
var URLSearchParams = require('../index').URLSearchParams; | ||
require('../index'); | ||
var expect = require('chai').expect; | ||
@@ -54,2 +54,8 @@ | ||
}); | ||
it('使用另一个 URLSearchParams 对象构造', function () { | ||
var obj = getSimpleObj(); | ||
var b = new URLSearchParams(obj); | ||
expect(b.toString()).to.be.equal(obj.toString()); | ||
}); | ||
}); | ||
@@ -184,2 +190,42 @@ | ||
describe('Iterator 迭代器', function () { | ||
it('entries', function () { | ||
var obj = getSimpleObj(), | ||
ret = []; | ||
for (var p of obj.entries()) { | ||
ret.push(p[0] + ',' + p[1]); | ||
} | ||
expect(ret.join(';')).to.be.equal('a,1;b,2;c,3'); | ||
}); | ||
it('for...of', function () { | ||
var obj = getSimpleObj(), | ||
ret = []; | ||
for (var p of obj) { | ||
ret.push(p[0] + ',' + p[1]); | ||
} | ||
expect(ret.join(';')).to.be.equal('a,1;b,2;c,3'); | ||
}); | ||
it('keys', function () { | ||
var obj = getSimpleObj(), | ||
ret = []; | ||
for (var key of obj.keys()) { | ||
ret.push(key); | ||
} | ||
expect(ret.join(';')).to.be.equal('a;b;c'); | ||
}); | ||
it('values', function () { | ||
var obj = getSimpleObj(), | ||
ret = []; | ||
for (var value of obj.values()) { | ||
ret.push(value); | ||
} | ||
expect(ret.join(';')).to.be.equal('1;2;3'); | ||
}); | ||
}); | ||
describe('其它', function () { | ||
@@ -186,0 +232,0 @@ |
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
15741
413
138