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
package/src/errors.ts ADDED
@@ -0,0 +1,29 @@
1
+ /**
2
+ * 内核报出的错误类别,与 Rust 核心 `IztroError` 的 code 一一对应:
3
+ * - `invalid_date`:日期格式非法、日期不存在(含改历空缺 1582-10-05 至 10-14),或超出 1-9999 年
4
+ * - `invalid_time_index`:时辰索引不在 0-12
5
+ * - `invalid_argument`:其余入参或配置非法,如未知的性别、语言、星耀标识
6
+ * - `internal`:库内部缺陷或 WebAssembly 实例故障(trap)
7
+ */
8
+ export type ErrorCode = 'invalid_date' | 'invalid_time_index' | 'invalid_argument' | 'internal';
9
+
10
+ /**
11
+ * 排盘与查询的失败。`message` 是内核给出的英文原文,`code` 供程序按类别判断。
12
+ *
13
+ * 调用方用错接口(如未初始化、传错参数类型)抛的是标准的 `Error` / `TypeError`,不是本类。
14
+ */
15
+ export class IztroError extends Error {
16
+ /** 机器可读的错误类别 */
17
+ readonly code: ErrorCode;
18
+
19
+ constructor(code: ErrorCode, message: string, options?: ErrorOptions) {
20
+ super(message, options);
21
+ this.name = 'IztroError';
22
+ this.code = code;
23
+ }
24
+ }
25
+
26
+ /** JS 侧校验失败时的 `IztroError`:code 为 invalid_argument,与内核同类报错同形 */
27
+ export function invalidArgument(message: string): IztroError {
28
+ return new IztroError('invalid_argument', message);
29
+ }
@@ -0,0 +1,418 @@
1
+ /**
2
+ * 运限。
3
+ *
4
+ * 大限、小限、流年、流月、流日、流时六个层级各有自己的宫名排布与四化,流耀按层级挂在十二宫上。
5
+ * 运限由星盘发起,持有发起它的那张盘;查询方法可免传星盘。
6
+ */
7
+
8
+ import { MUTAGEN_ORDER, Scope, type EarthlyBranch, type HeavenlyStem, type MonthPart, type Mutagen, type PalaceName } from './enums.js';
9
+ import { invalidArgument } from './errors.js';
10
+ import { horoscopeContext, horoscopeContexts, horoscopeQuery, type HoroscopeTarget } from './internal.js';
11
+ import type { Astrolabe } from './astrolabe.js';
12
+ import type { Palace } from './palace.js';
13
+ import { PatternHit } from './pattern.js';
14
+ import { Star, starIdentifiers } from './star-object.js';
15
+ import type { SurroundedPalaces } from './surpalaces.js';
16
+ import type {
17
+ AgeItemData,
18
+ DecadalListItemData,
19
+ HoroscopeData,
20
+ HoroscopeItemData,
21
+ HoroscopeTextOptions,
22
+ HoroscopeYearlyData,
23
+ KnowledgeOptions,
24
+ MonthlyListItemData,
25
+ PatternConfig,
26
+ PatternHitData,
27
+ YearlyDecStar,
28
+ YearlyListItemData,
29
+ } from './types.js';
30
+
31
+ /** 层级深浅:本命 0、大限 1 … 流时 5,越大越细;与内核 `Scope::depth` 一致 */
32
+ const SCOPE_DEPTH: readonly Scope[] = [
33
+ Scope.ORIGIN,
34
+ Scope.DECADAL,
35
+ Scope.YEARLY,
36
+ Scope.MONTHLY,
37
+ Scope.DAILY,
38
+ Scope.HOURLY,
39
+ ];
40
+
41
+ /** 格局文本的选项 */
42
+ export interface PatternTextOptions extends KnowledgeOptions {
43
+ /** 判定口径;省略取默认 */
44
+ config?: PatternConfig | undefined;
45
+ }
46
+
47
+ /** 运限的一层(大限、小限、流年、流月、流日、流时)与各运限列表项的共同部分 */
48
+ export class HoroscopeItem {
49
+ /** 该层命宫所在的宫位索引 0-11 */
50
+ readonly index: number;
51
+ /** 层级显示名(大限/童限/小限/流年/流月/流日/流时,按排盘语言翻译) */
52
+ readonly name: string;
53
+ /**
54
+ * 层级标识。大限层未起运时为 childhood(童限)而非 decadal:两者是不同的解盘语义,
55
+ * 程序判断一律用本字段而非译文。
56
+ */
57
+ readonly nameKey: string;
58
+ /** 该层天干(翻译文本) */
59
+ readonly heavenlyStem: string;
60
+ /** 该层天干标识 */
61
+ readonly heavenlyStemKey: HeavenlyStem;
62
+ /** 该层地支(翻译文本) */
63
+ readonly earthlyBranch: string;
64
+ /** 该层地支标识 */
65
+ readonly earthlyBranchKey: EarthlyBranch;
66
+ /** 该层十二宫名(翻译文本,按宫位索引排列) */
67
+ readonly palaceNames: readonly string[];
68
+ /** 该层十二宫名标识 */
69
+ readonly palaceNameKeys: readonly PalaceName[];
70
+ /** 四化星名 [禄, 权, 科, 忌](翻译文本) */
71
+ readonly mutagen: readonly string[];
72
+ /**
73
+ * 被化的四颗星标识 [禄, 权, 科, 忌],与 `Palace.mutagenStarKeys` 同义;
74
+ * 单数的 `mutagenKey` 才是四化类型 `Mutagen` 的标识。
75
+ */
76
+ readonly mutagenStarKeys: readonly string[];
77
+ /** 流耀:十二宫各一组;该层没有流耀时为 undefined */
78
+ readonly stars: readonly (readonly Star[])[] | undefined;
79
+
80
+ constructor(data: HoroscopeItemData) {
81
+ this.index = data.index;
82
+ this.name = data.name;
83
+ this.nameKey = data.nameKey;
84
+ this.heavenlyStem = data.heavenlyStem;
85
+ this.heavenlyStemKey = data.heavenlyStemKey;
86
+ this.earthlyBranch = data.earthlyBranch;
87
+ this.earthlyBranchKey = data.earthlyBranchKey;
88
+ this.palaceNames = [...data.palaceNames];
89
+ this.palaceNameKeys = [...data.palaceNameKeys];
90
+ this.mutagen = [...data.mutagen];
91
+ this.mutagenStarKeys = [...data.mutagenStarKeys];
92
+ this.stars = data.stars?.map((group) => group.map((s) => new Star(s)));
93
+ }
94
+
95
+ /** 宫名标识(或当前语言宫名)在该层所在的宫位索引;找不到为 undefined */
96
+ palaceIndexByName(name: PalaceName | string): number | undefined {
97
+ for (let i = 0; i < this.palaceNameKeys.length; i++) {
98
+ if (name === this.palaceNameKeys[i] || name === this.palaceNames[i]) return i;
99
+ }
100
+ return undefined;
101
+ }
102
+ }
103
+
104
+ /** 小限 */
105
+ export class AgeItem extends HoroscopeItem {
106
+ /** 虚岁 */
107
+ readonly nominalAge: number;
108
+
109
+ constructor(data: AgeItemData) {
110
+ super(data);
111
+ this.nominalAge = data.nominalAge;
112
+ }
113
+ }
114
+
115
+ /** 流年(含流年十二神) */
116
+ export class HoroscopeYearly extends HoroscopeItem {
117
+ /** 流年十二神:岁前、将前十二神按目标年支排布 */
118
+ readonly yearlyDecStar: Readonly<YearlyDecStar>;
119
+
120
+ constructor(data: HoroscopeYearlyData) {
121
+ super(data);
122
+ this.yearlyDecStar = structuredClone(data.yearlyDecStar);
123
+ }
124
+ }
125
+
126
+ /** 大限列表项:一整个大限,含起止虚岁与起止农历年份 */
127
+ export class DecadalListItem extends HoroscopeItem {
128
+ /** 该大限所在的本命宫名(按排盘语言翻译) */
129
+ readonly palaceName: string;
130
+ /** 本命宫名标识 */
131
+ readonly palaceNameKey: PalaceName;
132
+ /** 起止虚岁 [起, 止],含两端 */
133
+ readonly ageRange: readonly [number, number];
134
+ /** 起止农历年份 [起, 止],含两端 */
135
+ readonly yearRange: readonly [number, number];
136
+
137
+ constructor(data: DecadalListItemData) {
138
+ super(data);
139
+ this.palaceName = data.palaceName;
140
+ this.palaceNameKey = data.palaceNameKey;
141
+ this.ageRange = [data.ageRange[0], data.ageRange[1]];
142
+ this.yearRange = [data.yearRange[0], data.yearRange[1]];
143
+ }
144
+ }
145
+
146
+ /** 流年列表项:某个大限内的一个流年 */
147
+ export class YearlyListItem extends HoroscopeItem {
148
+ /** 该流年的虚岁 */
149
+ readonly age: number;
150
+ /** 农历年份 */
151
+ readonly year: number;
152
+
153
+ constructor(data: YearlyListItemData) {
154
+ super(data);
155
+ this.age = data.age;
156
+ this.year = data.year;
157
+ }
158
+ }
159
+
160
+ /** 流月列表项:某个农历年的一个流月段 */
161
+ export class MonthlyListItem extends HoroscopeItem {
162
+ /** 该流月的虚岁 */
163
+ readonly age: number;
164
+ /** 农历年份 */
165
+ readonly year: number;
166
+ /** 农历月份(正月为 1);闰月与同月号的常规月共用本字段,以 isLeapMonth 区分 */
167
+ readonly month: number;
168
+ /** 本段是否属于闰月 */
169
+ readonly isLeapMonth: boolean;
170
+ /** 分段:整月、闰月前半、闰月后半 */
171
+ readonly part: MonthPart;
172
+ /** 本段覆盖的农历日区间 [起, 止],含两端 */
173
+ readonly dayRange: readonly [number, number];
174
+
175
+ constructor(data: MonthlyListItemData) {
176
+ super(data);
177
+ this.age = data.age;
178
+ this.year = data.year;
179
+ this.month = data.month;
180
+ this.isLeapMonth = data.isLeapMonth;
181
+ this.part = data.part;
182
+ this.dayRange = [data.dayRange[0], data.dayRange[1]];
183
+ }
184
+ }
185
+
186
+ /** 运限 */
187
+ export class Horoscope {
188
+ /** 目标农历日期 */
189
+ readonly lunarDate: string;
190
+ /** 目标阳历日期 `Y-M-D` */
191
+ readonly solarDate: string;
192
+ /** 大限(未起运时为童限) */
193
+ readonly decadal: HoroscopeItem;
194
+ /** 小限 */
195
+ readonly age: AgeItem;
196
+ /** 流年 */
197
+ readonly yearly: HoroscopeYearly;
198
+ /** 流月 */
199
+ readonly monthly: HoroscopeItem;
200
+ /** 流日 */
201
+ readonly daily: HoroscopeItem;
202
+ /** 流时 */
203
+ readonly hourly: HoroscopeItem;
204
+ readonly #data: HoroscopeData;
205
+
206
+ /** 由内核 DTO、发起它的星盘与发起目标构造;通常经 `Astrolabe.horoscope*` 取得 */
207
+ constructor(data: HoroscopeData, chart: Astrolabe, target: HoroscopeTarget) {
208
+ this.#data = data;
209
+ horoscopeContexts.set(this, { chart, target });
210
+ this.lunarDate = data.lunarDate;
211
+ this.solarDate = data.solarDate;
212
+ this.decadal = new HoroscopeItem(data.decadal);
213
+ this.age = new AgeItem(data.age);
214
+ this.yearly = new HoroscopeYearly(data.yearly);
215
+ this.monthly = new HoroscopeItem(data.monthly);
216
+ this.daily = new HoroscopeItem(data.daily);
217
+ this.hourly = new HoroscopeItem(data.hourly);
218
+ }
219
+
220
+ /** 运限的 DTO(与 iztro 的 JSON 同形)的深拷贝 */
221
+ toJSON(): HoroscopeData {
222
+ return structuredClone(this.#data);
223
+ }
224
+
225
+ /**
226
+ * 运限的语义化文本:面向语言模型与人的完整描述(Markdown 子集,按排盘语言输出)。
227
+ *
228
+ * @throws IztroError `knowledge: true` 而排盘语言没有内嵌包;`knowledgeDepth: 'summary'` 而未给 knowledge
229
+ * 或包缺摘要;`through` 为 origin、不是运限层级,或比查询精度细
230
+ */
231
+ toText(options: HoroscopeTextOptions = {}): string {
232
+ return horoscopeQuery(this, 'horoscopeToText', {
233
+ knowledge: options.knowledge,
234
+ detail: options.detail,
235
+ knowledgeDepth: options.knowledgeDepth,
236
+ through: options.through,
237
+ patternConfig: options.config,
238
+ }) as string;
239
+ }
240
+
241
+ /** 不带释义的语义化文本,同 `toText()` */
242
+ toString(): string {
243
+ return this.toText();
244
+ }
245
+
246
+ /** 发起这次运限查询的星盘 */
247
+ astrolabe(): Astrolabe {
248
+ return horoscopeContext(this).chart;
249
+ }
250
+
251
+ /**
252
+ * 查询精度:目标实际指定到的最细层级。`horoscopeOfYear` 为 yearly,`horoscopeOfMonth` 为 monthly,
253
+ * 按日期发起的为 hourly。
254
+ *
255
+ * 比它细的层级是内核替调用方挑的代表日的,不是查询的答案:`toText` 默认只写到这一层,
256
+ * 宫位查询、文本与格局的层级参数比它细时报 `IztroError`。
257
+ */
258
+ precision(): Scope {
259
+ const { target } = horoscopeContext(this);
260
+ if (target.year === undefined) return Scope.HOURLY;
261
+ if (target.month === undefined) return Scope.YEARLY;
262
+ return Scope.MONTHLY;
263
+ }
264
+
265
+ /** 指定层级的运限项;origin 或未知层级为 undefined(小限不按层级取,见 `age`) */
266
+ scopeItem(scope: Scope): HoroscopeItem | undefined {
267
+ switch (scope) {
268
+ case Scope.DECADAL:
269
+ return this.decadal;
270
+ case Scope.YEARLY:
271
+ return this.yearly;
272
+ case Scope.MONTHLY:
273
+ return this.monthly;
274
+ case Scope.DAILY:
275
+ return this.daily;
276
+ case Scope.HOURLY:
277
+ return this.hourly;
278
+ default:
279
+ return undefined;
280
+ }
281
+ }
282
+
283
+ /** 小限所在的宫位;不传星盘取发起本次查询的那张盘 */
284
+ agePalace(astrolabe?: Astrolabe): Palace | undefined {
285
+ return (astrolabe ?? this.astrolabe()).palaces[this.age.index];
286
+ }
287
+
288
+ /**
289
+ * 指定运限层级下的宫位。
290
+ *
291
+ * @param name 宫名标识或当前语言的宫名
292
+ * @param scope 运限层级;origin 按本命宫名取
293
+ * @param astrolabe 星盘;不传取发起本次查询的那张盘
294
+ * @throws IztroError scope 不是层级标识,或比查询精度细
295
+ */
296
+ palace(name: PalaceName | string, scope: Scope, astrolabe?: Astrolabe): Palace | undefined {
297
+ this.#checkScope(scope);
298
+ const chart = astrolabe ?? this.astrolabe();
299
+ if (scope === Scope.ORIGIN) return chart.palace(name);
300
+ const idx = this.scopeItem(scope)?.palaceIndexByName(name);
301
+ return idx === undefined ? undefined : chart.palaces[idx];
302
+ }
303
+
304
+ /**
305
+ * 指定运限层级下某宫的三方四正。
306
+ *
307
+ * @throws IztroError 同 `palace`
308
+ */
309
+ surroundPalaces(name: PalaceName | string, scope: Scope, astrolabe?: Astrolabe): SurroundedPalaces | undefined {
310
+ const chart = astrolabe ?? this.astrolabe();
311
+ const p = this.palace(name, scope, chart);
312
+ return p === undefined ? undefined : chart.surroundedPalaces(p.index);
313
+ }
314
+
315
+ /**
316
+ * 指定运限层级下某宫是否有该层的运限四化(看主星与辅星);origin 恒为 false。
317
+ *
318
+ * @throws IztroError 同 `palace`
319
+ */
320
+ hasHoroscopeMutagen(name: PalaceName | string, scope: Scope, mutagen: Mutagen, astrolabe?: Astrolabe): boolean {
321
+ this.#checkScope(scope);
322
+ if (scope === Scope.ORIGIN) return false;
323
+ const item = this.scopeItem(scope);
324
+ const p = this.palace(name, scope, astrolabe);
325
+ if (item === undefined || p === undefined) return false;
326
+ const starKey = item.mutagenStarKeys[MUTAGEN_ORDER.indexOf(mutagen)];
327
+ return starKey !== undefined && [...p.majorStars, ...p.minorStars].some((s) => s.key === starKey);
328
+ }
329
+
330
+ /**
331
+ * 指定运限宫位是否包含指定的 **全部** 流耀(星耀标识或翻译名);流耀取大限与流年两层。
332
+ *
333
+ * @throws IztroError 同 `palace`
334
+ */
335
+ hasHoroscopeStars(name: PalaceName | string, scope: Scope, stars: readonly string[], astrolabe?: Astrolabe): boolean {
336
+ const p = this.palace(name, scope, astrolabe);
337
+ if (p === undefined) return false;
338
+ const ids = this.#flowStarIdentifiers(p.index);
339
+ return stars.every((s) => ids.has(s));
340
+ }
341
+
342
+ /**
343
+ * 指定运限宫位是否包含指定流耀中的 **任意一颗**。
344
+ *
345
+ * @throws IztroError 同 `palace`
346
+ */
347
+ hasOneOfHoroscopeStars(name: PalaceName | string, scope: Scope, stars: readonly string[], astrolabe?: Astrolabe): boolean {
348
+ const p = this.palace(name, scope, astrolabe);
349
+ if (p === undefined) return false;
350
+ const ids = this.#flowStarIdentifiers(p.index);
351
+ return stars.some((s) => ids.has(s));
352
+ }
353
+
354
+ /**
355
+ * 指定运限宫位是否 **一颗都不** 包含指定的流耀。
356
+ *
357
+ * @throws IztroError 同 `palace`
358
+ */
359
+ notHaveHoroscopeStars(name: PalaceName | string, scope: Scope, stars: readonly string[], astrolabe?: Astrolabe): boolean {
360
+ const p = this.palace(name, scope, astrolabe);
361
+ if (p === undefined) return false;
362
+ const ids = this.#flowStarIdentifiers(p.index);
363
+ return stars.every((s) => !ids.has(s));
364
+ }
365
+
366
+ /**
367
+ * 某运限层视角的格局命中:以该层命宫为命宫、合并该层流曜与四化后重跑全部规则,
368
+ * 另加两条行运格(禄衰马困、风云际会);origin 等同本命盘。
369
+ *
370
+ * @throws IztroError scope 不是层级标识,或比查询精度细
371
+ */
372
+ patterns(scope: Scope, config?: PatternConfig, astrolabe?: Astrolabe): PatternHit[] {
373
+ const data = horoscopeQuery(this, 'horoscopePatterns', { scope, patternConfig: config }, astrolabe) as PatternHitData[];
374
+ return data.map((d) => new PatternHit(d));
375
+ }
376
+
377
+ /**
378
+ * 某运限层视角格局命中的语义化文本;宫名按该层重排后的宫名书写。
379
+ *
380
+ * @throws IztroError 同 `patterns`,以及 knowledge / knowledgeDepth 的报错(见 `toText`)
381
+ */
382
+ patternsToText(scope: Scope, options: PatternTextOptions = {}, astrolabe?: Astrolabe): string {
383
+ return horoscopeQuery(
384
+ this,
385
+ 'horoscopePatternsToText',
386
+ {
387
+ scope,
388
+ patternConfig: options.config,
389
+ knowledge: options.knowledge,
390
+ knowledgeDepth: options.knowledgeDepth,
391
+ },
392
+ astrolabe,
393
+ ) as string;
394
+ }
395
+
396
+ /** 宫位查询的 scope 须是层级标识,且不比查询精度细;报错正文与内核同类校验相同 */
397
+ #checkScope(scope: Scope): void {
398
+ const depth = SCOPE_DEPTH.indexOf(scope);
399
+ if (depth < 0) {
400
+ const keys = SCOPE_DEPTH.map((s) => `'${s}'`);
401
+ throw invalidArgument(`invalid scope '${scope}': expected ${keys.slice(0, -1).join(', ')} or ${keys.at(-1)}`);
402
+ }
403
+ const precision = this.precision();
404
+ if (depth > SCOPE_DEPTH.indexOf(precision)) {
405
+ throw invalidArgument(
406
+ `invalid scope '${scope}': the horoscope target only specifies layers down to '${precision}'`,
407
+ );
408
+ }
409
+ }
410
+
411
+ /** 大限与流年两层在指定宫位的流耀标识 */
412
+ #flowStarIdentifiers(palaceIndex: number): Set<string> {
413
+ return starIdentifiers([
414
+ ...(this.decadal.stars?.[palaceIndex] ?? []),
415
+ ...(this.yearly.stars?.[palaceIndex] ?? []),
416
+ ]);
417
+ }
418
+ }
package/src/i18n.ts ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * 标识与译名的双向查找。
3
+ *
4
+ * 星盘对象的每个字段都同时给出译名与 `*Key` 标识,通常不必再手工翻译;这三个函数用于手上只有标识
5
+ * (或只有某种语言的译名)、需要换算的场合。覆盖星耀、宫位(含身宫、来因宫)、天干、地支、亮度、四化、
6
+ * 五行局、性别、生肖、时辰、星座、运限层级,合计 260 个标识。
7
+ */
8
+
9
+ import type { Language } from './enums.js';
10
+ import { query } from './runtime.js';
11
+
12
+ /** 标识译成指定语言的文本;未知标识为 undefined */
13
+ export function translate(key: string, language: Language = 'zh-CN'): string | undefined {
14
+ return (query('translate', { key, language }) as string | null) ?? undefined;
15
+ }
16
+
17
+ /**
18
+ * 由任意语言的译名反查标识(iztro 的 `kot`);查不到为 undefined。
19
+ *
20
+ * 逐语言、每种语言内逐标识比对取先命中者,顺序与 iztro 一致。同一译名在多个类目下同形时
21
+ * (en-US 的 "horse" 既是生肖马也是天马),用 `keyFilter` 限定标识名须含的子串来消歧:
22
+ * "Maj" 只看十四主星、"Min" 只看辅星、"Heavenly" / "Earthly" 只看干支、"Palace" 只看宫位、"Hour" 只看时辰。
23
+ * 限定后无匹配为 undefined,不退回未限定的结果。
24
+ */
25
+ export function keyOf(text: string, keyFilter?: string): string | undefined {
26
+ return (query('keyOf', { text, keyFilter }) as string | null) ?? undefined;
27
+ }
28
+
29
+ /**
30
+ * 全部 260 个可翻译标识。顺序即 `keyOf` 的反查次序,与 iztro 各语言翻译文件的合并次序一致:
31
+ * 运限层级、生肖、时辰、星座、五行局、天干、地支、亮度、四化、星耀、宫位、性别。
32
+ */
33
+ export function allKeys(): string[] {
34
+ return query('allKeys') as string[];
35
+ }
package/src/index.ts ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * 两个入口(`node.ts`、`browser.ts`)共有的公开 API;入口只在此之上各自提供 `init`。
3
+ *
4
+ * 模块划分与 iztro 对应:`astro`(排盘与轻量查询)、`util`、`star`、`data`,另有 `i18n` 与知识包、格局、反推。
5
+ */
6
+
7
+ export * as astro from './astro.js';
8
+ export * as util from './util.js';
9
+ export * as star from './star.js';
10
+ export * as data from './data.js';
11
+ export * as i18n from './i18n.js';
12
+
13
+ export * from './enums.js';
14
+ export { IztroError, type ErrorCode } from './errors.js';
15
+ export type { WasmSource } from './runtime.js';
16
+ export { isInitialized } from './runtime.js';
17
+
18
+ export { Astrolabe } from './astrolabe.js';
19
+ export { Palace, type Decadal, type PalaceTarget } from './palace.js';
20
+ export { RawChineseDate, type RawDates } from './raw-dates.js';
21
+ export { Star } from './star-object.js';
22
+ export { SurroundedPalaces, FlankingPalaces } from './surpalaces.js';
23
+ export {
24
+ Horoscope,
25
+ HoroscopeItem,
26
+ AgeItem,
27
+ HoroscopeYearly,
28
+ DecadalListItem,
29
+ YearlyListItem,
30
+ MonthlyListItem,
31
+ type PatternTextOptions,
32
+ } from './horoscope.js';
33
+ export { PatternHit, PatternStar } from './pattern.js';
34
+ export {
35
+ KnowledgePack,
36
+ type Source,
37
+ type StarAttributes,
38
+ type StarEntry,
39
+ type PatternEntry,
40
+ type TextEntry,
41
+ type ConceptEntry,
42
+ type TopicEntry,
43
+ } from './knowledge.js';
44
+ export {
45
+ solarDatesByBazi,
46
+ reverseChart,
47
+ type Pillar,
48
+ type BirthCandidate,
49
+ type StarPosition,
50
+ type ReverseCriteria,
51
+ type ReverseResult,
52
+ } from './reverse.js';
53
+ export type { SolarOptions, LunarOptions, SoulAndBody, DecadalsAndAges, Plugin } from './astro.js';
54
+ export type { BirthParam, RearrangeableBirthParam, YearlyStarIndex } from './star.js';
55
+ export type { StarInfo, HeavenlyStemInfo, EarthlyBranchInfo, Constants } from './data.js';
56
+ export type * from './types.js';