Skip to content

Commit

Permalink
refactor(intg): specialize expressionToDisplay
Browse files Browse the repository at this point in the history
This bases on the `evaluation` used
  • Loading branch information
lemredd committed Dec 27, 2022
1 parent 522f415 commit 665fc77
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/components/CalculatorContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ function solvePercentage(base: number, percent: number) {
return percentageResult
}
function passToRightEntry(newRightEntry: number) {
rightEntry.value = newRightEntry
}
const isEntryValueEmpty = computed(() => entry.value === "0")
const mayPassToRightEntry = computed(() => Boolean(leftEntry.value) && Boolean(operation.value))
const expressionToEvaluate = computed(() => {
Expand All @@ -53,15 +49,26 @@ const expressionToDisplay = computed(() => {
break
}
case "%": {
const mustEvaluateRightEntry = mayPassToRightEntry.value && rightEntry.value
const newRightEntry = solvePercentage(rightEntry.value, leftEntry.value)
if (mustEvaluateRightEntry) passToRightEntry(Number(newRightEntry))
value = entry.value
break
}
case "1/x": {
value = `1/(${leftEntry.value})`
break
}
case "": {
value = `sqr(${leftEntry.value})`
break
}
case "": {
value = `√(${leftEntry.value})`
break
}
}
return value
})
const hasEvaluatedResult = computed(() => previousResult.value === entry.value)
function appendToEntryScreen(valueToAppend: string|number) {
if (isEntryValueEmpty.value || mustResetOnNextEntry.value) entry.value = String(valueToAppend)
Expand Down Expand Up @@ -89,8 +96,6 @@ function setOperationValue(newOperation: Operations) {
operation.value = newOperation
}
const hasEvaluatedResult = computed(() => previousResult.value === entry.value)
const isEvaluatingBasicOperation = computed(() => evaluation.value === "=")
function evaluateExpression(evaluationMethod: Evaluations) {
function evaluateBasicOperation() {
Expand Down Expand Up @@ -121,11 +126,13 @@ function evaluateExpression(evaluationMethod: Evaluations) {
break
}
case "": {
leftEntry.value = Number(entry.value)
const sqr = Number(entry.value) * Number(entry.value)
entry.value = String(sqr)
break
}
case "": {
leftEntry.value = Number(entry.value)
const sqrt = Math.sqrt(Number(entry.value))
entry.value = String(sqrt)
break
Expand Down

0 comments on commit 665fc77

Please sign in to comment.