From 9d6b26e49b732e5e1eb0790391a49e67bb2afa2a Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Tue, 14 Jan 2025 18:11:49 +1100 Subject: launcher: fix math derive and vars Using create breaks derive cause broken dependency Also don't check by ref in select Better error message --- services/math.ts | 43 +++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 28 deletions(-) (limited to 'services/math.ts') diff --git a/services/math.ts b/services/math.ts index 9920044..8fbf757 100644 --- a/services/math.ts +++ b/services/math.ts @@ -1,12 +1,5 @@ import { GLib, GObject, property, readFile, register, writeFileAsync } from "astal"; -import { - create, - derivativeDependencies, - evaluateDependencies, - rationalizeDependencies, - simplifyDependencies, - type MathNode, -} from "mathjs/number"; +import { derivative, evaluate, rationalize, simplify } from "mathjs/number"; import { CACHE_DIR } from "../utils/constants"; export interface HistoryItem { @@ -24,18 +17,11 @@ export default class Math extends GObject.Object { return this.instance; } - static math = create({ - simplifyDependencies, - derivativeDependencies, - rationalizeDependencies, - evaluateDependencies, - }); - readonly #maxHistory = 20; readonly #path = `${CACHE_DIR}/math-history.json`; readonly #history: HistoryItem[] = []; - #variables: Record = {}; + #variables: Record = {}; #lastExpression: HistoryItem | null = null; @property(Object) @@ -69,7 +55,7 @@ export default class Math extends GObject.Object { * @returns If the item was successfully selected */ select(item: HistoryItem) { - const idx = this.#history.indexOf(item); + const idx = this.#history.findIndex(i => i.equation === item.equation && i.result === item.result); if (idx >= 0) { this.#history.splice(idx, 1); this.#history.unshift(item); @@ -113,30 +99,31 @@ export default class Math extends GObject.Object { if (equation.includes("=")) { const [left, right] = equation.split("="); try { - this.#variables[left] = Math.math.simplify(right); + this.#variables[left.trim()] = simplify(right).toString(); } catch { - this.#variables[left] = parseFloat(right); + this.#variables[left.trim()] = right.trim(); } - result = this.#variables[left].toString(); + result = this.#variables[left.trim()]; icon = "equal"; } else if (equation.startsWith("simplify")) { - result = Math.math.simplify(equation.slice(8), this.#variables).toString(); + result = simplify(equation.slice(8), this.#variables).toString(); icon = "function"; } else if (equation.startsWith("derive")) { - const respectTo = equation.slice(6).split(" ")[0]; - result = Math.math.derivative(equation.slice(7 + respectTo.length), respectTo).toString(); + const respectTo = equation.split(" ")[1]; + if (!respectTo) throw new Error("Format: derive "); + result = derivative(equation.slice(7 + respectTo.length), respectTo).toString(); icon = "function"; } else if (equation.startsWith("rationalize")) { - result = Math.math.rationalize(equation.slice(11), this.#variables).toString(); + result = rationalize(equation.slice(11), this.#variables).toString(); icon = "function"; } else { - result = Math.math.evaluate(equation, this.#variables).toString(); + result = evaluate(equation, this.#variables).toString(); icon = "calculate"; } - } catch { - result = equation; - icon = "error"; + } catch (e) { equation = "Invalid equation"; + result = String(e); + icon = "error"; } return (this.#lastExpression = { equation, result, icon }); -- cgit v1.2.3-freya