Skip to content

Commit

Permalink
Add proxy when browser doesn't support object constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrybendy committed Jun 20, 2017
1 parent 3ebc2b9 commit 2374527
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@
(function(self) {
'use strict';

if (self.URLSearchParams && (new self.URLSearchParams({a:1})).toString() === 'a=1') {
return;
}


var __URLSearchParams__ = "__URLSearchParams__",
var nativeURLSearchParams = self.URLSearchParams ? self.URLSearchParams : null,
isSupportObjectConstructor = nativeURLSearchParams && (new nativeURLSearchParams({a: 1})).toString() === 'a=1',
__URLSearchParams__ = "__URLSearchParams__",
prototype = URLSearchParams.prototype,
iterable = !!(self.Symbol && self.Symbol.iterator);

if (nativeURLSearchParams && isSupportObjectConstructor) {
return;
}


/**
* Make a URLSearchParams instance
*
* @param {object|string|URLSearchParams} search
* @constructor
*/
function URLSearchParams (search) {
function URLSearchParams(search) {
search = search || "";

this [__URLSearchParams__] = {};
Expand All @@ -52,7 +53,7 @@
}

var pairs = search.split("&");
for (var j = 0; j < pairs.length; j ++) {
for (var j = 0; j < pairs.length; j++) {
var value = pairs [j],
index = value.indexOf('=');

Expand Down Expand Up @@ -88,7 +89,7 @@
*
* @param {string} name
*/
prototype.delete = function (name) {
prototype.delete = function(name) {
delete this [__URLSearchParams__] [name];
};

Expand All @@ -98,7 +99,7 @@
* @param {string} name
* @returns {string|null}
*/
prototype.get = function (name) {
prototype.get = function(name) {
var dict = this [__URLSearchParams__];
return name in dict ? dict[name][0] : null;
};
Expand All @@ -109,7 +110,7 @@
* @param {string} name
* @returns {Array}
*/
prototype.getAll = function (name) {
prototype.getAll = function(name) {
var dict = this [__URLSearchParams__];
return name in dict ? dict [name].slice(0) : [];
};
Expand All @@ -120,7 +121,7 @@
* @param {string} name
* @returns {boolean}
*/
prototype.has = function (name) {
prototype.has = function(name) {
return name in this [__URLSearchParams__];
};

Expand All @@ -142,7 +143,7 @@
* @param {function} callback
* @param {object} thisArg
*/
prototype.forEach = function (callback, thisArg) {
prototype.forEach = function(callback, thisArg) {
var dict = this [__URLSearchParams__];
Object.getOwnPropertyNames(dict).forEach(function(name) {
dict[name].forEach(function(value) {
Expand All @@ -156,7 +157,7 @@
*
* @returns {string}
*/
prototype.toString = function () {
prototype.toString = function() {
var dict = this[__URLSearchParams__], query = [], i, key, name, value;
for (key in dict) {
name = encode(key);
Expand All @@ -171,13 +172,13 @@
/**
* Sort all name-value pairs
*/
prototype.sort = function () {
prototype.sort = function() {
var dict = this[__URLSearchParams__], keys = [], k, i, ret = {};
for (k in dict) {
keys.push(k);
}
keys.sort();
for (i = 0; i < keys.length; i ++) {
for (i = 0; i < keys.length; i++) {
ret[keys[i]] = dict[keys[i]];
}
this[__URLSearchParams__] = ret;
Expand All @@ -190,9 +191,9 @@
*
* @returns {function}
*/
prototype.keys = function () {
prototype.keys = function() {
var items = [];
this.forEach(function (item, name) {
this.forEach(function(item, name) {
items.push([name]);
});
return makeIterator(items);
Expand All @@ -204,9 +205,9 @@
*
* @returns {function}
*/
prototype.values = function () {
prototype.values = function() {
var items = [];
this.forEach(function (item) {
this.forEach(function(item) {
items.push([item]);
});
return makeIterator(items);
Expand All @@ -218,9 +219,9 @@
*
* @returns {function}
*/
prototype.entries = function () {
prototype.entries = function() {
var items = [];
this.forEach(function (item, name) {
this.forEach(function(item, name) {
items.push([name, item]);
});
return makeIterator(items);
Expand All @@ -242,7 +243,7 @@
'%20': '+',
'%00': '\x00'
};
return encodeURIComponent(str).replace(/[!'\(\)~]|%20|%00/g, function (match) {
return encodeURIComponent(str).replace(/[!'\(\)~]|%20|%00/g, function(match) {
return replace[match];
});
}
Expand All @@ -253,24 +254,31 @@

function makeIterator(arr) {
var iterator = {
next: function () {
next: function() {
var value = arr.shift();
return {done: value === undefined, value: value};
}
};

if (iterable) {
iterator[self.Symbol.iterator] = function () {
iterator[self.Symbol.iterator] = function() {
return iterator;
};
}

return iterator;
}

self.URLSearchParams = URLSearchParams;

self.URLSearchParams.polyfill = true;
// Use proxy if browser doesn't support object constructor
self.URLSearchParams = (nativeURLSearchParams && !isSupportObjectConstructor) ?
new Proxy(nativeURLSearchParams, {
construct: function(target, args) {
return new target((new URLSearchParams(args[0]).toString()));
}
}) :
URLSearchParams;

self.URLSearchParams.polyfill = true;

})(typeof global !== 'undefined' ? global : (typeof window !== 'undefined' ? window : this));

0 comments on commit 2374527

Please sign in to comment.