Skip to content

Commit

Permalink
Merge pull request #178 from SylarLong/feat-165
Browse files Browse the repository at this point in the history
Feat: global configuration & plugin support
  • Loading branch information
SylarLong authored Feb 25, 2024
2 parents 8034c2d + 2f7bfda commit faddc2e
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 22 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
- 🛠️ 修复(fix)
- 🧹 琐事(Chore)

## v2.3.0

- 🪄 功能(feature)

🇨🇳

- 支持全局插件 #165
- 支持全局配置 #165

🇺🇸

- support global plugin #165
- support global configuration #165

## v2.2.3

- 🛠️ 修复(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.2.3",
"version": "2.3.0",
"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.1.1"
"lunar-lite": "^0.1.2"
}
}
74 changes: 74 additions & 0 deletions src/__tests__/astro/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { astro } from '../..';
import FunctionalAstrolabe from '../../astro/FunctionalAstrolabe';

export interface IAstrolabe extends FunctionalAstrolabe {
myNewFunc: () => string;
majorStar: () => string;
}

// 创建一个插件函数
export function myTestPlugin(this: IAstrolabe): void {
// 实现插件应用逻辑
this.myNewFunc = () => {
return this.fiveElementsClass;
};
}

// 创建二个插件函数
export function myTestPlugin2(this: IAstrolabe): void {
// 实现插件应用逻辑
this.majorStar = () => {
let stars = this.palace('命宫')
?.majorStars.filter((item) => item.type === 'major' && !['禄存', '天马'].includes(item.name))
.map((item) => item.name)
.join(',');

if (!stars) {
stars = this.palace('迁移')
?.majorStars.filter((item) => item.type === 'major' && !['禄存', '天马'].includes(item.name))
.map((item) => item.name)
.join(',');
}

return stars ?? '';
};
}

astro.loadPlugins([myTestPlugin]);
astro.loadPlugin(myTestPlugin2);

astro.config({
mutagens: { : ['太阳', '武曲', '天同', '天相'] },
brightness: {
贪狼: ['旺', '旺', '旺', '旺', '旺', '旺', '旺', '旺', '旺', '旺', '旺', '旺'],
},
});

describe('plugin test', () => {
test('plugin', () => {
const astrolabe = astro.bySolar<IAstrolabe>('2023-10-18', 4, 'female');

expect(astrolabe.myNewFunc()).toEqual('火六局');
expect(astrolabe.majorStar()).toEqual('七杀');
});

test('changed configuration', () => {
const astrolabe = astro.bySolar<IAstrolabe>('2010-10-18', 4, 'female');

expect(astrolabe.palace('命宫')?.hasMutagen('忌')).toBeFalsy();
expect(astrolabe.palace('夫妻')?.hasMutagen('忌')).toBeTruthy();
expect(astrolabe.star('贪狼').withBrightness('旺')).toBeTruthy();
});

test('not changed configuration', () => {
const astrolabe = astro.bySolar<IAstrolabe>('2011-10-18', 4, 'female');

expect(astrolabe.palace('命宫')?.hasMutagen('权')).toBeTruthy();
expect(astrolabe.palace('命宫')?.hasMutagen('忌')).toBeTruthy();
expect(astrolabe.palace('福德')?.hasMutagen('科')).toBeTruthy();
expect(astrolabe.palace('田宅')?.hasMutagen('禄')).toBeFalsy();
expect(astrolabe.palace('财帛')?.fliesTo('夫妻', '科')).toBeTruthy();
expect(astrolabe.palace('财帛')?.fliesTo('仆役', '忌')).toBeTruthy();
expect(astrolabe.star('紫微').withBrightness('旺')).toBeTruthy();
});
});
18 changes: 17 additions & 1 deletion src/astro/FunctionalAstrolabe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dayjs from 'dayjs';
import { getHeavenlyStemAndEarthlyBranchBySolarDate, normalizeDateStr, solar2lunar } from 'lunar-lite';
import { EARTHLY_BRANCHES } from '../data';
import { Astrolabe, Horoscope } from '../data/types';
import { Astrolabe, Horoscope, Plugin } from '../data/types';
import { EarthlyBranchKey, EarthlyBranchName, HeavenlyStemName, kot, PalaceName, StarKey, StarName, t } from '../i18n';
import { getHoroscopeStar, getYearly12 } from '../star';
import { IFunctionalStar } from '../star/FunctionalStar';
Expand Down Expand Up @@ -192,6 +192,14 @@ const _getHoroscopeBySolarDate = (
* 文档地址:https://docs.iztro.com/posts/astrolabe.html#functionalastrolabe
*/
export interface IFunctionalAstrolabe extends Astrolabe {
/**
* 插件注入方法
*
* @version v2.3.0
*
* @param plugin 插件函数
*/
use(plugin: Plugin): void;
/**
* 获取运限数据
*
Expand Down Expand Up @@ -292,6 +300,9 @@ export default class FunctionalAstrolabe implements IFunctionalAstrolabe {
fiveElementsClass;
palaces;

// 保存插件列表
private plugins: Plugin[] = [];

constructor(data: Astrolabe) {
this.gender = data.gender;
this.solarDate = data.solarDate;
Expand All @@ -312,6 +323,11 @@ export default class FunctionalAstrolabe implements IFunctionalAstrolabe {
return this;
}

use(plugin: Plugin): void {
this.plugins.push(plugin);
plugin.apply(this);
}

star = (starName: StarName): IFunctionalStar => {
let targetStar: IFunctionalStar | undefined;

Expand Down
76 changes: 70 additions & 6 deletions src/astro/astro.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,75 @@
import { getHeavenlyStemAndEarthlyBranchBySolarDate, getSign, getZodiac, lunar2solar, solar2lunar } from 'lunar-lite';
import { CHINESE_TIME, EARTHLY_BRANCHES, HEAVENLY_STEMS, TIME_RANGE, earthlyBranches } from '../data';
import { Language } from '../data/types';
import { EarthlyBranchKey, EarthlyBranchName, GenderName, HeavenlyStemKey, kot, setLanguage, t } from '../i18n';
import { Config, Language, Plugin } from '../data/types';
import {
BrightnessKey,
EarthlyBranchKey,
EarthlyBranchName,
GenderName,
HeavenlyStemKey,
StarKey,
kot,
setLanguage,
t,
} from '../i18n';
import { getAdjectiveStar, getBoShi12, getchangsheng12, getMajorStar, getMinorStar, getYearly12 } from '../star';
import { fixIndex, translateChineseDate } from '../utils';
import FunctionalAstrolabe from './FunctionalAstrolabe';
import FunctionalPalace, { IFunctionalPalace } from './FunctionalPalace';
import { getPalaceNames, getSoulAndBody, getHoroscope, getFiveElementsClass } from './palace';

const _plugins = [] as Plugin[];
const _mutagens: Partial<Record<HeavenlyStemKey, StarKey[]>> = {};
const _brightness: Partial<Record<StarKey, BrightnessKey[]>> = {};

/**
* 批量加载插件
*
* @version v2.3.0
*
* @param plugins 插件方法数组
*/
export const loadPlugins = (plugins: Plugin[]) => {
Array.prototype.push.apply(_plugins, plugins);
};

/**
* 加载单个插件
*
* @version v2.3.0
*
* @param plugin 插件方法
*/
export const loadPlugin = (plugin: Plugin) => {
_plugins.push(plugin);
};

/**
* 全局配置四化和亮度
*
* 由于key和value都有可能是不同语言传进来的,
* 所以需会将key和value转化为对应的i18n key。
*
* @version 2.3.0
*
* @param {Config} param0 自定义配置
*/
export const config = ({ mutagens, brightness }: Config) => {
if (mutagens) {
Object.entries(mutagens).forEach(([key, value]) => {
_mutagens[kot<HeavenlyStemKey>(key)] = value.map((item) => kot<StarKey>(item)) ?? [];
});
}

if (brightness) {
Object.entries(brightness).forEach(([key, value]) => {
_brightness[kot<StarKey>(key)] = value.map((item) => kot<BrightnessKey>(item)) ?? [];
});
}
};

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

/**
* 通过阳历获取星盘信息
*
Expand Down Expand Up @@ -40,13 +102,13 @@ export const astrolabeBySolarDate = (
* @param language 输出语言
* @returns 星盘信息
*/
export const bySolar = (
export function bySolar<T extends FunctionalAstrolabe>(
solarDateStr: string,
timeIndex: number,
gender: GenderName,
fixLeap: boolean = true,
language?: Language,
) => {
): T {
language && setLanguage(language);

const palaces: IFunctionalPalace[] = [];
Expand Down Expand Up @@ -121,8 +183,10 @@ export const bySolar = (
palaces,
});

return result;
};
_plugins.map((plugin) => result.use(plugin));

return result as T;
}

/**
* 通过农历获取星盘信息
Expand Down
22 changes: 21 additions & 1 deletion src/data/types/astro.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { IFunctionalPalace } from '../../astro/FunctionalPalace';
import { EarthlyBranchName, FiveElementsClassName, HeavenlyStemName, PalaceName, StarName } from '../../i18n';
import {
Brightness,
EarthlyBranchName,
FiveElementsClassName,
HeavenlyStemName,
PalaceName,
StarName,
} from '../../i18n';
import FunctionalStar from '../../star/FunctionalStar';
import { HeavenlyStemAndEarthlyBranchDate, LunarDate } from 'lunar-lite/lib/types';

Expand Down Expand Up @@ -150,3 +157,16 @@ export type Astrolabe = {
/** 十二宫数据 */
palaces: IFunctionalPalace[];
};

/**
* 定义一个接口,表示插件函数的类型
* */
export type Plugin = () => void;

export type ConfigMutagens = Partial<Record<HeavenlyStemName, StarName[]>>;
export type ConfigBrightness = Partial<Record<StarName, Brightness[]>>;

export type Config = {
mutagens?: ConfigMutagens;
brightness?: ConfigBrightness;
};
28 changes: 25 additions & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ import {
import FunctionalStar from '../star/FunctionalStar';
import { HeavenlyStemAndEarthlyBranchDate } from 'lunar-lite/lib/types';
import { solar2lunar } from 'lunar-lite';
import { getConfig } from '../astro';

const getTargetMutagens = (heavenlyStem: HeavenlyStemKey) => {
const { mutagens } = getConfig();
let result;

if (mutagens && mutagens[heavenlyStem]) {
result = mutagens[heavenlyStem] ?? [];
} else {
result = heavenlyStems[heavenlyStem].mutagen ?? [];
}

return result;
};

/**
* 用于处理索引,将索引锁定在 0~max 范围内
Expand Down Expand Up @@ -58,21 +72,29 @@ export const earthlyBranchIndexToPalaceIndex = (earthlyBranchName: EarthlyBranch
*/
export const getBrightness = (starName: StarName, index: number): Brightness => {
const star = kot<keyof typeof STARS_INFO>(starName);
const { brightness } = getConfig();
const targetBrightness = brightness[star] ? brightness[star] : STARS_INFO[star]?.brightness;

if (!targetBrightness) {
return '';
}

return t<Brightness>(STARS_INFO[star]?.brightness[fixIndex(index)]);
return t<Brightness>(targetBrightness[fixIndex(index)]);
};

export const getMutagen = (starName: StarName, heavenlyStemName: HeavenlyStemName): Mutagen => {
const heavenlyStem = kot<HeavenlyStemKey>(heavenlyStemName, 'Heavenly');
const starKey = kot<StarKey>(starName);
const target = getTargetMutagens(heavenlyStem);

return t<Mutagen>(MUTAGEN[heavenlyStems[heavenlyStem].mutagen.indexOf(starKey as never)]);
return t<Mutagen>(MUTAGEN[target.indexOf(starKey as never)]);
};

export const getMutagensByHeavenlyStem = (heavenlyStemName: HeavenlyStemName): StarName[] => {
const heavenlyStem = kot<HeavenlyStemKey>(heavenlyStemName, 'Heavenly');
const target = getTargetMutagens(heavenlyStem);

return heavenlyStems[heavenlyStem].mutagen.map((star) => t<StarName>(star));
return target.map((star) => t<StarName>(star));
};

/**
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.1.1:
version "0.1.1"
resolved "https://registry.npmjs.org/lunar-lite/-/lunar-lite-0.1.1.tgz#c2c114b95eae24b84ebc0702f616ed573772c244"
integrity sha512-ttdkDY4uZg6LtE8r4m/Vt+z48ReT8StYXPY2eXqJ6tOG3C4I11g/f/gAJXplJIRTZd+F1xOFa52tUiP2UIkqzQ==
lunar-lite@^0.1.2:
version "0.1.2"
resolved "https://registry.npmjs.org/lunar-lite/-/lunar-lite-0.1.2.tgz#a66b08cbc6c8df3a31d4e166052593662f489144"
integrity sha512-3SKo8zTDdbS1Ynn4qHUPNOW9oviqL8d2qdHSMS9jfNbmIdh6DbYKaY9dnXpMS5I1dzG9ztAuwVJ8MBAupv89aA==
dependencies:
lunar-typescript "^1.6.13"
lunar-typescript "^1.7.3"

lunar-typescript@^1.6.13:
version "1.6.13"
resolved "https://registry.npmjs.org/lunar-typescript/-/lunar-typescript-1.6.13.tgz#99c9975f8ba4bc43af813e0155db0926d024cbcf"
integrity sha512-6gBJepWWRiCvpbxxeiogsd/ZIggBbH1xmS090Kat/FiM0AGFnso0bmYXL1q/opr8qrMdnjsBlMZqBvNiRKACGA==
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==

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

0 comments on commit faddc2e

Please sign in to comment.