All files / app/selectors search.ts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 6112x 9x     12x                     12x           12x             12x   12x       12x                                                    
export const hasSearch = searchText => {
  return searchText && searchText.toString().trim() !== '';
};
 
export const matchesSearch = (testText, searchText) => {
  if (!hasSearch(searchText)) {
    return true;
  }
  if (!testText) {
    return false;
  }
  const realSearchText = searchText.toString().toLowerCase();
  return testText.toLowerCase().includes(realSearchText);
};
 
export const matchesAuthorSearch = (testText, searchText) => {
  return matchesSearch(
    testText,
    searchText.replace(/authors?\s*[\:\=]\s*/, '')
  );
};
export const matchesCommitSearch = (testText, searchText) => {
  return matchesSearch(
    testText,
    searchText.replace(/commits?\s*[\:\=]\s*/, '')
  );
};
 
export const fileSearchRegex = /files?\s*[\:\=]\s*/;
 
export const matchesFileSearch = (testText, searchText) => {
  return matchesSearch(testText, searchText.replace(fileSearchRegex, ''));
};
 
export const commitsMatchSearch = (commit, searchText) => {
  if (!hasSearch(searchText)) {
    return true;
  }
  const commits = !Array.isArray(commit) ? [commit] : commit;
  for (const commit of commits) {
    let matchesFileName = false;
    for (const file of commit.files) {
      matchesFileName = matchesFileSearch(file.name, searchText);
      if (matchesFileName) {
        break;
      }
    }
    if (
      matchesFileName ||
      matchesCommitSearch(commit.message, searchText) ||
      matchesCommitSearch(commit.id, searchText) ||
      matchesCommitSearch(commit.body, searchText) ||
      matchesAuthorSearch(commit.authorName, searchText) ||
      matchesAuthorSearch(commit.authorEmail, searchText)
    ) {
      return true;
    }
  }
  return false;
};