Charting
init loads the core, bySolar / byLunar cast the chart, ChartConfig configures it.
import { init, astro } from 'x-iztro';
await init();
const chart = astro.bySolar('2000-8-16', 2, 'female');Every entry point throws IztroError on invalid input, with a machine-readable .code; see Errors.
init
Purpose Load the WebAssembly core. Every other API is synchronous and must wait for it.
Signature
function init(source?: WasmSource): Promise<void>;
type WasmSource =
| URL | string // wasm address; in Node, file: URLs and file paths are read from disk, others fetched
| Response | Promise<Response> // a request already made
| ArrayBuffer | ArrayBufferView // the wasm bytes
| WebAssembly.Module; // an already compiled moduleParameters
| Name | Type | Required | Description |
|---|---|---|---|
source | WasmSource | no | Where the core comes from; omitted, the packaged x_iztro.wasm (new URL('./x_iztro.wasm', import.meta.url)). In Node a string starting with file:, http(s): or data: is a URL and anything else a file path (Windows absolute paths included) |
Behaviour
- Returns immediately once loaded; calls made while a load is in progress share that load, and their own
sourceis not used. - A response with
Content-Type: application/wasmis compiled while it downloads (instantiateStreaming); anything else is read in full and then compiled, with the same result. - A failure to fetch, compile or instantiate throws
IztroError(codeinternal, reason incause); calling again retries. - If the core ever stops mid-call (a trap, a stack overflow, a corrupt result buffer), that call throws
internaland the instance is retired;init()again rebuilds it. Neither valid nor invalid input causes this.
isInitialized() tells whether the core is currently usable.
ChartConfig
Chart configuration: six switches plus two optional override tables. Every field is optional and defaults
to iztro's value, so {} is the same as passing nothing.
| Field | Type | Default | Values |
|---|---|---|---|
yearDivide | YearDivide | 'normal' | normal lunar new year / exact Lichun |
horoscopeDivide | HoroscopeDivide | 'normal' | normal first of the month / exact solar term |
ageDivide | AgeDivide | 'normal' | normal at new year / birthday on the birthday |
dayDivide | DayDivide | 'forward' | forward late Rat hour belongs to the next day / current to the same day |
algorithm | Algorithm | 'default' | default / zhongzhou |
astroType | AstroType | 'heaven' | heaven / earth / human chart |
mutagens | Partial<Record<HeavenlyStem, [string, string, string, string]>> | — | stem key → four star keys (lu, quan, ke, ji), replacing that stem's row |
brightness | Partial<Record<string, (Brightness | '')[]>> | — | star key → twelve brightness keys, index 0 is Yin, empty string for none |
The meaning of each switch and its school background are in Configuration.
const earth = astro.bySolar('2000-8-16', 2, 'female', {
config: {
astroType: 'earth',
mutagens: { gengHeavenly: ['taiyangMaj', 'wuquMaj', 'tianfuMaj', 'tiantongMaj'] },
},
});
console.log(earth.fiveElementsClass, earth.config.astroType);
console.log(earth.palace('soulPalace')!.mutagenStarKeys);Output
土五局 earth
[ 'tiantongMaj', 'tianjiMaj', 'wenchangMin', 'lianzhenMaj' ]Edge cases
bySolar
Purpose Cast a natal chart from a solar date. For the Zi Wei meaning see the Python reference.
Signature
function bySolar(
solarDate: string,
timeIndex: TimeIndex,
gender: Gender,
options?: { fixLeap?: boolean; language?: Language; config?: ChartConfig },
): Astrolabe;Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
solarDate | string | yes | — | 'Y-M-D', separator - / or ., optional HH:mm(:ss) (the time is ignored), years 1–9999 |
timeIndex | TimeIndex | yes | — | Hour index 0–12; 0 early Rat, 12 late Rat |
gender | Gender | yes | — | 'male' / 'female' |
options.fixLeap | boolean | no | true | First half of a leap month counts as that month, second half as the next |
options.language | Language | no | 'zh-CN' | Display language |
options.config | ChartConfig | no | defaults | See above |
The optional arguments after gender go in an object: with two booleans and a language code side by side,
positional arguments are easy to swap without any error.
const chart = astro.bySolar('2000-8-16', 2, 'female');
console.log(chart.solarDate, chart.chineseDate, chart.time, chart.timeRange);
console.log(chart.soul, chart.body, chart.fiveElementsClass);
const en = astro.bySolar('2000-8-16', 2, 'female', { language: 'en-US' });
console.log(en.lunarDate, en.soul, en.palace('soulPalace')!.name);Output
2000-8-16 庚辰 甲申 丙午 庚寅 寅时 03:00~05:00
破军 文昌 木三局
Lunar 2000-7-17 rebel soulEdge cases
byLunar
Purpose Cast a chart from a lunar date.
Signature
function byLunar(
lunarDate: string,
timeIndex: TimeIndex,
gender: Gender,
options?: { isLeapMonth?: boolean; fixLeap?: boolean; language?: Language; config?: ChartConfig },
): Astrolabe;options.isLeapMonth defaults to false and has no effect when the month has no leap month; the rest is as in bySolar.
const lunar = astro.byLunar('2000-7-17', 2, 'female');
console.log(lunar.solarDate, lunar.lunarDate);Output
2000-8-16 二〇〇〇年七月十七Horoscopes and rearranging
Horoscopes, rearranging and semantic text are astrolabe methods, see Astrolabe:
chart.horoscope(date, timeIndex), chart.horoscopeOfYear(year), chart.horoscopeOfMonth(year, month),
chart.rearranged(stem, branch), chart.toText(options).