diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-01-14 18:11:49 +1100 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-01-14 18:11:49 +1100 |
| commit | 9d6b26e49b732e5e1eb0790391a49e67bb2afa2a (patch) | |
| tree | 4d448f5bed9f277a6801658874abe3e057f21063 /services/math.ts | |
| parent | launcher: better math (diff) | |
| download | caelestia-shell-9d6b26e49b732e5e1eb0790391a49e67bb2afa2a.tar.gz caelestia-shell-9d6b26e49b732e5e1eb0790391a49e67bb2afa2a.tar.bz2 caelestia-shell-9d6b26e49b732e5e1eb0790391a49e67bb2afa2a.zip | |
launcher: fix math derive and vars
Using create breaks derive cause broken dependency
Also don't check by ref in select
Better error message
Diffstat (limited to 'services/math.ts')
| -rw-r--r-- | services/math.ts | 43 |
1 files changed, 15 insertions, 28 deletions
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<string, number | MathNode> = {}; + #variables: Record<string, string> = {}; #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 <respect-to> <equation>"); + 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 }); |