Skip to content

Commit

Permalink
Allow ignoring of dimension filters in crossfilter.allFiltered() (squ…
Browse files Browse the repository at this point in the history
  • Loading branch information
skidr0w authored and esjewett committed Mar 8, 2018
1 parent 6fa6921 commit d8971b4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/crossfilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,26 @@ function crossfilter() {
triggerOnChange('dataRemoved');
}

// Return true if the data element at index i is filtered IN.
// Optionally, ignore the filters of any dimensions in the ignore_dimensions list.
function isElementFiltered(i, ignore_dimensions) {
function maskForDimensions(dimensions) {
var n,
d,
id,
len,
id,
mask = Array(filters.subarrays);
for (n = 0; n < filters.subarrays; n++) { mask[n] = ~0; }
if (ignore_dimensions) {
for (d = 0, len = ignore_dimensions.length; d < len; d++) {
// The top bits of the ID are the subarray offset and the lower bits are the bit
// offset of the "one" mask.
id = ignore_dimensions[d].id();
mask[id >> 7] &= ~(0x1 << (id & 0x3f));
}
for (d = 0, len = dimensions.length; d < len; d++) {
// The top bits of the ID are the subarray offset and the lower bits are the bit
// offset of the "one" mask.
id = dimensions[d].id();
mask[id >> 7] &= ~(0x1 << (id & 0x3f));
}
return mask;
}

// Return true if the data element at index i is filtered IN.
// Optionally, ignore the filters of any dimensions in the ignore_dimensions list.
function isElementFiltered(i, ignore_dimensions) {
var mask = maskForDimensions(ignore_dimensions || []);
return filters.zeroExceptMask(i,mask);
}

Expand Down Expand Up @@ -1415,13 +1418,14 @@ function crossfilter() {
return data;
}

// Returns row data with all dimension filters applied
function allFiltered() {
// Returns row data with all dimension filters applied, except for filters in ignore_dimensions
function allFiltered(ignore_dimensions) {
var array = [],
i = 0;
i = 0,
mask = maskForDimensions(ignore_dimensions || []);

for (i = 0; i < n; i++) {
if (filters.zero(i)) {
if (filters.zeroExceptMask(i, mask)) {
array.push(data[i]);
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/crossfilter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,23 @@ suite.addBatch({
data.quantity.filterAll();
data.total.filterAll();
}
},
"is affected by all dimensions filters, except those in ignore_dimensions": function(data) {
try {
data.quantity.filterExact(2);
var raw = data.allFiltered([data.quantity]);
assert.equal(raw.length, 43);

data.total.filterRange([190, 300]);
raw = data.allFiltered([data.total]);
assert.equal(raw.length, 35);

raw = data.allFiltered([data.quantity, data.total]);
assert.equal(raw.length, 43);
} finally {
data.quantity.filterAll();
data.total.filterAll();
}
}
},

Expand Down

0 comments on commit d8971b4

Please sign in to comment.