UNPKG

@thiagoprz/fulltext-search-pipe

Version:

Angular pipe with full text search functionality

79 lines (73 loc) 2.17 kB
import { Pipe, Injectable, NgModule } from '@angular/core'; /** * FulltextSearchPipe * @author Thiago Przyczynski <przyczynski@gmail.com> */ class FulltextSearchPipe { constructor() { } /** * Transform method pipe * @param string value * @param string query * @param any field */ transform(value, query, field) { return query ? value.reduce((prev, next) => { if (typeof field == 'string') { query = query.toString(); if (next[field] && this.removeAccents(next[field].toLowerCase()).includes(this.removeAccents(query.toLowerCase()))) { prev.push(next); } } else { for (let i in field) { if (next[field[i]] && this.removeAccents(next[field[i]].toLowerCase()).includes(this.removeAccents(query.toLowerCase()))) { prev.push(next); break; } } } return prev; }, []) : value; } /** * Removing accents * @param string value */ removeAccents(value) { return value .replace(/á/g, 'a') .replace(/ã/g, 'a') .replace(/à/g, 'a') .replace(/é/g, 'e') .replace(/í/g, 'i') .replace(/ó/g, 'o') .replace(/ô/g, 'o') .replace(/õ/g, 'o') .replace(/ú/g, 'u'); } } FulltextSearchPipe.decorators = [ { type: Pipe, args: [{ name: 'FullTextSearch' },] }, { type: Injectable } ]; FulltextSearchPipe.ctorParameters = () => []; class FulltextSearchPipeModule { } FulltextSearchPipeModule.decorators = [ { type: NgModule, args: [{ declarations: [FulltextSearchPipe], imports: [], exports: [FulltextSearchPipe] },] } ]; /* * Public API Surface of fulltext-search-pipe */ /** * Generated bundle index. Do not edit. */ export { FulltextSearchPipe, FulltextSearchPipeModule }; //# sourceMappingURL=thiagoprz-fulltext-search-pipe.js.map