-
Notifications
You must be signed in to change notification settings - Fork 134
/
pivot.min.js
1 lines (1 loc) · 8.21 KB
/
pivot.min.js
1
var pivot=function(){"use strict";function r(r){return ve=[],de=[],me={},ce={},fe={},be={rowLabels:{},columnLabels:{},summaries:{}},void 0===r&&(r={}),void 0!==r.fields&&S(r.fields),void 0!==r.filters&&z(r.filters),void 0!==r.rowLabels&&B(r.rowLabels),void 0!==r.columnLabels&&G(r.columnLabels),void 0!==r.summaries&&Q(r.summaries),void 0!==r.csv&&p(r.csv),void 0!==r.json&&h(r.json),pivot}function e(){return r()}function n(r){var e;return void 0===r?e=I():r===!1&&(e="Pass showFields as true in order to view fields here."),{fields:e,filters:fe,rowLabels:v(be.rowLabels),columnLabels:v(be.columnLabels),summaries:v(be.summaries)}}function t(){return{pad:a,padRight:o,padLeft:i,formatDate:u,formatTime:s,isArray:l,isRegExp:c,shallowClone:f,objectKeys:v,objectType:d,sortNumerically:m}}function a(r,e,n,t){for(void 0===t&&(t=" "),e=e.toString(),t=t.toString();e.length<n;)"left"===r?e=t+e:e+=t;return e}function o(r,e,n){return a("right",r,e,n)}function i(r,e,n){return a("left",r,e,n)}function u(r){return r.getUTCFullYear()+"-"+i(r.getUTCMonth()+1,2,"0")+"-"+i(r.getUTCDate(),2,"0")}function s(r){return u(r)+" "+i(r.getUTCHours(),2,"0")+":"+i(r.getUTCMinutes(),2,"0")}function l(r){return Array.isArray?Array.isArray(r):"array"==d(r)}function c(r){return"regexp"==d(r)}function f(r){var e={};for(var n in r)r.hasOwnProperty(n)&&(e[n]=r[n]);return e}function v(r){if(Object.keys)return Object.keys(r);var e=[];for(key in r)e.push(key);return e}function d(r){return{}.toString.call(r).match(/\s([a-z|A-Z]+)/)[1].toLowerCase()}function m(r){return r.sort(function(r,e){return r-e})}function b(r){for(var e=[],n=-1,t=r.length;++n<t;){var a=ce[r[n]];void 0===a&&(a=$(r[n])),e.push(a)}return e}function h(r){var e,n=k("pseudo");"string"===d(r)&&(r=JSON.parse(r)),ve=[];for(var t=-1,a=r.length;++t<a;)0===t?e=b(r[t]):ve.push(w(r[t],e,n))}function p(r){var e,n=k("pseudo");ve=g(r,function(r,t){return 0!==t?w(r,e,n):(e=b(r),void 0)})}function g(r,e){function n(){if(s.lastIndex>=r.length)return i;if(a)return a=!1,o;var e=s.lastIndex;if(34===r.charCodeAt(e)){for(var n=e;n++<r.length;)if(34===r.charCodeAt(n)){if(34!==r.charCodeAt(n+1))break;n++}s.lastIndex=n+2;var t=r.charCodeAt(n+1);return 13===t?(a=!0,10===r.charCodeAt(n+2)&&s.lastIndex++):10===t&&(a=!0),r.substring(e+1,n).replace(/""/g,'"')}var u=s.exec(r);return u?(a=44!==u[0].charCodeAt(0),r.substring(e,u.index)):(s.lastIndex=r.length,r.substring(e))}var t,a,o={},i={},u=[],s=/\r\n|[,\r\n]/g,l=0;for(s.lastIndex=0;(t=n())!==i;){for(var c=[];t!==o&&t!==i;)c.push(t),t=n();(!e||(c=e(c,l++)))&&u.push(c)}return u}function w(r,e,n){for(var t={},a=-1,o=e.length;++a<o;){var i=V(e[a].name,r[a]);t[e[a].name]=i,M(e[a].name,i)}for(a=-1,o=n.length;++a<o;){var u=n[a],i=V(u.name,u.pseudoFunction(t,u));t[u.name]=i,M(u.name,i)}return t}function y(r){var e={all:O,set:z,apply:P,add:F};return void 0!==r?e[r]:e}function L(r){void 0===r&&(r=fe);var e;for(e in r)if(r.hasOwnProperty(e))if(c(r[e]));else if(l(r[e]))for(var n=-1,t=r[e].length;++n<t;)r[e][n]=V(e,r[e][n]);else r[e]=V(e,r[e])}function F(r){for(var e in r)r.hasOwnProperty(e)&&(fe[e]=r[e]);L()}function O(){return fe}function z(r){fe=r,X(),L()}function P(r){var e=de,n=[];void 0!==r&&z(r);var t=C();e=t?de:ve;for(var a=e.length,o=v(fe).length,i=-1;++i<a;){var u=e[i],s=0;for(var l in fe)fe.hasOwnProperty(l)&&u.hasOwnProperty(l)&&x(fe[l],u[l])&&(s+=1);s===o&&n.push(u)}return de=n,me=f(fe),X(),de}function x(r,e){if(!l(r))return c(r)?r.test(e):e===r;for(var n=-1,t=r.length;++n<t;)if(r[n]===e)return!0;return!1}function C(){var r=0,e=v(me).length;for(var n in me)me.hasOwnProperty(n)&&me.hasOwnProperty(n)&&fe[n]===me[n]&&(r+=1);return e>0&&r>=e}function A(r){var e={columnLabelable:k("columnLabelable"),rowLabelable:k("rowLabelable"),summarizable:k("summarizable"),filterable:k("filterable"),pseudo:k("pseudo"),clone:I,add:$,all:j,set:S,get:T};return void 0!==r?e[r]:e}function S(r){ce={};for(var e=-1,n=r.length;++e<n;)$(r[e])}function I(){var r=[];for(var e in ce){var n={};for(var t in ce[e])ce[e].hasOwnProperty(t)&&"values"!==t&&(n[t]=ce[e][t]);r.push(n)}return r}function j(){var r=[];for(var e in ce)ce.hasOwnProperty(e)&&(r[ce[e].index]=ce[e]);return r}function k(r){var e=[];for(var n in ce)ce.hasOwnProperty(n)&&ce[n][r]===!0&&e.push(ce[n]);return e}function T(r){return ce[r]}function D(r,e){for(var n=0,t=-1,a=r.length;++t<a;)n+=r[t][e.dataSource];return n}function N(r,e){return D(r,e)/r.length}function U(r){return r.length}function $(r){if("string"===d(r)&&(r={name:r}),void 0===r.type&&(r.type="string"),void 0===r.pseudo&&(r.pseudo=!1),void 0===r.rowLabelable&&(r.rowLabelable=!0),void 0===r.columnLabelable&&(r.columnLabelable=!1),void 0===r.filterable&&(r.filterable=!1),void 0===r.dataSource&&(r.dataSource=r.name),r.summarizable&&(r.rowLabelable||r.columnLabelable||r.filterable)){var e=f(r);e.rowLabelable=!1,e.filterable=!1,e.dataSource=r.name,e.name=e.summarizable!==!0?e.name+"_"+e.summarizable:e.name+"_count",$(e),r.summarizable=!1,r.summarizeFunction=void 0}else if(r.summarizable){if(void 0===r.summarizeFunction){switch(r.summarizable){case"sum":r.summarizeFunction=D;break;case"avg":r.summarizeFunction=N;break;default:r.summarizeFunction=U}r.summarizable=!0}}else r.summarizable=!1;return r.pseudo&&void 0===r.pseudoFunction&&(r.pseudoFunction=function(){return""}),void 0===r.displayFunction&&(r.displayFunction=R),r.values={},r.displayValues={},r.index=v(ce).length,ce[r.name]=r,r}function M(r,e){void 0!==ce[r]&&ce[r].filterable!==!1&&(void 0===ce[r].values[e]?ce[r].values[e]={count:1,displayValue:ce[r].displayFunction(e,r)}:ce[r].values[e].count+=1)}function R(r,e){var n;switch("string"===d(e)&&(n=ce[e]),void 0===n&&(n=$(e)),n.type){case"cents":return"$"+(r/100).toFixed(2);case"currency":return"$"+r.toFixed(2);case"date":return u(new Date(r));case"time":return s(new Date(r));default:return r}}function V(r,e){var n;switch("string"===d(r)&&(n=ce[r]),void 0===n&&(n=$(r)),n.type){case"integer":case"cents":return"number"===d(e)?e:parseInt(e,10);case"float":case"currency":return"number"===d(e)?e:parseFloat(e,10);case"date":case"time":switch(d(e)){case"number":case"date":return e;default:return/^\d+$/.test(e)?parseInt(e):Date.parse(e)}default:return e.toString()}}function _(r){var e={raw:ve,all:de};return void 0!==r?e[r]:e}function E(){return{all:H,rowLabels:J,columnLabels:K,summaries:Y}}function H(){return be}function J(){return{set:B,get:be.rowLabels}}function K(){return{set:G,get:be.columnLabels}}function Y(){return{set:Q,get:be.summaries}}function Z(r,e){"string"===d(e)&&(e=ce[e]),X(),be[r][e.name]=e}function q(r,e){be[r]={},X();for(var n=-1,t=e.length;++n<t;)Z(r,e[n])}function B(r){q("rowLabels",r)}function G(r){q("columnLabels",r)}function Q(r){q("summaries",r)}function W(){return{all:re,columns:ue}}function X(){he=void 0,pe=void 0}function re(){return void 0!==he&&void 0!==pe?ie():(P(),he={},pe=[],ee(),v(be.columnLabels).length>0?ne():(se(),ae()),ie())}function ee(){for(var r=-1,e=de.length;++r<e;){var n=de[r],t="";for(var a in be.rowLabels)be.rowLabels.hasOwnProperty(a)&&(0===r&&pe.push({fieldName:a,width:1,type:"row"}),t+=a+":"+n[a]+"|");if(void 0===he[t]){he[t]={};for(var a in be.rowLabels)be.rowLabels.hasOwnProperty(a)&&(he[t][a]=ce[a].displayFunction(n[a],a));he[t].rows=[]}he[t].rows.push(n)}}function ne(){for(var r in be.columnLabels)if(be.columnLabels.hasOwnProperty(r)){var e={};for(var n in he){var t=te(he[n].rows,ce[r]);for(var a in t)void 0===e[a]?e[a]=1:e[a]+=1,he[n][a]=oe(t[a])}le(r,e)}return he}function te(r,e){for(var n=-1,t=r.length,a={};++n<t;){var o=r[n][e.name];void 0===a[o]&&(a[o]={rows:[]}),a[o].rows.push(r[n])}return a}function ae(){for(var r in he)oe(he[r]);return he}function oe(r){for(var e in be.summaries)be.summaries.hasOwnProperty(e)&&(r[e]=ce[e].summarizeFunction(r.rows,ce[e]),r[e]=ce[e].displayFunction(r[e],e));return r}function ie(){for(var r=[],e=v(he).sort(),n=-1,t=e.length;++n<t;)r.push(he[e[n]]);return r}function ue(){return(void 0===he||void 0===pe)&&re(),pe}function se(){for(var r in be.summaries)be.summaries.hasOwnProperty(r)&&pe.push({fieldName:r,width:1,type:"summary"});return pe}function le(r,e){for(var n=v(e).sort(ce[r].sortFunction),t=-1,a=n.length,o=v(be.summaries).length;++t<a;)pe.push({fieldName:n[t],width:o,type:"column"});return pe}var ce,fe,ve,de,me,be,he,pe;return r(),{init:r,reset:e,config:n,utils:t,csv:p,json:h,data:_,results:W,fields:A,filters:y,display:E}}();