UNPKG

reductio

Version:

Reductio: Crossfilter groupings

58 lines (49 loc) 1.29 kB
import crossfilter from 'crossfilter2'; var nest = { add: function (keyAccessors, prior, path) { var i; // Current key accessor var arrRef; var newRef; return function (p, v, nf) { if(prior) prior(p, v, nf); arrRef = path(p).nest; keyAccessors.forEach(function(a) { newRef = arrRef.filter(function(d) { return d.key === a(v); })[0]; if(newRef) { // There is another level. arrRef = newRef.values; } else { // Next level doesn't yet exist so we create it. newRef = []; arrRef.push({ key: a(v), values: newRef }); arrRef = newRef; } }); arrRef.push(v); return p; }; }, remove: function (keyAccessors, prior, path) { var arrRef; return function (p, v, nf) { if(prior) prior(p, v, nf); arrRef = path(p).nest; keyAccessors.forEach(function(a) { arrRef = arrRef.filter(function(d) { return d.key === a(v); })[0].values; }); // Array contains an actual reference to the row, so just splice it out. arrRef.splice(arrRef.indexOf(v), 1); // If the leaf now has length 0 and it's not the base array remove it. // TODO return p; }; }, initial: function (prior, path) { return function (p) { p = prior(p); path(p).nest = []; return p; }; } }; export default nest;