-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Reed Nelson
committed
Nov 16, 2023
1 parent
726ee93
commit a832177
Showing
3 changed files
with
38 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[ | ||
"This is the first quote.", | ||
"This is the second quote.", | ||
"This is the third quote." | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// src/routes/index.ts | ||
|
||
import fs from 'fs/promises'; | ||
import path from 'path'; | ||
|
||
const getRandomQuote = (quotes: string[]): string => { | ||
const randomIndex = Math.floor(Math.random() * quotes.length); | ||
return quotes[randomIndex]; | ||
}; | ||
|
||
// const getWeeklyQuote = (quotes: string[]): string => { | ||
// const today = new Date(); | ||
// const startOfYear = new Date(today.getFullYear(), 0, 1); | ||
// const weekNumber = Math.ceil((today - startOfYear) / (7 * 24 * 60 * 60 * 1000)); | ||
// return quotes[weekNumber % quotes.length]; | ||
// }; | ||
|
||
const getQuote = async (): Promise<string> => { | ||
try { | ||
const jsonFilePath = path.resolve('src/assets/quotes.json'); | ||
const jsonData = await fs.readFile(jsonFilePath, 'utf-8'); | ||
const quotes = JSON.parse(jsonData); | ||
return getRandomQuote(quotes); | ||
} catch (error) { | ||
throw new Error(`Error reading or processing the JSON file: ${error}`); | ||
} | ||
}; | ||
|
||
export default getQuote; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters