| 578 | * any of the whitespace separated terms must be contained in any column |
| 579 | */ |
| 580 | _search(source) { |
| 581 | let searchTextArray; |
| 582 | if (this.multiColumnSearch || !this.strictSearch) { |
| 583 | // ignore leading and trailing whitespaces |
| 584 | searchTextArray = this.searchText.trim().toLowerCase().split(/\s+/); |
| 585 | } else { |
| 586 | searchTextArray = [ this.searchText.toLowerCase() ]; |
| 587 | } |
| 588 | const searchTermCount = searchTextArray.length; |
| 589 | const multipleTerms = searchTermCount > 1; |
| 590 | const nonStrictMultiCol = multipleTerms && !this.strictSearch && this.multiColumnSearch; |
| 591 | const nonStrictSingleCol = multipleTerms && !this.strictSearch && !this.multiColumnSearch; |
| 592 | this.filteredData = source.filter((row, r) => { |
| 593 | const keys = Object.keys(row); |
| 594 | // only clone array if necessary |
| 595 | let searchTerms = multipleTerms ? searchTextArray.slice() : searchTextArray; |
| 596 | // for loops are ugly, but performance matters here. |
| 597 | // And you cant break from a forEach. |
| 598 | // http://jsperf.com/for-vs-foreach/66 |
| 599 | for (let i = 0, keysLength = keys.length; i < keysLength; i++) { |
| 600 | const key = keys[i]; |
| 601 | const colInfo = this.colInfos[key]; |
| 602 | if (colInfo && colInfo.searchable) { |
| 603 | const { |
| 604 | format, |
| 605 | filterFormatted, |
| 606 | filterValue, |
| 607 | formatExtraData |
| 608 | } = colInfo; |
| 609 | let targetVal; |
| 610 | if (filterFormatted && format) { |
| 611 | targetVal = format(row[key], row, formatExtraData, r); |
| 612 | } else if (filterValue) { |
| 613 | targetVal = filterValue(row[key], row); |
| 614 | } else { |
| 615 | targetVal = row[key]; |
| 616 | } |
| 617 | if (targetVal !== null && typeof targetVal !== 'undefined') { |
| 618 | targetVal = targetVal.toString().toLowerCase(); |
| 619 | if (nonStrictSingleCol && searchTermCount > searchTerms.length) { |
| 620 | // reset search terms for single column search |
| 621 | searchTerms = searchTextArray.slice(); |
| 622 | } |
| 623 | for (let j = searchTerms.length - 1; j > -1; j--) { |
| 624 | if (targetVal.indexOf(searchTerms[j]) !== -1) { |
| 625 | if (nonStrictMultiCol || searchTerms.length === 1) { |
| 626 | // match found: the last or only one |
| 627 | return true; |
| 628 | } |
| 629 | // match found: but there are more search terms to check for |
| 630 | searchTerms.splice(j, 1); |
| 631 | } else if (!this.multiColumnSearch) { |
| 632 | // one of the search terms was not found in this column |
| 633 | break; |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | } |