Skip to content

Commit

Permalink
✨ add withOptions function
Browse files Browse the repository at this point in the history
  • Loading branch information
SylarLong committed Apr 5, 2024
1 parent 451702d commit ada5124
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 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.1

- ✨ 改进(enhancement)

🇨🇳

- 增加 `withOptions` 方法

🇺🇸

- add `withOptions` function

## v2.4.0

- ✨ 改进(enhancement)
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.0",
"version": "2.4.1",
"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: 52 additions & 0 deletions src/__tests__/astro/astro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,58 @@ describe('Astrolabe', () => {
expect(result).toHaveProperty('fiveElementsClass', '火六局');
});

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

expect(result).toHaveProperty('solarDate', '2000-2-4');
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', '火六局');
});

test('withOptions() 2', () => {
const result = astro.withOptions({
type: 'lunar',
dateStr: '1999-12-29',
timeIndex: 2,
gender: 'female',
isLeapMonth: false,
fixLeap: true,
language: 'zh-CN',
config: {
yearDivide: 'exact',
},
});

expect(result).toHaveProperty('solarDate', '2000-2-4');
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', '土五局');
});

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

Expand Down
29 changes: 28 additions & 1 deletion src/astro/astro.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getHeavenlyStemAndEarthlyBranchBySolarDate, getSign, getZodiac, lunar2solar, solar2lunar } from 'lunar-lite';
import { CHINESE_TIME, EARTHLY_BRANCHES, HEAVENLY_STEMS, TIME_RANGE, earthlyBranches } from '../data';
import { Config, Language, Plugin } from '../data/types';
import { Config, Language, Option, Plugin } from '../data/types';
import {
BrightnessKey,
EarthlyBranchKey,
Expand Down Expand Up @@ -259,6 +259,33 @@ export const byLunar = (
return bySolar(solarDate.toString(), timeIndex, gender, fixLeap, language);
};

/**
* 获取排盘信息。
*
* @param param0 排盘参数
* @returns 星盘信息
*/
export const withOptions = ({
type = 'solar',
dateStr,
timeIndex,
gender,
isLeapMonth,
fixLeap,
language,
config: cfg,
}: Option) => {
if (cfg) {
config(cfg);
}

if (type === 'solar') {
return bySolar(dateStr, timeIndex, gender, fixLeap, language);
}

return byLunar(dateStr, timeIndex, gender, isLeapMonth, fixLeap, language);
};

/**
* 通过公历获取十二生肖
*
Expand Down
13 changes: 13 additions & 0 deletions src/data/types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import {
Brightness,
EarthlyBranchName,
FiveElementsClassName,
GenderName,
HeavenlyStemName,
PalaceName,
StarName,
} from '../../i18n';
import FunctionalStar from '../../star/FunctionalStar';
import { HeavenlyStemAndEarthlyBranchDate, LunarDate } from 'lunar-lite/lib/types';
import { Language } from './general';

/**
* 运限对象
Expand Down Expand Up @@ -173,3 +175,14 @@ export type Config = {
brightness?: ConfigBrightness;
yearDivide?: 'normal' | 'exact';
};

export type Option = {
type: 'solar' | 'lunar';
dateStr: string;
timeIndex: number;
gender: GenderName;
isLeapMonth?: boolean;
fixLeap?: boolean;
language?: Language;
config?: Config;
};

0 comments on commit ada5124

Please sign in to comment.