UNPKG

jsexpr

Version:

String and JSON expression interpolator and evaluator. Interpolates or evaluates a string against a json object, or transforms an object into another based on a json template

1,449 lines (1,278 loc) 181 kB
"use strict"; var _typeof2 = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; (function () { function r(e, n, t) { function o(i, f) { if (!n[i]) { if (!e[i]) { var c = "function" == typeof require && require;if (!f && c) return c(i, !0);if (u) return u(i, !0);var a = new Error("Cannot find module '" + i + "'");throw a.code = "MODULE_NOT_FOUND", a; }var p = n[i] = { exports: {} };e[i][0].call(p.exports, function (r) { var n = e[i][1][r];return o(n || r); }, p, p.exports, r, e, n, t); }return n[i].exports; }for (var u = "function" == typeof require && require, i = 0; i < t.length; i++) { o(t[i]); }return o; }return r; })()({ 1: [function (require, module, exports) { "use strict"; (function () { var root = this; var previous_jsexpr = root.jsexpr; var jsexpr = require('./index.js'); if (typeof exports !== 'undefined') { if (typeof module !== 'undefined' && module.exports) { exports = module.exports = jsexpr; } exports.jsexpr = jsexpr; } if (typeof window !== 'undefined') { window.jsexpr = jsexpr; } }).call(this); }, { "./index.js": 2 }], 2: [function (require, module, exports) { /*jshint esversion: 6 */ var expr = require('./lib'); module.exports = expr; }, { "./lib": 5 }], 3: [function (require, module, exports) { var CACHE = {}; function fneval(obj, key) { try { return eval("this." + key); } catch (err) { return undefined; } } function cacheeval(obj, key) { if (!CACHE[key]) { var rkey = key.replace(/'/g, "\\'"); var rx = /^[a-zA-Z$_@]/; var fn = eval("(function(){\n let rx = /^[a-zA-Z$_]/;\n return '" + rkey + "'.startsWith('this.') || '" + rkey + "'=='this' || !rx.test('" + rkey + "')?\n function() {\n let r = undefined;\n try {r=" + key + ";}\n catch(err){}\n return r;\n } :\n function() {\n let r = undefined;\n try {r=this." + (rx.test(key) ? key : '$___$') + ";}\n catch(err){try{r=" + key + ";}catch(err){}}\n return r;\n }\n })()"); CACHE[key] = fn; } return CACHE[key].call(obj); } var EVALS = { eval: function _eval(obj, key) { var v = fneval.call(obj, obj, key); return v === undefined ? "" : v; }, iteval: function iteval(obj, key) { var arr = key.split("."); arr.forEach(function (key) { if (obj == null || obj == undefined) return;else obj = obj[key]; }); var v = obj || undefined; return v === undefined ? "" : v; }, ceval: function ceval(obj, key) { var v = cacheeval(obj, key); return v === undefined ? "" : v; }, valwalk: function valwalk(src, ops, path) { if (!src) return src; for (var k in src) { var newpath = "" + path + (path ? '.' : '') + k; var rop = ops[newpath]; if (rop !== undefined) src[k] = rop;else if (_typeof2(src[k]) == "object") EVALS.valwalk(src[k], ops, newpath); }; return src; } }; module.exports = EVALS; }, {}], 4: [function (require, module, exports) { var EVALS = require('./evals.js'), extend = require("extend"), dayjs = require('dayjs'), Mingo = require("mingo"); function instance(token) { var RX = new RegExp("\\" + token + "\\{[^\\}]+\\}", 'g'); var RX_RPL_PARSE = new RegExp("\\" + token + "\\{([^\\}]+)\\}"); var RX_RPL_TOKEN = new RegExp("\\" + token + "\\{|\\}", 'g'); var RX_FILTER = new RegExp("^[A-Z_]+\\:"); var FILTERS = { JSON: function (_JSON) { function JSON(_x) { return _JSON.apply(this, arguments); } JSON.toString = function () { return _JSON.toString(); }; return JSON; }(function (args) { var nexpr = args[1]; var spaces = args[2]; if (args.length == 2) { if (isNaN(nexpr)) { spaces = 2; } else { nexpr = 'this';spaces = args[1]; } } else if (args.length == 1) { nexpr = 'this'; spaces = 2; } spaces = parseInt(spaces); var fnxpr = tokens("${" + nexpr + "}"); return function (entry) { return JSON.stringify(fnxpr(entry), null, spaces); }; }), DATE: function DATE(args) { args.shift(); if (args.length == 0) { args = ["null||new Date()", '|YYYY-MM-DDTHH:mm:ss.SSSZ']; } else if (args.length == 1) { args = ["null||new Date()", "|" + args[0]]; } var nexpr = tokens("${" + args.shift() + "}"); var format = args.join(":").split('|'); return function (entry) { var res = nexpr(entry); var dt = dayjs(res, format[0] || undefined); if (format[1]) { return dt.format(format[1]); } else { return dt.toDate(); } }; }, SUBSTR: function SUBSTR(args) { args.shift(); var nexpr = tokens("${" + args.shift() + "}"); var format = args.join(":").split('|'); var start = parseInt(format[0]); var end = parseInt(format[1]); if (isNaN(start)) start = 0; if (isNaN(end)) end = undefined; return function (entry) { var res = nexpr(entry); return _typeof2(res == 'string') ? res.substring(start, end) : res; }; } }; function fnassign(path) { var npath = path.split('.').map(function (t, i) { return "['" + t + "']"; }).join(''); var fn = "(function(path){\n\t\t\treturn function(obj,val) {\n\t\t\t\ttry {\n\t\t\t\t\t// Ensure path\n\t\t\t\t\tlet root = obj;\n\t\t\t\t\tlet kpath = path.split('.');\n\t\t\t\t\tfor(let i=0; i<kpath.length;i++) {\n\t\t\t\t\t\tlet k = kpath[i];\n\t\t\t\t\t\tif(!root[k]) root[k] = {};\n\t\t\t\t\t\troot = root[k];\n\t\t\t\t\t}\n\t\n\t\t\t\t\treturn obj" + npath + " = val;\n\t\t\t\t}catch(err) {}\n\t\t\t}\n\t\t})('" + path + "')"; return eval(fn); } function parse(expr, method) { method = method || "ceval"; var m = expr.match(RX); if (m) { m.forEach(function (token) { var key = token.replace(RX_RPL_PARSE, "$1").trim().replace(/'/g, "\\'"); expr = expr.replace(token, "__val(entry,'" + key + "')"); }); } var fn = new Function("entry", "__val", "return (" + expr + ")"); return function (entry) { return fn(entry, EVALS[method]); }; } function tokens(expr, method) { method = EVALS[method || "ceval"]; var list = [], len = 0; var m = expr.match(RX) || []; m.forEach(function (token) { var idx = expr.indexOf(token); var t = expr.substring(0, idx); var rtoken = token.replace(RX_RPL_TOKEN, ""); expr = expr.substring(idx + token.length); list.push(t); // Filter if (RX_FILTER.test(rtoken)) { var args = rtoken.split(":"); var fn = FILTERS[args[0]](args); list.push(fn); } // Evaluator else { list.push(function (entry) { return method(entry, rtoken); }); } }); list.push(expr); list = list.filter(function (l) { return l != ""; }); len = list.length; if (len > 1) { return function (entry) { var ret = ""; for (var i = 0; i < len; i++) { var t = list[i]; ret += typeof t == "string" ? t : t(entry); } return ret; }; } else { return function (entry) { var t = list[0]; if (typeof t == 'undefined') return undefined; return typeof t == "string" ? t : t(entry); }; } } function jsontokens(json) { var ops = [], len = 0; function walk(json, path) { if (!json) return; Object.keys(json).forEach(function (k) { var newpath = "" + path + (path ? '.' : '') + k; var t = json[k]; if (typeof t == "string") { ops.push({ path: newpath, fn: tokens(t) }); } else { walk(t, newpath); } }); } walk(json, ""); len = ops.length; return function (entry) { var map = {}; for (var i = 0; i < len; i++) { var op = ops[i]; map[op.path] = op.fn(entry); } return EVALS.valwalk(extend(true, {}, json), map, ""); }; } function mingotokens(json) { var xpr = Array.isArray(json.$) ? json.$ : [json.$]; var aggr = new Mingo.Aggregator(xpr); return function (input) { var isArray = Array.isArray(input); var res = aggr.run(isArray ? input : [input]); if (!isArray && res.length <= 1) return res[0];else return res; }; } function exprfn(input, replace) { if (typeof input == 'number') { return function (obj) { return input; }; } else if ((typeof input === "undefined" ? "undefined" : _typeof2(input)) == "object") { var ninput = extend({}, input); delete ninput['$']; var prfn = input["$"] ? mingotokens(input, replace) : function (input) { return input; }; var nxfn = Object.keys(ninput).length ? jsontokens(ninput, replace) : function (input) { return input; }; return function (obj) { var prres = prfn(obj); var nxres = nxfn(prres); if (typeof nxres._ !== 'undefined' && Object.keys(nxres).length == 1) return nxres._;else return nxres; }; } else { return tokens(input); } } function traverse(object, callback) { for (var key in object) { object[key] = callback(object, key, object[key]); } for (var _key2 in object) { if (_typeof2(object[_key2]) == 'object') { traverse(object[_key2], callback); } } } function filter(name, fncallback) { FILTERS[name] = fncallback; } return { fn: parse, eval: parse, assign: fnassign, expr: exprfn, expression: exprfn, traverse: traverse, filter: filter }; } module.exports = instance; }, { "./evals.js": 3, "dayjs": 7, "extend": 8, "mingo": 9 }], 5: [function (require, module, exports) { var expression = require('./expression'), mingo = require('./mingo_ex'); var instance = expression('$'); instance.newInstance = function (token) { return expression(token); }; instance.filter = function (name, fncallback) { instance.FILTERS[name] = fncallback; }; module.exports = instance; }, { "./expression": 4, "./mingo_ex": 6 }], 6: [function (require, module, exports) { var mingo = require('mingo'), jsexpr = require('./expression')('$'); var EV_CACHE = {}; var EX_CACHE = {}; try { mingo.addOperators(mingo.OP_QUERY, function (_) { return { $starts: function $starts(selector, value, args) { args = Array.isArray(args) ? args : [args]; return (value || "").startsWith(args[0]); }, $startsWith: function $startsWith(selector, value, args) { args = Array.isArray(args) ? args : [args]; return (value || "").startsWith(args[0]); }, $ends: function $ends(selector, value, args) { args = Array.isArray(args) ? args : [args]; return (value || "").endsWith(args[0]); }, $endsWith: function $endsWith(selector, value, args) { args = Array.isArray(args) ? args : [args]; return (value || "").endsWith(args[0]); }, $contains: function $contains(selector, value, args) { args = Array.isArray(args) ? args : [args]; return (value || "").indexOf(args[0]) >= 0; } }; }); mingo.addOperators(mingo.OP_EXPRESSION, function (_) { return { $eval: function $eval(selector, value, args) { if (!EV_CACHE[value]) { EV_CACHE[value] = jsexpr.eval(value); } return EV_CACHE[value](selector); }, $expr: function $expr(selector, value, args) { if (!EX_CACHE[value]) { EX_CACHE[value] = jsexpr.expr(value); } return EX_CACHE[value](selector); }, $keyval: function $keyval(selector, value, args) { var val = _.computeValue(selector, value); return val.reduce(function (map, item) { map[item[0]] = item[1] || "_"; return map; }, {}); }, $trim: function $trim(selector, value, args) { var chars = new Set((value.chars || '').split('')); var val = _.computeValue(selector, value.input).split(''); while (chars.has(val[0])) { val.shift(); }while (chars.has(val[val.length - 1])) { val.pop(); }return val.join(''); }, $starts: function $starts(selector, value, args) { var val = _.computeValue(selector, value[0]); return (val || "").startsWith(value[1]); }, $startsWidth: function $startsWidth(selector, value, args) { var val = _.computeValue(selector, value[0]); return (val || "").startsWith(value[1]); }, $ends: function $ends(selector, value, args) { var val = _.computeValue(selector, value[0]); return (val || "").endsWith(value[1]); }, $endsWith: function $endsWith(selector, value, args) { var val = _.computeValue(selector, value[0]); return (val || "").endsWith(value[1]); }, $contains: function $contains(selector, value, args) { var val = _.computeValue(selector, value[0]); return (val || "").indexOf(value[1]) >= 0; } }; }); } catch (err) {} module.exports = mingo; }, { "./expression": 4, "mingo": 9 }], 7: [function (require, module, exports) { !function (t, e) { "object" == (typeof exports === "undefined" ? "undefined" : _typeof2(exports)) && "undefined" != typeof module ? module.exports = e() : "function" == typeof define && define.amd ? define(e) : (t = "undefined" != typeof globalThis ? globalThis : t || self).dayjs = e(); }(this, function () { "use strict"; var t = 1e3, e = 6e4, n = 36e5, r = "millisecond", i = "second", s = "minute", u = "hour", a = "day", o = "week", f = "month", h = "quarter", c = "year", d = "date", $ = "Invalid Date", l = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/, y = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g, M = { name: "en", weekdays: "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"), months: "January_February_March_April_May_June_July_August_September_October_November_December".split("_") }, m = function m(t, e, n) { var r = String(t);return !r || r.length >= e ? t : "" + Array(e + 1 - r.length).join(n) + t; }, g = { s: m, z: function z(t) { var e = -t.utcOffset(), n = Math.abs(e), r = Math.floor(n / 60), i = n % 60;return (e <= 0 ? "+" : "-") + m(r, 2, "0") + ":" + m(i, 2, "0"); }, m: function t(e, n) { if (e.date() < n.date()) return -t(n, e);var r = 12 * (n.year() - e.year()) + (n.month() - e.month()), i = e.clone().add(r, f), s = n - i < 0, u = e.clone().add(r + (s ? -1 : 1), f);return +(-(r + (n - i) / (s ? i - u : u - i)) || 0); }, a: function a(t) { return t < 0 ? Math.ceil(t) || 0 : Math.floor(t); }, p: function p(t) { return { M: f, y: c, w: o, d: a, D: d, h: u, m: s, s: i, ms: r, Q: h }[t] || String(t || "").toLowerCase().replace(/s$/, ""); }, u: function u(t) { return void 0 === t; } }, D = "en", v = {};v[D] = M;var p = function p(t) { return t instanceof _; }, S = function S(t, e, n) { var r;if (!t) return D;if ("string" == typeof t) v[t] && (r = t), e && (v[t] = e, r = t);else { var i = t.name;v[i] = t, r = i; }return !n && r && (D = r), r || !n && D; }, w = function w(t, e) { if (p(t)) return t.clone();var n = "object" == (typeof e === "undefined" ? "undefined" : _typeof2(e)) ? e : {};return n.date = t, n.args = arguments, new _(n); }, O = g;O.l = S, O.i = p, O.w = function (t, e) { return w(t, { locale: e.$L, utc: e.$u, x: e.$x, $offset: e.$offset }); };var _ = function () { function M(t) { this.$L = S(t.locale, null, !0), this.parse(t); }var m = M.prototype;return m.parse = function (t) { this.$d = function (t) { var e = t.date, n = t.utc;if (null === e) return new Date(NaN);if (O.u(e)) return new Date();if (e instanceof Date) return new Date(e);if ("string" == typeof e && !/Z$/i.test(e)) { var r = e.match(l);if (r) { var i = r[2] - 1 || 0, s = (r[7] || "0").substring(0, 3);return n ? new Date(Date.UTC(r[1], i, r[3] || 1, r[4] || 0, r[5] || 0, r[6] || 0, s)) : new Date(r[1], i, r[3] || 1, r[4] || 0, r[5] || 0, r[6] || 0, s); } }return new Date(e); }(t), this.$x = t.x || {}, this.init(); }, m.init = function () { var t = this.$d;this.$y = t.getFullYear(), this.$M = t.getMonth(), this.$D = t.getDate(), this.$W = t.getDay(), this.$H = t.getHours(), this.$m = t.getMinutes(), this.$s = t.getSeconds(), this.$ms = t.getMilliseconds(); }, m.$utils = function () { return O; }, m.isValid = function () { return !(this.$d.toString() === $); }, m.isSame = function (t, e) { var n = w(t);return this.startOf(e) <= n && n <= this.endOf(e); }, m.isAfter = function (t, e) { return w(t) < this.startOf(e); }, m.isBefore = function (t, e) { return this.endOf(e) < w(t); }, m.$g = function (t, e, n) { return O.u(t) ? this[e] : this.set(n, t); }, m.unix = function () { return Math.floor(this.valueOf() / 1e3); }, m.valueOf = function () { return this.$d.getTime(); }, m.startOf = function (t, e) { var n = this, r = !!O.u(e) || e, h = O.p(t), $ = function $(t, e) { var i = O.w(n.$u ? Date.UTC(n.$y, e, t) : new Date(n.$y, e, t), n);return r ? i : i.endOf(a); }, l = function l(t, e) { return O.w(n.toDate()[t].apply(n.toDate("s"), (r ? [0, 0, 0, 0] : [23, 59, 59, 999]).slice(e)), n); }, y = this.$W, M = this.$M, m = this.$D, g = "set" + (this.$u ? "UTC" : "");switch (h) {case c: return r ? $(1, 0) : $(31, 11);case f: return r ? $(1, M) : $(0, M + 1);case o: var D = this.$locale().weekStart || 0, v = (y < D ? y + 7 : y) - D;return $(r ? m - v : m + (6 - v), M);case a:case d: return l(g + "Hours", 0);case u: return l(g + "Minutes", 1);case s: return l(g + "Seconds", 2);case i: return l(g + "Milliseconds", 3);default: return this.clone();} }, m.endOf = function (t) { return this.startOf(t, !1); }, m.$set = function (t, e) { var n, o = O.p(t), h = "set" + (this.$u ? "UTC" : ""), $ = (n = {}, n[a] = h + "Date", n[d] = h + "Date", n[f] = h + "Month", n[c] = h + "FullYear", n[u] = h + "Hours", n[s] = h + "Minutes", n[i] = h + "Seconds", n[r] = h + "Milliseconds", n)[o], l = o === a ? this.$D + (e - this.$W) : e;if (o === f || o === c) { var y = this.clone().set(d, 1);y.$d[$](l), y.init(), this.$d = y.set(d, Math.min(this.$D, y.daysInMonth())).$d; } else $ && this.$d[$](l);return this.init(), this; }, m.set = function (t, e) { return this.clone().$set(t, e); }, m.get = function (t) { return this[O.p(t)](); }, m.add = function (r, h) { var d, $ = this;r = Number(r);var l = O.p(h), y = function y(t) { var e = w($);return O.w(e.date(e.date() + Math.round(t * r)), $); };if (l === f) return this.set(f, this.$M + r);if (l === c) return this.set(c, this.$y + r);if (l === a) return y(1);if (l === o) return y(7);var M = (d = {}, d[s] = e, d[u] = n, d[i] = t, d)[l] || 1, m = this.$d.getTime() + r * M;return O.w(m, this); }, m.subtract = function (t, e) { return this.add(-1 * t, e); }, m.format = function (t) { var e = this;if (!this.isValid()) return $;var n = t || "YYYY-MM-DDTHH:mm:ssZ", r = O.z(this), i = this.$locale(), s = this.$H, u = this.$m, a = this.$M, o = i.weekdays, f = i.months, h = function h(t, r, i, s) { return t && (t[r] || t(e, n)) || i[r].substr(0, s); }, c = function c(t) { return O.s(s % 12 || 12, t, "0"); }, d = i.meridiem || function (t, e, n) { var r = t < 12 ? "AM" : "PM";return n ? r.toLowerCase() : r; }, l = { YY: String(this.$y).slice(-2), YYYY: this.$y, M: a + 1, MM: O.s(a + 1, 2, "0"), MMM: h(i.monthsShort, a, f, 3), MMMM: h(f, a), D: this.$D, DD: O.s(this.$D, 2, "0"), d: String(this.$W), dd: h(i.weekdaysMin, this.$W, o, 2), ddd: h(i.weekdaysShort, this.$W, o, 3), dddd: o[this.$W], H: String(s), HH: O.s(s, 2, "0"), h: c(1), hh: c(2), a: d(s, u, !0), A: d(s, u, !1), m: String(u), mm: O.s(u, 2, "0"), s: String(this.$s), ss: O.s(this.$s, 2, "0"), SSS: O.s(this.$ms, 3, "0"), Z: r };return n.replace(y, function (t, e) { return e || l[t] || r.replace(":", ""); }); }, m.utcOffset = function () { return 15 * -Math.round(this.$d.getTimezoneOffset() / 15); }, m.diff = function (r, d, $) { var l, y = O.p(d), M = w(r), m = (M.utcOffset() - this.utcOffset()) * e, g = this - M, D = O.m(this, M);return D = (l = {}, l[c] = D / 12, l[f] = D, l[h] = D / 3, l[o] = (g - m) / 6048e5, l[a] = (g - m) / 864e5, l[u] = g / n, l[s] = g / e, l[i] = g / t, l)[y] || g, $ ? D : O.a(D); }, m.daysInMonth = function () { return this.endOf(f).$D; }, m.$locale = function () { return v[this.$L]; }, m.locale = function (t, e) { if (!t) return this.$L;var n = this.clone(), r = S(t, e, !0);return r && (n.$L = r), n; }, m.clone = function () { return O.w(this.$d, this); }, m.toDate = function () { return new Date(this.valueOf()); }, m.toJSON = function () { return this.isValid() ? this.toISOString() : null; }, m.toISOString = function () { return this.$d.toISOString(); }, m.toString = function () { return this.$d.toUTCString(); }, M; }(), b = _.prototype;return w.prototype = b, [["$ms", r], ["$s", i], ["$m", s], ["$H", u], ["$W", a], ["$M", f], ["$y", c], ["$D", d]].forEach(function (t) { b[t[1]] = function (e) { return this.$g(e, t[0], t[1]); }; }), w.extend = function (t, e) { return t.$i || (t(e, _, w), t.$i = !0), w; }, w.locale = S, w.isDayjs = p, w.unix = function (t) { return w(1e3 * t); }, w.en = v[D], w.Ls = v, w.p = {}, w; }); }, {}], 8: [function (require, module, exports) { 'use strict'; var hasOwn = Object.prototype.hasOwnProperty; var toStr = Object.prototype.toString; var defineProperty = Object.defineProperty; var gOPD = Object.getOwnPropertyDescriptor; var isArray = function isArray(arr) { if (typeof Array.isArray === 'function') { return Array.isArray(arr); } return toStr.call(arr) === '[object Array]'; }; var isPlainObject = function isPlainObject(obj) { if (!obj || toStr.call(obj) !== '[object Object]') { return false; } var hasOwnConstructor = hasOwn.call(obj, 'constructor'); var hasIsPrototypeOf = obj.constructor && obj.constructor.prototype && hasOwn.call(obj.constructor.prototype, 'isPrototypeOf'); // Not own constructor property must be Object if (obj.constructor && !hasOwnConstructor && !hasIsPrototypeOf) { return false; } // Own properties are enumerated firstly, so to speed up, // if last one is own, then all properties are own. var key; for (key in obj) {/**/} return typeof key === 'undefined' || hasOwn.call(obj, key); }; // If name is '__proto__', and Object.defineProperty is available, define __proto__ as an own property on target var setProperty = function setProperty(target, options) { if (defineProperty && options.name === '__proto__') { defineProperty(target, options.name, { enumerable: true, configurable: true, value: options.newValue, writable: true }); } else { target[options.name] = options.newValue; } }; // Return undefined instead of __proto__ if '__proto__' is not an own property var getProperty = function getProperty(obj, name) { if (name === '__proto__') { if (!hasOwn.call(obj, name)) { return void 0; } else if (gOPD) { // In early versions of node, obj['__proto__'] is buggy when obj has // __proto__ as an own property. Object.getOwnPropertyDescriptor() works. return gOPD(obj, name).value; } } return obj[name]; }; module.exports = function extend() { var options, name, src, copy, copyIsArray, clone; var target = arguments[0]; var i = 1; var length = arguments.length; var deep = false; // Handle a deep copy situation if (typeof target === 'boolean') { deep = target; target = arguments[1] || {}; // skip the boolean and the target i = 2; } if (target == null || (typeof target === "undefined" ? "undefined" : _typeof2(target)) !== 'object' && typeof target !== 'function') { target = {}; } for (; i < length; ++i) { options = arguments[i]; // Only deal with non-null/undefined values if (options != null) { // Extend the base object for (name in options) { src = getProperty(target, name); copy = getProperty(options, name); // Prevent never-ending loop if (target !== copy) { // Recurse if we're merging plain objects or arrays if (deep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)))) { if (copyIsArray) { copyIsArray = false; clone = src && isArray(src) ? src : []; } else { clone = src && isPlainObject(src) ? src : {}; } // Never move original objects, clone them setProperty(target, { name: name, newValue: extend(deep, clone, copy) }); // Don't bring in undefined values } else if (typeof copy !== 'undefined') { setProperty(target, { name: name, newValue: copy }); } } } } } // Return the modified object return target; }; }, {}], 9: [function (require, module, exports) { //! mingo.js 2.5.3 //! Copyright (c) 2020 Francis Asante //! MIT (function (global, factory) { (typeof exports === "undefined" ? "undefined" : _typeof2(exports)) === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = global || self, global.mingo = factory()); })(this, function () { 'use strict'; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && _typeof2(Symbol.iterator) === "symbol") { _typeof = function _typeof(obj) { return typeof obj === "undefined" ? "undefined" : _typeof2(obj); }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj === "undefined" ? "undefined" : _typeof2(obj); }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; }return arr2; } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } // Javascript native types var T_NULL = 'null'; var T_UNDEFINED = 'undefined'; var T_BOOL = 'bool'; var T_BOOLEAN = 'boolean'; var T_NUMBER = 'number'; var T_STRING = 'string'; var T_DATE = 'date'; var T_REGEX = 'regex'; var T_REGEXP = 'regexp'; var T_ARRAY = 'array'; var T_OBJECT = 'object'; var T_FUNCTION = 'function'; // no array, object, or function types var JS_SIMPLE_TYPES = [T_NULL, T_UNDEFINED, T_BOOLEAN, T_NUMBER, T_STRING, T_DATE, T_REGEXP]; // operator classes var OP_EXPRESSION = 'expression'; var OP_GROUP = 'group'; var OP_PIPELINE = 'pipeline'; var OP_PROJECTION = 'projection'; var OP_QUERY = 'query'; var MISSING = function MISSING() {}; /** * Utility functions */ if (!Array.prototype.includes) { Object.defineProperty(Array.prototype, 'includes', { value: function value(valueToFind, fromIndex) { if (this == null) { throw new TypeError('"this" is null or not defined'); } // 1. Let O be ? ToObject(this value). var o = Object(this); // 2. Let len be ? ToLength(? Get(O, "length")). var len = o.length >>> 0; // 3. If len is 0, return false. if (len === 0) { return false; } // 4. Let n be ? ToInteger(fromIndex). // (If fromIndex is undefined, this step produces the value 0.) var n = fromIndex | 0; // 5. If n ≥ 0, then // a. Let k be n. // 6. Else n < 0, // a. Let k be len + n. // b. If k < 0, let k be 0. var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); function sameValueZero(x, y) { return x === y || typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y); } // 7. Repeat, while k < len while (k < len) { // a. Let elementK be the result of ? Get(O, ! ToString(k)). // b. If SameValueZero(valueToFind, elementK) is true, return true. if (sameValueZero(o[k], valueToFind)) { return true; } // c. Increase k by 1. k++; } // 8. Return false return false; } }); } var arrayPush = Array.prototype.push; function assert(condition, message) { if (!condition) err(message); } /** * Deep clone an object */ function cloneDeep(obj) { switch (jsType(obj)) { case T_ARRAY: return obj.map(cloneDeep); case T_OBJECT: return objectMap(obj, cloneDeep); default: return obj; } } /** * Shallow clone an object */ function clone(obj) { switch (jsType(obj)) { case T_ARRAY: return into([], obj); case T_OBJECT: return Object.assign({}, obj); default: return obj; } } function getType(v) { if (v === null) return 'Null'; if (v === undefined) return 'Undefined'; return v.constructor.name; } function jsType(v) { return getType(v).toLowerCase(); } function isBoolean(v) { return jsType(v) === T_BOOLEAN; } function isString(v) { return jsType(v) === T_STRING; } function isNumber(v) { return jsType(v) === T_NUMBER; } var isArray = Array.isArray || function (v) { return !!v && v.constructor === Array; }; function isObject(v) { return !!v && v.constructor === Object; } function isObjectLike(v) { return v === Object(v); } // objects, arrays, functions, date, custom object function isDate(v) { return jsType(v) === T_DATE; } function isRegExp(v) { return jsType(v) === T_REGEXP; } function isFunction(v) { return jsType(v) === T_FUNCTION; } function isNil(v) { return v === null || v === undefined; } function isNull(v) { return v === null; } function isUndefined(v) { return v === undefined; } function inArray(arr, item) { return arr.includes(item); } function notInArray(arr, item) { return !inArray(arr, item); } function truthy(arg) { return !!arg; } function isEmpty(x) { return isNil(x) || isArray(x) && x.length === 0 || isObject(x) && keys(x).length === 0 || !x; } // ensure a value is an array function ensureArray(x) { return isArray(x) ? x : [x]; } function has(obj, prop) { return obj.hasOwnProperty(prop); } function err(s) { throw new Error(s); } var keys = Object.keys; // ////////////////// UTILS //////////////////// /** * Iterate over an array or object * @param {Array|Object} obj An object-like value * @param {Function} fn The callback to run per item * @param {*} ctx The object to use a context * @return {void} */ function each(obj, fn, ctx) { fn = fn.bind(ctx); if (isArray(obj)) { for (var i = 0, len = obj.length; i < len; i++) { if (fn(obj[i], i, obj) === false) break; } } else { for (var k in obj) { if (obj.hasOwnProperty(k)) { if (fn(obj[k], k, obj) === false) break; } } } } /** * Transform values in an object * * @param {Object} obj An object whose values to transform * @param {Function} fn The transform function * @param {*} ctx The value to use as the "this" context for the transform * @return {Array|Object} Result object after applying the transform */ function objectMap(obj, fn, ctx) { fn = fn.bind(ctx); var o = {}; var objKeys = keys(obj); for (var i = 0; i < objKeys.length; i++) { var k = objKeys[i]; o[k] = fn(obj[k], k); } return o; } /** * Deep merge objects or arrays. * When the inputs have unmergeable types, the source value (right hand side) is returned. * If inputs are arrays of same length and all elements are mergable, elements in the same position are merged together. * If any of the elements are unmergeable, elements in the source are appended to the target. * @param target {Object|Array} the target to merge into * @param obj {Object|Array} the source object */ function merge(target, obj) { var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; // take care of missing inputs if (target === MISSING) return obj; if (obj === MISSING) return target; var inputs = [target, obj]; if (!(inputs.every(isObject) || inputs.every(isArray))) { throw Error('mismatched types. must both be array or object'); } // default options opt.flatten = opt.flatten || false; if (isArray(target)) { if (opt.flatten) { var i = 0; var j = 0; while (i < target.length && j < obj.length) { target[i] = merge(target[i++], obj[j++], opt); } while (j < obj.length) { target.push(obj[j++]); } } else { arrayPush.apply(target, obj); } } else { Object.keys(obj).forEach(function (k) { if (target.hasOwnProperty(k)) { target[k] = merge(target[k], obj[k], opt); } else { target[k] = obj[k]; } }); } return target; } /** * Reduce any array-like object * @param collection * @param fn * @param accumulator * @returns {*} */ function reduce(collection, fn, accumulator) { if (isArray(collection)) return collection.reduce(fn, accumulator); // array-like objects each(collection, function (v, k) { return accumulator = fn(accumulator, v, k, collection); }); return accumulator; } /** * Returns the intersection between two arrays * * @param {Array} xs The first array * @param {Array} ys The second array * @return {Array} Result array */ function intersection(xs, ys) { var hashes = ys.map(hashCode); return xs.filter(function (v) { return inArray(hashes, hashCode(v)); }); } /** * Returns the union of two arrays * * @param {Array} xs The first array * @param {Array} ys The second array * @return {Array} The result array */ function union(xs, ys) { return into(into([], xs), ys.filter(notInArray.bind(null, xs))); } /** * Flatten the array * * @param {Array} xs The array to flatten * @param {Number} depth The number of nested lists to iterate */ function flatten(xs) { var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -1; var arr = []; function flatten2(ys, iter) { for (var i = 0, len = ys.length; i < len; i++) { if (isArray(ys[i]) && (iter > 0 || iter < 0)) { flatten2(ys[i], Math.max(-1, iter - 1)); } else { arr.push(ys[i]); } } } flatten2(xs, depth); return arr; } /** * Unwrap a single element array to specified depth * @param {Array} arr * @param {Number} depth */ function unwrap(arr, depth) { if (depth < 1) return arr; while (depth-- && isArray(arr) && arr.length === 1) { arr = arr[0]; } return arr; } /** * Determine whether two values are the same or strictly equivalent * * @param {*} a The first value * @param {*} b The second value * @return {Boolean} Result of comparison */ function isEqual(a, b) { var lhs = [a]; var rhs = [b]; while (lhs.length > 0) { a = lhs.pop(); b = rhs.pop(); // strictly equal must be equal. if (a === b) continue; // unequal types and functions cannot be equal. var type = jsType(a); if (type !== jsType(b) || type === T_FUNCTION) return false; // leverage toString for Date and RegExp types switch (type) { case T_ARRAY: if (a.length !== b.length) return false; //if (a.length === b.length && a.length === 0) continue into(lhs, a); into(rhs, b); break; case T_OBJECT: // deep compare objects var ka = keys(a); var kb = keys(b); // check length of keys early if (ka.length !== kb.length) return false; // we know keys are strings so we sort before comparing ka.sort(); kb.sort(); // compare keys for (var i = 0, len = ka.length; i < len; i++) { var temp = ka[i]; if (temp !== kb[i]) { return false; } else { // save later work lhs.push(a[temp]); rhs.push(b[temp]); } } break; default: // compare encoded values if (encode(a) !== encode(b)) return false; } } return lhs.length === 0; } /** * Return a new unique version of the collection * @param {Array} xs The input collection * @return {Array} A new collection with unique values */ function unique(xs) { var h = {}; var arr = []; each(xs, function (item) { var k = hashCode(item); if (!has(h, k)) { arr.push(item); h[k] = 0; } }); return arr; } /** * Encode value to string using a simple non-colliding stable scheme. * * @param value * @returns {*} */ function encode(value) { var type = jsType(value); switch (type) { case T_BOOLEAN: case T_NUMBER: case T_REGEXP: return value.toString(); case T_STRING: return JSON.stringify(value); case T_DATE: return value.toISOString(); case T_NULL: case T_UNDEFINED: return type; case T_ARRAY: return '[' + value.map(encode) + ']'; default: var prefix = type === T_OBJECT ? '' : "".concat(getType(value)); var objKeys = keys(value); objKeys.sort(); return "".concat(prefix, "{") + objKeys.map(function (k) { return "".concat(encode(k), ":").concat(encode(value[k])); }) + '}'; } } /** * Generate hash code * This selected function is the result of benchmarking various hash functions. * This version performs well and can hash 10^6 documents in ~3s with on average 100 collisions. * * @param value * @returns {*} */ function hashCode(value) { if (isNil(value)) return null; var hash = 0; var s = encode(value); var i = s.length; while (i) { hash = (hash << 5) - hash ^ s.charCodeAt(--i); } return hash >>> 0; } /** * Default compare function * @param {*} a * @param {*} b */ function compare(a, b) { if (a < b) return -1; if (a > b) return 1; return 0; } /** * Returns a (stably) sorted copy of list, ranked in ascending order by the results of running each value through iteratee * * This implementation treats null/undefined sort keys as less than every other type * * @param {Array} collection * @param {Function} fn The function used to resolve sort keys * @param {Function} cmp The comparator function to use for comparing values * @return {Array} Returns a new sorted array by the given iteratee */ function sortBy(collection, fn, cmp) { var sorted = []; var result = []; var hash = {}; cmp = cmp || compare; if (isEmpty(collection)) return collection; for (var i = 0; i < collection.length; i++) { var obj = collection[i]; var key = fn(obj, i); // objects with nil keys will go in first if (isNil(key)) { result.push(obj); } else { if (hash[key]) { hash[key].push(obj); } else { hash[key] = [obj]; } sorted.push(key); } } // use native array sorting but enforce stableness sorted.sort(cmp); for (var _i = 0; _i < sorted.length; _i++) { into(result, hash[sorted[_i]]); } return result; } /** * Groups the collection into sets by the returned key * * @param collection * @param fn {Function} to compute the group key of an item in the collection * @returns {{keys: Array, groups: Array}} */ function groupBy(collection, fn) { var result = { 'keys': [], 'groups': [] }; var lookup = {}; each(collection, function (obj) { var key = fn(obj); var hash = hashCode(key); var index = -1; if (lookup[hash] === undefined) { index = result.keys.length; lookup[hash] = index; result.keys.push(key); result.groups.push([]); } index = lookup[hash]; result.groups[index].push(obj); }); return result; } /** * Push elements in given array into target array * * @param {*} target The array to push into * @param {*} xs The array of elements to push */ function into(target, xs) { arrayPush.apply(target, xs); return target; } /** * Find the insert index for the give