launcher: fix math derive and vars
Using create breaks derive cause broken dependency Also don't check by ref in select Better error message
This commit is contained in:
parent
cb487aefb1
commit
9d6b26e49b
1 changed files with 15 additions and 28 deletions
|
|
@ -1,12 +1,5 @@
|
||||||
import { GLib, GObject, property, readFile, register, writeFileAsync } from "astal";
|
import { GLib, GObject, property, readFile, register, writeFileAsync } from "astal";
|
||||||
import {
|
import { derivative, evaluate, rationalize, simplify } from "mathjs/number";
|
||||||
create,
|
|
||||||
derivativeDependencies,
|
|
||||||
evaluateDependencies,
|
|
||||||
rationalizeDependencies,
|
|
||||||
simplifyDependencies,
|
|
||||||
type MathNode,
|
|
||||||
} from "mathjs/number";
|
|
||||||
import { CACHE_DIR } from "../utils/constants";
|
import { CACHE_DIR } from "../utils/constants";
|
||||||
|
|
||||||
export interface HistoryItem {
|
export interface HistoryItem {
|
||||||
|
|
@ -24,18 +17,11 @@ export default class Math extends GObject.Object {
|
||||||
return this.instance;
|
return this.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
static math = create({
|
|
||||||
simplifyDependencies,
|
|
||||||
derivativeDependencies,
|
|
||||||
rationalizeDependencies,
|
|
||||||
evaluateDependencies,
|
|
||||||
});
|
|
||||||
|
|
||||||
readonly #maxHistory = 20;
|
readonly #maxHistory = 20;
|
||||||
readonly #path = `${CACHE_DIR}/math-history.json`;
|
readonly #path = `${CACHE_DIR}/math-history.json`;
|
||||||
readonly #history: HistoryItem[] = [];
|
readonly #history: HistoryItem[] = [];
|
||||||
|
|
||||||
#variables: Record<string, number | MathNode> = {};
|
#variables: Record<string, string> = {};
|
||||||
#lastExpression: HistoryItem | null = null;
|
#lastExpression: HistoryItem | null = null;
|
||||||
|
|
||||||
@property(Object)
|
@property(Object)
|
||||||
|
|
@ -69,7 +55,7 @@ export default class Math extends GObject.Object {
|
||||||
* @returns If the item was successfully selected
|
* @returns If the item was successfully selected
|
||||||
*/
|
*/
|
||||||
select(item: HistoryItem) {
|
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) {
|
if (idx >= 0) {
|
||||||
this.#history.splice(idx, 1);
|
this.#history.splice(idx, 1);
|
||||||
this.#history.unshift(item);
|
this.#history.unshift(item);
|
||||||
|
|
@ -113,30 +99,31 @@ export default class Math extends GObject.Object {
|
||||||
if (equation.includes("=")) {
|
if (equation.includes("=")) {
|
||||||
const [left, right] = equation.split("=");
|
const [left, right] = equation.split("=");
|
||||||
try {
|
try {
|
||||||
this.#variables[left] = Math.math.simplify(right);
|
this.#variables[left.trim()] = simplify(right).toString();
|
||||||
} catch {
|
} catch {
|
||||||
this.#variables[left] = parseFloat(right);
|
this.#variables[left.trim()] = right.trim();
|
||||||
}
|
}
|
||||||
result = this.#variables[left].toString();
|
result = this.#variables[left.trim()];
|
||||||
icon = "equal";
|
icon = "equal";
|
||||||
} else if (equation.startsWith("simplify")) {
|
} 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";
|
icon = "function";
|
||||||
} else if (equation.startsWith("derive")) {
|
} else if (equation.startsWith("derive")) {
|
||||||
const respectTo = equation.slice(6).split(" ")[0];
|
const respectTo = equation.split(" ")[1];
|
||||||
result = Math.math.derivative(equation.slice(7 + respectTo.length), respectTo).toString();
|
if (!respectTo) throw new Error("Format: derive <respect-to> <equation>");
|
||||||
|
result = derivative(equation.slice(7 + respectTo.length), respectTo).toString();
|
||||||
icon = "function";
|
icon = "function";
|
||||||
} else if (equation.startsWith("rationalize")) {
|
} 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";
|
icon = "function";
|
||||||
} else {
|
} else {
|
||||||
result = Math.math.evaluate(equation, this.#variables).toString();
|
result = evaluate(equation, this.#variables).toString();
|
||||||
icon = "calculate";
|
icon = "calculate";
|
||||||
}
|
}
|
||||||
} catch {
|
} catch (e) {
|
||||||
result = equation;
|
|
||||||
icon = "error";
|
|
||||||
equation = "Invalid equation";
|
equation = "Invalid equation";
|
||||||
|
result = String(e);
|
||||||
|
icon = "error";
|
||||||
}
|
}
|
||||||
|
|
||||||
return (this.#lastExpression = { equation, result, icon });
|
return (this.#lastExpression = { equation, result, icon });
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue