w-ng5
Version:
Angular Generic Filters to *ngFor
100 lines (97 loc) • 3.27 kB
JavaScript
import { Pipe, NgModule } from '@angular/core';
var FilterPipe = /** @class */ (function () {
function FilterPipe() {
}
FilterPipe.prototype.transform = function (values, filter) {
if (filter instanceof Array) {
return this.checkComplexType(values, filter);
}
else {
return this.checkSimpleType(values, filter);
}
};
FilterPipe.prototype.checkSimpleType = function (values, filter) {
if (!values || !filter) {
return values;
}
return values.filter(function (item) { return item.toString().toLowerCase().indexOf(filter.toString().toLowerCase()) !== -1; });
};
FilterPipe.prototype.checkComplexType = function (values, filter) {
var _this = this;
if (!values || !filter || filter.length === 0) {
return values;
}
var result = [];
values.forEach(function (row) {
var match = false;
filter.forEach(function (field) {
match = match || _this.checkValue(row, field);
});
if (match) {
result.push(row);
}
});
return result;
};
FilterPipe.prototype.checkValue = function (item, filter) {
if (!filter || !filter.field || !filter.value || filter.value === '') {
return true;
}
if (this.existDot(filter.field)) {
return this.parseValue(item, filter.value, filter.field);
}
else {
if (item[filter.field]) {
return item[filter.field].toString().toLowerCase().indexOf(filter.value.toString().toLowerCase()) !== -1;
}
else {
return false;
}
}
};
FilterPipe.prototype.parseValue = function (reference, search, filter) {
var fields = filter.split('.');
return this.existFieldValue(reference, search, fields, 0);
};
FilterPipe.prototype.existDot = function (path) {
return path.indexOf('.') > -1;
};
FilterPipe.prototype.existFieldValue = function (obj, search, fieldFind, indexFind) {
var ref = obj[fieldFind[indexFind]];
if (indexFind === fieldFind.length - 1) {
if (ref) {
return ref.toString().toLowerCase().indexOf(search.toString().toLowerCase()) !== -1;
}
else {
return false;
}
}
else if (ref) {
return this.existFieldValue(ref, search, fieldFind, ++indexFind);
}
else {
return false;
}
};
return FilterPipe;
}());
FilterPipe.decorators = [
{ type: Pipe, args: [{
name: 'filter',
pure: true
},] },
];
var PipesModule = /** @class */ (function () {
function PipesModule() {
}
return PipesModule;
}());
PipesModule.decorators = [
{ type: NgModule, args: [{
declarations: [FilterPipe],
exports: [FilterPipe],
providers: [FilterPipe]
},] },
];
export { FilterPipe, PipesModule };
//# sourceMappingURL=w-ng5.js.map