Skip to content

Commit

Permalink
Merge pull request #191 from SylarLong/fix/190
Browse files Browse the repository at this point in the history
🛠️ Wrong results using the parameter yearDivide to 'normal'.
  • Loading branch information
SylarLong authored May 5, 2024
2 parents 214bab2 + 6c80375 commit e9ade61
Show file tree
Hide file tree
Showing 9 changed files with 103 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.3

- 🛠️ 修复(fix)

🇨🇳

- 新增运限分界点参数以满足不同需求 #190

🇺🇸

- Wrong results using the parameter yearDivide to 'normal'. #190

## v2.4.2

- 🛠️ 修复(fix)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iztro",
"version": "2.4.2",
"version": "2.4.3",
"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 Expand Up @@ -71,6 +71,6 @@
"dependencies": {
"dayjs": "^1.11.10",
"i18next": "^23.5.1",
"lunar-lite": "^0.2.0"
"lunar-lite": "^0.2.3"
}
}
63 changes: 63 additions & 0 deletions src/__tests__/astro/astro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,20 @@ describe('Astrolabe', () => {
expect(horoscope.yearly).toHaveProperty('heavenlyStem', '庚');
});

test('check special date `1995-3-30`', () => {
astro.config({ yearDivide: 'normal' });

const result = astro.bySolar('1995-03-30', 0, 'male', true);

expect(result).toHaveProperty('solarDate', '1995-03-30');
expect(result).toHaveProperty('lunarDate', '一九九五年二月三十');

const result2 = astro.byLunar('1995-2-30', 0, 'male', true);

expect(result2).toHaveProperty('solarDate', '1995-3-30');
expect(result2).toHaveProperty('lunarDate', '一九九五年二月三十');
});

test('withOptions()', () => {
const result = astro.withOptions({
type: 'lunar',
Expand Down Expand Up @@ -700,6 +714,55 @@ describe('Astrolabe', () => {
expect(result).toHaveProperty('fiveElementsClass', '土五局');
});

test('withOptions() 3', () => {
const result = astro.withOptions({
type: 'lunar',
dateStr: '1979-12-28',
timeIndex: 0,
gender: 'female',
isLeapMonth: false,
fixLeap: true,
language: 'zh-CN',
config: {
yearDivide: 'normal',
horoscopeDivide: 'normal',
},
});

expect(result).toHaveProperty('solarDate', '1980-2-14');
expect(result).toHaveProperty('lunarDate', '一九七九年腊月廿八');
expect(result).toHaveProperty('chineseDate', '己未 戊寅 丁巳 庚子');
expect(result).toHaveProperty('time', '早子时');
expect(result).toHaveProperty('zodiac', '羊');
expect(result).toHaveProperty('earthlyBranchOfSoulPalace', '丑');
expect(result).toHaveProperty('earthlyBranchOfBodyPalace', '丑');
expect(result).toHaveProperty('soul', '巨门');
expect(result).toHaveProperty('body', '天相');
expect(result).toHaveProperty('fiveElementsClass', '水二局');

const horoscope = result.horoscope('1980-2-14');

expect(horoscope.yearly.earthlyBranch).toBe('未');

const result2 = astro.withOptions({
type: 'lunar',
dateStr: '1979-12-28',
timeIndex: 0,
gender: 'female',
isLeapMonth: false,
fixLeap: true,
language: 'zh-CN',
config: {
yearDivide: 'normal',
horoscopeDivide: 'exact',
},
});

const horoscope2 = result2.horoscope('1980-2-14');

expect(horoscope2.yearly.earthlyBranch).toBe('申');
});

test('bySolar() fix leap month', () => {
const result = astro.bySolar('2023-4-10', 4, '女', true);

Expand Down
5 changes: 3 additions & 2 deletions src/astro/FunctionalAstrolabe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IFunctionalPalace } from './FunctionalPalace';
import { IFunctionalSurpalaces } from './FunctionalSurpalaces';
import { getPalaceNames } from './palace';
import FunctionalHoroscope, { IFunctionalHoroscope } from './FunctionalHoroscope';
import { getConfig } from './astro';

/**
* 获取运限数据
Expand All @@ -36,8 +37,8 @@ const _getHoroscopeBySolarDate = (
targetDate,
timeIndex || convertTimeIndex,
{
// 运限是以立春为界
year: 'exact',
// 运限是以立春为界,但为了满足部分流派允许配置
year: getConfig().horoscopeDivide,
},
);
// 虚岁
Expand Down
8 changes: 7 additions & 1 deletion src/astro/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const _brightness: Partial<Record<StarKey, BrightnessKey[]>> = {};
* exact:立春分界
*/
let _yearDivide: 'normal' | 'exact' = 'exact';
let _horoscopeDivide: 'normal' | 'exact' = 'exact';

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

if (horoscopeDivide) {
_horoscopeDivide = horoscopeDivide;
}
};

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

/**
Expand Down
3 changes: 2 additions & 1 deletion src/data/types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ export type Config = {
mutagens?: ConfigMutagens;
brightness?: ConfigBrightness;
yearDivide?: 'normal' | 'exact';
horoscopeDivide?: 'normal' | 'exact';
};

export type Option = {
type: 'solar' | 'lunar';
dateStr: string;
Expand Down
4 changes: 2 additions & 2 deletions src/star/decorativeStar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ export const getYearly12 = (solarDateStr: string | Date): { suiqian12: StarName[
const jiangqian12: StarName[] = [];
const suiqian12: StarName[] = [];
const { yearly } = getHeavenlyStemAndEarthlyBranchBySolarDate(solarDateStr, 0, {
// 流年神煞应该用立春为界
year: 'exact',
// 流年神煞应该用立春为界,但为了满足不同流派的需求允许配置
year: getConfig().horoscopeDivide,
});

const ts12shen: StarKey[] = [
Expand Down
6 changes: 3 additions & 3 deletions src/star/location.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getHeavenlyStemAndEarthlyBranchBySolarDate, getTotalDaysOfLunarMonth, solar2lunar } from 'lunar-lite';
import { getFiveElementsClass, getSoulAndBody } from '../astro';
import { getConfig, getFiveElementsClass, getSoulAndBody } from '../astro';
import { EARTHLY_BRANCHES, FiveElementsClass, HEAVENLY_STEMS, PALACES } from '../data';
import {
EarthlyBranchKey,
Expand Down Expand Up @@ -561,8 +561,8 @@ export const getGuGuaIndex = (earthlyBranchName: EarthlyBranchName) => {
*/
export const getYearlyStarIndex = (solarDate: string, timeIndex: number, fixLeap?: boolean) => {
const { yearly } = getHeavenlyStemAndEarthlyBranchBySolarDate(solarDate, timeIndex, {
// 流耀应该用立春为界
year: 'exact',
// 流耀应该用立春为界,但为了满足不同流派的需求允许配置
year: getConfig().horoscopeDivide,
});
const { soulIndex, bodyIndex } = getSoulAndBody(solarDate, timeIndex, fixLeap);
const heavenlyStem = kot<HeavenlyStemKey>(yearly[0], 'Heavenly');
Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3870,17 +3870,17 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"

lunar-lite@^0.2.0:
version "0.2.0"
resolved "https://registry.npmjs.org/lunar-lite/-/lunar-lite-0.2.0.tgz#12214860bb753b0840aaf553ae3754b4c4998af1"
integrity sha512-Dskql5YCpd6xjtXj3Rl61A1qxT0zaPdold/tM/n6m0r7bfZ6FfEFom7zzKPJMuD0XUNji5C/ItuaMR+LAjqAVQ==
lunar-lite@^0.2.3:
version "0.2.3"
resolved "https://registry.npmjs.org/lunar-lite/-/lunar-lite-0.2.3.tgz#415274503dae18880ea0ad7db2c517468c89681a"
integrity sha512-sI8BMt3Q9RFvRJSKOdrutxRVc+REVmo709aeUKlAJFOg04+/PS9z/wORDVLJzo+fgBE7cdiIKGbNk1yU6emepg==
dependencies:
lunar-typescript "^1.7.3"
lunar-typescript "^1.7.5"

lunar-typescript@^1.7.3:
version "1.7.3"
resolved "https://registry.npmjs.org/lunar-typescript/-/lunar-typescript-1.7.3.tgz#841d997687a0145a1fe9f65db735675f66d06660"
integrity sha512-VXMdgh2Psrn3vtfrai4lPug3Mt7ijYGVTICDARA8tLzqGv3Io/OvS23wxzFi/AbSzxt93u2wWhgv3VGKPSbUgQ==
lunar-typescript@^1.7.5:
version "1.7.5"
resolved "https://registry.npmjs.org/lunar-typescript/-/lunar-typescript-1.7.5.tgz#8a407f9db10aaf896f1477564dbfef741463e245"
integrity sha512-AlOwYrxHRCR9Plba5TlZY0NVv6aFobmR1gxfiAE1KxjDzBXoDtT2KrsBYCVaErGokiL8qLMS1FNwGPiPRX2a4g==

make-dir@^4.0.0:
version "4.0.0"
Expand Down

0 comments on commit e9ade61

Please sign in to comment.