INNER CODE UNIT · TypeScript
randomQuote
PiyushSuthar/github-readme-quotes · src/fetcher/fetch-quotes.ts:21
const randomQuote = (data: ParseDataParams[]): ParseDataParams => {
// Filter out invalid entries.
const validQuotes = data.filter(quote => quote && typeof quote.text === 'string');
// If no valid quotes exist, throw an error.
if (validQuotes.length === 0) {
throw new Error('No valid quotes found in the data.');
}
// Get a random quote.
let randQuote = validQuotes[Math.floor(Math.random() * validQuotes.length)];
// Check length and recurse if necessary.
return randQuote.text.length < 220 ? randQuote : randomQuote(validQuotes);
};
export async function fetchQuotes(): Promise<ParseDataReturn> {
const response = await axios.get(