UNPKG

amos-tool

Version:

amos ui tool

159 lines (157 loc) 6.37 kB
"use strict"; var utils = require("./utils"), _LSFN = function() { var t = []; return function() { var n = window.location.search; if (utils.isEmpty(n)) throw new Error("the [window.location.search] not empty!"); var i = n.length > 1 ? n.substring(1) : null; utils.isNull(i) || (t = [].slice.call(i.split("&"))); }(), { getValue: function(n) { t.forEach(function(t) { var i = t.split("="); if (i[0] === n) return i[1]; }); }, getParameters: function() { var n = []; return t.forEach(function(t) { n.push(t.split("=")[0]); }), n; } }; }, _LocationSearch = { _keyValuePairs: [], init: function() { var t = window.location.search; if (utils.isEmpty(t)) throw new Error("the [window.location.search] not empty!"); var n = t.length > 1 ? t.substring(1) : null; return utils.isNull(n) || (_LocationSearch._keyValuePairs = [].slice.call(n.split("&"))), this; }, getValue: function(t) { _LocationSearch._keyValuePairs.forEach(function(n) { var i = n.split("="); if (i[0] === t) return i[1]; }); }, getParameters: function() { var t = []; return _LocationSearch._keyValuePairs.forEach(function(n) { t.push(n.split("=")[0]); }), t; } }, LocationParam = { parse: function(t) { var n, i, e, r, o; t || (t = window.location.search); var a = {}, s = []; if (!utils.isEmpty(t) && t.length > 1) for (r = 0, o = (s = t.slice(1 + t.indexOf("?"), t.length).split("&")).length; r < o; r++) n = (i = (i = s[r]).split("="))[0], e = i[1], n in a ? (utils.isArray(a[n]) || (a[n] = [ a[n] ]), a[n].push(decodeURIComponent(e))) : a[n] = decodeURIComponent(e); return a; }, paramSearch: function(t, n) { if (utils.isUndefined(n) || utils.isNull(n)) { if (n = window.location.search, utils.isUndefined(t) || utils.isNull(t)) { for (var i = n.substr(1, n.length - 1).split("&"), e = {}, r = 0; r < i.length; r++) { var o = i[r].split("="); 1 === o.length ? e[o[0]] = "" : 2 === o.length ? e[o[0]] = decodeURIComponent(o[1]) : console.log("there is something wrong when use [paramSearch]!"); } return e; } try { var a = new RegExp(t + "=([^&]+)?"); return decodeURIComponent(n.match(a)[1]); } catch (t) { return null; } } }, getLocationParams: function() { var t, n, i = {}, e = window.location.href; if (!utils.isEmpty(e)) { var r = e.indexOf("?"); if (r < -1) console.log("the location.href is invalid!"); else for (var o = (e = e.substr(r + 1)).split("&"), a = 0; a < o.length; a++) (r = o[a].indexOf("=")) > 0 && (t = o[a].substring(0, r), n = o[a].substr(r + 1), i[t] = decodeURIComponent(n)); } return i; }, getLocationParamByName: function(t) { var n = new RegExp("(^|&?)".concat(t, "=([^&]*)(&|$)")), i = window.location.search.substr(1).match(n); if (null != i) return decodeURIComponent(i[2]); if (!utils.isEmpty(window.location.hash)) { var e = window.location.hash.substr(1).match(n); if (null != e) return decodeURIComponent(e[2]); } return null; }, getParameter: function(t, n) { var i = new RegExp("(\\?|#|&)" + n + "=([^&#]*)(&|#|$)"), e = t.match(i); return e ? e[2] : ""; }, extractParam: function(t, n) { if (!t) return null; var i = new RegExp("(^|&?)".concat(n, "=([^&]*)(&|$)")), e = t.substr(1).match(i); return utils.isNil(e) ? null : decodeURIComponent(e[2]); }, extractParams: function(t) { var n, i, e = {}, r = t || window.location.href; if (!utils.isEmpty(r)) { var o = r.indexOf("?"); if (o < -1) console.log("the location.href is invalid!"); else for (var a = (r = r.substr(o + 1)).split("&"), s = 0; s < a.length; s++) (o = a[s].indexOf("=")) > 0 && (n = a[s].substring(0, o), i = a[s].substr(o + 1), e[n] = decodeURIComponent(i)); } return e; }, extractParamAll: function(t) { var n, i, e = arguments.length > 1 && void 0 !== arguments[1] && arguments[1], r = {}, o = t || window.location.href; if (!utils.isEmpty(o)) { var a = o.indexOf("?"); if (a < -1) console.log("the location.href is invalid!"); else for (var s = (o = o.substr(a + 1)).split("&"), l = 0; l < s.length; l++) (a = s[l].indexOf("=")) > 0 && (n = s[l].substring(0, a), i = s[l].substr(a + 1), r[n] = e ? decodeURIComponent(i) : i); } return r; }, completeParam: function(t, n, i) { var e = arguments; 1 === e.length ? n || (n = window.location.href) : 2 === e.length && utils.isBoolean(n) && (i = n, n = window.location.href), utils.isUndefined(i) && (i = !1), n || (n = window.location.href); var r = (Object.keys(t) || []).reduce(function(n, e) { var r = t[e]; return i && (utils.isNull(r) || utils.isUndefined(r)) && (r = ""), n += "&".concat(e, "=").concat(r); }, ""); return r ? (r = r.slice(1), -1 !== n.indexOf("?") ? n.endsWith("?") ? "".concat(n).concat(r) : "".concat(n, "&").concat(r) : "".concat(n, "?").concat(r)) : n; }, changeParam: function(t, n) { if (!t || utils.isEmpty(n)) return t; var i = t; return Object.keys(n).forEach(function(t) { var e = n[t]; utils.isUndefined(e) ? e = "" : utils.isArray(e) || utils.isObject(e) && (e = JSON.stringify(e)), i = function(t, n, i) { var e = n + "=([^&]*)", r = n + "=" + i; if (t.match(e)) { var o = new RegExp("(".concat(n, "=)([^&]*)"), "gi"); return t.replace(o, r); } return t.match("[?]") ? t + "&" + r : t + "?" + r; }(i, t, e); }), i; }, encodeParam: function(t, n, i) { if (utils.isNil(n) || !utils.isObject(t)) return t; utils.isString(n) && (n = [ n ]); for (var e = utils.clone(t), r = 0, o = n.length; r < o; r++) { var a = n[r]; utils.isNil(e[a]) || (i ? e[a] = i(e[a]) : utils.isString(e[a]) ? e[a] = encodeURIComponent(e[a]) : (utils.isOnlyObject(e[a]) || utils.isArray(e[a])) && (e[a] = encodeURIComponent(JSON.stringify(e[a])))); } return e; }, replaceUrl: function(t, n) { var i = n || location.href; i = LocationParam.changeParam(i, t), location.replace(i); }, LocationSearch: _LocationSearch, LSFN: _LSFN }; module.exports = LocationParam;