dsh-metaphysics 0.50.1 → 0.55.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/engine/diff.js CHANGED
@@ -12,6 +12,15 @@ const IGNORE_KEYS = new Set([
12
12
  'trueSolarText', 'civilText', 'offsetMinutes',
13
13
  'input', // 只是把调用参数回显出来,不是盘面变化
14
14
  'jd', // 内部儒略日,人类不可读;对外展示用 time 等格式化字段
15
+ // 奇门把「这次用了哪个取局流派 / 晚子时归哪一天」写在结果里;换了开关它必然变,
16
+ // 但它只是口径回显,不是盘面。不剔掉的话,两张完全相同的盘会被报成「2 处差异」。
17
+ 'juMethod', 'juMethodNote', 'lateZiHourMode',
18
+ // 紫微把「闰月按哪种政策起盘」回显在 lunar 里;非闰月盘换政策时它也会变,
19
+ // 但农历月份与宫位一字未动,不该报成差异。
20
+ 'leapPolicy',
21
+ // 九类流派开关的回显(含 rules.schools 那行说明)同理:换开关必然改这段话,
22
+ // 真正要看的是星位、四化、宫位有没有动。
23
+ 'schools',
15
24
  ]);
16
25
 
17
26
  const isObject = (v) => v !== null && typeof v === 'object';
@@ -53,7 +62,30 @@ const LABELS = [
53
62
  [/^pillars\.\w+\.dishi$|^pillars\.\w+\.zizuo$/, '十二长生'],
54
63
  [/^pillars\.\w+\.nayin$/, '纳音'],
55
64
  [/^dayMaster\./, '日主'],
65
+ [/^palaces\.(taiyuan|minggong|shengong)\./, '命身宫'],
66
+ [/^palaces\.\d+\.(palace|branch|stem|ganzhi)$/, '宫位'],
67
+ [/^palaces\.\d+\.stars\.\d+\.(name|brightness|type)$/, '星曜'],
68
+ [/^palaces\.\d+\.shensha\./, '神煞'],
69
+ [/^palaces\.\d+\.(daxian|xiaoxian)\./, '大限'],
70
+ [/^palaces\.\d+\.sanfang\./, '三方四正'],
71
+ [/^palaces\.\d+\./, '宫位'],
56
72
  [/^palaces\./, '命身宫'],
73
+ [/^soul\./, '命宫'],
74
+ [/^body\./, '身宫'],
75
+ [/^doujun\./, '流年斗君'],
76
+ [/^siZhu\.year$/, '年柱'],
77
+ [/^siZhu\.month$/, '月柱'],
78
+ [/^siZhu\.day$/, '日柱'],
79
+ [/^siZhu\.time$/, '时柱'],
80
+ [/^ju$|^juNumber$/, '局数'],
81
+ [/^yuan$/, '三元'],
82
+ [/^dun$/, '阴阳遁'],
83
+ [/^daxian(List)?\./, '大限'],
84
+ [/^fly(Chains|Composite)?\./, '飞星'],
85
+ [/^pattern/, '格局'],
86
+ [/^schools\./, '流派回显'],
87
+ [/^dayun\.jiao\./, '起运'],
88
+ [/^dayun\.toNextJieDays$/, '起运时间'],
57
89
  [/^dayun\.list\.(\d+)\.ganzhi$/, '大运干支'],
58
90
  [/^dayun\.startAge\./, '起运时间'],
59
91
  [/^dayun\./, '大运评分'],
@@ -63,7 +95,6 @@ const LABELS = [
63
95
  [/^dayGanZhi$/, '日辰'],
64
96
  [/^hourZhi$/, '时支'],
65
97
  [/^yueJiang\./, '月将'],
66
- [/^ju(Num|Name)?$/, '局数'],
67
98
  [/^xunKong$/, '旬空'],
68
99
  [/^lines\.(\d+)\./, '爻'],
69
100
  [/^shensha\./, '神煞'],
@@ -85,7 +116,15 @@ const CATEGORY = [
85
116
  [/^wuxing\./, '五行计数'],
86
117
  [/^dayMaster\./, '日主'],
87
118
  [/^relations\./, '干支关系'],
119
+ [/^palaces\.(taiyuan|minggong|shengong)\./, '命身宫'],
120
+ [/^palaces\.\d+\./, '宫位'],
88
121
  [/^palaces\./, '命身宫'],
122
+ [/^soul\.|^body\.|^doujun\./, '命身宫'],
123
+ [/^siZhu\./, '四柱'],
124
+ [/^ju$|^juNumber$|^yuan$|^dun$/, '局数'],
125
+ [/^daxian/, '大限'],
126
+ [/^fly/, '飞星'],
127
+ [/^pattern/, '格局'],
89
128
  [/^jieqi\./, '节气'],
90
129
  [/^solarTime\./, '真太阳时'],
91
130
  [/^input\./, '输入回显'],
@@ -98,9 +137,15 @@ const CATEGORY = [
98
137
  */
99
138
  const ROOT = [
100
139
  [/^pillars\.(year|month|day|hour)\.ganzhi$/, '柱'],
140
+ [/^palaces\.(minggong|shengong)\.ganzhi$/, '命身宫'],
141
+ [/^soul\.ganzhi$/, '命宫'],
142
+ [/^body\.branch$/, '身宫'],
143
+ [/^siZhu\.(year|month|day|time)$/, '柱'],
144
+ [/^dayun\.jiao\.at$/, '起运'],
101
145
  [/^dayGanZhi$/, '日辰'],
102
146
  [/^hourZhi$/, '时支'],
103
- [/^ju(Num|Name)?$/, '局数'],
147
+ [/^ju$|^juNumber$|^juName$/, '局数'],
148
+ [/^dun$/, '阴阳遁'],
104
149
  [/^lines\.\d+\.(ganzhi|yinYang|name)$/, '爻'],
105
150
  [/^yueJiang\.(name|zhi)$/, '月将'],
106
151
  ];
@@ -141,7 +186,7 @@ export function summarize(diffs) {
141
186
  const cats = [...new Set(diffs.map((d) => categoryOf(d.path)).filter(Boolean))];
142
187
  const head = roots.length
143
188
  ? `根因 ${roots.length} 处:` + roots.slice(0, 4)
144
- .map((d) => `${labelOf(d.path)} ${fmt(d.from)}→${fmt(d.to)}`).join('、')
189
+ .map((d) => `${labelOf(d.path)} ${formatValue(d.from)}→${formatValue(d.to)}`).join('、')
145
190
  + (roots.length > 4 ? ' 等' : '')
146
191
  : '无单一根因';
147
192
  const tail = cats.length
@@ -150,7 +195,7 @@ export function summarize(diffs) {
150
195
  return head + tail + '。';
151
196
  }
152
197
 
153
- const fmt = (v) => {
198
+ export const formatValue = (v) => {
154
199
  if (v === undefined) return '—';
155
200
  if (v === null) return 'null';
156
201
  if (typeof v === 'string') return v;
@@ -175,14 +220,14 @@ export function renderDiff(diffs, { title = '', limit = 12 } = {}) {
175
220
  lines.push('');
176
221
  lines.push(' 根因(口径直接改动):');
177
222
  for (const d of roots) {
178
- lines.push(` ${labelOf(d.path).padEnd(6)} ${fmt(d.from)} → ${fmt(d.to)}`);
223
+ lines.push(` ${labelOf(d.path).padEnd(6)} ${formatValue(d.from)} → ${formatValue(d.to)}`);
179
224
  }
180
225
  }
181
226
  if (rest.length) {
182
227
  lines.push('');
183
228
  lines.push(` 连带变化(${rest.length} 处,展示前 ${Math.min(limit, rest.length)} 处):`);
184
229
  for (const d of rest.slice(0, limit)) {
185
- lines.push(` ${labelOf(d.path).padEnd(6)} ${fmt(d.from)} → ${fmt(d.to)}`);
230
+ lines.push(` ${labelOf(d.path).padEnd(6)} ${formatValue(d.from)} → ${formatValue(d.to)}`);
186
231
  }
187
232
  if (rest.length > limit) {
188
233
  const byCat = {};
@@ -195,3 +240,108 @@ export function renderDiff(diffs, { title = '', limit = 12 } = {}) {
195
240
  }
196
241
  return lines.join('\n');
197
242
  }
243
+
244
+ /**
245
+ * 紫微九类流派开关:read 同时认平铺参数与 schools 嵌套,
246
+ * 非法值按引擎规则回落默认;alt 在默认与代表档之间来回切。
247
+ */
248
+ const ziweiSchoolSwitch = (key, title, short, def, alt, labels) => ({
249
+ key,
250
+ title,
251
+ short,
252
+ read: (a) => {
253
+ const raw = a[key] !== undefined ? a[key] : (a.schools && a.schools[key]);
254
+ return labels[raw] ? raw : def;
255
+ },
256
+ alt: (v) => (v === alt ? def : alt),
257
+ label: (v) => labels[v] || v,
258
+ });
259
+
260
+ /**
261
+ * 口径开关表:每个体系 2–3 组「只差一个维度」的对照。
262
+ * read 取当前口径(没传时按引擎默认),alt 给另一口径,label 是给人看的短名。
263
+ * UI、CLI 与工具参数 diff 共用这一份,默认值不在别处再抄一遍。
264
+ */
265
+ export const CALIBER_SWITCHES = {
266
+ bazi: [
267
+ {
268
+ key: 'lateZi',
269
+ title: '晚子时:23:00 后是否换日',
270
+ short: '晚子时',
271
+ read: (a) => a.lateZi !== false,
272
+ alt: (v) => !v,
273
+ label: (v) => (v ? '换日' : '不换日'),
274
+ },
275
+ {
276
+ key: 'trueSolar',
277
+ title: '真太阳时:是否按出生地经度修正',
278
+ short: '真太阳时',
279
+ read: (a) => a.trueSolar !== false,
280
+ alt: (v) => !v,
281
+ label: (v) => (v ? '修正' : '不修正'),
282
+ },
283
+ ],
284
+ ziwei: [
285
+ {
286
+ key: 'leapMonth',
287
+ title: '闰月归月:按日切分 / 整月归次月',
288
+ short: '闰月归月',
289
+ read: (a) => (a.leapMonth === 'next' || a.leapMonth === 'prev' ? a.leapMonth : 'split'),
290
+ alt: (v) => (v === 'split' ? 'next' : 'split'),
291
+ label: (v) => ({ split: '按日切分', next: '整月归次月', prev: '整月归本月' }[v] || v),
292
+ },
293
+ {
294
+ key: 'trueSolar',
295
+ title: '真太阳时:是否按出生地经度修正',
296
+ short: '真太阳时',
297
+ read: (a) => a.trueSolar !== false,
298
+ alt: (v) => !v,
299
+ label: (v) => (v ? '修正' : '不修正'),
300
+ },
301
+ ziweiSchoolSwitch('brightnessSchool', '亮度表:默认 / QS 七档', '亮度表', 'default', 'QS', {
302
+ default: '默认', QS: 'QS(七档)', XD1: 'XD1(七档)', ZZ: 'ZZ(五档)', XD2: 'XD2(五档)',
303
+ }),
304
+ ziweiSchoolSwitch('gengYearSihua', '庚干四化:阳武阴同 / 同科阴忌', '庚干四化', 'GYWYT', 'GYWTY', {
305
+ GYWYT: '阳武阴同', GYWTY: '同科阴忌', GYWFT: '府科同忌', GYWFX: '府科相忌', GYWTX: '同科相忌',
306
+ }),
307
+ ziweiSchoolSwitch('tianmaSchool', '天马:按年支 / 按生月', '天马', 'year-branch', 'month-branch', {
308
+ 'year-branch': '年支', 'month-branch': '生月',
309
+ }),
310
+ ziweiSchoolSwitch('tiankongSchool', '天空:不排 / 年支顺行', '天空', 'off', 'normal', {
311
+ off: '不排', normal: '年支顺行', 'shun-jia-shi': '加生时',
312
+ }),
313
+ ziweiSchoolSwitch('jiekongSchool', '截空旬空:不排 / 正副双星', '截空旬空', 'off', 'double', {
314
+ off: '不排', single: '单星', double: '正副双星', zhanyan: '占验派',
315
+ }),
316
+ ziweiSchoolSwitch('kuiyueSchool', '魁钺:qs1 / qs2', '魁钺', 'qs1', 'qs2', {
317
+ qs1: 'qs1', qs2: 'qs2', 'zhong-yiming': '中一鸣', network: '网络',
318
+ }),
319
+ ziweiSchoolSwitch('tianshiTianshang', '天伤天使:常规 / 中州互换', '天伤天使', 'normal', 'zhongzhou', {
320
+ normal: '常规', zhongzhou: '中州(阴男阳女互换)',
321
+ }),
322
+ ziweiSchoolSwitch('changshengSchool', '长生:阴阳顺逆 / 火土起寅', '长生', 'yin-yang-shun-ni', 'huo-tu', {
323
+ 'yin-yang-shun-ni': '阴阳顺逆', 'shui-tu': '水二', 'huo-tu': '火土(土五局起寅)',
324
+ }),
325
+ ziweiSchoolSwitch('lateZishi', '晚子时:归当日 / 归次日', '晚子时', 'current-day', 'next-day', {
326
+ 'current-day': '归当日', 'next-day': '归次日', 'all-current': '全归当日', 'day-cur-time-next': '日归当日·时归次日',
327
+ }),
328
+ ],
329
+ qimen: [
330
+ {
331
+ key: 'juMethod',
332
+ title: '取局流派:拆补 / 超神接气 / 置闰',
333
+ short: '取局流派',
334
+ read: (a) => (['chaibu', 'chaoshen', 'zhirun'].includes(a.juMethod) ? a.juMethod : 'chaibu'),
335
+ alt: (v) => (v === 'chaibu' ? 'zhirun' : 'chaibu'),
336
+ label: (v) => ({ chaibu: '拆补法', chaoshen: '超神接气', zhirun: '置闰法' }[v] || v),
337
+ },
338
+ {
339
+ key: 'lateZiHourMode',
340
+ title: '晚子时:23:00 后归当日 / 次日',
341
+ short: '晚子时归日',
342
+ read: (a) => (a.lateZiHourMode === 'next-day' ? 'next-day' : 'same-day'),
343
+ alt: (v) => (v === 'next-day' ? 'same-day' : 'next-day'),
344
+ label: (v) => (v === 'next-day' ? '归次日' : '归当日'),
345
+ },
346
+ ],
347
+ };
@@ -0,0 +1,76 @@
1
+ // 影子盘指纹:同一组归一化参数 + 同一引擎版本 → 同一个短 ID。
2
+ //
3
+ // 两层 ID:
4
+ // baseId —— 只含参数(system / 归一化参数 / seed / asOf),跨版本不变;
5
+ // id —— 再叠引擎版本,版本一变 ID 就变。
6
+ // 版本升级后重排同一张盘,baseId 相同、id 不同,据此自动对盘。
7
+ //
8
+ // 这里只做纯函数:不读时间、不读随机、不读 locale,保证换机器/换时区结果一致。
9
+
10
+ const ALPHABET = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'; // Crockford base32(去掉 I/L/O/U)
11
+
12
+ /** 稳定序列化:键排序、NFC、去 undefined、-0 → 0;不依赖 locale。 */
13
+ export function canonicalJson(value) {
14
+ return JSON.stringify(sortValue(value));
15
+ }
16
+
17
+ function sortValue(value) {
18
+ if (value === null) return null;
19
+ const t = typeof value;
20
+ if (t === 'string') return typeof value.normalize === 'function' ? value.normalize('NFC') : value;
21
+ if (t === 'number') return Number.isFinite(value) ? (Object.is(value, -0) ? 0 : value) : null;
22
+ if (t === 'boolean') return value;
23
+ if (Array.isArray(value)) return value.map(sortValue);
24
+ if (t === 'object') {
25
+ const out = {};
26
+ for (const key of Object.keys(value).sort()) {
27
+ const item = value[key];
28
+ if (item === undefined || typeof item === 'function' || typeof item === 'symbol') continue;
29
+ out[key] = sortValue(item);
30
+ }
31
+ return out;
32
+ }
33
+ return null;
34
+ }
35
+
36
+ /** 35 位短哈希,编码成 7 个 base32 字符;纯字符运算,跨 Node / 浏览器一致。 */
37
+ export function shortHash(text) {
38
+ const s = String(text);
39
+ let h1 = 0x811c9dc5;
40
+ let h2 = 0x9e3779b9;
41
+ for (let i = 0; i < s.length; i += 1) {
42
+ const code = s.charCodeAt(i);
43
+ for (const byte of [code & 0xff, code >>> 8]) {
44
+ h1 = Math.imul(h1 ^ byte, 0x01000193);
45
+ h2 = Math.imul(h2 ^ byte, 0x85ebca6b);
46
+ }
47
+ }
48
+ let n = (h1 >>> 0) * 8 + (h2 >>> 29); // < 2^35,Number 精确
49
+ let out = '';
50
+ for (let i = 0; i < 7; i += 1) {
51
+ out = ALPHABET[n % 32] + out;
52
+ n = Math.floor(n / 32);
53
+ }
54
+ return out;
55
+ }
56
+
57
+ /**
58
+ * @param {{system:string, params:object, seed?:number|null, asOf?:string|null, version?:string}} input
59
+ * @returns {{id:string, baseId:string, version:string, system:string, payload:string}}
60
+ */
61
+ export function fingerprintOf(input = {}) {
62
+ const system = String(input.system || '');
63
+ const version = String(input.version || 'dev');
64
+ const seed = input.seed === undefined || input.seed === null || input.seed === '' ? null : Number(input.seed);
65
+ const asOf = input.asOf ? String(input.asOf) : null;
66
+ const params = input.params && typeof input.params === 'object' ? input.params : {};
67
+ const basePayload = canonicalJson({ system, params, seed, asOf });
68
+ const fullPayload = canonicalJson({ base: basePayload, version });
69
+ return {
70
+ id: `LY-${shortHash(fullPayload)}`,
71
+ baseId: `LY-${shortHash(basePayload)}`,
72
+ version,
73
+ system,
74
+ payload: basePayload,
75
+ };
76
+ }