data-filter
Version:
Data engine is small data management lib for some sort and filter.
312 lines (261 loc) • 8.49 kB
JavaScript
/** @license Data-Engine v3.1.1
* data-filter.development.js
*
* Copyright Jan Silhan
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
if (process.env.NODE_ENV !== "production") {
(function() {
'use strict';
var FilterValue = require('filter-value');
var Sort = require('data-sort');
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Filter engine
*
* @class Filter
*/
var Filter = function () {
/**
* Creates an instance of Filter.
* @param {any} data - initial data
*
* @memberOf Filter
*/
function Filter() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
data = _ref.data,
sortEngine = _ref.sortEngine;
_classCallCheck(this, Filter);
_initialiseProps.call(this);
this.filters = {};
this.SortEngine = sortEngine;
this.Data = data;
this.filtered = this.getData();
}
/**
* Setter for data
*
* @param {Array} data - original data
* @memberOf Filter
*/
_createClass(Filter, [{
key: 'Data',
set: function (data) {
if (data && Array.isArray(data)) {
this.data = data;
this.updateFilter();
return;
}
this.data = [];
this.filtered = [];
}
/**
* Setter for data
* @return {Array<any>} original data
* @memberOf Filter
*/
,
get: function () {
return this.getData();
}
/**
* Helper function for creating new filterValue
* @private
* @param {string} name - name of collumn
* @param {any} value - value of filter
* @param {string} type - static type for value
* @returns {FilterValue} new filter value
*/
/**
* Simple add filter function
* @param {string} name - name of collumn
* @param {any} value - value of filter
* @param {string} type - static type for value
* @return {Array<any>} filtered array
*/
/**
* Add or modify filter value
*
*
* @param {Array<FilterValue>} items - array of filter items
* @throws {TypeError} when item isn't instance of FilterValue
* @return {Array} new filtered array
* @memberOf Filter
*/
/**
* Remove one or as many filters as you add value
* only updating when at least one filter was removed
*
* @param {Array<string | FilterValue>} item - filter item
*
* @return {Array<any>} new filtered array
* @memberOf Filter
*/
/**
* Clears all filters,
* it's possible that sort is still active so we need to filter anyway
*
* @return {Array<any>} new filtered array
* @memberOf Filter
*/
/**
* Update filtered array.
*
* @return {Array} new filtered array
* @memberOf Filter
*/
/**
* Filter line by all criteria.
* Helper function for filtering.
* @private
* @param {object} line - line from original data.
* @return {bool}
* @memberOf Filter
*/
/**
* Simple getter
*
* @returns {array} filtered data
*
* @memberOf Filter
*/
}, {
key: 'FilteredData',
get: function () {
return this.getFilteredData();
}
/**
* Helper function when sort is not in filter
* filtering without sort
* @private
* @return {Array<any>} filtered data
*/
/**
* Helper function with sort
* Filtering with sort
* @private
* @return {Array<any>} filtered and sorted array
*/
/**
* Getter for filter
* @param {string} name - name of filter
* @returns {FilterValue | null} return filter value
*/
}, {
key: 'SortEngine',
/**
* Setter for sort engine
* @param {Sort} sortEngine - instance of Sort
*/
set: function (sortEngine) {
if (sortEngine instanceof Sort) {
this.sortEngine = sortEngine;
this.updateFce = this.filterWSort;
} else {
this.sortEngine = null;
this.updateFce = this.filterWOSort;
}
}
/**
* Getter for sort engine
* @returns {Sort} - instance of Sort
*/
,
get: function () {
return this.sortEngine;
}
}]);
return Filter;
}();
Filter.FilterValue = FilterValue;
Filter.Sort = Sort;
var _initialiseProps = function () {
var _this = this;
this.updateFce = this.filterWOSort;
this.data = [];
this.filtered = [];
this.sortEngine = null;
this.setData = function (data) {
_this.Data = data;
};
this.getData = function () {
return _this.data;
};
this.createFilter = function (name, value, type) {
return new FilterValue(name, value, type);
};
this.addFilter = function (name, value, type) {
return _this.update(_this.createFilter(name, value, type));
};
this.update = function () {
for (var _len = arguments.length, items = Array(_len), _key = 0; _key < _len; _key++) {
items[_key] = arguments[_key];
}
var returnFunc = _this.getFilteredData;
items.forEach(function (item) {
// Exception when item isn't filterValue!
if (!(item instanceof FilterValue)) {
throw new TypeError(item + ' has to have filterValue instance');
}
_this.filters[item.Name] = item;
returnFunc = _this.updateFilter;
});
return returnFunc();
};
this.removeFilters = function () {
for (var _len2 = arguments.length, names = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
names[_key2] = arguments[_key2];
}
var returnFunc = _this.getFilteredData;
names.forEach(function (item) {
var removalName = typeof item === 'string' ? item : item.Name;
if (_this.filters[removalName]) {
delete _this.filters[removalName];
returnFunc = _this.updateFilter;
}
});
return returnFunc();
};
this.clearFilters = function () {
_this.filters = {};
_this.updateFilter();
return _this.FilteredData;
};
this.updateFilter = function () {
_this.filtered = _this.updateFce(_this.data.filter(_this.filterAll));
return _this.FilteredData;
};
this.filterAll = function (line) {
return Object.keys(_this.filters).every(function (key) {
return _this.filters[key].compare(line[key]);
});
};
this.getFilteredData = function () {
return _this.filtered;
};
this.filterWOSort = function (data) {
return data;
};
this.filterWSort = function (data) {
return _this.SortEngine.setData(data);
};
this.getFilter = function (name) {
if (_this.filters[name]) {
return _this.filters[name];
}
return null;
};
};
var dataFilter = Object.freeze({
default: Filter
});
var Filter$2 = ( dataFilter && Filter ) || dataFilter;
var dataFilter$1 = Filter$2['default'] ? Filter$2['default'] : Filter$2;
module.exports = dataFilter$1;
})();
}