js-filtered-datastore
About
A facade for an underlying datastore on which an arbitrary number of filters can be applied (transparently to the underlying datastore).
This is useful when the calculation of filters is time-consuming and needs to be done only once or when the backing datastore is not to be tampered with and you need another place to keep information on which indices are filtered.
The constructor expects two functions that interface with the underlying datastore:
getter
: take an integer returns a valuesizer
: takes no arguments, returns the size of the datastore
E.g. to use an array as the backing datastore:
const arr = ... // some array FilteredDataStore ds = arri arrlength ;
A helper facility to create a FilteredDataStore
out of an array is provided in the
factory method: FilteredDataStore.fromArray
.
The facade exposes the following methods:
ds; // returns the filter that corresponds to a given predicate function ds; // installs an array of filters ds; // clears any installed filters ds; // returns the size of the (filtered) datastore ds; // returns the i-th element of the datastore (subject to filtering) ds; // returns an array of the filtered values ds // returns the size of the backing datastore (without taking filtering into account)
Use
; ; const arr = _; const ds = fdsFilteredDataStore; ds; // 100 const mul3filter = ds; const mul5filter = ds; ds; ds; // 7 for let i = 0 ; i < ds ; i++ ; ds; ds; // 100 once again
Test
npm test
Release History
- 0.0.1 Initial release
- 0.0.2 fixed bug in allFiltered()
- 0.0.3 added getUnderlyingSize method
- 0.0.4 only conditionally include the Babel polyfill