Skip to content

Commit

Permalink
style(intg): reorder code based on type
Browse files Browse the repository at this point in the history
`ref` values are grouped together and separated from `computed` values
methods on the other hand are placed below
  • Loading branch information
lemredd committed Dec 27, 2022
1 parent 3ae4ad2 commit 67a09b6
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions src/components/CalculatorContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,31 @@ import CorrectionButton from "@/CalculatorContainer/CorrectionButton.vue"
import OperationalButton from "@/CalculatorContainer/OperationalButton.vue"
const entry = ref("0")
const leftEntry = ref<number|null>(null)
const operation = ref<Operations|null>(null)
const rightEntry = ref<number|null>(null)
const mustResetOnNextEntry = ref(false)
const previousResult = ref("0")
const evaluation = ref<Evaluations|null>(null)
const isEntryValueEmpty = computed(() => entry.value === "0")
const mayPassToRightEntry = computed(() => Boolean(leftEntry.value) && Boolean(operation.value))
const expressionToEvaluate = computed(() => {
let value = ""
if (leftEntry.value) value += leftEntry.value
if (operation.value) value += operation.value
if (rightEntry.value) value += rightEntry.value
return value
})
const expressionToDisplay = computed(() => {
let value = Array.from(expressionToEvaluate.value).join(" ")
return value
})
function appendToEntryScreen(valueToAppend: string|number) {
if (isEntryValueEmpty.value || mustResetOnNextEntry.value) entry.value = String(valueToAppend)
else entry.value += String(valueToAppend)
Expand All @@ -30,13 +53,10 @@ function popOneDigit() {
entry.value = entryDigits.join("")
}
const leftEntry = ref<number|null>(null)
const operation = ref<Operations|null>(null)
const rightEntry = ref<number|null>(null)
const mayPassToRightEntry = computed(() => Boolean(leftEntry.value) && Boolean(operation.value))
function setOperationValue(newOperation: Operations) {
if (!leftEntry.value) leftEntry.value = Number(entry.value)
if (!leftEntry.value) {
leftEntry.value = Number(entry.value)
}
if (mayPassToRightEntry.value) rightEntry.value = Number(entry.value)
mustResetOnNextEntry.value = true
Expand All @@ -47,27 +67,7 @@ function setOperationValue(newOperation: Operations) {
operation.value = newOperation
}
const expressionToEvaluate = computed(() => {
let value = ""
if (leftEntry.value) value += leftEntry.value
if (operation.value) value += operation.value
if (rightEntry.value) value += rightEntry.value
return value
})
const expressionToDisplay = computed(() => {
let value = ""
if (operation.value) value = `${leftEntry.value} ${operation.value}`
if (isEvaluatingBasicOperation.value) value += ` ${rightEntry.value} ${evaluation.value}`
return value
})
const previousResult = ref("0")
const hasEvaluatedResult = computed(() => previousResult.value === entry.value)
const evaluation = ref<Evaluations|null>(null)
const isEvaluatingBasicOperation = computed(() => evaluation.value === "=")
function evaluateExpression(evaluationMethod: Evaluations) {
Expand All @@ -87,7 +87,6 @@ function evaluateExpression(evaluationMethod: Evaluations) {
const percent = Number(previousResult.value) / 100
const percentageResult = String(evaluate(`${base} * ${percent}`))
entry.value = percentageResult
// TODO: replace with proper expression
// expressionValue.value = percentageResult
break
}
Expand Down

0 comments on commit 67a09b6

Please sign in to comment.