Skip to content

Commit

Permalink
fixed bug: display fails if author's name has a dot
Browse files Browse the repository at this point in the history
  • Loading branch information
stv-beep committed Sep 12, 2022
1 parent 6a4edcb commit 7ed4853
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# F1 quotes api 0.4.4
# F1 quotes api

<img alt="Version" src="https://img.shields.io/badge/version-0.4.5-green.svg" />

<img src="https://img.shields.io/badge/npm-%3E%3D8.5.0-blue.svg" />

<img src="https://img.shields.io/badge/node-%3E%3D16.14.2-blue.svg" />

##### An API showing several quotes said by famous Formula 1 drivers and Formula 1 personalities.

Expand All @@ -11,7 +17,7 @@



## Developer:
## Dev:

```npm install```

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "f1-quotes-api",
"version": "0.4.4",
"version": "0.4.5",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
45 changes: 30 additions & 15 deletions src/routes/driversQuotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,41 @@ axios.get(top10)
.then(response => {
const html = response.data
const $ = load(html)
quotes = [] //reloading array
quotes = [] //cleaning array
/* getting the quote div including the author, then slicing the quote and the author */
$(quoteContent).each(function () {
const lastDotIndex = $(this).text().lastIndexOf('.')
const quote = $(this).text().replace(/\n/g, '').slice(0, lastDotIndex-1)
const author = $(this).text().slice(lastDotIndex+1).replace(/\n/g, '')
const specialQuote = quote.slice(0, quote.lastIndexOf('.')+1) //quote with an image
quote.slice(-1) === '.' || quote.slice(-1) === '!' || quote.slice(-1) === '?' ?
quotes.push({
quote,
author: author
}) :
quotes.push({
quote: specialQuote,
author: author
})

if ($(this).text().slice(lastDotIndex-2, lastDotIndex) === 'Jr') {//if there's a '.' in author's name
const rawQuote = $(this).text().replace('Jr.', 'Jr')
const lastDotIndex = rawQuote.lastIndexOf('.')
const quote = rawQuote.replace(/\n/g, '').slice(0, lastDotIndex-1)
const author = rawQuote.slice(lastDotIndex+1).replace(/\n/g, '')
const specialQuote = quote.slice(1, quote.lastIndexOf('.')+1) //quote with an image
quotes.push({
quote: specialQuote,
author: author
})
} else {
const quote = $(this).text().replace(/\n/g, '').slice(0, lastDotIndex-1)
const author = $(this).text().slice(lastDotIndex+1).replace(/\n/g, '')
const specialQuote = quote.slice(1, quote.lastIndexOf('.')+1) //quote with an image
quote.slice(-1) === '.' || quote.slice(-1) === '!' || quote.slice(-1) === '?' ?
quotes.push({
quote,
author: author
}) :
quotes.push({
quote: specialQuote,
author: author
})
}
})
}).catch(err => console.log(err))
router.get('/', (req, res) => { //quotes
res.status(200) ? res.send(quotes) : res.json(quotesErrorMessage)
return (quotes != null || res.status(200))
? res.send(quotes)
: res.json(quotesErrorMessage)
})


Expand All @@ -61,7 +76,7 @@ router.get('/:driverId', (req, res) => { //quotes/:driverId
.then(response => {
const html = response.data
const $ = load(html)
specificQuotes = [] //reloading array
specificQuotes = [] //cleaning array
/* getting the quote div including the author, then slicing the quote and the author */
$(specificQuoteContent, html).each(function () {
let rawQuote = $(this).text().replace(/\n/g, '')
Expand Down

0 comments on commit 7ed4853

Please sign in to comment.