|
(function (global, factory) { |
|
var exp = global.URLSearchParams || factory(global); |
|
|
|
if (typeof module !== 'undefined' && module.exports) { |
|
module.exports = exp; |
|
} |
|
else if (typeof define === 'function' && define.amd) { |
|
define(exp); |
|
} |
|
else { |
|
Object.defineProperty(global, 'URLSearchParams', { value: exp, configurable: true, writable: true }); |
|
} |
|
})(Function('return this')(), function (global) { |
|
var hop = Object.prototype.hasOwnProperty, |
|
ts = String, |
|
priv = global.Symbol ? Symbol() : '_params', |
|
toSpace = /\+/g, |
|
toPlus = /%20/g, |
|
decode = function (s) { return decodeURIComponent(s.replace(toSpace, ' ')); }, |
|
encode = function (s) { return encodeURIComponent(s).replace(toPlus, '+'); }, |
|
methods = { |
|
append: append, |
|
get: get, |
|
getAll: getAll, |
|
has: has, |
|
set: set, |
|
toString: toString, |
|
delete: del |
|
}; |
|
|
|
function URLSearchParams(init) { |
|
var match, m1, |
|
params = Object.create(null), |
|
search = /([^&=]+)=?([^&]*)/g, |
|
|
|
init = init || ''; |
|
while (match = search.exec(init)) { |
|
m1 = decode(match[1]); |
|
|
|
if (!params[m1]) { |
|
params[m1] = []; |
|
} |
|
params[m1].push(decode(match[2]) || ''); |
|
} |
|
Object.defineProperty(this, priv, { value: params }); |
|
} |
|
|
|
function append(k, v) { |
|
(this[priv][ts(k)] = this[priv][ts(k)] || []).push(ts(v)); |
|
} |
|
|
|
function del(k) { |
|
delete this[priv][ts(k)]; |
|
} |
|
|
|
function get(k) { |
|
var a = getAll.call(this, k); |
|
return a.length ? a.pop() : null; |
|
} |
|
|
|
function getAll(k) { |
|
return (this[priv][ts(k)] || []).slice(0); |
|
} |
|
|
|
function has(k) { |
|
return hop.call(this[priv], ts(k)); |
|
} |
|
|
|
function set(k, v) { |
|
(this[priv][ts(k)] = []).push(ts(v)); |
|
} |
|
|
|
function toString() { |
|
var obj = this[priv]; |
|
return Object.keys(obj).map(function (k) { |
|
var key = encode(k); |
|
return obj[k].map(function (p) { |
|
return key + '=' + encode(p); |
|
}).join('&'); |
|
}).join('&'); |
|
} |
|
|
|
Object.getOwnPropertyNames(methods).forEach(function (k) { |
|
Object.defineProperty(URLSearchParams.prototype, k, { value: methods[k], configurable: true, writable: true }); |
|
}); |
|
|
|
return URLSearchParams; |
|
}); |