Skip to content

Commit

Permalink
fixed bug: display fails if author's name has a dot v2
Browse files Browse the repository at this point in the history
  • Loading branch information
stv-beep committed Sep 12, 2022
1 parent b021b16 commit 33f0df2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<h1 align="center">F1 quotes api 🏎</h1>


<h1 align="center">F1 quotes api 🏎</h1>

<p>
<img alt="Version" src="https://img.shields.io/badge/version-0.4.5-green.svg"/>
<img alt="Version" src="https://img.shields.io/badge/version-0.4.6-cyan.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"/>
</p>
Expand Down
2 changes: 1 addition & 1 deletion 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.5",
"version": "0.4.6",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
46 changes: 27 additions & 19 deletions src/routes/driversQuotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,38 @@ router.get('/:driverId', (req, res) => { //quotes/:driverId
const $ = load(html)
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, '')
$(specificQuoteContent).each(function () {
let lastDotIndex = 0
let rawQuote = $(this).text().replace(/\n/g, '')
let author = ''
let quote = ''
if (rawQuote.includes('?')) {
lastDotIndex = $(this).text().lastIndexOf('?')
lastDotIndex = rawQuote.lastIndexOf('?')
} else if (rawQuote.includes('!')) {
lastDotIndex = $(this).text().lastIndexOf('!')
lastDotIndex = rawQuote.lastIndexOf('!')
} else {
lastDotIndex = $(this).text().lastIndexOf('.')
lastDotIndex = rawQuote.lastIndexOf('.')
if (rawQuote.slice(rawQuote.length-3, rawQuote.length) === 'Jr.') {//if there's a '.' in author's name
rawQuote = rawQuote.replace('Jr.', 'Jr')
author = rawQuote.slice(rawQuote.lastIndexOf('.')+1, rawQuote.length)
rawQuote.slice(0,1) === ' ' ? quote = rawQuote.slice(1, rawQuote.length-author.length) :
quote = rawQuote.slice(0, rawQuote.lastIndexOf('.')+1)

specificQuotes.push({
quote: quote,
author: author
})
} else {
author = rawQuote.slice(rawQuote.lastIndexOf('.')+1, rawQuote.length)
rawQuote.slice(0,1) === ' ' ? quote = rawQuote.slice(1, rawQuote.length-author.length) :
quote = rawQuote.slice(0, rawQuote.lastIndexOf('.')+1)

specificQuotes.push({
quote: quote,
author: author
})
}
}
let quote = rawQuote.slice(0, lastDotIndex-2)
const author = $(this).text().slice(lastDotIndex+1).replace(/\n/g, '')
if (!author.includes('.')) {
const specialQuote = quote.slice(0, quote.lastIndexOf('.')+1) //quote with an image
quote.slice(-1) === '.' || quote.slice(-1) === '!' || quote.slice(-1) === '?' ?
specificQuotes.push({
quote,
author: author
}) :
specificQuotes.push({
quote: specialQuote,
author: author
})
}
})
res.json(specificQuotes)
}).catch(err => console.log(err))
Expand Down

0 comments on commit 33f0df2

Please sign in to comment.