local-quotes icon indicating copy to clipboard operation
local-quotes copied to clipboard

[FR] Quote Author wildcard

Open sundevista opened this issue 10 months ago • 0 comments

Discussed in https://github.com/sundevista/local-quotes/discussions/32

Originally posted by BigGHS April 21, 2024 I have some of my quote aithors include a description, for example"Heraclitus of Ephesus, c. 535 BCE – 475 BCE". The "*" search option will pick up this author but to specifically search for Heraclitus quotes, I have to typi in the complete name. To address this I modified the following 2 functions in main.js to recognise the "!" as a wildcard, e.g. "!heracl" will pick up quotes by Heraclitus, as will "!535 BCE". Here is the revised code:

function getValidAuthorsFromAdvancedSearch(quoteVault, search) {
  const terms = search.split("||").map(term => term.trim());
  let validAuthors = [];

  if (terms.includes("*")) {
    return quoteVault.map(a => a.author); // Return all authors if "*" is present
  }

  terms.forEach(term => {
    if (term.startsWith("!") && term.length > 1) {
      const substring = term.slice(1).toLowerCase();
      validAuthors = validAuthors.concat(
        quoteVault.filter(a => a.author.toLowerCase().includes(substring)).map(a => a.author)
      );
    }
  });

  return Array.from(new Set(validAuthors)); // Remove duplicates and return
}

function searchQuote(quoteVault, search, useWeightedRandom) {
  const result = { author: null, text: null };
  if (search === "*") {
    result.author = useWeightedRandom ? getWeightedRandomAuthor(quoteVault) : getRandomAuthor(quoteVault);
  } else {
    const authorList = getValidAuthorsFromAdvancedSearch(quoteVault, search);
    if (authorList.length > 0) {
      result.author = useWeightedRandom ? getWeightedRandomAuthor(quoteVault, authorList) : getRandomArrayItem(authorList);
    }
  }
  result.text = result.author ? getRandomQuoteOfAuthor(quoteVault, result.author) : "No quote found";
  return result;
}
```</div>

sundevista avatar Apr 23 '24 07:04 sundevista