-
Notifications
You must be signed in to change notification settings - Fork 42
/
reductio.min.js
2 lines (2 loc) · 16.6 KB
/
reductio.min.js
1
2
// https://github.com/esjewett/reductio v1.0.0 Copyright 2020 Ethan Jewett
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n(require("crossfilter2")):"function"==typeof define&&define.amd?define(["crossfilter2"],n):(e=e||self).reductio=n(e.crossfilter)}(this,(function(e){"use strict";e=e&&e.hasOwnProperty("default")?e.default:e;var n=function(e,n,t){return function(u,r,i){return e(r,i)?n&&n(u,r,i):t&&t(u,r,i),u}},t=function(e,n,t){return function(u,r,i){return e(r,i)?n&&n(u,r,i):t&&t(u,r,i),u}},u=function(e,n,t){return function(u,r,i){return e&&e(u,r,i),n(u)[t]++,u}},r=function(e,n,t){return function(u,r,i){return e&&e(u,r,i),n(u)[t]--,u}},i=function(e,n,t){return function(u){return e&&(u=e(u)),n(u)[t]=0,u}},o=function(e,n,t){return function(u,r,i){return n&&n(u,r,i),t(u).sum=t(u).sum+e(r),u}},c=function(e,n,t){return function(u,r,i){return n&&n(u,r,i),t(u).sum=t(u).sum-e(r),u}},s=function(e,n){return function(t){return t=e(t),n(t).sum=0,t}},a=function(e,n,t){return function(e,u,r){return n&&n(e,u,r),t(e).count>0?t(e).avg=t(e).sum/t(e).count:t(e).avg=0,e}},l=function(e,n,t){return function(e,u,r){return n&&n(e,u,r),t(e).count>0?t(e).avg=t(e).sum/t(e).count:t(e).avg=0,e}},d=function(e,n){return function(t){return t=e(t),n(t).avg=0,t}},f=function(e,n){var t;return function(u,r,i){return e&&e(u,r,i),t=Math.floor(n(u).valueList.length/2),n(u).valueList.length%2?n(u).median=n(u).valueList[t]:n(u).median=(n(u).valueList[t-1]+n(u).valueList[t])/2,u}},v=function(e,n){var t;return function(u,r,i){return e&&e(u,r,i),t=Math.floor(n(u).valueList.length/2),0===n(u).valueList.length?(n(u).median=void 0,u):(1===n(u).valueList.length||n(u).valueList.length%2?n(u).median=n(u).valueList[t]:n(u).median=(n(u).valueList[t-1]+n(u).valueList[t])/2,u)}},m=function(e,n){return function(t){return t=e(t),n(t).median=void 0,t}},g=function(e,n){return function(t,u,r){return e&&e(t,u,r),n(t).min=n(t).valueList[0],t}},h=function(e,n){return function(t,u,r){return e&&e(t,u,r),0===n(t).valueList.length?(n(t).min=void 0,t):(n(t).min=n(t).valueList[0],t)}},p=function(e,n){return function(t){return t=e(t),n(t).min=void 0,t}},A=function(e,n){return function(t,u,r){return e&&e(t,u,r),n(t).max=n(t).valueList[n(t).valueList.length-1],t}},x=function(e,n){return function(t,u,r){return e&&e(t,u,r),0===n(t).valueList.length?(n(t).max=void 0,t):(n(t).max=n(t).valueList[n(t).valueList.length-1],t)}},y=function(e,n){return function(t){return t=e(t),n(t).max=void 0,t}},L=function(e,n,t){var u,r;return function(i,o,c){return n&&n(i,o,c),u=t(i).bisect(t(i).values,e(o),0,t(i).values.length),(r=t(i).values[u])&&r[0]===e(o)?r[1]++:t(i).values.splice(u,0,[e(o),1]),i}},I=function(e,n,t){var u;return function(r,i,o){return n&&n(r,i,o),u=t(r).bisect(t(r).values,e(i),0,t(r).values.length),t(r).values[u][1]--,r}},b=function(n,t){return function(u){return u=n(u),t(u).values=[],t(u).bisect=e.bisect.by((function(e){return e[0]})).left,u}},R=function(n,t,u){var r,i=e.bisect.by((function(e){return e})).left;return function(e,o,c){return t&&t(e,o,c),r=i(u(e).valueList,n(o),0,u(e).valueList.length),u(e).valueList.splice(r,0,n(o)),e}},S=function(n,t,u){var r,i=e.bisect.by((function(e){return e})).left;return function(e,o,c){return t&&t(e,o,c),r=i(u(e).valueList,n(o),0,u(e).valueList.length),u(e).valueList.splice(r,1),e}},O=function(e,n){return function(t){return t=e(t),n(t).valueList=[],t}},q=function(e,n,t){var u,r;return function(i,o,c){return n&&n(i,o,c),u=t(i).bisect(t(i).values,e(o),0,t(i).values.length),(r=t(i).values[u])&&r[0]===e(o)&&0!==r[1]||t(i).exceptionCount++,i}},K=function(e,n,t){var u,r;return function(i,o,c){return n&&n(i,o,c),u=t(i).bisect(t(i).values,e(o),0,t(i).values.length),(r=t(i).values[u])&&r[0]===e(o)&&1===r[1]&&t(i).exceptionCount--,i}},P=function(e,n){return function(t){return t=e(t),n(t).exceptionCount=0,t}},w=function(e,n,t,u){var r,i;return function(o,c,s){return t&&t(o,c,s),r=u(o).bisect(u(o).values,e(c),0,u(o).values.length),(i=u(o).values[r])&&i[0]===e(c)&&0!==i[1]||(u(o).exceptionSum=u(o).exceptionSum+n(c)),o}},C=function(e,n,t,u){var r,i;return function(o,c,s){return t&&t(o,c,s),r=u(o).bisect(u(o).values,e(c),0,u(o).values.length),(i=u(o).values[r])&&i[0]===e(c)&&1===i[1]&&(u(o).exceptionSum=u(o).exceptionSum-n(c)),o}},V=function(e,n){return function(t){return t=e(t),n(t).exceptionSum=0,t}},T=function(n,t,u){var r,i=e.bisect.by((function(e){return e})).left,o=e.bisect.by((function(e){return e.x})).right;return function(e,c,s){return t&&t(e,c,s),(r=u(e).histogram[o(u(e).histogram,n(c),0,u(e).histogram.length)-1]).y++,r.splice(i(r,n(c),0,r.length),0,n(c)),e}},E=function(n,t,u){var r,i=e.bisect.by((function(e){return e})).left,o=e.bisect.by((function(e){return e.x})).right;return function(e,c,s){return t&&t(e,c,s),(r=u(e).histogram[o(u(e).histogram,n(c),0,u(e).histogram.length)-1]).y--,r.splice(i(r,n(c),0,r.length),1),e}},k=function(e,n,t){return function(u){u=n(u),t(u).histogram=[];for(var r=[],i=1;i<e.length;i++)(r=[]).x=e[i-1],r.dx=e[i]-e[i-1],r.y=0,t(u).histogram.push(r);return u}},j=function(e,n,t){return function(u,r,i){return n&&n(u,r,i),t(u).sumOfSq=t(u).sumOfSq+e(r)*e(r),u}},M=function(e,n,t){return function(u,r,i){return n&&n(u,r,i),t(u).sumOfSq=t(u).sumOfSq-e(r)*e(r),u}},N=function(e,n){return function(t){return t=e(t),n(t).sumOfSq=0,t}},U=function(e,n){return function(t,u,r){if(e&&e(t,u,r),n(t).count>0){n(t).std=0;var i=n(t).sumOfSq-n(t).sum*n(t).sum/n(t).count;i>0&&(n(t).std=Math.sqrt(i/(n(t).count-1)))}else n(t).std=0;return t}},Y=function(e,n){return function(t,u,r){if(e&&e(t,u,r),n(t).count>0){n(t).std=0;var i=n(t).sumOfSq-n(t).sum*n(t).sum/n(t).count;i>0&&(n(t).std=Math.sqrt(i/(n(t).count-1)))}else n(t).std=0;return t}},B=function(e,n){return function(t){return t=e(t),n(t).std=0,t}},G=function(e,n,t){var u,r;return function(i,o,c){return n&&n(i,o,c),u=t(i).nest,e.forEach((function(e){(r=u.filter((function(n){return n.key===e(o)}))[0])?u=r.values:(r=[],u.push({key:e(o),values:r}),u=r)})),u.push(o),i}},X=function(e,n,t){var u;return function(r,i,o){return n&&n(r,i,o),u=t(r).nest,e.forEach((function(e){u=u.filter((function(n){return n.key===e(i)}))[0].values})),u.splice(u.indexOf(i),1),r}},z=function(e,n){return function(t){return t=e(t),n(t).nest=[],t}},D=function(e,n,t){return function(u){function r(e){return function(){return t[e](n(u))}}for(var i in e&&(u=e(u)),t)n(u)[i]=r(i);return u}},F=function(e,n,t){return function(u,r,i){for(var o in n&&n(u,r,i),e)t(u)[o]=e[o](t(u),r);return u}},H=function(e,n,t){return function(e,u,r){return n&&n(e,u,r),t(e).dataList.push(u),e}},J=function(e,n,t){return function(e,u,r){return n&&n(e,u,r),t(e).dataList.splice(t(e).dataList.indexOf(u),1),e}},Q=function(e,n){return function(t){return e&&(t=e(t)),n(t).dataList=[],t}},W=function(e,n,t){return function(n,u,r){return e&&e(n,u,r),t(n,u)}},Z=function(e,n,t){return function(n,u,r){return e&&e(n,u,r),t(n,u)}},$=function(e,n,t){return function(n){return e&&(n=e(n)),t(n)}};var _={build:function e(_,ee,ne){ne||(ne=function(e){return e});var te={reduceAdd:ee.reduceAdd,reduceRemove:ee.reduceRemove,reduceInitial:ee.reduceInitial};(_.count||_.std)&&(ee.reduceAdd=u(ee.reduceAdd,ne,_.count),ee.reduceRemove=r(ee.reduceRemove,ne,_.count),ee.reduceInitial=i(ee.reduceInitial,ne,_.count)),_.sum&&(ee.reduceAdd=o(_.sum,ee.reduceAdd,ne),ee.reduceRemove=c(_.sum,ee.reduceRemove,ne),ee.reduceInitial=s(ee.reduceInitial,ne)),_.avg&&(_.count&&_.sum?(ee.reduceAdd=a(_.sum,ee.reduceAdd,ne),ee.reduceRemove=l(_.sum,ee.reduceRemove,ne),ee.reduceInitial=d(ee.reduceInitial,ne)):console.error("You must set .count(true) and define a .sum(accessor) to use .avg(true).")),_.exceptionCount&&(_.exceptionAccessor?(ee.reduceAdd=q(_.exceptionAccessor,ee.reduceAdd,ne),ee.reduceRemove=K(_.exceptionAccessor,ee.reduceRemove,ne),ee.reduceInitial=P(ee.reduceInitial,ne)):console.error("You must define an .exception(accessor) to use .exceptionCount(true).")),_.exceptionSum&&(_.exceptionAccessor?(ee.reduceAdd=w(_.exceptionAccessor,_.exceptionSum,ee.reduceAdd,ne),ee.reduceRemove=C(_.exceptionAccessor,_.exceptionSum,ee.reduceRemove,ne),ee.reduceInitial=V(ee.reduceInitial,ne)):console.error("You must define an .exception(accessor) to use .exceptionSum(accessor).")),(_.valueList||_.median||_.min||_.max)&&(ee.reduceAdd=R(_.valueList,ee.reduceAdd,ne),ee.reduceRemove=S(_.valueList,ee.reduceRemove,ne),ee.reduceInitial=O(ee.reduceInitial,ne)),_.dataList&&(ee.reduceAdd=H(_.dataList,ee.reduceAdd,ne),ee.reduceRemove=J(_.dataList,ee.reduceRemove,ne),ee.reduceInitial=Q(ee.reduceInitial,ne)),_.median&&(ee.reduceAdd=f(ee.reduceAdd,ne),ee.reduceRemove=v(ee.reduceRemove,ne),ee.reduceInitial=m(ee.reduceInitial,ne)),_.min&&(ee.reduceAdd=g(ee.reduceAdd,ne),ee.reduceRemove=h(ee.reduceRemove,ne),ee.reduceInitial=p(ee.reduceInitial,ne)),_.max&&(ee.reduceAdd=A(ee.reduceAdd,ne),ee.reduceRemove=x(ee.reduceRemove,ne),ee.reduceInitial=y(ee.reduceInitial,ne)),_.exceptionAccessor&&(ee.reduceAdd=L(_.exceptionAccessor,ee.reduceAdd,ne),ee.reduceRemove=I(_.exceptionAccessor,ee.reduceRemove,ne),ee.reduceInitial=b(ee.reduceInitial,ne)),_.histogramValue&&_.histogramThresholds&&(ee.reduceAdd=T(_.histogramValue,ee.reduceAdd,ne),ee.reduceRemove=E(_.histogramValue,ee.reduceRemove,ne),ee.reduceInitial=k(_.histogramThresholds,ee.reduceInitial,ne)),_.sumOfSquares&&(ee.reduceAdd=j(_.sumOfSquares,ee.reduceAdd,ne),ee.reduceRemove=M(_.sumOfSquares,ee.reduceRemove,ne),ee.reduceInitial=N(ee.reduceInitial,ne)),_.std&&(_.sumOfSquares&&_.sum?(ee.reduceAdd=U(ee.reduceAdd,ne),ee.reduceRemove=Y(ee.reduceRemove,ne),ee.reduceInitial=B(ee.reduceInitial,ne)):console.error("You must set .sumOfSq(accessor) and define a .sum(accessor) to use .std(true). Or use .std(accessor).")),_.custom&&(ee.reduceAdd=W(ee.reduceAdd,ne,_.custom.add),ee.reduceRemove=Z(ee.reduceRemove,ne,_.custom.remove),ee.reduceInitial=$(ee.reduceInitial,ne,_.custom.initial)),_.nestKeys&&(ee.reduceAdd=G(_.nestKeys,ee.reduceAdd,ne),ee.reduceRemove=X(_.nestKeys,ee.reduceRemove,ne),ee.reduceInitial=z(ee.reduceInitial,ne)),_.aliasKeys&&(ee.reduceInitial=D(ee.reduceInitial,ne,_.aliasKeys)),_.aliasPropKeys&&(ee.reduceAdd=F(_.aliasPropKeys,ee.reduceAdd,ne),ee.reduceRemove=F(_.aliasPropKeys,ee.reduceRemove,ne)),_.filter&&(ee.reduceAdd=n(_.filter,ee.reduceAdd,te.reduceAdd,ne),ee.reduceRemove=t(_.filter,ee.reduceRemove,te.reduceRemove,ne)),_.values&&Object.getOwnPropertyNames(_.values).forEach((function(n){var t;ee.reduceInitial=(t=ee.reduceInitial,function(e){return e=t(e),ne(e)[n]={},e}),e(_.values[n].parameters,ee,(function(e){return e[n]}))}))}};function ee(e){if(null==e)throw new TypeError("Cannot convert undefined or null to object");for(var n=Object(e),t=1;t<arguments.length;++t){var u=arguments[t];if(null!=u)for(var r in u)u.hasOwnProperty(r)&&(n[r]=u[r])}return n}var ne={build:function e(n,t){function u(e){if("string"==typeof e){var n=e;return function(e){return e[n]}}return e}function r(e){if("string"==typeof e){var n=e;return function(e){return+e[n]}}return e}n.fromObject=function(e){return arguments.length?(ee(t,e),n):t},n.toObject=function(){return t},n.count=function(e,u){return arguments.length?(u||(u="count"),t.count=u,n):t.count},n.sum=function(e){return arguments.length?(e=r(e),t.sum=e,n):t.sum},n.avg=function(e){return arguments.length?("function"==typeof(e=r(e))?(t.sum&&t.sum!==e&&console.warn("SUM aggregation is being overwritten by AVG aggregation"),t.sum=e,t.avg=!0,t.count="count"):t.avg=e,n):t.avg},n.exception=function(e){return arguments.length?(e=u(e),t.exceptionAccessor=e,n):t.exceptionAccessor},n.filter=function(e){return arguments.length?(t.filter=e,n):t.filter},n.valueList=function(e){return arguments.length?(e=u(e),t.valueList=e,n):t.valueList},n.median=function(e){return arguments.length?("function"==typeof(e=r(e))&&(t.valueList&&t.valueList!==e&&console.warn("VALUELIST accessor is being overwritten by median aggregation"),t.valueList=e),t.median=e,n):t.median},n.min=function(e){return arguments.length?("function"==typeof(e=r(e))&&(t.valueList&&t.valueList!==e&&console.warn("VALUELIST accessor is being overwritten by min aggregation"),t.valueList=e),t.min=e,n):t.min},n.max=function(e){return arguments.length?("function"==typeof(e=r(e))&&(t.valueList&&t.valueList!==e&&console.warn("VALUELIST accessor is being overwritten by max aggregation"),t.valueList=e),t.max=e,n):t.max},n.exceptionCount=function(e){return arguments.length?("function"==typeof(e=u(e))?(t.exceptionAccessor&&t.exceptionAccessor!==e&&console.warn("EXCEPTION accessor is being overwritten by exception count aggregation"),t.exceptionAccessor=e,t.exceptionCount=!0):t.exceptionCount=e,n):t.exceptionCount},n.exceptionSum=function(e){return arguments.length?(e=r(e),t.exceptionSum=e,n):t.exceptionSum},n.histogramValue=function(e){return arguments.length?(e=r(e),t.histogramValue=e,n):t.histogramValue},n.histogramBins=function(e){return arguments.length?(t.histogramThresholds=e,n):t.histogramThresholds},n.std=function(e){return arguments.length?("function"==typeof(e=r(e))?(t.sumOfSquares=e,t.sum=e,t.count="count",t.std=!0):t.std=e,n):t.std},n.sumOfSq=function(e){return arguments.length?(e=r(e),t.sumOfSquares=e,n):t.sumOfSquares},n.value=function(n,u){if(arguments.length&&"string"==typeof n)return t.values||(t.values={}),t.values[n]={},t.values[n].parameters={order:!1,avg:!1,count:!1,sum:!1,exceptionAccessor:!1,exceptionCount:!1,exceptionSum:!1,filter:!1,valueList:!1,median:!1,histogramValue:!1,min:!1,max:!1,histogramThresholds:!1,std:!1,sumOfSquares:!1,values:!1,nestKeys:!1,aliasKeys:!1,aliasPropKeys:!1,groupAll:!1,dataList:!1,custom:!1},e(t.values[n],t.values[n].parameters),u&&(t.values[n].accessor=u),t.values[n];console.error("'value' requires a string argument.")},n.nest=function(e){return arguments.length?(e.map(u),t.nestKeys=e,n):t.nestKeys},n.alias=function(e){return arguments.length?(t.aliasKeys=e,n):t.aliasKeys},n.aliasProp=function(e){return arguments.length?(t.aliasPropKeys=e,n):t.aliasPropKeys},n.groupAll=function(e){return arguments.length?(t.groupAll=e,n):t.groupAll},n.dataList=function(e){return arguments.length?(t.dataList=e,n):t.dataList},n.custom=function(e){return arguments.length?(t.custom=e,n):t.custom}}};var te=function(e){return function(n){return n[e]}};const ue=function(e,n){return e||(e=function(e){return e}),function(t,u){n&&n(t,u);var r=e(t),i=e(u);return void 0!==i.count&&(r.count+=i.count),void 0!==i.sum&&(r.sum+=i.sum),void 0!==i.avg&&(r.avg=r.sum/r.count),t}},re=function(e,n,t){n.reduceInitial();var u=t.values?Object.keys(t.values):[],r=ue();if(u.length)for(var i=0;i<u.length;++i)r=ue(te(u[i]),r);return function(t,u){if(!arguments.length)return e();if(t===1/0||!t)return e();var i=e(),o=t-1;if(i.length<=t)return i;var c=i.slice(0,o),s={key:u||"Others"};s.value=n.reduceInitial();for(var a=o;a<i.length;++a)r(s.value,i[a].value);return c.push(s),c}};var ie=function(e){if("function"==typeof e)return e;if(~e.indexOf(".")){var n=e.split(".");return function(e){return n.reduce((function(e,n){return e[n]}),e)}}return function(n){return n[e]}};function oe(e,n){return e<n?-1:e>n?1:e>=n?0:NaN}var ce=function(e,n){return function(t,u){return n(e(t),e(u))}};function se(e){return function(n,t){return 1===arguments.length&&(t=oe),e().sort(ce(ie(n),t))}}function ae(){var n={order:!1,avg:!1,count:!1,sum:!1,exceptionAccessor:!1,exceptionCount:!1,exceptionSum:!1,filter:!1,valueList:!1,median:!1,histogramValue:!1,min:!1,max:!1,histogramThresholds:!1,std:!1,sumOfSquares:!1,values:!1,nestKeys:!1,aliasKeys:!1,aliasPropKeys:!1,groupAll:!1,dataList:!1,custom:!1},t={};function u(u){if(t={reduceAdd:function(e){return e},reduceRemove:function(e){return e},reduceInitial:function(){return{}}},_.build(n,t),n.groupAll)if(u.top)console.warn("'groupAll' is defined but attempting to run on a standard dimension.group(). Must run on dimension.groupAll().");else{var r,i,o,c,s,a=e.bisect.by((function(e){return e.key})).left;u.reduce((function(e,u,l){for(o=n.groupAll(u),c=o.length,i=0;i<c;i++)s=o[i],e[r=a(e,s,0,e.length)]&&e[r].key===s||e.splice(r,0,{key:s,value:t.reduceInitial()}),t.reduceAdd(e[r].value,u,l);return e}),(function(e,u,s){for(o=n.groupAll(u),c=o.length,i=0;i<c;i++)r=a(e,o[i],0,e.length),t.reduceRemove(e[r].value,u,s);return e}),(function(){return[]})),u.all||(u.all=function(){return this.value()})}else u.reduce(t.reduceAdd,t.reduceRemove,t.reduceInitial);return le(u,n,t),u}return ne.build(u,n),u}!function(e){e.postprocessors={},e.registerPostProcessor=function(n,t){e.postprocessors[n]=t},e.registerPostProcessor("cap",re),e.registerPostProcessor("sortBy",se)}(ae);const le=function(e){return function(n,t,u){n.post=function(){var r=function(){return r.all()};r.all=function(){return n.all()};var i=e.postprocessors;return Object.keys(i).forEach((function(e){r[e]=function(){var n=r.all,o=[].slice.call(arguments);return r.all=function(){return i[e](n,u,t).apply(null,o)},r}})),r}}}(ae);return ae}));