The receipt carries a small chart record copied from the evidence card (natal positions with D9 signs, running Vimshottari and Narayana periods, slow transits with the next sign change); the page renders it as a folded set of tables. A first answer that reads the chart but stops without its 判断依据 section gets one continuation that writes only that section. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXp2Vp62Ur3C7yRWQdSq
173 lines
7.2 KiB
TypeScript
173 lines
7.2 KiB
TypeScript
/**
|
||
* 「盘面数据」 under an answer (owner 2026-11-16): the natal positions, the
|
||
* running periods and the slow transits this answer was written from, as a
|
||
* small record on the receipt. The page builds the tables from it; the model
|
||
* does not write them. Every value is copied from the evidence card.
|
||
*/
|
||
import { z } from "zod";
|
||
|
||
export const CONSULT_CHART_DATA_TITLE = "盘面数据";
|
||
|
||
const text = z.string().min(1).max(80);
|
||
const date = z.string().regex(/^\d{4}-\d{2}-\d{2}$/);
|
||
|
||
export const consultChartDataSchema = z.object({
|
||
natal: z.array(z.object({
|
||
body: text,
|
||
sign: text,
|
||
house: z.number().int().min(1).max(12).optional(),
|
||
degree: z.number().min(0).max(30).optional(),
|
||
retrograde: z.boolean().optional(),
|
||
d9: text.optional(),
|
||
}).strict()).max(12),
|
||
periods: z.array(z.object({
|
||
system: z.enum(["vimshottari", "narayana"]),
|
||
level: z.enum(["md", "ad", "pd"]),
|
||
lord: text,
|
||
start: date.optional(),
|
||
end: date.optional(),
|
||
}).strict()).max(8),
|
||
transits: z.array(z.object({
|
||
body: text,
|
||
sign: text,
|
||
degree: z.number().min(0).max(30).optional(),
|
||
house: z.number().int().min(1).max(12).optional(),
|
||
retrograde: z.boolean().optional(),
|
||
nextDate: date.optional(),
|
||
nextSign: text.optional(),
|
||
}).strict()).max(6),
|
||
parameters: z.object({
|
||
ayanamsa: text.optional(),
|
||
nodeMode: text.optional(),
|
||
referenceDate: date.optional(),
|
||
}).strict().optional(),
|
||
}).strict();
|
||
export type ConsultChartData = z.infer<typeof consultChartDataSchema>;
|
||
|
||
type Json = Record<string, unknown>;
|
||
const record = (value: unknown): Json => (value && typeof value === "object" && !Array.isArray(value) ? value as Json : {});
|
||
const str = (value: unknown): string | undefined => (typeof value === "string" && value.trim() ? value.trim() : undefined);
|
||
const num = (value: unknown): number | undefined => (typeof value === "number" && Number.isFinite(value) ? value : undefined);
|
||
const day = (value: unknown): string | undefined => {
|
||
const found = str(value)?.slice(0, 10);
|
||
return found && /^\d{4}-\d{2}-\d{2}$/.test(found) ? found : undefined;
|
||
};
|
||
const degree = (value: unknown): number | undefined => {
|
||
const found = num(value);
|
||
return found !== undefined && found >= 0 && found <= 30 ? Math.round(found * 100) / 100 : undefined;
|
||
};
|
||
const house = (value: unknown): number | undefined => {
|
||
const found = num(value);
|
||
return found !== undefined && Number.isInteger(found) && found >= 1 && found <= 12 ? found : undefined;
|
||
};
|
||
function compact<T extends Json>(row: T): T {
|
||
return Object.fromEntries(Object.entries(row).filter(([, value]) => value !== undefined)) as T;
|
||
}
|
||
|
||
const NATAL_ORDER = ["Sun", "Moon", "Mars", "Mercury", "Jupiter", "Venus", "Saturn", "Rahu", "Ketu"] as const;
|
||
const TRANSIT_ORDER = ["Saturn", "Jupiter", "Rahu", "Ketu"] as const;
|
||
|
||
/** The record for one answer, or null when the card has no natal positions. Card values only. */
|
||
export function consultChartDataFromCard(card: unknown): ConsultChartData | null {
|
||
const base = record(record(card).base);
|
||
const natal = record(base.natal);
|
||
const timing = record(base.timing);
|
||
const ascendant = record(natal.ascendant);
|
||
const planets = record(natal.planets);
|
||
const d9 = record(natal.d9);
|
||
const d9Planets = record(d9.planets);
|
||
const rows: ConsultChartData["natal"] = [];
|
||
const ascSign = str(ascendant.sign);
|
||
if (ascSign) {
|
||
rows.push(compact({ body: "Ascendant", sign: ascSign, house: 1, degree: degree(ascendant.degree_in_sign ?? ascendant.degree), d9: str(d9.lagna) }));
|
||
}
|
||
for (const name of NATAL_ORDER) {
|
||
const planet = record(planets[name]);
|
||
const sign = str(planet.sign);
|
||
if (!sign) continue;
|
||
rows.push(compact({
|
||
body: name,
|
||
sign,
|
||
house: house(planet.house),
|
||
degree: degree(planet.degree_in_sign),
|
||
retrograde: planet.retrograde === true ? true : undefined,
|
||
d9: str(record(d9Planets[name]).d9_sign),
|
||
}));
|
||
}
|
||
if (rows.length === 0) return null;
|
||
|
||
const periods: ConsultChartData["periods"] = [];
|
||
const vimshottari = record(timing.vimshottari);
|
||
for (const [key, level] of [["mahadasha", "md"], ["antardasha", "ad"], ["pratyantardasha", "pd"]] as const) {
|
||
const row = record(vimshottari[key]);
|
||
const lord = str(row.lord);
|
||
if (lord) periods.push(compact({ system: "vimshottari" as const, level, lord, start: day(row.start), end: day(row.end) }));
|
||
}
|
||
const narayana = record(timing.narayana);
|
||
// The card carries the Narayana dates inside one sentence (主运 … 起 至 止;子运 … 起 至 止).
|
||
const running = str(record(narayana.method_status).running) ?? "";
|
||
const spans = [...running.matchAll(/(\d{4}-\d{2}-\d{2}) 至 (\d{4}-\d{2}-\d{2})/g)];
|
||
for (const [index, [key, level]] of ([["md", "md"], ["ad", "ad"]] as const).entries()) {
|
||
const sign = str(record(narayana[key]).sign);
|
||
if (!sign) continue;
|
||
const span = spans[index];
|
||
periods.push(compact({ system: "narayana" as const, level, lord: sign, start: span?.[1], end: span?.[2] }));
|
||
}
|
||
|
||
const transitsDetail = record(timing.transits_detail);
|
||
const bodies = record(transitsDetail.bodies);
|
||
const keyDates = Array.isArray(transitsDetail.key_dates) ? transitsDetail.key_dates.map(record) : [];
|
||
const transits: ConsultChartData["transits"] = [];
|
||
for (const name of TRANSIT_ORDER) {
|
||
const body = record(bodies[name]);
|
||
const sign = str(body.sign);
|
||
if (!sign) continue;
|
||
const next = keyDates.find((row) => row.kind === "ingress" && String(row.body).toLowerCase() === name.toLowerCase());
|
||
transits.push(compact({
|
||
body: name,
|
||
sign,
|
||
degree: degree(body.degree_in_sign),
|
||
house: house(body.house_from_lagna),
|
||
retrograde: body.retrograde === true ? true : undefined,
|
||
nextDate: day(next?.date),
|
||
nextSign: next ? str(next.to_sign) : undefined,
|
||
}));
|
||
}
|
||
|
||
const parameters = record(base.parameters);
|
||
const parameterRow = compact({
|
||
ayanamsa: str(parameters.ayanamsa),
|
||
nodeMode: str(parameters.node_mode),
|
||
referenceDate: day(transitsDetail.reference_date),
|
||
});
|
||
const parsed = consultChartDataSchema.safeParse({
|
||
natal: rows,
|
||
periods,
|
||
transits,
|
||
...(Object.keys(parameterRow).length ? { parameters: parameterRow } : {}),
|
||
});
|
||
return parsed.success ? parsed.data : null;
|
||
}
|
||
|
||
const AYANAMSA_LABEL: Readonly<Record<string, string>> = { raman: "Raman", lahiri: "Lahiri", kp: "KP" };
|
||
const NODE_LABEL: Readonly<Record<string, string>> = { mean: "平均节点", true: "真实节点" };
|
||
export const CONSULT_CHART_PERIOD_SYSTEM: Readonly<Record<ConsultChartData["periods"][number]["system"], string>> = {
|
||
vimshottari: "Vimshottari",
|
||
narayana: "Narayana",
|
||
};
|
||
export const CONSULT_CHART_PERIOD_LEVEL: Readonly<Record<ConsultChartData["periods"][number]["level"], string>> = {
|
||
md: "大运",
|
||
ad: "小运",
|
||
pd: "细运",
|
||
};
|
||
|
||
/** One line under the title: 「Raman,整宫制,平均节点」. */
|
||
export function consultChartParameterLine(data: ConsultChartData): string {
|
||
const parts = [
|
||
data.parameters?.ayanamsa ? (AYANAMSA_LABEL[data.parameters.ayanamsa] ?? data.parameters.ayanamsa) : undefined,
|
||
"整宫制",
|
||
data.parameters?.nodeMode ? (NODE_LABEL[data.parameters.nodeMode] ?? data.parameters.nodeMode) : undefined,
|
||
].filter((part): part is string => Boolean(part));
|
||
return parts.join(",");
|
||
}
|