@liangjiehappy/dsh-calc-geometry 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,17 @@
1
- # Temporary Holding Version
1
+ # 几何计算器
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 算圆、三角形、矩形的周长、面积、体积。设计、加工、装修时快速核对。纯本地计算,不联网。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ dsh plugin --profile web add @liangjiehappy/dsh-calc-geometry
9
+ ```
10
+
11
+ ## 用法
12
+
13
+ 在会话里说「半径 5 的圆,面积多少」,或直接调用工具 `calc_geometry`。
14
+
15
+ ## 许可
16
+
17
+ MIT
@@ -0,0 +1,4 @@
1
+ # 几何计算器
2
+ - insert:
3
+ - id: dsh-calc-geometry
4
+ name: "@liangjiehappy/dsh-calc-geometry"
package/lib/index.js ADDED
@@ -0,0 +1,138 @@
1
+ /**
2
+ * 几何计算器
3
+ *
4
+ * 算圆、三角形、矩形的周长、面积、体积。设计、加工、装修时快速核对。纯本地计算,不联网。
5
+ */
6
+
7
+ export const name = "dsh-calc-geometry";
8
+
9
+ export const inject = ["tools"];
10
+
11
+ const TEXT_OUTPUT = {
12
+ schema: {
13
+ type: "object",
14
+ additionalProperties: false,
15
+ properties: {
16
+ text: { type: "string" },
17
+ },
18
+ required: ["text"],
19
+ },
20
+ render: (_args, value) => [
21
+ { type: "text", text: (value && value.text) || "" },
22
+ ],
23
+ };
24
+
25
+ // ══════════════════════════════════════════════════════════
26
+ // 通用工具函数
27
+ // ══════════════════════════════════════════════════════════
28
+
29
+ const TITLE = "__CALC_TITLE__";
30
+
31
+ function fmt(n, digits) {
32
+ if (!Number.isFinite(n)) return String(n);
33
+ const d = digits == null ? 2 : digits;
34
+ if (Math.abs(n) >= 1e12) return n.toExponential(6);
35
+ let s = n.toFixed(d);
36
+ if (d > 0) s = s.replace(/0+$/, "").replace(/\.$/, "");
37
+ return s;
38
+ }
39
+
40
+ function money(n) {
41
+ if (!Number.isFinite(n)) return String(n);
42
+ return n.toLocaleString("zh-CN", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
43
+ }
44
+
45
+ function num(v, def) {
46
+ const n = Number(v);
47
+ return Number.isFinite(n) ? n : (def == null ? 0 : def);
48
+ }
49
+
50
+ function line(label, value, unit) {
51
+ const pad = " ";
52
+ return pad + label + ":" + value + (unit ? " " + unit : "");
53
+ }
54
+
55
+ function head(title) {
56
+ return [title, "=".repeat(52)];
57
+ }
58
+
59
+ // ══════════════════════════════════════════════════════════
60
+ // 工具
61
+ // ══════════════════════════════════════════════════════════
62
+
63
+ export function apply(ctx, config) {
64
+ void config;
65
+
66
+ ctx.tools.register({
67
+ name: "calc_geometry",
68
+ description:
69
+ "几何计算。支持圆(半径/直径)、矩形、三角形、球、圆柱、圆锥的周长/面积/体积。",
70
+ parameters: {
71
+ shape: { type: "string", description: "形状:circle(默认)/ rect / triangle / sphere / cylinder / cone", enum: ["circle", "rect", "triangle", "sphere", "cylinder", "cone"] },
72
+ a: { type: "number", required: true, description: "第一个尺寸(半径/长/底/半径/半径/半径)" },
73
+ b: { type: "number", description: "第二个尺寸(宽/高/高/高)" },
74
+ c: { type: "number", description: "第三个尺寸(仅三角形用)" },
75
+ },
76
+ output: TEXT_OUTPUT,
77
+ async execute(args) {
78
+ try {
79
+ const a = args || {};
80
+ const sp = String(a.shape || "circle").toLowerCase();
81
+ const x = num(a.a), y = num(a.b), z = num(a.c);
82
+ const PI = Math.PI;
83
+ const L = head("几何计算");
84
+ if (sp === "circle") {
85
+ if (x <= 0) return { text: "半径要大于 0" };
86
+ L.push(line("半径", fmt(x, 3)));
87
+ L.push(line("直径", fmt(x * 2, 3)));
88
+ L.push(line("周长", fmt(2 * PI * x, 4)));
89
+ L.push(line("面积", fmt(PI * x * x, 4)));
90
+ } else if (sp === "rect") {
91
+ if (x <= 0 || y <= 0) return { text: "长宽要大于 0" };
92
+ L.push(line("长 × 宽", fmt(x, 3) + " × " + fmt(y, 3)));
93
+ L.push(line("周长", fmt(2 * (x + y), 4)));
94
+ L.push(line("面积", fmt(x * y, 4)));
95
+ if (z > 0) {
96
+ L.push(line("高", fmt(z, 3)));
97
+ L.push(line("体积", fmt(x * y * z, 4)));
98
+ L.push(line("表面积", fmt(2 * (x * y + x * z + y * z), 4)));
99
+ }
100
+ } else if (sp === "triangle") {
101
+ if (x <= 0 || y <= 0) return { text: "底和高要大于 0" };
102
+ L.push(line("底", fmt(x, 3)));
103
+ L.push(line("高", fmt(y, 3)));
104
+ L.push(line("面积", fmt(x * y / 2, 4)));
105
+ if (z > 0) {
106
+ L.push(line("另一边", fmt(z, 3)));
107
+ L.push(" 注意:只知道三边时请用海伦公式,这里只算底×高/2。");
108
+ }
109
+ } else if (sp === "sphere") {
110
+ if (x <= 0) return { text: "半径要大于 0" };
111
+ L.push(line("半径", fmt(x, 3)));
112
+ L.push(line("表面积", fmt(4 * PI * x * x, 4)));
113
+ L.push(line("体积", fmt(4 / 3 * PI * x ** 3, 4)));
114
+ } else if (sp === "cylinder") {
115
+ if (x <= 0 || y <= 0) return { text: "半径和高要大于 0" };
116
+ L.push(line("半径 × 高", fmt(x, 3) + " × " + fmt(y, 3)));
117
+ L.push(line("底面积", fmt(PI * x * x, 4)));
118
+ L.push(line("侧面积", fmt(2 * PI * x * y, 4)));
119
+ L.push(line("表面积", fmt(2 * PI * x * x + 2 * PI * x * y, 4)));
120
+ L.push(line("体积", fmt(PI * x * x * y, 4)));
121
+ } else if (sp === "cone") {
122
+ if (x <= 0 || y <= 0) return { text: "半径和高要大于 0" };
123
+ const slant = Math.sqrt(x * x + y * y);
124
+ L.push(line("半径 × 高", fmt(x, 3) + " × " + fmt(y, 3)));
125
+ L.push(line("母线", fmt(slant, 4)));
126
+ L.push(line("底面积", fmt(PI * x * x, 4)));
127
+ L.push(line("侧面积", fmt(PI * x * slant, 4)));
128
+ L.push(line("体积", fmt(PI * x * x * y / 3, 4)));
129
+ } else {
130
+ return { text: "不支持的形状: " + sp };
131
+ }
132
+ return { text: L.join("\n") };
133
+ } catch (e) {
134
+ return { text: "计算失败: " + String((e && e.message) || e) };
135
+ }
136
+ },
137
+ });
138
+ }
package/package.json CHANGED
@@ -1,6 +1,16 @@
1
1
  {
2
2
  "name": "@liangjiehappy/dsh-calc-geometry",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "算圆、三角形、矩形的周长、面积、体积。设计、加工、装修时快速核对。纯本地计算,不联网。",
5
+ "type": "module",
6
+ "main": "lib/index.js",
7
+ "files": ["lib", "cordis.patch.yml", "README.md"],
8
+ "keywords": ["dsh", "dsh-plugin", "calc", "calculator", "geometry", "计算器"],
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/moldkey/dsh-calc-geometry.git"
13
+ },
14
+ "dsh": { "bundle": { "patch": "./cordis.patch.yml" } },
15
+ "engines": { "dsh": ">=0.2.0-rc.2" }
16
+ }