@liangjiehappy/dsh-calc-fx 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-fx
9
+ ```
10
+
11
+ ## 用法
12
+
13
+ 在会话里说「100 美元,汇率 7.2,换多少人民币」,或直接调用工具 `calc_fx`。
14
+
15
+ ## 许可
16
+
17
+ MIT
@@ -0,0 +1,4 @@
1
+ # 汇率换算(手动)
2
+ - insert:
3
+ - id: dsh-calc-fx
4
+ name: "@liangjiehappy/dsh-calc-fx"
package/lib/index.js ADDED
@@ -0,0 +1,101 @@
1
+ /**
2
+ * 汇率换算(手动)
3
+ *
4
+ * 手动汇率换算。不联网,你给汇率我算。适合估算和记账。纯本地计算,不联网。
5
+ */
6
+
7
+ export const name = "dsh-calc-fx";
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_fx",
68
+ description:
69
+ "手动汇率换算。输入金额、汇率、方向,返回换算结果。不联网,汇率由你自己提供。",
70
+ parameters: {
71
+ amount: { type: "number", required: true, description: "金额" },
72
+ rate: { type: "number", required: true, description: "汇率" },
73
+ direction: { type: "string", description: "方向:to_cny(外币→人民币,默认)或 from_cny(人民币→外币)", enum: ["to_cny", "from_cny"] },
74
+ },
75
+ output: TEXT_OUTPUT,
76
+ async execute(args) {
77
+ try {
78
+ const a = args || {};
79
+ const amt = num(a.amount), rate = num(a.rate);
80
+ if (rate <= 0) return { text: "汇率要大于 0" };
81
+ const dir = String(a.direction || "to_cny").toLowerCase();
82
+ const L = head("汇率换算");
83
+ if (dir === "from_cny") {
84
+ L.push(line("人民币", money(amt) + " 元"));
85
+ L.push(line("汇率", fmt(rate, 4)));
86
+ L.push(line("外币", money(amt / rate)));
87
+ } else {
88
+ L.push(line("外币", money(amt)));
89
+ L.push(line("汇率", fmt(rate, 4)));
90
+ L.push(line("人民币", money(amt * rate) + " 元"));
91
+ }
92
+ L.push("");
93
+ L.push(" 汇率由你提供,不是实时价。");
94
+ L.push(" 要实时汇率的话用 fx-rate 那个插件(联网)。");
95
+ return { text: L.join("\n") };
96
+ } catch (e) {
97
+ return { text: "计算失败: " + String((e && e.message) || e) };
98
+ }
99
+ },
100
+ });
101
+ }
package/package.json CHANGED
@@ -1,6 +1,16 @@
1
1
  {
2
2
  "name": "@liangjiehappy/dsh-calc-fx",
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", "fx", "计算器"],
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/moldkey/dsh-calc-fx.git"
13
+ },
14
+ "dsh": { "bundle": { "patch": "./cordis.patch.yml" } },
15
+ "engines": { "dsh": ">=0.2.0-rc.2" }
16
+ }