Skip to content

Commit

Permalink
✨ add nominal age divide. #225
Browse files Browse the repository at this point in the history
  • Loading branch information
SylarLong committed Mar 2, 2025
1 parent a288e5c commit 63d4581
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 20 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
- 🛠️ 修复(fix)
- 🧹 琐事(Chore)

## v2.4.5

- ✨ 改进(enhancement)

🇨🇳

- 新增虚岁分割点配置 #225

🇺🇸

- add nominal age divide. #225

## v2.4.4

- 🪄 功能(feature)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iztro",
"version": "2.4.4",
"version": "2.4.5",
"description": "轻量级紫微斗数星盘生成库。可以通过出生年月日获取到紫微斗数星盘信息、生肖、星座等信息。A lightweight kit to astrolabe generator of The Purple Star Astrology (Zi Wei Dou Shu). The Purple Star Astrology(Zi Wei Dou Shu) is a Chinese ancient astrology. You're able to get your horoscope and personality from the astrolabe",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
52 changes: 43 additions & 9 deletions src/__tests__/astro/astro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ describe('Astrolabe', () => {
'子女',
]);
expect(horoscope.decadal).toHaveProperty('mutagen', ['太阳', '武曲', '太阴', '天同']);
expect(horoscope.age).toHaveProperty('index', 10);
expect(horoscope.age).toHaveProperty('nominalAge', 23);
expect(horoscope.age).toHaveProperty('index', 9);
expect(horoscope.age).toHaveProperty('nominalAge', 24);
expect(horoscope.yearly).toHaveProperty('index', 1);
expect(horoscope.yearly).toHaveProperty('heavenlyStem', '癸');
expect(horoscope.yearly).toHaveProperty('earthlyBranch', '卯');
Expand Down Expand Up @@ -158,9 +158,9 @@ describe('Astrolabe', () => {

const agePalace = horoscope.agePalace();

expect(agePalace).toHaveProperty('name', '迁移');
expect(agePalace).toHaveProperty('heavenlyStem', '');
expect(agePalace).toHaveProperty('earthlyBranch', '');
expect(agePalace).toHaveProperty('name', '仆役');
expect(agePalace).toHaveProperty('heavenlyStem', '');
expect(agePalace).toHaveProperty('earthlyBranch', '');

const originalPalace = horoscope.palace('命宫', 'origin');

Expand Down Expand Up @@ -300,8 +300,8 @@ describe('Astrolabe', () => {
'자녀',
]);
expect(horoscope.decadal).toHaveProperty('mutagen', ['태양', '무곡', '태음', '천동']);
expect(horoscope.age).toHaveProperty('index', 10);
expect(horoscope.age).toHaveProperty('nominalAge', 23);
expect(horoscope.age).toHaveProperty('index', 9);
expect(horoscope.age).toHaveProperty('nominalAge', 24);
expect(horoscope.yearly).toHaveProperty('index', 1);
expect(horoscope.yearly).toHaveProperty('heavenlyStem', '계');
expect(horoscope.yearly).toHaveProperty('earthlyBranch', '묘');
Expand Down Expand Up @@ -466,8 +466,8 @@ describe('Astrolabe', () => {
'Tử Nữ',
]);
expect(horoscope.decadal).toHaveProperty('mutagen', ['Thái Dương', 'Vũ Khúc', 'Thái Âm', 'Thiên Đồng']);
expect(horoscope.age).toHaveProperty('index', 10);
expect(horoscope.age).toHaveProperty('nominalAge', 23);
expect(horoscope.age).toHaveProperty('index', 9);
expect(horoscope.age).toHaveProperty('nominalAge', 24);
expect(horoscope.yearly).toHaveProperty('index', 1);
expect(horoscope.yearly).toHaveProperty('heavenlyStem', 'Quý');
expect(horoscope.yearly).toHaveProperty('earthlyBranch', 'Mão');
Expand Down Expand Up @@ -878,4 +878,38 @@ describe('Astrolabe', () => {
expect(horo3.decadal.name).toEqual('童限');
expect(horo3.decadal.index).toEqual(astrolabe.palace('疾厄')?.index);
});

test('nominalAge: nomal', () => {
const astrolabe = astro.withOptions({
type: 'solar',
dateStr: '2000-8-16',
timeIndex: 2,
gender: 'female',
config: {
ageDivide: 'normal',
},
});

const horo1 = astrolabe.horoscope('2023-8-19 3:12');

expect(horo1.age.index).toEqual(9);
expect(horo1.age.nominalAge).toEqual(24);
});

test('nominalAge: birthday', () => {
const astrolabe = astro.withOptions({
type: 'solar',
dateStr: '2000-8-16',
timeIndex: 2,
gender: 'female',
config: {
ageDivide: 'birthday',
},
});

const horo1 = astrolabe.horoscope('2023-8-19 3:12');

expect(horo1.age.index).toEqual(10);
expect(horo1.age.nominalAge).toEqual(23);
});
});
23 changes: 14 additions & 9 deletions src/astro/FunctionalAstrolabe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,20 @@ const _getHoroscopeBySolarDate = (
// 是否童限
let isChildhood = false;

// 假如目标日期已经过了生日,则需要加1岁
// 比如 2022年九月初一 出生的人,在出生后虚岁为 1 岁
// 但在 2023年九月初二 以后,虚岁则为 2 岁
if (
(_date.lunarYear === _birthday.lunarYear &&
_date.lunarMonth === _birthday.lunarMonth &&
_date.lunarDay > _birthday.lunarDay) ||
_date.lunarMonth > _birthday.lunarMonth
) {
if (getConfig().ageDivide === 'birthday') {
// 假如目标日期已经过了生日,则需要加1岁
// 比如 2022年九月初一 出生的人,在出生后虚岁为 1 岁
// 但在 2023年九月初二 以后,虚岁则为 2 岁
if (
(_date.lunarYear === _birthday.lunarYear &&
_date.lunarMonth === _birthday.lunarMonth &&
_date.lunarDay > _birthday.lunarDay) ||
_date.lunarMonth > _birthday.lunarMonth
) {
nominalAge += 1;
}
} else {
// 以自然年为界,直接加1岁
nominalAge += 1;
}

Expand Down
16 changes: 15 additions & 1 deletion src/astro/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ const _brightness: Partial<Record<StarKey, BrightnessKey[]>> = {};
let _yearDivide: 'normal' | 'exact' = 'exact';
let _horoscopeDivide: 'normal' | 'exact' = 'exact';

/**
* 小限分割点,默认为生日。
*
* @version v2.4.5
*
* normal: 只考虑年份,不考虑
*/
let _ageDivide: 'normal' | 'birthday' = 'normal';

/**
* 批量加载插件
*
Expand Down Expand Up @@ -65,7 +74,7 @@ export const loadPlugin = (plugin: Plugin) => {
*
* @param {Config} param0 自定义配置
*/
export const config = ({ mutagens, brightness, yearDivide, horoscopeDivide }: Config) => {
export const config = ({ mutagens, brightness, yearDivide, ageDivide, horoscopeDivide }: Config) => {
if (mutagens) {
Object.entries(mutagens).forEach(([key, value]) => {
_mutagens[kot<HeavenlyStemKey>(key)] = value.map((item) => kot<StarKey>(item)) ?? [];
Expand All @@ -85,12 +94,17 @@ export const config = ({ mutagens, brightness, yearDivide, horoscopeDivide }: Co
if (horoscopeDivide) {
_horoscopeDivide = horoscopeDivide;
}

if (ageDivide) {
_ageDivide = ageDivide;
}
};

export const getConfig = () => ({
mutagens: _mutagens,
brightness: _brightness,
yearDivide: _yearDivide,
ageDivide: _ageDivide,
horoscopeDivide: _horoscopeDivide,
});

Expand Down
6 changes: 6 additions & 0 deletions src/data/types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,16 @@ export type ConfigMutagens = Partial<Record<HeavenlyStemName, StarName[]>>;
export type ConfigBrightness = Partial<Record<StarName, Brightness[]>>;

export type Config = {
/** 四化配置 */
mutagens?: ConfigMutagens;
/** 星耀亮度配置 */
brightness?: ConfigBrightness;
/** 年分割点配置,normal为正月初一分界,exact为立春分割 */
yearDivide?: 'normal' | 'exact';
/** 运限分割点配置,normal为正月初一分界,exact为立春分割 */
horoscopeDivide?: 'normal' | 'exact';
/** 小限分割点配置,normal为以自然年分界,birthday为生日分界 */
ageDivide?: 'normal' | 'birthday';
};

export type Option = {
Expand Down

0 comments on commit 63d4581

Please sign in to comment.