Skip to content

Instantly share code, notes, and snippets.

@breeko
Created July 20, 2018 10:53
Show Gist options
  • Select an option

  • Save breeko/3d58e38a434292e97d31e7460d9c2dd7 to your computer and use it in GitHub Desktop.

Select an option

Save breeko/3d58e38a434292e97d31e7460d9c2dd7 to your computer and use it in GitHub Desktop.
setOperand function for OpenCalc
setOperand(op: Operation): ?Operation {
let newNumber: ?string;
if (!Validator.validDigit(op.stringVal, this.queue)) {
return null;
}
let lastOp: ?Operation = lastOrNull(this.queue);
if (lastOp && (
this.cleared ||
lastOp.operationArgs.has(OperationArgs.NotParseable) ||
op.operationArgs.has(OperationArgs.NotParseable) && lastOp.operationType === OperationType.Constant)
) {
this.queue.pop();
} else if (lastOp && lastOp.operationType === OperationType.Constant) {
const lastNumber = this.queue.pop();
newNumber = lastNumber.stringVal + op.stringVal;
}
if (newNumber) {
return newOperation(newNumber, this.queue);
} else {
return op;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment