url-search-params-polyfill
Advanced tools
Comparing version 1.3.0 to 2.0.0
188
index.js
@@ -9,7 +9,12 @@ /** | ||
(function(self) { | ||
'use strict'; | ||
if (self.URLSearchParams && (new self.URLSearchParams({a:1})).toString() === 'a=1') { | ||
var nativeURLSearchParams = self.URLSearchParams ? self.URLSearchParams : null, | ||
isSupportObjectConstructor = nativeURLSearchParams && (new nativeURLSearchParams({a: 1})).toString() === 'a=1', | ||
__URLSearchParams__ = "__URLSearchParams__", | ||
prototype = URLSearchParamsPolyfill.prototype, | ||
iterable = !!(self.Symbol && self.Symbol.iterator); | ||
if (nativeURLSearchParams && isSupportObjectConstructor) { | ||
return; | ||
@@ -19,7 +24,2 @@ } | ||
var __URLSearchParams__ = "__URLSearchParams__", | ||
prototype = URLSearchParams.prototype, | ||
iterable = !!(self.Symbol && self.Symbol.iterator); | ||
/** | ||
@@ -31,43 +31,14 @@ * Make a URLSearchParams instance | ||
*/ | ||
function URLSearchParams (search) { | ||
function URLSearchParamsPolyfill(search) { | ||
search = search || ""; | ||
this [__URLSearchParams__] = {}; | ||
// support construct object with another URLSearchParams instance | ||
if (search instanceof URLSearchParams) { | ||
if (search instanceof URLSearchParams || search instanceof URLSearchParamsPolyfill) { | ||
search = search.toString(); | ||
} | ||
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]); | ||
this.append(i, str); | ||
} | ||
} | ||
this [__URLSearchParams__] = parseToDict(search); | ||
} | ||
} else { | ||
// remove first '?' | ||
if (search.indexOf("?") === 0) { | ||
search = search.slice(1); | ||
} | ||
var pairs = search.split("&"); | ||
for (var j = 0; j < pairs.length; j ++) { | ||
var value = pairs [j], | ||
index = value.indexOf('='); | ||
if (-1 < index) { | ||
this.append( | ||
decode(value.slice(0, index)), | ||
decode(value.slice(index + 1)) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
@@ -80,8 +51,3 @@ * Appends a specified key/value pair as a new search parameter. | ||
prototype.append = function(name, value) { | ||
var dict = this [__URLSearchParams__]; | ||
if (name in dict) { | ||
dict[name].push('' + value); | ||
} else { | ||
dict[name] = ['' + value]; | ||
} | ||
appendTo(this [__URLSearchParams__], name, value); | ||
}; | ||
@@ -95,3 +61,3 @@ | ||
*/ | ||
prototype.delete = function (name) { | ||
prototype.delete = function(name) { | ||
delete this [__URLSearchParams__] [name]; | ||
@@ -106,3 +72,3 @@ }; | ||
*/ | ||
prototype.get = function (name) { | ||
prototype.get = function(name) { | ||
var dict = this [__URLSearchParams__]; | ||
@@ -118,3 +84,3 @@ return name in dict ? dict[name][0] : null; | ||
*/ | ||
prototype.getAll = function (name) { | ||
prototype.getAll = function(name) { | ||
var dict = this [__URLSearchParams__]; | ||
@@ -130,3 +96,3 @@ return name in dict ? dict [name].slice(0) : []; | ||
*/ | ||
prototype.has = function (name) { | ||
prototype.has = function(name) { | ||
return name in this [__URLSearchParams__]; | ||
@@ -148,17 +114,2 @@ }; | ||
/** | ||
* | ||
* | ||
* @param {function} callback | ||
* @param {object} thisArg | ||
*/ | ||
prototype.forEach = function (callback, thisArg) { | ||
var dict = this [__URLSearchParams__]; | ||
Object.getOwnPropertyNames(dict).forEach(function(name) { | ||
dict[name].forEach(function(value) { | ||
callback.call(thisArg, value, name, this); | ||
}, this); | ||
}, this); | ||
}; | ||
/** | ||
* Returns a string containg a query string suitable for use in a URL. | ||
@@ -168,3 +119,3 @@ * | ||
*/ | ||
prototype.toString = function () { | ||
prototype.toString = function() { | ||
var dict = this[__URLSearchParams__], query = [], i, key, name, value; | ||
@@ -181,7 +132,37 @@ for (key in dict) { | ||
/* | ||
* Apply polifill to global object and append other prototype into it | ||
*/ | ||
self.URLSearchParams = (nativeURLSearchParams && !isSupportObjectConstructor) ? | ||
new Proxy(nativeURLSearchParams, { | ||
construct: function(target, args) { | ||
return new target((new URLSearchParamsPolyfill(args[0]).toString())); | ||
} | ||
}) : | ||
URLSearchParamsPolyfill; | ||
var USPProto = self.URLSearchParams.prototype; | ||
USPProto.polyfill = true; | ||
/** | ||
* | ||
* @param {function} callback | ||
* @param {object} thisArg | ||
*/ | ||
USPProto.forEach = USPProto.forEach || function(callback, thisArg) { | ||
var dict = parseToDict(this.toString()); | ||
Object.getOwnPropertyNames(dict).forEach(function(name) { | ||
dict[name].forEach(function(value) { | ||
callback.call(thisArg, value, name, this); | ||
}, this); | ||
}, this); | ||
}; | ||
/** | ||
* Sort all name-value pairs | ||
*/ | ||
prototype.sort = function () { | ||
var dict = this[__URLSearchParams__], keys = [], k, i, ret = {}; | ||
USPProto.sort = USPProto.sort || function() { | ||
var dict = parseToDict(this.toString()), keys = [], k, i, j; | ||
for (k in dict) { | ||
@@ -191,9 +172,14 @@ keys.push(k); | ||
keys.sort(); | ||
for (i = 0; i < keys.length; i ++) { | ||
ret[keys[i]] = dict[keys[i]]; | ||
for (i = 0; i < keys.length; i++) { | ||
this.delete(keys[i]); | ||
} | ||
this[__URLSearchParams__] = ret; | ||
for (i = 0; i < keys.length; i++) { | ||
var key = keys[i], values = dict[key]; | ||
for (j = 0; j < values.length; j++) { | ||
this.append(key, values[j]); | ||
} | ||
} | ||
}; | ||
/** | ||
@@ -205,5 +191,5 @@ * Returns an iterator allowing to go through all keys of | ||
*/ | ||
prototype.keys = function () { | ||
USPProto.keys = USPProto.keys || function() { | ||
var items = []; | ||
this.forEach(function (item, name) { | ||
this.forEach(function(item, name) { | ||
items.push([name]); | ||
@@ -220,5 +206,5 @@ }); | ||
*/ | ||
prototype.values = function () { | ||
USPProto.values = USPProto.values || function() { | ||
var items = []; | ||
this.forEach(function (item) { | ||
this.forEach(function(item) { | ||
items.push([item]); | ||
@@ -235,5 +221,5 @@ }); | ||
*/ | ||
prototype.entries = function () { | ||
USPProto.entries = USPProto.entries || function() { | ||
var items = []; | ||
this.forEach(function (item, name) { | ||
this.forEach(function(item, name) { | ||
items.push([name, item]); | ||
@@ -246,3 +232,3 @@ }); | ||
if (iterable) { | ||
prototype[self.Symbol.iterator] = prototype.entries; | ||
USPProto[self.Symbol.iterator] = USPProto[self.Symbol.iterator] || USPProto.entries; | ||
} | ||
@@ -261,3 +247,3 @@ | ||
}; | ||
return encodeURIComponent(str).replace(/[!'\(\)~]|%20|%00/g, function (match) { | ||
return encodeURIComponent(str).replace(/[!'\(\)~]|%20|%00/g, function(match) { | ||
return replace[match]; | ||
@@ -273,3 +259,3 @@ }); | ||
var iterator = { | ||
next: function () { | ||
next: function() { | ||
var value = arr.shift(); | ||
@@ -281,3 +267,3 @@ return {done: value === undefined, value: value}; | ||
if (iterable) { | ||
iterator[self.Symbol.iterator] = function () { | ||
iterator[self.Symbol.iterator] = function() { | ||
return iterator; | ||
@@ -290,7 +276,41 @@ }; | ||
self.URLSearchParams = URLSearchParams; | ||
function parseToDict(search) { | ||
var dict = {}; | ||
self.URLSearchParams.polyfill = true; | ||
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); | ||
} | ||
} | ||
} else { | ||
// remove first '?' | ||
if (search.indexOf("?") === 0) { | ||
search = search.slice(1); | ||
} | ||
var pairs = search.split("&"); | ||
for (var j = 0; j < pairs.length; j++) { | ||
var value = pairs [j], | ||
index = value.indexOf('='); | ||
if (-1 < index) { | ||
appendTo(dict, decode(value.slice(0, index)), decode(value.slice(index + 1))); | ||
} | ||
} | ||
} | ||
return dict; | ||
} | ||
function appendTo(dict, name, value) { | ||
if (name in dict) { | ||
dict[name].push('' + value); | ||
} else { | ||
dict[name] = ['' + value]; | ||
} | ||
} | ||
})(typeof global !== 'undefined' ? global : (typeof window !== 'undefined' ? window : this)); |
{ | ||
"name": "url-search-params-polyfill", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"description": "a simple polyfill for javascript URLSearchParams", | ||
@@ -12,5 +12,4 @@ "homepage": "https://github.com/jerrybendy/url-search-params-polyfill", | ||
"scripts": { | ||
"test": "mocha test/*.test.js", | ||
"testWatch": "mocha test/*.test.js -w", | ||
"istanbul": "istanbul cover _mocha -- test/*.test.js" | ||
"test": "mocha test/index.test.js && mocha test/partial-support.test.js", | ||
"istanbul": "istanbul cover _mocha -- test/index.test.js" | ||
}, | ||
@@ -17,0 +16,0 @@ "keywords": [ |
@@ -5,3 +5,6 @@ # URLSearchParams polyfill [![](https://img.shields.io/npm/v/url-search-params-polyfill.svg)](https://www.npmjs.com/package/url-search-params-polyfill) | ||
This library can use for both browsers and nodeJs. | ||
Some browsers have native URLSearchParams class support, but not full. The new `2.x` version will detect if browsers have full feature support and extend it ([#9](https://github.com/jerrybendy/url-search-params-polyfill/pull/9)). | ||
## Installation | ||
@@ -8,0 +11,0 @@ |
@@ -11,2 +11,3 @@ /** | ||
var PREFIX = global.partialSupportTest ? '[PARTIAL] ' : '[FULL] '; | ||
@@ -23,3 +24,3 @@ function getSimpleObj () { | ||
describe('Constructor', function () { | ||
describe(PREFIX + 'Constructor', function () { | ||
@@ -64,3 +65,3 @@ it('Construct with a search string', function () { | ||
describe('Append data', function () { | ||
describe(PREFIX + 'Append data', function () { | ||
@@ -83,3 +84,3 @@ it('Append data', function () { | ||
describe('Get data', function () { | ||
describe(PREFIX + 'Get data', function () { | ||
@@ -122,3 +123,3 @@ it('Get simple data', function () { | ||
describe('Delete data', function () { | ||
describe(PREFIX + 'Delete data', function () { | ||
@@ -145,3 +146,3 @@ it('Remove simple data', function () { | ||
describe('Has check key exists', function () { | ||
describe(PREFIX + 'Has check key exists', function () { | ||
@@ -157,3 +158,3 @@ it('Check the key exists', function () { | ||
describe('Set data', function () { | ||
describe(PREFIX + 'Set data', function () { | ||
@@ -175,3 +176,3 @@ it('Set a new data', function () { | ||
describe('ForEach', function () { | ||
describe(PREFIX + 'ForEach', function () { | ||
@@ -196,3 +197,3 @@ it('ForEach', function () { | ||
describe('Iterator', function () { | ||
describe(PREFIX + 'Iterator', function () { | ||
@@ -238,3 +239,3 @@ it('entries', function () { | ||
describe('Sort', function () { | ||
describe(PREFIX + 'Sort', function () { | ||
it ('Sort keys', function () { | ||
@@ -248,3 +249,3 @@ var obj = new URLSearchParams('q=flag&key=hello&s=world'); | ||
describe('Others', function () { | ||
describe(PREFIX + 'Others', function () { | ||
@@ -251,0 +252,0 @@ var testObj = { |
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
22509
7
611
147