Skip to content

Commit 67ba955

Browse files
committed
🛠️ 修复星耀位置错误及获取主星方法逻辑错误 #269
1 parent 40fcfa2 commit 67ba955

File tree

5 files changed

+122
-18
lines changed

5 files changed

+122
-18
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
- 🛠️ 修复(fix)
88
- 🧹 琐事(Chore)
99

10+
## v2.5.6
11+
12+
- 🛠️ 修复(fix)
13+
14+
- 天使、天伤、天才三颗杂耀在地盘和人盘中位置错误。
15+
- 命宫主星计算错误。#269
16+
1017
## v2.5.5
1118

1219
- 🛠️ 修复(fix)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "iztro",
3-
"version": "2.5.5",
3+
"version": "2.5.6",
44
"description": "轻量级紫微斗数星盘生成库。可以通过出生年月日获取到紫微斗数星盘信息、生肖、星座等信息。This is a lightweight kit for generating astrolabes for Zi Wei Dou Shu (The Purple Star Astrology), an ancient Chinese astrology. It allows you to obtain your horoscope and personality analysis.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/__tests__/astro/astro.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,10 @@ describe('Astrolabe', () => {
908908
expect(getMajorStarBySolarDate('2023-4-7', 0, true, 'ko-KR')).toEqual('탐랑');
909909
});
910910

911+
test('getMajorStarBySolarDate()', () => {
912+
expect(getMajorStarBySolarDate('1987-05-16', 7)).toEqual('天机,天梁');
913+
});
914+
911915
test('getMajorStarByLunarDate() leap month', () => {
912916
expect(getMajorStarByLunarDate('2023-2-17', 0)).toEqual('紫微,贪狼');
913917
expect(getMajorStarByLunarDate('2023-2-17', 0, true)).toEqual('贪狼');
@@ -1012,6 +1016,26 @@ describe('Astrolabe', () => {
10121016
expect(soulPalace?.decadal).toStrictEqual({ range: [6, 15], heavenlyStem: '丙', earthlyBranch: '寅' });
10131017
});
10141018

1019+
test('withOptions() with human type', () => {
1020+
astro.config({ algorithm: 'zhongzhou' });
1021+
1022+
const result = withOptions({
1023+
dateStr: '1999-05-03',
1024+
type: 'solar',
1025+
timeIndex: 8,
1026+
gender: 'male',
1027+
astroType: 'human',
1028+
});
1029+
1030+
const tiancaiIndex = result?.star('天才')?.palace()?.index;
1031+
const tianshangIndex = result?.star('天伤')?.palace()?.index;
1032+
const tianshiIndex = result?.star('天使')?.palace()?.index;
1033+
1034+
expect(tiancaiIndex).toEqual(11);
1035+
expect(tianshangIndex).toEqual(3);
1036+
expect(tianshiIndex).toEqual(1);
1037+
});
1038+
10151039
test('withOptions() to fix GitHub#242&#244', () => {
10161040
astro.config({ yearDivide: 'normal' });
10171041

src/astro/astro.ts

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,20 @@ import {
1414
setLanguage,
1515
t,
1616
} from '../i18n';
17-
import { getAdjectiveStar, getBoShi12, getchangsheng12, getMajorStar, getMinorStar, getYearly12 } from '../star';
17+
import {
18+
getAdjectiveStar,
19+
getBoShi12,
20+
getchangsheng12,
21+
getMajorStar,
22+
getMinorStar,
23+
getTianshiTianshangIndex,
24+
getYearly12,
25+
} from '../star';
1826
import { fixIndex, translateChineseDate } from '../utils';
1927
import FunctionalAstrolabe from './FunctionalAstrolabe';
2028
import FunctionalPalace, { IFunctionalPalace } from './FunctionalPalace';
2129
import { getPalaceNames, getSoulAndBody, getHoroscope, getFiveElementsClass } from './palace';
30+
import FunctionalStar from '../star/FunctionalStar';
2231

2332
const _plugins = [] as Plugin[];
2433
const _mutagens: Partial<Record<HeavenlyStemKey, StarKey[]>> = {};
@@ -356,7 +365,60 @@ export function rearrangeAstrolable<T extends FunctionalAstrolabe>({
356365
});
357366

358367
astrolable.fiveElementsClass = fiveElementsClass;
368+
369+
// 重新获取天使、天伤的索引
370+
const { tianshiIndex, tianshangIndex } = getTianshiTianshangIndex(
371+
astrolable.gender as GenderName,
372+
kot(astrolable.rawDates.chineseDate.yearly[1]) as EarthlyBranchKey,
373+
soulIndex,
374+
);
375+
// 重新获取天才星的位置
376+
const tiancaiIndex = fixIndex(
377+
soulIndex + EARTHLY_BRANCHES.indexOf(kot(astrolable.rawDates.chineseDate.yearly[1]) as EarthlyBranchKey),
378+
);
379+
359380
astrolable.palaces.forEach((palace, i) => {
381+
const _tianshangIdx = palace.adjectiveStars.findIndex((item) => kot(item.name) === 'tianshang');
382+
383+
if (_tianshangIdx !== -1 && tianshangIndex !== i) {
384+
// 当天伤不应该在该宫位时,删之
385+
palace.adjectiveStars.splice(_tianshangIdx, 1);
386+
}
387+
388+
if (_tianshangIdx === -1 && tianshangIndex === i) {
389+
// 当天伤应该在该宫位却不在,加之
390+
palace.adjectiveStars.push(new FunctionalStar({ name: t('tianshang'), type: 'adjective', scope: 'origin' }));
391+
}
392+
393+
const _tianshiIdx = palace.adjectiveStars.findIndex((item) => kot(item.name) === 'tianshi');
394+
395+
if (_tianshiIdx !== -1 && tianshiIndex !== i) {
396+
// 当天使不应该在该宫位时,删之
397+
palace.adjectiveStars.splice(_tianshiIdx, 1);
398+
}
399+
400+
if (_tianshiIdx === -1 && tianshiIndex === i) {
401+
// 当天使应该在该宫位却不在,加之
402+
palace.adjectiveStars.push(new FunctionalStar({ name: t('tianshi'), type: 'adjective', scope: 'origin' }));
403+
}
404+
405+
const _tiancaiIndex = palace.adjectiveStars.findIndex((item) => kot(item.name) === 'tiancai');
406+
407+
if (_tiancaiIndex !== -1 && tiancaiIndex !== i) {
408+
// 当天才不应该在该宫位时,删之
409+
palace.adjectiveStars.splice(_tiancaiIndex, 1);
410+
}
411+
412+
if (_tiancaiIndex === -1 && tiancaiIndex === i) {
413+
// 当天才应该在该宫位却不在,加之
414+
palace.adjectiveStars.push(new FunctionalStar({ name: t('tiancai'), type: 'adjective', scope: 'origin' }));
415+
}
416+
417+
if (_tianshiIdx === -1 && tianshiIndex === i) {
418+
// 当天使应该在该宫位却不在,加之
419+
palace.adjectiveStars.push(new FunctionalStar({ name: t('tianshi'), type: 'adjective', scope: 'origin' }));
420+
}
421+
360422
palace.name = palaceNames[i];
361423
palace.majorStars = majorStars[i];
362424
palace.changsheng12 = changsheng12[i];
@@ -484,16 +546,16 @@ export const getMajorStarBySolarDate = (
484546
) => {
485547
language && setLanguage(language);
486548

487-
const { bodyIndex } = getSoulAndBody({ solarDate: solarDateStr, timeIndex, fixLeap });
549+
const { soulIndex } = getSoulAndBody({ solarDate: solarDateStr, timeIndex, fixLeap });
488550
const majorStars = getMajorStar({ solarDate: solarDateStr, timeIndex, fixLeap });
489-
const stars = majorStars[bodyIndex].filter((star) => star.type === 'major');
551+
const stars = majorStars[soulIndex].filter((star) => star.type === 'major');
490552

491553
if (stars.length) {
492554
return stars.map((star) => t(star.name)).join(',');
493555
}
494556

495557
// 如果命宫为空宫,则借对宫主星
496-
return majorStars[fixIndex(bodyIndex + 6)]
558+
return majorStars[fixIndex(soulIndex + 6)]
497559
.filter((star) => star.type === 'major')
498560
.map((star) => t(star.name))
499561
.join(',');

src/star/location.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
EarthlyBranchKey,
66
EarthlyBranchName,
77
FiveElementsClassKey,
8+
GenderName,
89
HeavenlyStemKey,
910
HeavenlyStemName,
1011
kot,
@@ -637,7 +638,7 @@ export const getDahaoIndex = (earthlyBranchKey: EarthlyBranchKey) => {
637638
*/
638639
export const getYearlyStarIndex = (param: AstrolabeParam) => {
639640
const { solarDate, timeIndex, gender, fixLeap } = param;
640-
const { horoscopeDivide, algorithm } = getConfig();
641+
const { horoscopeDivide } = getConfig();
641642
const { yearly } = getHeavenlyStemAndEarthlyBranchBySolarDate(solarDate, timeIndex, {
642643
// 流耀应该用立春为界,但为了满足不同流派的需求允许配置
643644
year: horoscopeDivide,
@@ -718,18 +719,7 @@ export const getYearlyStarIndex = (param: AstrolabeParam) => {
718719
const nianjieIndex = getNianjieIndex(yearly[1]);
719720
const dahaoAdjIndex = getDahaoIndex(earthlyBranch);
720721

721-
const genderYinyang = ['male', 'female'];
722-
const sameYinyang = yinyang === genderYinyang.indexOf(kot(gender!));
723-
724-
let tianshangIndex = fixIndex(PALACES.indexOf('friendsPalace') + soulIndex);
725-
let tianshiIndex = fixIndex(PALACES.indexOf('healthPalace') + soulIndex);
726-
727-
if (algorithm === 'zhongzhou' && !sameYinyang) {
728-
// 中州派的天使天伤与通行版本不一样
729-
// 天伤奴仆、天使疾厄、夹迁移宫最易寻得
730-
// 凡阳男阴女,皆依此诀,但若为阴男阳女,则改为天伤居疾厄、天使居奴仆。
731-
[tianshiIndex, tianshangIndex] = [tianshangIndex, tianshiIndex];
732-
}
722+
const { tianshiIndex, tianshangIndex } = getTianshiTianshangIndex(gender!, earthlyBranch, soulIndex);
733723

734724
return {
735725
xianchiIndex,
@@ -762,6 +752,27 @@ export const getYearlyStarIndex = (param: AstrolabeParam) => {
762752
};
763753
};
764754

755+
export const getTianshiTianshangIndex = (gender: GenderName, earthlyBranch: EarthlyBranchKey, soulIndex: number) => {
756+
// 判断命主出生年年支阴阳属性,如果结果为 0 则为阳,否则为阴
757+
const yinyang = EARTHLY_BRANCHES.indexOf(earthlyBranch) % 2;
758+
759+
const { algorithm } = getConfig();
760+
761+
const genderYinyang = ['male', 'female'];
762+
const sameYinyang = yinyang === genderYinyang.indexOf(kot(gender!));
763+
let tianshangIndex = fixIndex(PALACES.indexOf('friendsPalace') + soulIndex);
764+
let tianshiIndex = fixIndex(PALACES.indexOf('healthPalace') + soulIndex);
765+
766+
if (algorithm === 'zhongzhou' && !sameYinyang) {
767+
// 中州派的天使天伤与通行版本不一样
768+
// 天伤奴仆、天使疾厄、夹迁移宫最易寻得
769+
// 凡阳男阴女,皆依此诀,但若为阴男阳女,则改为天伤居疾厄、天使居奴仆。
770+
[tianshiIndex, tianshangIndex] = [tianshangIndex, tianshiIndex];
771+
}
772+
773+
return { tianshangIndex, tianshiIndex };
774+
};
775+
765776
/**
766777
* 获取年解的索引
767778
*

0 commit comments

Comments
 (0)