Skip to content

Commit

Permalink
refactor: use if one-liner where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
viliusddd committed Jun 5, 2024
1 parent bb8850e commit ebd3c01
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 38 deletions.
18 changes: 6 additions & 12 deletions src/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,25 @@ export default class Display {
/** Remove chart if it already exists */
resetChart() {
const chartStatus = Chart.getChart('chart') // eslint-disable-line
if (chartStatus != undefined) {
chartStatus.destroy()
}
if (chartStatus != undefined) chartStatus.destroy()
}

/**
* Restart or reset the app.
* If restart is triggered while timer isn't finished - mark as cancel.
* @param {boolean} reset - if the value is positive - change
* @param {boolean} isFullReset - if the value is positive - change
* the api url, otherwise leave the current one.
*/
restart(reset = false) {
if (this.timerElement.innerText !== '0s') {
restart(isFullReset = false) {
if (this.timerElement.innerText !== '0s')
this.appElement.classList.add('cancel')
}

this.resetDisplay()
this.resetChart()
this.resetStatsElements()

if (reset) {
this.create()
} else {
this.create(CURRENT_API_URL)
}
if (isFullReset) this.create()
else this.create(CURRENT_API_URL)
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ export default class Key {
* - if the letter is incorrect - append it to word end.
* Oherwise ignore it.
* - correct letter.
* @param {boolean} correct - true when match event.key.
* @param {boolean} isCorrect - true when match event.key.
*/
next(correct) {
const name = (correct === true) ? 'correct' : 'incorrect'
next(isCorrect) {
const name = (isCorrect === true) ? 'correct' : 'incorrect'

if (!this.letterNode) {
if (!correct) this.appendLetter();
if (!isCorrect) this.appendLetter();
} else {
this.letterNode.classList.add(name)
this.cursor.move(this.letterNode)
Expand Down
9 changes: 3 additions & 6 deletions src/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ export default class Stats {
const match = JSON.parse(sessionStorage.getItem('stats'))

let matches = retrieveLocalItem('matches')
if (matches) matches = matches.slice(-19)

if (!matches) {
matches = [match]
}
if (matches) matches = matches.slice(-19)
else matches = [match]

matches.push(match)

Expand Down Expand Up @@ -74,9 +72,8 @@ export default class Stats {
const corrWords = siblings.filter(sib => !sib.classList.contains('error'))

let accuracy = 0
if (keystrokes > 0) {
if (keystrokes > 0)
accuracy = toFixedWithoutZeros(keysCorrect / keystrokes * 100)
}

const stats = {
date: dateTimeNow(),
Expand Down
23 changes: 7 additions & 16 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ export function dateTimeNow() {
* completed touch-typing exercise.
*/
export function retrieveLocalItem(key) {
if (localStorage.getItem(key)) {
return JSON.parse(localStorage.getItem(key))
}
if (localStorage.getItem(key)) return JSON.parse(localStorage.getItem(key))
}

/**
Expand All @@ -54,23 +52,16 @@ export async function getApiJson(url, element) {
try {
response = await fetch(url)

if (!response.ok) {
throw response
}
if (!response.ok) throw response

const respJson = await response.json()

if (respJson.status == 404) {
throw respJson
} else {
return respJson
}
if (respJson.status == 404) throw respJson
else return respJson

} catch (err) {
if (err.status) {
element.innerText = `${err.status}: connection failed.`
} else if (err.name) {
element.innerText = `Fails with ${err.name}.`
}
if (err.status) element.innerText = `${err.status}: connection failed.`
else if (err.name) element.innerText = `Fails with ${err.name}.`

element.classList.add('network-error')

Expand Down

0 comments on commit ebd3c01

Please sign in to comment.