I Ching
Import
import { getIChingReading, getHexagram, getHexagramDescription, castThreeCoins, formatRecord, getIChingReadingContent, getHexagramText, formatRecordContent,} from 'kaabalah/iching';kaabalah/iching is pure TypeScript. It does not load the ephemeris.
What you store
A reading is six classical line values, bottom to top. Store those six values. Everything else is derived.
| Value | Line | Moves |
|---|---|---|
| 6 | old yin | yes |
| 7 | young yang | no |
| 8 | young yin | no |
| 9 | old yang | yes |
Line 1 is the bottom line. Line 6 is the top. Binary strings use the same order, with yang = 1.
A reading
import { getIChingReading, linesFromHexagram } from 'kaabalah/iching';
const reading = getIChingReading('879777');
// The same cast, named by hexagram and moving lines:getIChingReading(30, [2, 4]);getIChingReading({ hexagramNumber: 30, movingLines: [2, 4] });
linesFromHexagram(30, [2, 4]); // [7, 6, 7, 9, 8, 7]getIChingReading returns:
lines,lineCode, and sortedmovingLinescastandtransformed.transformedisnullwhen no line moves- on each state:
hexagram,han,nuclear,kaabalah, andsovereign lineTransforms, with both the destination-palace relative and the cast-palace relativeoracle, the texts Zhu Xi’s rule selects
With six moving lines, 乾 uses 用九 and 坤 uses 用六. Other casts use the transformed judgment. The Great Image stays separate from the judgment.
getIChingReading does not interpret. It does not include getHexagramDescription.
Invalid input throws RangeError. Duplicate moving positions are rejected.
Hexagrams
HEXAGRAMS is King Wen order. getHexagram(number) and HEXAGRAMS_BY_NUMBER use the number as identity. Romanizations are display only: 履 and 旅 are both Lü, and the Wade-Giles spellings Ch’ien and Chien are easy to mix up.
import { getHexagram, getHexagramByTrigrams, getHexagramName } from 'kaabalah/iching';
const creative = getHexagram(1);creative.hanzi; // 乾creative.text.judgment; // { chinese, english }creative.text.image;creative.text.lines; // six lines, bottom firstcreative.lower; // trigramcreative.upper;
getHexagramByTrigrams('qian', 'qian'); // lower trigram firstgetHexagramName(52, 'pt'); // "a montanha"getHexagramName(52, 'pt-BR'); // samegetHexagramName(52); // EnglishgetHexagramByBinary takes a bottom-first binary string. TRIGRAMS and getTrigram(idOrBinary) give the eight trigrams: Chinese name, image, family, Later Heaven direction, and phase.
Plain descriptions
getHexagramDescription is plain words for the judgment, the image, and the picture the two trigrams make. It is not the classical text.
import { getHexagramDescription } from 'kaabalah/iching';
getHexagramDescription(1); // EnglishgetHexagramDescription(1, 'pt'); // Portuguese, including pt-BRgetHexagramDescription(65); // undefinedHEXAGRAM_DESCRIPTIONS is the frozen record of all 64. An unknown number returns undefined.
The rest of a figure
These are also on the reading. Call them alone when you already have a number.
| Function | What it returns |
|---|---|
getNuclearHexagram(number) | Lines 2/3/4 below, lines 3/4/5 above. Only 乾 and 坤 nucleate into themselves. |
getPalace(number) | Palace trigram, generation, 世 and 應 |
getHanHexagram(number) | 納甲 stem and branch, phase, 六親, and the 世/應 relation |
getLineTransforms(input) | Before and after for each moving line. after.relative uses the destination palace. afterInCastPalace.relative uses the cast palace. |
selectZhuXiTexts(input) | The judgment, line texts, or 用九 / 用六 the oracle speaks |
getHexagramKaabalah(number) | Gate, family sphere, ruling Ark Annu, and the three pillars |
getSovereignHexagram(number) | 十二辟卦 lunar month, or null. The solar window is an approximate range, not a calculated solar term. |
getTrigramGate, getFamilySphere, getTrigramPillars, and listHexagramGates expose the Kaabalah layer one trigram at a time. Five phases stay independent of Tree spheres and of western elements.
Portuguese
All 514 classical texts (64 judgments, 64 images, 384 lines, 用九 and 用六) have a Portuguese rendering. The Chinese and the literal English stay on every text; the localized variants add a content field that holds the selected language.
import { getHexagramText, getIChingReadingContent, getOracleTextContent, formatRecordContent, getHexagramName,} from 'kaabalah/iching';
getHexagramText(25, 'pt-BR')?.judgment;// { chinese, english, content: 'Grande sucesso; a constância é favorável. ...' }
const reading = getIChingReadingContent('879777', 'pt-BR');reading.oracle[0].text.content; // Portuguese text of the selected oracle linereading.cast.hexagram.text.judgment; // each hexagram's text also carries `content`
formatRecordContent('879777', 'pt-BR', { date: '15-11-2026', question: 'Qual o caminho?' });getHexagramName(52, 'pt-BR'); // "a montanha"pt and pt-BR select Portuguese. Any other value, or none, selects English, so content equals the English text. getIChingReadingContent selects the same oracle texts as getIChingReading (the Zhu Xi rules are unchanged) and only adds content; getOracleTextContent(selection, language) applies the language to one selected text. formatRecordContent is formatRecord with Portuguese labels and hexagram names, and ICHING_RECORD_LABELS and HEXAGRAM_NAMES_PT expose the underlying tables. Unknown hexagram numbers return undefined from getHexagramText.
Lower-level helpers
parseCastLines(input) validates a cast (string, array, or { hexagramNumber, movingLines }) and returns the six values. getMovingLines, getCastHexagram, and getTransformedHexagram derive the three basic facts from any accepted input, and getSixRelative, getBranchRelation, and getPhaseRelation are the 六親 and 沖/合 relation tables. LINE_POSITIONS, PALACE_TABLE, PHASE_HANZI, SOVEREIGN_HEXAGRAMS, and HEXAGRAM_TEXTS (the raw bilingual text rows) are exported as frozen data.
Casting
import { castThreeCoins, formatRecord } from 'kaabalah/iching';
const lines = castThreeCoins(); // three coins, six linesconst fixed = castThreeCoins(() => 0.25); // your RNG, for tests
formatRecord(lines, { date: '15-11-2026', question: '(daily reading)' });Each line draws three coins. Tails counts 2, heads counts 3. That is 18 calls to the RNG. The RNG must return finite numbers in [0, 1). The odds are 1/8, 3/8, 3/8, 1/8 for 6, 7, 8, 9. Nothing in the cast is seeded from a question or a date.
CLI
kaabalah iching:cast --json --compactkaabalah iching:hexagram 25 --json --compactkaabalah iching:reading --lines 879777 --json --compactkaabalah iching:reading --hexagram 30 --moving 2,4 --json --compactkaabalah iching:reading --input-json='{"hexagramNumber":25,"movingLines":[4]}' --json --compactkaabalah iching:reading --lines 879777 --language pt-BR --no-json| Command | Library call | Notes |
|---|---|---|
iching:reading | getIChingReading, or getIChingReadingContent with --language pt-BR | --lines, or --hexagram with --moving; or --input-json with lines / hexagramNumber and movingLines |
iching:hexagram <n> | getHexagram plus the Han, nuclear, Kaabalah, and sovereign layers | Returns { hexagram, han, nuclear, kaabalah, sovereign } |
iching:cast | castThreeCoins, then the same reading | The only non-deterministic command |
Pass lines as a six-digit string or as a six-number array (through --input-json). Do not pass lines and a hexagram together. JSON stdout is one document. These commands do not start the ephemeris. --language pt-BR adds the Portuguese content to the classical texts and prints human output in Portuguese; JSON keys and error codes stay in English. Bad input exits with code 1 and INVALID_ARGUMENT (missing input is MISSING_ARGUMENT). See the CLI reference for flags and output fields.
Sources
The Chinese is the Zhou Yi judgment, the Great Image, and the line texts, from Kanripo KR1a0001 at a pinned revision. 彖傳, 小象, and 文言 are not included. The English on hexagram.text was written from that Chinese. It is a literal rendering, not a forecast.
The plain descriptions are a separate layer, for showing a reading in ordinary words.