amos-tool
Version:
amos ui tool
63 lines (61 loc) • 3.46 kB
JavaScript
;
var utils = require("./utils"), has = Object.prototype.hasOwnProperty, defaults = {
allowDots: !1,
allowPrototypes: !1,
arrayLimit: 20,
decoder: utils.decode,
delimiter: "&",
depth: 5,
parameterLimit: 1e3,
plainObjects: !1,
strictNullHandling: !1
}, parseValues = function(e, t) {
for (var r = {}, l = t.ignoreQueryPrefix ? e.replace(/^\?/, "") : e, a = t.parameterLimit === 1 / 0 ? void 0 : t.parameterLimit, i = l.split(t.delimiter, a), o = 0; o < i.length; ++o) {
var s, n, c = i[o], d = c.indexOf("]="), p = -1 === d ? c.indexOf("=") : d + 1;
-1 === p ? (s = t.decoder(c, defaults.decoder), n = t.strictNullHandling ? null : "") : (s = t.decoder(c.slice(0, p), defaults.decoder),
n = t.decoder(c.slice(p + 1), defaults.decoder)), has.call(r, s) ? r[s] = [].concat(r[s]).concat(n) : r[s] = n;
}
return r;
}, parseObject = function(e, t, r) {
for (var l = t, a = e.length - 1; a >= 0; --a) {
var i, o = e[a];
if ("[]" === o) i = (i = []).concat(l); else {
i = r.plainObjects ? Object.create(null) : {};
var s = "[" === o.charAt(0) && "]" === o.charAt(o.length - 1) ? o.slice(1, -1) : o, n = parseInt(s, 10);
!isNaN(n) && o !== s && String(n) === s && n >= 0 && r.parseArrays && n <= r.arrayLimit ? (i = [])[n] = l : i[s] = l;
}
l = i;
}
return l;
}, parseKeys = function(e, t, r) {
if (e) {
var l = r.allowDots ? e.replace(/\.([^.[]+)/g, "[$1]") : e, a = /(\[[^[\]]*])/g, i = /(\[[^[\]]*])/.exec(l), o = i ? l.slice(0, i.index) : l, s = [];
if (o) {
if (!r.plainObjects && has.call(Object.prototype, o) && !r.allowPrototypes) return;
s.push(o);
}
for (var n = 0; null !== (i = a.exec(l)) && n < r.depth; ) {
if (n += 1, !r.plainObjects && has.call(Object.prototype, i[1].slice(1, -1)) && !r.allowPrototypes) return;
s.push(i[1]);
}
return i && s.push("[" + l.slice(i.index) + "]"), parseObject(s, t, r);
}
};
module.exports = function(e, t) {
var r = t ? utils.assign({}, t) : {};
if (null !== r.decoder && void 0 !== r.decoder && "function" != typeof r.decoder) throw new TypeError("Decoder has to be a function.");
if (r.ignoreQueryPrefix = !0 === r.ignoreQueryPrefix, r.delimiter = "string" == typeof r.delimiter || utils.isRegExp(r.delimiter) ? r.delimiter : defaults.delimiter,
r.depth = "number" == typeof r.depth ? r.depth : defaults.depth, r.arrayLimit = "number" == typeof r.arrayLimit ? r.arrayLimit : defaults.arrayLimit,
r.parseArrays = !1 !== r.parseArrays, r.decoder = "function" == typeof r.decoder ? r.decoder : defaults.decoder,
r.allowDots = "boolean" == typeof r.allowDots ? r.allowDots : defaults.allowDots,
r.plainObjects = "boolean" == typeof r.plainObjects ? r.plainObjects : defaults.plainObjects,
r.allowPrototypes = "boolean" == typeof r.allowPrototypes ? r.allowPrototypes : defaults.allowPrototypes,
r.parameterLimit = "number" == typeof r.parameterLimit ? r.parameterLimit : defaults.parameterLimit,
r.strictNullHandling = "boolean" == typeof r.strictNullHandling ? r.strictNullHandling : defaults.strictNullHandling,
"" === e || null == e) return r.plainObjects ? Object.create(null) : {};
for (var l = "string" == typeof e ? parseValues(e, r) : e, a = r.plainObjects ? Object.create(null) : {}, i = Object.keys(l), o = 0; o < i.length; ++o) {
var s = i[o], n = parseKeys(s, l[s], r);
a = utils.merge(a, n, r);
}
return utils.compact(a);
};