mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 02:12:00 +03:00
🧹 Refactor evalBin to improve readability
Extracted percent and quantity arithmetic logic into smaller helper functions (`evalPercentArith` and `evalQuantityArith`) to simplify the main `evalBin` dispatcher. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
f5b1fc038b
commit
68820f7932
2 changed files with 40 additions and 34 deletions
4
package-lock.json
generated
4
package-lock.json
generated
|
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "summarum",
|
"name": "summarum",
|
||||||
"version": "1.2.1",
|
"version": "1.2.2",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "summarum",
|
"name": "summarum",
|
||||||
"version": "1.2.1",
|
"version": "1.2.2",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@codemirror/autocomplete": "^6.18.0",
|
"@codemirror/autocomplete": "^6.18.0",
|
||||||
"@codemirror/commands": "^6.8.0",
|
"@codemirror/commands": "^6.8.0",
|
||||||
|
|
|
||||||
|
|
@ -199,12 +199,7 @@ function numericAdd(a: Quantity, b: Quantity, sign: 1 | -1): Quantity {
|
||||||
return qty(sign === 1 ? a.value.add(b.value) : a.value.sub(b.value), unit);
|
return qty(sign === 1 ? a.value.add(b.value) : a.value.sub(b.value), unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
function evalBin(op: "plus" | "minus" | "mul" | "div" | "mod" | "pow", l: Value, r: Value, ctx: EvalCtx): Value {
|
function evalPercentArith(op: "plus" | "minus" | "mul" | "div" | "mod" | "pow", l: Value, r: Value): Value {
|
||||||
// ---- dates
|
|
||||||
if (l.kind === "date" || r.kind === "date") return evalDateArith(op, l, r);
|
|
||||||
|
|
||||||
// ---- percent combinations
|
|
||||||
if (l.kind === "percent" || r.kind === "percent") {
|
|
||||||
if (l.kind === "percent" && r.kind === "percent") {
|
if (l.kind === "percent" && r.kind === "percent") {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case "plus": return pct(l.value.add(r.value));
|
case "plus": return pct(l.value.add(r.value));
|
||||||
|
|
@ -232,10 +227,9 @@ function evalBin(op: "plus" | "minus" | "mul" | "div" | "mod" | "pow", l: Value,
|
||||||
: qty(q.value.pow(p.div(100)), q.unit);
|
: qty(q.value.pow(p.div(100)), q.unit);
|
||||||
default: throw new EvalError("bad percent op");
|
default: throw new EvalError("bad percent op");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const a = l as Quantity;
|
function evalQuantityArith(op: "plus" | "minus" | "mul" | "div" | "mod" | "pow", a: Quantity, b: Quantity, ctx: EvalCtx): Value {
|
||||||
const b = r as Quantity;
|
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case "plus": return numericAdd(a, b, 1);
|
case "plus": return numericAdd(a, b, 1);
|
||||||
case "minus": return numericAdd(a, b, -1);
|
case "minus": return numericAdd(a, b, -1);
|
||||||
|
|
@ -281,6 +275,18 @@ function evalBin(op: "plus" | "minus" | "mul" | "div" | "mod" | "pow", l: Value,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function evalBin(op: "plus" | "minus" | "mul" | "div" | "mod" | "pow", l: Value, r: Value, ctx: EvalCtx): Value {
|
||||||
|
if (l.kind === "date" || r.kind === "date") {
|
||||||
|
return evalDateArith(op, l, r);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (l.kind === "percent" || r.kind === "percent") {
|
||||||
|
return evalPercentArith(op, l, r);
|
||||||
|
}
|
||||||
|
|
||||||
|
return evalQuantityArith(op, l as Quantity, r as Quantity, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
function evalBit(op: string, l: Value, r: Value): Value {
|
function evalBit(op: string, l: Value, r: Value): Value {
|
||||||
if (l.kind !== "quantity" || r.kind !== "quantity") throw new EvalError("bitwise needs integers");
|
if (l.kind !== "quantity" || r.kind !== "quantity") throw new EvalError("bitwise needs integers");
|
||||||
if (!l.value.isFinite() || !r.value.isFinite()) throw new EvalError("bitwise needs finite integers");
|
if (!l.value.isFinite() || !r.value.isFinite()) throw new EvalError("bitwise needs finite integers");
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue