forked from rwjblue/pivot.js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pivot.min.js
1 lines (1 loc) · 12.2 KB
/
pivot.min.js
1
var pivot = function () { "use strict"; function l(o) { n = [], r = [], i = {}, e = {}, t = {}; s = { rowLabels: {}, columnLabels: {}, summaries: {} }; if (o === undefined) o = {}; if (o.fields !== undefined) z(o.fields); if (o.filters !== undefined) F(o.filters); if (o.rowLabels !== undefined) ct(o.rowLabels); if (o.columnLabels !== undefined) ht(o.columnLabels); if (o.summaries !== undefined) pt(o.summaries); if (o.currencySymbolField !== undefined) u = o.currencySymbolField; if (o.defaultCurrencySymbol !== undefined) f = o.defaultCurrencySymbol; if (o.csv !== undefined) M(o.csv); if (o.json !== undefined) O(o.json); return pivot } function c() { return l() } function h(e) { var n; if (e === undefined) n = W(); else if (e === false) n = "Pass showFields as true in order to view fields here."; return { fields: n, filters: t, rowLabels: N(s.rowLabels), columnLabels: N(s.columnLabels), summaries: N(s.summaries) } } function p() { return { pad: d, padRight: v, padLeft: m, formatDate: g, formatTime: y, isArray: S, isRegExp: x, shallowClone: T, objectKeys: N, objectType: C, sortNumerically: k, sortDate: L, isNumber: E, formatCurrency: b } } function d(e, t, n, r) { if (r === undefined) r = " "; t = t.toString(); r = r.toString(); while (t.length < n) { if (e === "left") t = r + t; else t = t + r } return t } function v(e, t, n) { return d("right", e, t, n) } function m(e, t, n) { return d("left", e, t, n) } function g(e) { return e.getUTCFullYear() + "-" + m(e.getUTCMonth() + 1, 2, "0") + "-" + m(e.getUTCDate(), 2, "0") } function y(e) { return g(e) + " " + m(e.getUTCHours(), 2, "0") + ":" + m(e.getUTCMinutes(), 2, "0") } function b(e, t, n) { var r = n; if (S(r.rows)) { r = r.rows[0] } var i = r[u]; if (i === undefined) { i = f } var s = e; if (E(e)) { if (w()) { s = accounting.formatMoney(e, i) } else { s = i + e.toFixed(2) } } return s } function w() { return typeof accounting !== "undefined" && C(accounting) === "object" && C(accounting.formatMoney) === "function" } function E(e) { return !isNaN(parseFloat(e)) && isFinite(e) } function S(e) { if (!Array.isArray) return C(e) == "array"; else return Array.isArray(e) } function x(e) { return C(e) == "regexp" } function T(e) { var t = {}; for (var n in e) { if (e.hasOwnProperty(n)) t[n] = e[n] } return t } function N(e) { if (Object.keys) return Object.keys(e); var t = []; for (key in e) { t.push(key) } return t } function C(e) { return {}.toString.call(e).match(/\s([a-z|A-Z]+)/)[1].toLowerCase() } function k(e) { return e.sort(function (e, t) { return e - t }) } function L(e, t) { if (C(e) !== "date") e = new Date(e); if (C(t) !== "date") t = new Date(t); if (e > t) return 1; if (e < t) return -1; return 0 } function A(t) { var n = []; var r = {}, i = -1, s = t.length; while (++i < s) { var o = e[t[i]]; if (o === undefined) o = Z(t[i]); n.push(o) } return n } function O(e) { var t, r = V("pseudo"); if (C(e) === "string") e = JSON.parse(e); n = []; var i = {}, s = -1, o = e.length; while (++s < o) { if (s === 0) t = A(e[s]); else n.push(D(e[s], t, r)) } } function M(e) { var t, r = V("pseudo"); n = _(e, function (e, n) { if (n === 0) t = A(e); else return D(e, t, r) }) } function _(e, t) { function f() { if (s.lastIndex >= e.length) return r; if (a) { a = false; return n } var t = s.lastIndex; if (e.charCodeAt(t) === 34) { var i = t; while (i++ < e.length) { if (e.charCodeAt(i) === 34) { if (e.charCodeAt(i + 1) !== 34) break; i++ } } s.lastIndex = i + 2; var o = e.charCodeAt(i + 1); if (o === 13) { a = true; if (e.charCodeAt(i + 2) === 10) s.lastIndex++ } else if (o === 10) { a = true } return e.substring(t + 1, i).replace(/""/g, '"') } var u = s.exec(e); if (u) { a = u[0].charCodeAt(0) !== 44; return e.substring(t, u.index) } s.lastIndex = e.length; return e.substring(t) } var n = {}, r = {}, i = [], s = /\r\n|[,\r\n]/g, o = 0, u, a; s.lastIndex = 0; while ((u = f()) !== r) { var l = []; while (u !== n && u !== r) { l.push(u); u = f() } if (t && !(l = t(l, o++))) continue; i.push(l) } return i } function D(e, t, n) { var r = {}, i = -1, s = t.length; while (++i < s) { var o = nt(t[i].name, e[i]); r[t[i].name] = o } i = -1; while (++i < s) { var u = t[i].name; et(u, r[u], r) } i = -1, s = n.length; while (++i < s) { var a = n[i], o = nt(a.name, a.pseudoFunction(r, a)); r[a.name] = o; et(a.name, o, r) } return r } function P(e) { var t = { all: j, set: F, apply: I, add: B }; if (e !== undefined) { return t[e] } else { return t } } function H(e) { if (e === undefined) e = t; var n; for (n in e) { if (e.hasOwnProperty(n)) if (x(e[n])) { } else if (S(e[n])) { var r = -1, i = e[n].length; while (++r < i) { e[n][r] = nt(n, e[n][r]) } } else { e[n] = nt(n, e[n]) } } } function B(e) { for (var n in e) { if (e.hasOwnProperty(n)) t[n] = e[n] } H() } function j() { return t } function F(e) { t = e; vt(); H() } function I(e) { var s = r, o = []; if (e !== undefined) F(e); var u = R(); if (u) { s = r } else { s = n } var a = s.length, f = N(t).length, l = -1; while (++l < a) { var c = s[l], h = 0; for (var p in t) { if (t.hasOwnProperty(p) && c.hasOwnProperty(p) && q(t[p], c[p])) h += 1 } if (h === f) { o.push(c) } } r = o; i = T(t); vt(); return r } function q(e, t) { if (S(e)) { var n = -1, r = e.length; while (++n < r) { if (e[n] === t) return true } } else if (x(e)) { return e.test(t) } else { return t === e } return false } function R() { var e = 0, n = N(i).length; for (var r in i) { if (i.hasOwnProperty(r) && i.hasOwnProperty(r) && t[r] === i[r]) e += 1 } return n > 0 && e >= n } function U(e) { var t = { columnLabelable: V("columnLabelable"), rowLabelable: V("rowLabelable"), summarizable: V("summarizable"), filterable: V("filterable"), pseudo: V("pseudo"), clone: W, add: Z, all: X, set: z, get: $ }; if (e !== undefined) { return t[e] } else { return t } } function z(t) { e = {}; var n = -1, r = t.length; while (++n < r) { Z(t[n]) } } function W() { var t = []; for (var n in e) { var r = {}; for (var i in e[n]) { if (e[n].hasOwnProperty(i) && i !== "values") r[i] = e[n][i] } t.push(r) } return t } function X() { var t = []; for (var n in e) { if (e.hasOwnProperty(n)) t[e[n].index] = e[n] } return t } function V(t) { var n = []; for (var r in e) { if (e.hasOwnProperty(r) && e[r][t] === true) n.push(e[r]) } return n } function $(t) { return e[t] } function J(e, t) { var n = 0, r = -1, i = e.length, s = null; if (t.type == "currency" && u != null) { s = e[0][u] } while (++r < i) { var o = e[r]; if (s != null && s != o[u]) { n = "Multiple-Currency-Error"; break } else { n += o[t.dataSource] } } return n } function K(e, t) { var n = null, r = -1, i = e.length, s = null; if (t.type == "currency" && u != null) { s = e[0][u] } while (++r < i) { var o = e[r]; if (s != null && s != o[u]) { n = "Multiple-Currency-Error"; break } else { var a = o[t.dataSource]; if (n === null) n = a; if (a < n) n = a } } return n } function Q(e, t) { var n = null, r = -1, i = e.length, s = null; if (t.type == "currency" && u != null) { s = e[0][u] } while (++r < i) { var o = e[r]; if (s != null && s != o[u]) { n = "Multiple-Currency-Error"; break } else { var a = o[t.dataSource]; if (n === null) n = a; if (a > n) n = a } } return n } function G(e, t) { var n = J(e, t); if (n != "Multiple-Currency-Error") { n = n / e.length } return n } function Y(e, t) { return e.length } function Z(t) { if (C(t) === "string") t = { name: t }; if (t.type === undefined) t.type = "string"; if (t.pseudo === undefined) t.pseudo = false; if (t.rowLabelable === undefined) t.rowLabelable = true; if (t.columnLabelable === undefined) t.columnLabelable = false; if (t.filterable === undefined) t.filterable = false; if (t.dataSource === undefined) t.dataSource = t.name; if (t.type == "date") t.sortFunction = L; if (t.summarizable && (t.rowLabelable || t.columnLabelable || t.filterable)) { var n = T(t); n.rowLabelable = false; n.filterable = false; n.dataSource = t.name; if (n.summarizable !== true) n.name = n.name + "_" + n.summarizable; else n.name = n.name + "_count"; Z(n); t.summarizable = false; t.summarizeFunction = undefined } else if (t.summarizable) { if (t.summarizeFunction === undefined) { switch (t.summarizable) { case "sum": t.summarizeFunction = J; break; case "avg": t.summarizeFunction = G; break; case "min": t.summarizeFunction = K; break; case "max": t.summarizeFunction = Q; break; default: t.summarizeFunction = Y; break } t.summarizable = true } } else { t.summarizable = false } if (t.pseudo && t.pseudoFunction === undefined) t.pseudoFunction = function (e) { return "" }; if (t.displayFunction === undefined) t.displayFunction = tt; t.values = {}; t.displayValues = {}; t.index = N(e).length; e[t.name] = t; return t } function et(t, n, r) { if (e[t] === undefined || e[t].filterable === false) return; if (e[t].values[n] === undefined) { e[t].values[n] = { count: 1, displayValue: e[t].displayFunction(n, t, r) } } else { e[t].values[n].count += 1 } } function tt(t, n, r) { var i; if (C(n) === "string") i = e[n]; if (i === undefined) i = Z(n); switch (i.type) { case "cents": return b(t / 100, i, r); case "currency": return b(t, i, r); case "date": return g(new Date(t)); case "time": return y(new Date(t)); default: return t } } function nt(t, n) { var r, i; if (C(t) === "string") r = e[t]; if (r === undefined) r = Z(t); switch (r.type) { case "integer": case "cents": if (C(n) === "number") return n; else return parseInt(n, 10); case "float": case "currency": if (C(n) === "number") return n; else return parseFloat(n, 10); case "date": case "time": switch (C(n)) { case "number": case "date": return n; default: if (/^\d+$/.test(n)) return parseInt(n); else return Date.parse(n) }; default: return n.toString() } } function rt(e) { var t = { raw: n, all: r }; if (e !== undefined) { return t[e] } else { return t } } function it() { return { all: st, rowLabels: ot, columnLabels: ut, summaries: at } } function st() { return s } function ot() { return { set: ct, get: s.rowLabels } } function ut() { return { set: ht, get: s.columnLabels } } function at() { return { set: pt, get: s.summaries } } function ft(t, n) { if (C(n) === "string") n = e[n]; vt(); s[t][n.name] = n } function lt(e, t) { s[e] = {}; vt(); var n = -1, r = t.length; while (++n < r) { ft(e, t[n]) } } function ct(e) { lt("rowLabels", e) } function ht(e) { lt("columnLabels", e) } function pt(e) { lt("summaries", e) } function dt() { return { all: mt, columns: xt } } function vt() { o = undefined; a = undefined } function mt() { if (o !== undefined && a !== undefined) return St(); I(); o = {}; a = []; gt(); if (N(s.columnLabels).length > 0) yt(); else { Tt(); wt() } return St() } function gt() { var t = -1, n = r.length, i; while (++t < n) { var u = r[t], f = ""; for (var l in s.rowLabels) { if (s.rowLabels.hasOwnProperty(l)) { if (t === 0) a.push({ fieldName: l, width: 1, type: "row" }); f += l + ":" + u[l] + "|" } } if (o[f] === undefined) { o[f] = {}; for (var l in s.rowLabels) { if (s.rowLabels.hasOwnProperty(l)) o[f][l] = e[l].displayFunction(u[l], l, u) } o[f].rows = [] } o[f].rows.push(u) } } function yt() { for (var t in s.columnLabels) { if (s.columnLabels.hasOwnProperty(t)) { var n = {}; for (var r in o) { var i = e[t]; var u = bt(o[r], i); for (var a in u) { if (n[a] === undefined) n[a] = 1; else n[a] += 1; o[r][a] = Et(u[a]) } } Nt(t, n) } } return o } function bt(e, t) { var n = -1, r = e.rows.length, i = {}; while (++n < r) { var s = e.rows[n][t.name]; s = t.displayFunction(s, t, e); if (i[s] === undefined) i[s] = { rows: [] }; i[s].rows.push(e.rows[n]) } return i } function wt() { for (var e in o) { Et(o[e]) } return o } function Et(t) { var n = {}; for (var r in s.summaries) { if (s.summaries.hasOwnProperty(r)) { t[r] = e[r].summarizeFunction(t.rows, e[r]); t[r] = e[r].displayFunction(t[r], r, t) } } return t } function St() { var e = [], t = N(o).sort(), n = -1, r = t.length; while (++n < r) { e.push(o[t[n]]) } return e } function xt() { if (o === undefined || a === undefined) mt(); return a } function Tt() { for (var e in s.summaries) { if (s.summaries.hasOwnProperty(e)) a.push({ fieldName: e, width: 1, type: "summary" }) } return a } function Nt(t, n) { var r = N(n).sort(e[t].sortFunction), i = -1, o = r.length, u = N(s.summaries).length; while (++i < o) { a.push({ fieldName: r[i], width: u, type: "column" }) } return a } var e, t, n, r, i, s, o, u, a; var f = "$"; l(); return { init: l, reset: c, config: h, utils: p, csv: M, json: O, data: rt, results: dt, fields: U, filters: P, display: it } }()