quotes-nextjs

My favourite quotes. (nextjs)
git clone http://git.hanabi.in/repos/quotes-nextjs.git
Log | Files | Refs | LICENSE

helperFns.js (3492B)


      1 const join = require("path").join;
      2 
      3 export const errHandler = (err) => {
      4   console.error(err);
      5 };
      6 
      7 export function getDetailsANDQuotes(allFiles) {
      8   const details = allFiles.filter((_) => _ == "data.md")[0];
      9   const quotes = allFiles.filter((_) => _ != "data.md");
     10   return { details, quotes };
     11 }
     12 
     13 export function getMinMax(arr) {
     14   let [max, min] = [-Infinity, Infinity];
     15   for (let { count } of arr) {
     16     if (count > max) max = count;
     17     if (count < min) min = count;
     18   }
     19   return [min, max];
     20 }
     21 
     22 export async function getCover(author) {
     23   const readFile = require("util").promisify(require("fs").readFile);
     24   const matter = require("gray-matter");
     25 
     26   try {
     27     const dataBuff = await readFile(join("quotes", author, "data.md"));
     28     const dataStr = dataBuff.toString();
     29     const cover = matter(dataStr).data.cover;
     30     return cover;
     31   } catch (err) {
     32     errHandler(err);
     33   }
     34 }
     35 
     36 function swap(arr, i, j) {
     37   let temp = arr[i];
     38   arr[i] = arr[j];
     39   arr[j] = temp;
     40 }
     41 
     42 export function shuffleAndGiveRandom(arr) {
     43   for (let i = arr.length - 1; i >= 1; i--) {
     44     const j = Math.floor(Math.random() * i);
     45     swap(arr, i, j);
     46   }
     47   for (let i = arr.length - 1; i >= 1; i--) {
     48     const j = Math.floor(Math.random() * i);
     49     swap(arr, i, j);
     50   }
     51   const idx = Math.floor(arr.length * Math.random());
     52   return arr[idx];
     53 }
     54 
     55 export async function readFrontMatter(author, quote) {
     56   const readFile = require("util").promisify(require("fs").readFile);
     57   const matter = require("gray-matter");
     58 
     59   try {
     60     const fileBuffer = await readFile(join("quotes", author, quote));
     61     const fileStr = fileBuffer.toString();
     62     const frontMatter = matter(fileStr);
     63     return frontMatter;
     64   } catch (err) {
     65     errHandler(err);
     66   }
     67 }
     68 
     69 export function makeUri(quote) {
     70   return quote.replace(/\.md$/, "");
     71 }
     72 
     73 export function createQuoteObj(author, data, quote) {
     74   const { attributed, date, misattributed, title, unverified } = data;
     75   const uri = makeUri(quote);
     76   return {
     77     attributed,
     78     author,
     79     date,
     80     misattributed,
     81     title,
     82     unverified,
     83     uri,
     84   };
     85 }
     86 
     87 function extractInfoFrom(router) {
     88   const { q } = router.query;
     89   if(!q) router.replace("/400");
     90   let [and, or] = [false, false];
     91   if (q.includes(",")) and = true;
     92   if (q.includes("|")) or = true;
     93   if (and == or) router.replace("/400");
     94   const tags = !!and ? q.split(",") : q.split("|");
     95   return { and, or, tags };
     96 }
     97 
     98 function getAndQuotes(tags) {
     99   const quotes = require("./random-quotes.js");
    100   const filteredQuoted = [];
    101   for (let i = 0; i < quotes.length; i++) {
    102     const quote = quotes[i];
    103     let reject = false;
    104     for (let j = 0; j < tags.length; j++) {
    105       const tag = tags[j];
    106       if (!quote.tags.includes(tag)) {
    107         reject = true;
    108         break;
    109       }
    110     }
    111     if (!reject) filteredQuoted.push(quote);
    112   }
    113   return filteredQuoted;
    114 }
    115 
    116 function getOrQuotes(tags) {
    117   const quotes = require("./random-quotes.js");
    118   const filteredQuoted = [];
    119   for (let i = 0; i < quotes.length; i++) {
    120     const quote = quotes[i];
    121     let accept = false;
    122     for (let j = 0; j < tags.length; j++) {
    123       const tag = tags[j];
    124       if (quote.tags.includes(tag)) {
    125         accept = true;
    126         break;
    127       }
    128     }
    129     if (accept) filteredQuoted.push(quote);
    130   }
    131   return filteredQuoted;
    132 }
    133 
    134 export function chooseQuotes(router) {
    135   const { and, tags } = extractInfoFrom(router);
    136   const quotes = !!and ? getAndQuotes(tags) : getOrQuotes(tags);
    137   if (quotes.length == 0) router.replace("/404");
    138   return quotes;
    139 }