Configuration and plugins ^2.3.0
Welcome to iztro’s configuration and plugin features. Zi Wei Dou Shu(紫微斗數) has many schools, with slight differences in four transformations(四化) and star brightness(星曜亮度). To accommodate these differences and support extensions, iztro introduced global configuration and third-party plugins in v2.3.0.
Tips
If you have developed an open-source plugin library for iztro, please contact the author so it can be listed in the documentation for others to use.
Configuration
See the configuration definition for the available settings. To set global defaults, call astro.config() before calling a chart-generation method:
import { astro } from 'iztro';
astro.config({
language: 'en-US',
mutagens: { "geng": ['sun', 'warrior', 'fortunate', 'minister'] },
brightness: {
"wolf": ['[+2]', '[+2]', '[+2]', '[+2]', '[+2]', '[+2]', '[+2]', '[+2]', '[+2]', '[+2]', '[+2]', '[+2]'],
},
});
const chart = astro.bySolar('2000-8-16', 2, 'female');
console.log(chart.toJSON());Scroll here to load the runnable example.
When you generate a chart after running the code above, the supplied settings are merged with the defaults and applied. You only need to specify values that differ from the defaults. See the four-transformations table for the ten Heavenly Stems(十天干四化表) and the star brightness table(星曜亮度表) for the default values.
Besides mutagens and brightness, the following options let you adjust chart-calculation rules:
| Configuration items | Allowed values | Default value | Description |
|---|---|---|---|
language ^2.6.5 | Language | zh-CN | Default output language; an explicit language parameter takes precedence |
yearDivide ^2.4.0 | normal | exact | normal | Year stem-and-branch boundary: normal uses the first day of the first lunar month; exact uses the Start of Spring solar term. |
horoscopeDivide ^2.4.3 | normal | exact | normal | Year and month stem-and-branch boundaries used for horoscope periods |
ageDivide ^2.4.5 | normal | birthday | normal | Nominal-age boundary, affecting decadal and annual age periods: normal uses Lunar New Year; birthday compares the lunar birthday. |
dayDivide ^2.5.2 | forward | current | forward | Date assignment for the late Zi hour: forward assigns it to the following day; current to the same day. |
algorithm ^2.5.0 | default | zhongzhou | default | Star-placement rules: default uses the standard version; zhongzhou uses the Zhongzhou school version. |
For the age formula, lunar-birthday boundary and a decadal palace-index example, see nominal age and palace indices.
From v2.6.4, dayDivide: 'forward' calculates the entire chart and subsequent time-cycle data using the following day's early Zi hour(早子時), updating the lunar month, year and leap-month status together. current uses the current day's early Zi hour(早子時).
solarDate, time, timeRange and sign retain the original Gregorian birth date, birth time range and zodiac sign. lunarDate, chineseDate and rawDates reflect the date used to calculate the chart. In time-cycle results, solarDate also retains the requested date.
astro.config({ language: 'en-US', dayDivide: 'forward' });
const late = astro.bySolar('1996-8-13', 12, 'female', true, 'en-US');
const early = astro.bySolar('1996-8-14', 0, 'female', true, 'en-US');
console.log(late.lunarDate === early.lunarDate); // true
console.log(late.fiveElementsClass === early.fiveElementsClass); // trueScroll here to load the runnable example.
Note
From v2.6.2, astro.config() sets defaults only for charts created afterward. withOptions({ config }) overrides settings only for the chart being created and does not alter the global defaults. Existing charts and their horoscope periods and transformation analyses retain the settings and language used when they were created. If older code relied on one charting call changing global settings, call astro.config() explicitly instead.
setLanguage() ^2.6.5
Use astro.config({ language: 'en-US' }) to set the default output language for future charts, date helpers and standalone utilities. The initial value is zh-CN. withOptions({ config: { language } }) overrides the language for that chart only; an explicit language parameter takes precedence. Individual calls do not change global defaults, and existing charts retain the language used to create them.
astro.setLanguage(language) and astro.config({ language }) update the same global language. This entry point remains available for existing callers. Supported values are zh-CN, zh-TW, en-US, ja-JP, ko-KR, and vi-VN. An invalid language throws an error and leaves the previous setting unchanged.
type setLanguage = (language: Language) => void;astro.getConfig().language returns the currently configured language. astro.resetConfig() restores the global language to zh-CN without changing existing charts or their subsequent queries.
import { astro, util } from 'iztro';
const options = { language: 'en-US' } as const;
astro.setLanguage(options.language);
console.log(astro.getConfig().language);
console.log(astro.getLanguage());
console.log(util.getMutagensByHeavenlyStem('geng'));
astro.resetConfig();Scroll here to load the runnable example.
getLanguage() ^2.6.5
Get the currently effective language. Inside chart methods or plugins, this returns the language used to create that chart. Outside a calculation context, it returns the global language.
type getLanguage = () => Language;getConfig() ^2.3.0
Purpose
Get the currently effective global configuration. The returned
^2.6.2 The returned value is an independent deep copy. Modifying it or its transformation and brightness arrays does not change the library’s configuration.mutagensandbrightnessuse normalized key names within the library, suitable for debugging or confirming configuration status.Definition
tstype getConfig = () => { language: Language; mutagens: Partial<Record<HeavenlyStemKey, StarKey[]>>; brightness: Partial<Record<StarKey, BrightnessKey[]>>; yearDivide: 'normal' | 'exact'; ageDivide: 'normal' | 'birthday'; dayDivide: 'current' | 'forward'; horoscopeDivide: 'normal' | 'exact'; algorithm: 'default' | 'zhongzhou'; };parameters
None
return value
The currently effective configuration object
Example
tsimport { astro } from 'iztro'; astro.config({ language: 'en-US', algorithm: 'zhongzhou', ageDivide: 'birthday' }); const currentConfig = astro.getConfig();Scroll here to load the runnable example.
resetConfig() ^2.6.2
Restores the default settings and clears all custom four-transformation(四化) and brightness tables. Existing charts are unaffected.
astro.getConfig().language returns the currently configured language. astro.resetConfig() restores the global language to zh-CN without changing existing charts or their subsequent queries.
type resetConfig = () => void;The default boundary rules are yearDivide: 'normal', horoscopeDivide: 'normal', ageDivide: 'normal', and dayDivide: 'forward'; the default star-placement algorithm is algorithm: 'default'. This method takes no arguments and returns no value. It does not reset loaded plugins.
const chart = astro.withOptions({
language: 'en-US',
dateStr: '2000-8-16',
timeIndex: 3,
gender: 'male',
config: { ageDivide: 'birthday' },
});
astro.resetConfig();
const horoscope = chart.horoscope('2026-3-1');
console.log({
defaultAgeDivide: astro.getConfig().ageDivide,
nominalAge: horoscope.age.nominalAge,
agePalace: horoscope.agePalace()?.name,
});Scroll here to load the runnable example.
Plugins
An iztro plugin is a function that attaches functionality to a chart(星盤) object. You can extend the chart to suit your needs. When developing in TypeScript, declare the plugin’s methods in an interface extending FunctionalAstrolabe.
The interface is declared as follows:
export interface IAstrolabe extends FunctionalAstrolabe {
myNewFunc: () => string;
majorStar: () => string;
}Define myNewFunc and majorStar to suit your needs. The interface’s methods can be implemented in a single plugin function or across several plugin functions:
// Define the first plugin function
export function myTestPlugin(this: IAstrolabe): void {
// Implement the plugin logic
this.myNewFunc = () => {
return this.fiveElementsClass;
};
}
// Define the second plugin function
export function myTestPlugin2(this: IAstrolabe): void {
// Implement the plugin logic
this.majorStar = () => {
let stars = this.palace('soul')
?.majorStars.filter((item) => item.type === 'major' && !['money', 'horse'].includes(item.name))
.map((item) => item.name)
.join(',');
if (!stars) {
stars = this.palace('surface')
?.majorStars.filter((item) => item.type === 'major' && !['money', 'horse'].includes(item.name))
.map((item) => item.name)
.join(',');
}
return stars ?? '';
};
}To register plugins, import astro and call loadPlugin() or loadPlugins(). Plugins registered by either method apply to chart instances created afterward.
import { astro } from 'iztro';
// Register multiple plugins at once
astro.loadPlugins([myTestPlugin, myTestPlugin2]);
// Register plugins one at a time
astro.loadPlugin(myTestPlugin);
astro.loadPlugin(myTestPlugin2);To apply a plugin only to a particular existing chart(星盤), call that chart instance’s use() method:
const astrolabe = astro.bySolar<IAstrolabe>('2023-10-18', 4, 'female', true, 'en-US');
astrolabe.use(myTestPlugin);
astrolabe.myNewFunc();Tips
When using plugins in TypeScript, specify the generic type so the compiler recognizes the added methods. To use the plugin methods above, for example:
const astrolabe = astro.bySolar<IAstrolabe>('2023-10-18', 4, 'female', true, 'en-US');
// Call the method defined by the plugin
astrolabe.myNewFunc();
// Call the method defined by the second plugin
astrolabe.majorStar();import { astro } from 'iztro';
interface IAstrolabe extends ReturnType<typeof astro.bySolar> {
myNewFunc: () => string;
}
function myTestPlugin(this: IAstrolabe) {
this.myNewFunc = () => this.fiveElementsClass;
}
astro.loadPlugins([myTestPlugin]);
const astrolabe = astro.bySolar<IAstrolabe>('2023-10-18', 4, 'female', true, 'en-US');
console.log(astrolabe.myNewFunc());Scroll here to load the runnable example.