Skip to content

Commit

Permalink
Merge pull request #63 from lemredd/allowed_similar_next_entry
Browse files Browse the repository at this point in the history
Allowed similar next entry
  • Loading branch information
lemredd committed Jan 16, 2023
2 parents b5a80ed + 8b50c60 commit 5bc73e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
14 changes: 13 additions & 1 deletion src/components/CalculatorContainer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ describe("Component: CalculatorContainer", () => {

// Find the digit "1" button and click it
const digitalBtns = wrapper.findAll(".digital-button")
const [digital1Btn] = digitalBtns.filter(btn => btn.text() === "1")
const [digital1Btn, digital2Btn] = digitalBtns.filter(
btn => btn.text() === "1"
|| btn.text() === "2"
)
await digital1Btn.trigger("click")
expect(entryScrn.text()).toEqual("1")
expect(expressionScrn.text()).toEqual("")
Expand All @@ -35,6 +38,15 @@ describe("Component: CalculatorContainer", () => {
await equalBtn.trigger("click")
expect(expressionScrn.text()).toEqual("1 + 1 =")
expect(entryScrn.text()).toEqual("2")

await digital2Btn.trigger("click")
expect(entryScrn.text()).toEqual("2")
await additionBtn.trigger("click")
expect(expressionScrn.text()).toEqual("2 +")
await digital1Btn.trigger("click")
await equalBtn.trigger("click")
expect(expressionScrn.text()).toEqual("2 + 1 =")
expect(entryScrn.text()).toEqual("3")
})

it("can evaluate with incomplete operands", async() => {
Expand Down
26 changes: 11 additions & 15 deletions src/components/CalculatorContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,10 @@ const operation = ref<Operations|null>(null)
const previousResult = ref("0")
const previousExpressionEvaluated = ref("")
const evaluation = ref<Evaluations|null>(null)
const lastPassedEntry = ref<number|null>(null)
const historyList = ref<HistoryList>([])
const hasPreviousEntry = computed(() => Boolean(previousEntry.value) && Boolean(operation.value))
const rightEntry = computed(() => {
let entry: number|null = null
const [ unusedLeftOperand, rightOperand ] = previousExpressionEvaluated.value.split(operation.value as Operations)
const mayIdentifyRightEntry = previousExpressionEvaluated.value && rightOperand
if (mayIdentifyRightEntry) entry = Number(rightOperand)
return entry
})
const expressionToEvaluate = computed({
get() {
let value = ""
Expand All @@ -52,14 +44,14 @@ const expressionToEvaluate = computed({
previousExpressionEvaluated.value = newValue
}
})
const hasSavedPreviousResult = computed(() => previousResult.value === entry.value)
const hasSavedPreviousResult = computed(() => previousResult.value === entry.value && mustClearEntryOnNextAppend.value)
const expressionAndPreviousResultInformation = reactive({
hasSavedPreviousResult,
operation,
previousEntry,
previousResult,
rightEntry
rightEntry: lastPassedEntry
})
function popOneDigit() {
Expand All @@ -75,7 +67,7 @@ function clearEntryScreen() {
}
function clearAll(mustClearPreviousResult = true) {
entry.value = ""
previousEntry.value = 0
previousEntry.value = null
operation.value = null
evaluation.value = null
if (mustClearPreviousResult) {
Expand Down Expand Up @@ -105,7 +97,7 @@ function appendToEntryScreen(valueToAppend: Entries) {
function setOperationValue(newOperation: Operations) {
if (!operation.value) {
if (!previousEntry.value) previousEntry.value = Number(entry.value)
if (previousEntry.value === null) previousEntry.value = Number(entry.value)
clearEntryScreen()
} else if (!mustClearEntryOnNextAppend.value) {
const expressionToEvaluateEagerly = `${previousEntry.value}${operation.value}${entry.value}`
Expand Down Expand Up @@ -143,8 +135,12 @@ function retrieveEvaluationResults(newEvaluation: Evaluations, result: number) {
|| newEvaluation === ""
|| newEvaluation === ""
if (!previousExpressionEvaluated.value) previousExpressionEvaluated.value = expressionToEvaluate.value
if (hasSavedPreviousResult.value) expressionToEvaluate.value = `${previousResult.value}${operation.value}${rightEntry.value}`
previousExpressionEvaluated.value = expressionToEvaluate.value
if (!lastPassedEntry.value) {
const [ leftOperand, unusedRightOperand ] = previousExpressionEvaluated.value.split(operation.value as Operations)
lastPassedEntry.value = Number(leftOperand)
}
if (hasSavedPreviousResult.value) expressionToEvaluate.value = `${previousResult.value}${operation.value}${lastPassedEntry.value}`
if (mustSaveCurrentEntry) previousEntry.value = Number(entry.value)
evaluation.value = newEvaluation
Expand Down

0 comments on commit 5bc73e0

Please sign in to comment.