-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrig.js
More file actions
194 lines (169 loc) · 6.91 KB
/
Copy pathtrig.js
File metadata and controls
194 lines (169 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env node
/**
* trig — trigonometry calculator (detail mode)
*
* Usage: math trig sin(45) — assumes degrees
* math trig cos(pi/3) rad — explicit radians
* math trig table 30 — all functions for an angle
*/
"use strict";
const { readInput, writeResponse, detail, error, stripKeyword, evaluate, fmt, escHtml } = require("./lib");
const TRIG_FNS = [
{ name: "sin", fn: Math.sin, inv: "asin" },
{ name: "cos", fn: Math.cos, inv: "acos" },
{ name: "tan", fn: Math.tan, inv: "atan" },
{ name: "csc", fn: (x) => 1 / Math.sin(x), inv: null },
{ name: "sec", fn: (x) => 1 / Math.cos(x), inv: null },
{ name: "cot", fn: (x) => 1 / Math.tan(x), inv: null },
];
/**
* Parenthesis-aware function wrapper.
* Finds each call matching `pattern` (a regex like /\b(sin|cos|...)\s*\(/gi),
* locates the balanced closing `)`, extracts the full inner argument, and
* delegates to `wrapper(fnName, innerArg)` for the replacement string.
* Processes matches from end-to-start so splice indices stay valid.
*/
function wrapFunctions(expr, pattern, wrapper) {
const matches = [];
let m;
pattern.lastIndex = 0;
while ((m = pattern.exec(expr)) !== null) {
matches.push({ index: m.index, fn: m[1], afterParen: m.index + m[0].length });
}
// Work backwards to avoid index shifting
for (let i = matches.length - 1; i >= 0; i--) {
const { index, fn, afterParen } = matches[i];
// Walk forward from the char after '(' counting balanced parens
let depth = 1, j = afterParen;
while (j < expr.length && depth > 0) {
if (expr[j] === '(') depth++;
if (expr[j] === ')') depth--;
j++;
}
if (depth !== 0) continue; // unmatched parens — skip
const innerArg = expr.slice(afterParen, j - 1);
const replacement = wrapper(fn, innerArg);
expr = expr.slice(0, index) + replacement + expr.slice(j);
}
return expr;
}
/**
* Convert a trig expression from degree mode to radian mode.
* Handles nested calls like sin(cos(45)) by scanning balanced parens
* instead of the old [^)]+ regex that broke on nested expressions.
*/
function wrapTrigDegrees(expr) {
const ATRIG = /\b(asin|acos|atan)\s*\(/gi;
const TRIG = /\b(sin|cos|tan|csc|sec|cot)\s*\(/gi;
// First pass: wrap inverse trig results (rad -> deg)
expr = wrapFunctions(expr, ATRIG, (fn, innerArg) => {
return `(${fn}(${innerArg})*180/${Math.PI})`;
});
// Second pass: wrap direct trig args (deg -> rad)
expr = wrapFunctions(expr, TRIG, (fn, innerArg) => {
return `${fn}((${innerArg})*${Math.PI}/180)`;
});
return expr;
}
async function main() {
const input = await readInput();
let query = stripKeyword(input.query || "", "trig", "t");
if (!query) {
writeResponse(detail(
`<div style="font-family:monospace;padding:16px">
<h2 style="margin:0 0 12px">Trigonometry</h2>
<p style="color:#888;margin:0 0 8px">Evaluate trig functions (degrees by default)</p>
<code style="display:block;padding:2px 0">math trig sin(45)</code>
<code style="display:block;padding:2px 0">math trig cos(pi/3) rad</code>
<code style="display:block;padding:2px 0">math trig table 60</code>
</div>`,
[
{ label: "Functions", value: "sin cos tan csc sec cot" },
{ label: "Inverse", value: "asin acos atan" },
{ label: "Default", value: "Degrees (add 'rad' for radians)" },
]
));
return;
}
// Check for "table" subcommand: trig table 45
const tableMatch = query.match(/^table\s+(.+)/i);
if (tableMatch) {
return showTable(tableMatch[1]);
}
// Detect rad/deg mode
let isRad = false;
if (/\brad(ians?)?\s*$/i.test(query)) {
isRad = true;
query = query.replace(/\s*rad(ians?)?\s*$/i, "").trim();
} else if (/\bdeg(rees?)?\s*$/i.test(query)) {
query = query.replace(/\s*deg(rees?)?\s*$/i, "").trim();
}
try {
// When in degrees mode (default), wrap trig function arguments with
// degree-to-radian conversion so sin(45) means sin(45°), not sin(45 rad).
let evalExpr = query;
if (!isRad) {
evalExpr = wrapTrigDegrees(query);
}
const result = evaluate(evalExpr);
const modeLabel = isRad ? "radians" : "degrees";
// If the expression is a single trig function, show extra context
const fnMatch = query.match(/^(a?(?:sin|cos|tan)h?)\s*\((.+)\)$/i);
const metadata = [
{ label: "Expression", value: query },
{ label: "Mode", value: modeLabel },
{ label: "Result", value: fmt(result) },
];
// For inverse functions, also show the angle in degrees
if (fnMatch && fnMatch[1].startsWith("a")) {
if (isRad) {
// Result is in radians — show degree conversion
metadata.push({ label: "Result (deg)", value: fmt(result * 180 / Math.PI) + "°" });
metadata.push({ label: "Result (rad)", value: fmt(result) + " rad" });
} else {
// Result already in degrees (converted by degExpr) — show as-is
metadata.push({ label: "Result (deg)", value: fmt(result) + "°" });
metadata.push({ label: "Result (rad)", value: fmt(result * Math.PI / 180) + " rad" });
}
}
const md = `<div style="font-family:monospace;padding:16px">
<div style="font-size:14px;color:#888;margin-bottom:8px">${escHtml(query)} <span style="color:#666">(${modeLabel})</span></div>
<div style="font-size:32px;font-weight:bold;color:#81C784">= ${escHtml(fmt(result))}</div>
</div>`;
writeResponse(detail(md, metadata, [
{ title: "Copy", type: "copy", value: fmt(result) },
]));
} catch (err) {
writeResponse(error("Trig error", err.message));
}
}
function showTable(angleExpr) {
try {
const angleDeg = evaluate(angleExpr);
const angleRad = angleDeg * Math.PI / 180;
let rows = "";
for (const { name, fn } of TRIG_FNS) {
const val = fn(angleRad);
rows += `<tr><td style="padding:4px 12px 4px 0;color:#81C784;font-weight:bold">${name}</td>` +
`<td style="padding:4px 0">${fmt(val)}</td></tr>`;
}
const md = `<div style="font-family:monospace;padding:16px">
<h2 style="margin:0 0 4px">Trig Table</h2>
<div style="font-size:14px;color:#888;margin-bottom:12px">${escHtml(fmt(angleDeg))}° = ${escHtml(fmt(angleRad))} rad</div>
<table style="border-collapse:collapse">${rows}</table>
</div>`;
writeResponse(detail(md, [
{ label: "Angle (deg)", value: fmt(angleDeg) + "°" },
{ label: "Angle (rad)", value: fmt(angleRad) },
], [
{ title: "Copy table", type: "copy", value: TRIG_FNS.map(f => `${f.name}(${fmt(angleDeg)}°) = ${fmt(f.fn(angleRad))}`).join("\n") },
]));
} catch (err) {
writeResponse(error("Trig table error", err.message));
}
}
// When required as a module, export helpers for testing
if (require.main === module) {
main().catch(e => { try { writeResponse(error("Error", e.message)); } catch(_) {} process.exit(1); });
}
module.exports = { wrapFunctions, wrapTrigDegrees, TRIG_FNS };