x-iztro 0.0.0-init → 0.7.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.
Files changed (117) hide show
  1. package/dist/astro.d.ts +131 -0
  2. package/dist/astro.d.ts.map +1 -0
  3. package/dist/astro.js +179 -0
  4. package/dist/astro.js.map +1 -0
  5. package/dist/astrolabe.d.ts +222 -0
  6. package/dist/astrolabe.d.ts.map +1 -0
  7. package/dist/astrolabe.js +387 -0
  8. package/dist/astrolabe.js.map +1 -0
  9. package/dist/browser.d.ts +17 -0
  10. package/dist/browser.d.ts.map +1 -0
  11. package/dist/browser.js +19 -0
  12. package/dist/browser.js.map +1 -0
  13. package/dist/data.d.ts +88 -0
  14. package/dist/data.d.ts.map +1 -0
  15. package/dist/data.js +45 -0
  16. package/dist/data.js.map +1 -0
  17. package/dist/enums.d.ts +793 -0
  18. package/dist/enums.d.ts.map +1 -0
  19. package/dist/enums.js +744 -0
  20. package/dist/enums.js.map +1 -0
  21. package/dist/errors.d.ts +21 -0
  22. package/dist/errors.d.ts.map +1 -0
  23. package/dist/errors.js +19 -0
  24. package/dist/errors.js.map +1 -0
  25. package/dist/horoscope.d.ts +203 -0
  26. package/dist/horoscope.d.ts.map +1 -0
  27. package/dist/horoscope.js +364 -0
  28. package/dist/horoscope.js.map +1 -0
  29. package/dist/i18n.d.ts +25 -0
  30. package/dist/i18n.d.ts.map +1 -0
  31. package/dist/i18n.js +31 -0
  32. package/dist/i18n.js.map +1 -0
  33. package/dist/index.d.ts +28 -0
  34. package/dist/index.d.ts.map +1 -0
  35. package/dist/index.js +23 -0
  36. package/dist/index.js.map +1 -0
  37. package/dist/internal.d.ts +89 -0
  38. package/dist/internal.d.ts.map +1 -0
  39. package/dist/internal.js +98 -0
  40. package/dist/internal.js.map +1 -0
  41. package/dist/knowledge.d.ts +210 -0
  42. package/dist/knowledge.d.ts.map +1 -0
  43. package/dist/knowledge.js +190 -0
  44. package/dist/knowledge.js.map +1 -0
  45. package/dist/node.d.ts +16 -0
  46. package/dist/node.d.ts.map +1 -0
  47. package/dist/node.js +29 -0
  48. package/dist/node.js.map +1 -0
  49. package/dist/palace.d.ts +121 -0
  50. package/dist/palace.d.ts.map +1 -0
  51. package/dist/palace.js +234 -0
  52. package/dist/palace.js.map +1 -0
  53. package/dist/pattern.d.ts +60 -0
  54. package/dist/pattern.d.ts.map +1 -0
  55. package/dist/pattern.js +87 -0
  56. package/dist/pattern.js.map +1 -0
  57. package/dist/raw-dates.d.ts +41 -0
  58. package/dist/raw-dates.d.ts.map +1 -0
  59. package/dist/raw-dates.js +42 -0
  60. package/dist/raw-dates.js.map +1 -0
  61. package/dist/reverse.d.ts +69 -0
  62. package/dist/reverse.d.ts.map +1 -0
  63. package/dist/reverse.js +33 -0
  64. package/dist/reverse.js.map +1 -0
  65. package/dist/runtime.d.ts +71 -0
  66. package/dist/runtime.d.ts.map +1 -0
  67. package/dist/runtime.js +146 -0
  68. package/dist/runtime.js.map +1 -0
  69. package/dist/star-object.d.ts +44 -0
  70. package/dist/star-object.d.ts.map +1 -0
  71. package/dist/star-object.js +79 -0
  72. package/dist/star-object.js.map +1 -0
  73. package/dist/star.d.ts +215 -0
  74. package/dist/star.d.ts.map +1 -0
  75. package/dist/star.js +150 -0
  76. package/dist/star.js.map +1 -0
  77. package/dist/surpalaces.d.ts +68 -0
  78. package/dist/surpalaces.d.ts.map +1 -0
  79. package/dist/surpalaces.js +133 -0
  80. package/dist/surpalaces.js.map +1 -0
  81. package/dist/types-entry.d.ts +10 -0
  82. package/dist/types-entry.d.ts.map +1 -0
  83. package/dist/types-entry.js +9 -0
  84. package/dist/types-entry.js.map +1 -0
  85. package/dist/types.d.ts +402 -0
  86. package/dist/types.d.ts.map +1 -0
  87. package/dist/types.js +8 -0
  88. package/dist/types.js.map +1 -0
  89. package/dist/util.d.ts +47 -0
  90. package/dist/util.d.ts.map +1 -0
  91. package/dist/util.js +74 -0
  92. package/dist/util.js.map +1 -0
  93. package/dist/x_iztro.wasm +0 -0
  94. package/package.json +73 -1
  95. package/src/astro.ts +270 -0
  96. package/src/astrolabe.ts +435 -0
  97. package/src/browser.ts +21 -0
  98. package/src/data.ts +123 -0
  99. package/src/enums.ts +835 -0
  100. package/src/errors.ts +29 -0
  101. package/src/horoscope.ts +418 -0
  102. package/src/i18n.ts +35 -0
  103. package/src/index.ts +56 -0
  104. package/src/internal.ts +162 -0
  105. package/src/knowledge.ts +355 -0
  106. package/src/node.ts +31 -0
  107. package/src/palace.ts +259 -0
  108. package/src/pattern.ts +98 -0
  109. package/src/raw-dates.ts +56 -0
  110. package/src/reverse.ts +90 -0
  111. package/src/runtime.ts +207 -0
  112. package/src/star-object.ts +87 -0
  113. package/src/star.ts +294 -0
  114. package/src/surpalaces.ts +157 -0
  115. package/src/types-entry.ts +10 -0
  116. package/src/types.ts +448 -0
  117. package/src/util.ts +97 -0
@@ -0,0 +1,162 @@
1
+ /**
2
+ * 对象之间的内部关联与「从星盘再发起计算」的统一入口,不对外导出。
3
+ *
4
+ * 绑定层调用无状态:宫位的文本、运限、格局、按盘取材等都要把星盘的排盘上下文
5
+ * (生辰、语言、用户传入的配置原件、重排起点)原样送回内核再算一次。
6
+ * 上下文只在这里拼一处;用户配置原件含自定义四化与亮度表,DTO 回显的配置不含,
7
+ * 拿 DTO 的配置再算会让覆盖表在第二跳静默丢失。
8
+ */
9
+
10
+ import type { Astrolabe } from './astrolabe.js';
11
+ import type { Horoscope } from './horoscope.js';
12
+ import type { KnowledgePack } from './knowledge.js';
13
+ import type { Palace } from './palace.js';
14
+ import { query } from './runtime.js';
15
+ import type { Star } from './star-object.js';
16
+ import type { ChartConfig, Knowledge, PatternConfig } from './types.js';
17
+
18
+ /** 星盘再发起计算所需、DTO 里没有的上下文 */
19
+ export interface ChartContext {
20
+ /** 排盘时用户传入的配置原件;未传时为 undefined,内核取默认 */
21
+ readonly config: ChartConfig | undefined;
22
+ /** 重排起点 [天干标识, 地支标识];非重排盘为 undefined */
23
+ readonly from: readonly [string, string] | undefined;
24
+ }
25
+
26
+ /** 星盘 → 再发起计算的上下文 */
27
+ export const chartContexts = new WeakMap<Astrolabe, ChartContext>();
28
+ /** 宫位 → 所属星盘 */
29
+ export const palaceCharts = new WeakMap<Palace, Astrolabe>();
30
+ /** 星耀 → 所在宫位 */
31
+ export const starPalaces = new WeakMap<Star, Palace>();
32
+ /** 星耀 → 所属星盘 */
33
+ export const starCharts = new WeakMap<Star, Astrolabe>();
34
+
35
+ /** 知识包的内部状态 */
36
+ export interface PackState {
37
+ /** 包对象本身(不复制) */
38
+ readonly raw: Record<string, unknown>;
39
+ /** 本实例就是该语言的内嵌包时记语言,发给内核时只发 "builtin" 而不搬整包;其余为 undefined */
40
+ readonly builtinLanguage: string | undefined;
41
+ }
42
+
43
+ /** 知识包实例 → 内部状态 */
44
+ export const packStates = new WeakMap<KnowledgePack, PackState>();
45
+
46
+ /** 取知识包的内部状态;不是本库构造的实例即报 TypeError */
47
+ export function packState(pack: KnowledgePack): PackState {
48
+ const state = packStates.get(pack);
49
+ if (state === undefined) {
50
+ throw new TypeError('knowledge must be true or a KnowledgePack built by x-iztro');
51
+ }
52
+ return state;
53
+ }
54
+
55
+ /**
56
+ * 把 `knowledge` 选项落成内核入参:`true` 与「就是本盘语言的内嵌包」的实例都发 "builtin"
57
+ * (内核自取,免得每次搬整包),其余实例发包对象,`false` / `null` / 省略不发。
58
+ *
59
+ * @throws TypeError 既不是布尔也不是 KnowledgePack
60
+ */
61
+ export function wireKnowledge(knowledge: Knowledge | undefined, language: string): unknown {
62
+ if (knowledge === undefined || knowledge === null || knowledge === false) return undefined;
63
+ if (knowledge === true) return 'builtin';
64
+ if (typeof knowledge !== 'object') {
65
+ throw new TypeError(`knowledge must be true or a KnowledgePack, got ${typeof knowledge}`);
66
+ }
67
+ const state = packState(knowledge);
68
+ return state.builtinLanguage === language ? 'builtin' : state.raw;
69
+ }
70
+
71
+ /** 再发起计算时的额外入参;`knowledge` 在这里换成内核入参,其余原样转发 */
72
+ export interface ContextParams {
73
+ [key: string]: unknown;
74
+ knowledge?: Knowledge | undefined;
75
+ patternConfig?: PatternConfig | undefined;
76
+ }
77
+
78
+ /** 以星盘的排盘上下文(含重排起点)调用统一查询入口 */
79
+ export function chartQuery(chart: Astrolabe, kind: string, extra: ContextParams = {}): unknown {
80
+ const context = chartContexts.get(chart);
81
+ if (context === undefined) {
82
+ throw new Error('this Astrolabe was not created by x-iztro');
83
+ }
84
+ const params: Record<string, unknown> = {
85
+ ...birthParams(chart, context),
86
+ ...extra,
87
+ };
88
+ if ('knowledge' in extra) {
89
+ params.knowledge = wireKnowledge(extra.knowledge, chart.language);
90
+ }
91
+ return query(kind, params);
92
+ }
93
+
94
+ /** 星盘的生辰与排盘上下文,作为内核入参 */
95
+ export function birthParams(chart: Astrolabe, context: ChartContext): Record<string, unknown> {
96
+ return {
97
+ solarDate: chart.solarDate,
98
+ timeIndex: chart.timeIndex,
99
+ gender: chart.genderKey,
100
+ fixLeap: chart.fixLeap,
101
+ language: chart.language,
102
+ config: context.config ?? chart.config,
103
+ fromStem: context.from?.[0],
104
+ fromBranch: context.from?.[1],
105
+ };
106
+ }
107
+
108
+ /**
109
+ * 发起运限的目标,原样记下供再发起计算时转发:日期(可带时间)与时辰,或农历年份(可再配农历月号)。
110
+ * DTO 里的 `solarDate` 是规范化后的、省略时辰时已丢掉时间部分,不能拿来代替。
111
+ */
112
+ export interface HoroscopeTarget {
113
+ /** 目标阳历日期串;按农历年发起的为 undefined */
114
+ readonly date?: string;
115
+ /** 目标时辰索引;undefined 表示由目标日期串换算 */
116
+ readonly timeIndex?: number;
117
+ /** 目标农历年份;按日期发起的为 undefined */
118
+ readonly year?: number;
119
+ /** 目标农历月号,与 year 成对;其余发起方式为 undefined */
120
+ readonly month?: number;
121
+ }
122
+
123
+ /** 运限再发起计算所需、DTO 里没有的上下文 */
124
+ export interface HoroscopeContext {
125
+ /** 发起这次运限的星盘 */
126
+ readonly chart: Astrolabe;
127
+ /** 发起时的运限目标 */
128
+ readonly target: HoroscopeTarget;
129
+ }
130
+
131
+ /** 运限 → 再发起计算的上下文 */
132
+ export const horoscopeContexts = new WeakMap<Horoscope, HoroscopeContext>();
133
+
134
+ /** 取运限的上下文;不是本库构造的实例即报错 */
135
+ export function horoscopeContext(horoscope: Horoscope): HoroscopeContext {
136
+ const context = horoscopeContexts.get(horoscope);
137
+ if (context === undefined) {
138
+ throw new Error('this Horoscope was not created by x-iztro');
139
+ }
140
+ return context;
141
+ }
142
+
143
+ /**
144
+ * 以发起运限的排盘上下文再发起计算:星盘上下文加上发起时的运限目标。
145
+ *
146
+ * @param astrolabe 显式指定的星盘;省略取发起这次运限的那张盘
147
+ */
148
+ export function horoscopeQuery(
149
+ horoscope: Horoscope,
150
+ kind: string,
151
+ extra: ContextParams,
152
+ astrolabe?: Astrolabe,
153
+ ): unknown {
154
+ const { chart, target } = horoscopeContext(horoscope);
155
+ return chartQuery(astrolabe ?? chart, kind, {
156
+ targetDate: target.date,
157
+ targetTimeIndex: target.timeIndex,
158
+ targetYear: target.year,
159
+ targetMonth: target.month,
160
+ ...extra,
161
+ });
162
+ }
@@ -0,0 +1,355 @@
1
+ /**
2
+ * 知识包:语言无关标识 → 解读文本与门派属性。
3
+ *
4
+ * 内核只负责事实判定;星耀怎么解读、格局意味着什么、宫位与四化的含义属于门派观点,全部放在知识包里。
5
+ * 格式见仓库 `knowledge/SCHEMA.md`。内核内嵌一份默认包(源自 iztro-docs《学习》页,MIT),
6
+ * 使用者可整包替换,或用覆盖包逐条合并;合并在内核完成,各语言绑定规则一致。
7
+ */
8
+
9
+ import { invalidArgument } from './errors.js';
10
+ import type { Language } from './enums.js';
11
+ import { chartQuery, horoscopeQuery, packState, packStates } from './internal.js';
12
+ import type { Astrolabe } from './astrolabe.js';
13
+ import type { Horoscope } from './horoscope.js';
14
+ import { query } from './runtime.js';
15
+ import type { PatternConfig } from './types.js';
16
+
17
+ /** 支持的知识包格式版本,与内核 `knowledge::SCHEMA_VERSION` 一致 */
18
+ const SCHEMA_VERSION = 1;
19
+
20
+ /** 包的来源与许可信息 */
21
+ export interface Source {
22
+ /** 来源名称(如 iztro-docs) */
23
+ name?: string;
24
+ /** 来源仓库或页面地址 */
25
+ url?: string;
26
+ /** 提取时锁定的来源 commit */
27
+ commit?: string;
28
+ /** 来源的许可协议(如 MIT) */
29
+ license?: string;
30
+ /** 来源作者 */
31
+ author?: string;
32
+ /** 提取日期 YYYY-MM-DD */
33
+ retrievedAt?: string;
34
+ /** 改写说明:文本经过整理改写时注明 */
35
+ adapted?: string;
36
+ }
37
+
38
+ /** 星耀的门派属性(全部可选) */
39
+ export interface StarAttributes {
40
+ /** 阴阳(yin / yang) */
41
+ yinYang?: string;
42
+ /** 五行(wood / fire / earth / metal / water) */
43
+ fiveElements?: string;
44
+ /** 五行所带天干(jia…gui) */
45
+ stem?: string;
46
+ /** 五行的补充说明 */
47
+ fiveElementsNote?: string;
48
+ /** 斗分 */
49
+ dipper?: string;
50
+ /** 化气 */
51
+ chemistry?: string;
52
+ /** 职业(主何事) */
53
+ career?: string;
54
+ /** 职务 */
55
+ duty?: string;
56
+ /** 别号 */
57
+ aliases?: string[];
58
+ /** 五行色 */
59
+ elementColor?: string;
60
+ /** 能量色 */
61
+ energyColor?: string;
62
+ }
63
+
64
+ /** 一颗星耀的知识条目 */
65
+ export interface StarEntry {
66
+ /** 星耀标识 */
67
+ key: string;
68
+ /** 该语言的显示名 */
69
+ name?: string;
70
+ /** 类别(major / minor / adjective / dec / flow) */
71
+ category?: string;
72
+ /** 分组(杂耀的分类、神煞的组别) */
73
+ group?: string;
74
+ /** 门派属性;包里没写时为空对象 */
75
+ attributes: StarAttributes;
76
+ /** 解读正文 */
77
+ intro?: string;
78
+ /** 正文的摘要(单段);正文不超过摘要层上限(显示宽度 240,中文 120 字)的条目不写 */
79
+ summary?: string;
80
+ /** 与另一颗主星同宫的组合解读,键为对方星耀标识;包里没写时为空对象 */
81
+ combinations: Record<string, string>;
82
+ /** 组合解读的摘要,与 combinations 同键成对;包里没写时为空对象 */
83
+ combinationSummaries: Record<string, string>;
84
+ }
85
+
86
+ /** 一条格局的知识条目 */
87
+ export interface PatternEntry {
88
+ /** 格局标识 */
89
+ key: string;
90
+ /** 该语言的显示名 */
91
+ name?: string;
92
+ /** 古籍引文 */
93
+ quotes?: string[];
94
+ /** 来源对成立条件的文字描述 */
95
+ conditions?: string;
96
+ /** 解读正文 */
97
+ intro?: string;
98
+ /** 正文的摘要(单段);正文不超过摘要层上限的条目不写 */
99
+ summary?: string;
100
+ }
101
+
102
+ /** 只有名称与正文的条目(宫位、四化) */
103
+ export interface TextEntry {
104
+ /** 标识 */
105
+ key: string;
106
+ /** 该语言的显示名 */
107
+ name?: string;
108
+ /** 正文 */
109
+ intro?: string;
110
+ /** 正文的摘要(单段);正文不超过摘要层上限的条目不写 */
111
+ summary?: string;
112
+ }
113
+
114
+ /** 术语与基础概念条目 */
115
+ export interface ConceptEntry {
116
+ /** 条目 slug */
117
+ slug: string;
118
+ /** 标题 */
119
+ title?: string;
120
+ /** 正文 */
121
+ intro?: string;
122
+ }
123
+
124
+ /** 解读主题(如婚恋、财运):读这类问题要看哪些宫,`Astrolabe.topicToText` 按它选宫 */
125
+ export interface TopicEntry {
126
+ /** 主题 slug */
127
+ slug: string;
128
+ /** 显示名 */
129
+ title?: string;
130
+ /** 连同各自三方四正一起取的宫位标识,按出现顺序渲染 */
131
+ surround?: string[];
132
+ /** 只取本宫的宫位标识(可含 bodyPalace / originalPalace),排在 surround 各组之后 */
133
+ palaces?: string[];
134
+ /** 这个主题看什么、为什么取这些宫 */
135
+ intro?: string;
136
+ /** 正文的摘要(单段);正文不超过摘要层上限的条目不写 */
137
+ summary?: string;
138
+ }
139
+
140
+ type Raw = Record<string, unknown>;
141
+
142
+ /** 包对象的格式校验,与内核解析同语义:非对象、未声明 schema、schema 比支持版本新都拒绝 */
143
+ function validateSchema(raw: unknown): Raw {
144
+ if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {
145
+ throw invalidArgument('knowledge pack must be a JSON object');
146
+ }
147
+ const schema = (raw as Raw).schema ?? 0;
148
+ if (typeof schema !== 'number' || !Number.isInteger(schema) || schema <= 0) {
149
+ throw invalidArgument('knowledge pack must declare "schema" (currently 1)');
150
+ }
151
+ if (schema > SCHEMA_VERSION) {
152
+ throw invalidArgument(`knowledge pack schema ${schema} is newer than supported ${SCHEMA_VERSION}`);
153
+ }
154
+ return raw as Raw;
155
+ }
156
+
157
+ /** 取包里某一段(stars、patterns……)的对象;缺段时为空对象 */
158
+ function section(raw: Raw, name: string): Record<string, Raw> {
159
+ return (raw[name] ?? {}) as Record<string, Raw>;
160
+ }
161
+
162
+ /** 取某一段里键为 `key` 的条目;只认包自己的键,`constructor`、`toString` 这类原型链上的名字不算 */
163
+ function entryOf(raw: Raw, name: string, key: string): Raw | undefined {
164
+ const sec = section(raw, name);
165
+ return Object.hasOwn(sec, key) ? sec[key] : undefined;
166
+ }
167
+
168
+ function starEntry(key: string, d: Raw): StarEntry {
169
+ return {
170
+ ...(d as Omit<StarEntry, 'key' | 'attributes' | 'combinations' | 'combinationSummaries'>),
171
+ key,
172
+ attributes: { ...((d.attributes ?? {}) as StarAttributes) },
173
+ combinations: { ...((d.combinations ?? {}) as Record<string, string>) },
174
+ combinationSummaries: { ...((d.combinationSummaries ?? {}) as Record<string, string>) },
175
+ };
176
+ }
177
+
178
+ /**
179
+ * 一份知识包。持有包对象,查询方法返回条目对象;`toJSON()` 是包对象的深拷贝,`JSON.stringify(pack)` 即包的 JSON。
180
+ *
181
+ * 构造:`KnowledgePack.builtin()` 取内嵌默认包,`fromObject` / `fromJSON` 读自己的包,
182
+ * `merged(...overlays)` 叠加覆盖包,`forAstrolabe` / `forHoroscope` 按盘取材成子包。
183
+ */
184
+ export class KnowledgePack {
185
+ /** 只能经静态构造方法创建,以保证包对象已通过格式校验 */
186
+ private constructor(raw: Raw, builtinLanguage: string | undefined) {
187
+ packStates.set(this, { raw, builtinLanguage });
188
+ }
189
+
190
+ // ------ 构造 ------
191
+
192
+ /**
193
+ * 内嵌的默认包(源自 iztro-docs,MIT)。
194
+ *
195
+ * @throws IztroError 该语言没有默认包(目前只有 zh-CN)
196
+ */
197
+ static builtin(language: Language = 'zh-CN'): KnowledgePack {
198
+ return new KnowledgePack(query('knowledgePack', { language }) as Raw, language);
199
+ }
200
+
201
+ /**
202
+ * 由包对象构造(保留引用,不复制)。
203
+ *
204
+ * @throws IztroError 未声明 schema 或格式版本过新(与内核解析同语义)
205
+ */
206
+ static fromObject(raw: object): KnowledgePack {
207
+ return new KnowledgePack(validateSchema(raw), undefined);
208
+ }
209
+
210
+ /**
211
+ * 由 JSON 文本构造。
212
+ *
213
+ * @throws IztroError 非对象、未声明 schema 或格式版本过新
214
+ * @throws SyntaxError JSON 语法非法
215
+ */
216
+ static fromJSON(text: string): KnowledgePack {
217
+ return new KnowledgePack(validateSchema(JSON.parse(text)), undefined);
218
+ }
219
+
220
+ /**
221
+ * 按本命盘取材的子包:只含盘上出现的星(十二宫主辅杂与四组十二神;同宫主星的双星组合解读保留,
222
+ * 不同宫的剔除)、按 config 口径命中的格局、四化四条;不含宫位与术语条目。
223
+ * 元信息沿用本包,结果仍是标准包,可再合并、序列化或按标识查。重排盘按重排后的布局取材。
224
+ */
225
+ forAstrolabe(chart: Astrolabe, config?: PatternConfig): KnowledgePack {
226
+ return new KnowledgePack(
227
+ chartQuery(chart, 'knowledgeForChart', { knowledge: this, patternConfig: config }) as Raw,
228
+ undefined,
229
+ );
230
+ }
231
+
232
+ /**
233
+ * 按运限取材的子包:在 `forAstrolabe` 之上再加各层流耀的条目与各层视角命中的格局,
234
+ * 只取到运限的查询精度(`Horoscope.precision()`)那一层。
235
+ */
236
+ forHoroscope(horoscope: Horoscope, config?: PatternConfig): KnowledgePack {
237
+ return new KnowledgePack(
238
+ horoscopeQuery(horoscope, 'knowledgeForChart', { knowledge: this, patternConfig: config }) as Raw,
239
+ undefined,
240
+ );
241
+ }
242
+
243
+ /** 包对象的深拷贝 */
244
+ toJSON(): Record<string, unknown> {
245
+ return structuredClone(packState(this).raw);
246
+ }
247
+
248
+ /**
249
+ * 以本包为底依次叠加覆盖包,返回新包(本包不变)。
250
+ *
251
+ * 规则见 knowledge/SCHEMA.md:覆盖包的非空字段覆盖同键条目的对应字段,attributes / combinations
252
+ * 逐字段合并,数组字段整体替换;覆盖条目换了正文却没给对应摘要时,合并结果去掉描述旧正文的摘要。
253
+ *
254
+ * @throws IztroError 某个包不符合格式或格式版本过新
255
+ */
256
+ merged(...overlays: (KnowledgePack | object)[]): KnowledgePack {
257
+ const packs = [packState(this).raw, ...overlays.map((o) => (o instanceof KnowledgePack ? packState(o).raw : o))];
258
+ return new KnowledgePack(query('mergeKnowledgePacks', { knowledgePacks: packs }) as Raw, undefined);
259
+ }
260
+
261
+ // ------ 元信息 ------
262
+
263
+ /** 格式版本 */
264
+ get schema(): number {
265
+ return Number(packState(this).raw.schema ?? 0);
266
+ }
267
+
268
+ /** 包标识 */
269
+ get id(): string {
270
+ return String(packState(this).raw.id ?? '');
271
+ }
272
+
273
+ /** 包版本 */
274
+ get version(): string {
275
+ return String(packState(this).raw.version ?? '');
276
+ }
277
+
278
+ /** 文本语言 */
279
+ get language(): string {
280
+ return String(packState(this).raw.language ?? '');
281
+ }
282
+
283
+ /** 覆盖包所覆盖的包标识;独立包为 undefined */
284
+ get extends(): string | undefined {
285
+ return packState(this).raw.extends as string | undefined;
286
+ }
287
+
288
+ /** 来源与许可 */
289
+ get source(): Source {
290
+ return { ...((packState(this).raw.source ?? {}) as Source) };
291
+ }
292
+
293
+ // ------ 查询 ------
294
+
295
+ /** 星耀条目(键为星耀标识) */
296
+ star(key: string): StarEntry | undefined {
297
+ const d = entryOf(packState(this).raw, 'stars', key);
298
+ return d === undefined ? undefined : starEntry(key, d);
299
+ }
300
+
301
+ /** 格局条目(键为格局标识) */
302
+ pattern(key: string): PatternEntry | undefined {
303
+ const d = entryOf(packState(this).raw, 'patterns', key);
304
+ return d === undefined ? undefined : { ...d, key };
305
+ }
306
+
307
+ /** 宫位条目(键为宫位标识) */
308
+ palace(key: string): TextEntry | undefined {
309
+ const d = entryOf(packState(this).raw, 'palaces', key);
310
+ return d === undefined ? undefined : { ...d, key };
311
+ }
312
+
313
+ /** 四化条目(键为四化标识) */
314
+ mutagen(key: string): TextEntry | undefined {
315
+ const d = entryOf(packState(this).raw, 'mutagens', key);
316
+ return d === undefined ? undefined : { ...d, key };
317
+ }
318
+
319
+ /** 术语条目 */
320
+ concept(slug: string): ConceptEntry | undefined {
321
+ const d = entryOf(packState(this).raw, 'concepts', slug);
322
+ return d === undefined ? undefined : { ...d, slug };
323
+ }
324
+
325
+ /** 主题条目 */
326
+ topic(slug: string): TopicEntry | undefined {
327
+ const d = entryOf(packState(this).raw, 'topics', slug);
328
+ return d === undefined ? undefined : { ...d, slug };
329
+ }
330
+
331
+ /** 全部主题条目 */
332
+ topics(): TopicEntry[] {
333
+ return Object.entries(section(packState(this).raw, 'topics')).map(([slug, d]) => ({ ...d, slug }));
334
+ }
335
+
336
+ /** 全部星耀条目 */
337
+ stars(): StarEntry[] {
338
+ return Object.entries(section(packState(this).raw, 'stars')).map(([key, d]) => starEntry(key, d));
339
+ }
340
+
341
+ /** 全部格局条目 */
342
+ patterns(): PatternEntry[] {
343
+ return Object.entries(section(packState(this).raw, 'patterns')).map(([key, d]) => ({ ...d, key }));
344
+ }
345
+
346
+ /** 星耀解读正文 */
347
+ starIntro(key: string): string | undefined {
348
+ return this.star(key)?.intro;
349
+ }
350
+
351
+ /** 格局解读正文 */
352
+ patternIntro(key: string): string | undefined {
353
+ return this.pattern(key)?.intro;
354
+ }
355
+ }
package/src/node.ts ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Node.js 入口(package.json `exports` 的 node 条件;Next.js 服务端渲染与路由处理也落在这里)。
3
+ *
4
+ * 与浏览器入口的 API 完全相同,区别只在默认取内核的方式:`file:` 地址读文件,Node 的 `fetch` 不支持该协议。
5
+ */
6
+
7
+ import { readFile } from 'node:fs/promises';
8
+ import { initialize, type WasmSource } from './runtime.js';
9
+
10
+ export * from './index.js';
11
+
12
+ /**
13
+ * 字符串来源里当作 URL 的协议;其余字符串一律是文件路径。不能用「能否解析成 URL」来判断:
14
+ * Windows 绝对路径 `C:\x\x_iztro.wasm` 会被解析成协议为 `c:` 的 URL。
15
+ */
16
+ const URL_SCHEME = /^(?:file|https?|data):/i;
17
+
18
+ /**
19
+ * 装载 WebAssembly 内核。其余 API 都是同步的,调用前必须等它完成。已装好时直接返回;
20
+ * 装载进行中时后到的调用共用那一次装载,自己传入的 `source` 不被使用。
21
+ *
22
+ * @param source 内核来源;省略时读包内的 x_iztro.wasm。字符串可以是 URL 或文件路径
23
+ * @throws IztroError 读文件或实例化失败(code 为 internal),可再次调用重试
24
+ */
25
+ export function init(source?: WasmSource): Promise<void> {
26
+ return initialize(source ?? new URL('./x_iztro.wasm', import.meta.url), async (location) => {
27
+ if (typeof location === 'string' && !URL_SCHEME.test(location)) return readFile(location);
28
+ const url = new URL(location);
29
+ return url.protocol === 'file:' ? readFile(url) : fetch(url);
30
+ });
31
+ }