Skip to content

Commit

Permalink
🪄 support customized mutagens
Browse files Browse the repository at this point in the history
  • Loading branch information
SylarLong committed Feb 24, 2024
1 parent 7d1cf39 commit 3a40867
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"format": "prettier --write \"src/**/*.ts\"",
"lint": "eslint . --ext .ts",
"test": "jest --config jestconfig.json --coverage",
"t": "jest --testPathPattern=\"src/__tests__/astro/plugin.test.ts\"",
"prepare": "yarn build",
"prepublishOnly": "npm test && yarn lint && yarn build:umd",
"preversion": "yarn lint",
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/astro/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,31 @@ export function myTestPlugin2(this: IAstrolabe): void {
astro.loadPlugins([myTestPlugin]);
astro.loadPlugin(myTestPlugin2);

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

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();
});

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();
});
});
2 changes: 2 additions & 0 deletions src/astro/FunctionalAstrolabe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ export interface IFunctionalAstrolabe extends Astrolabe {
/**
* 插件注入方法
*
* @version v2.3.0
*
* @param plugin 插件函数
*/
use(plugin: Plugin): void;
Expand Down
48 changes: 45 additions & 3 deletions src/astro/astro.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,65 @@
import { getHeavenlyStemAndEarthlyBranchBySolarDate, getSign, getZodiac, lunar2solar, solar2lunar } from 'lunar-lite';
import { CHINESE_TIME, EARTHLY_BRANCHES, HEAVENLY_STEMS, TIME_RANGE, earthlyBranches } from '../data';
import { Language, Plugin } from '../data/types';
import { EarthlyBranchKey, EarthlyBranchName, GenderName, HeavenlyStemKey, kot, setLanguage, t } from '../i18n';
import { CHINESE_TIME, EARTHLY_BRANCHES, HEAVENLY_STEMS, TIME_RANGE, earthlyBranches, heavenlyStems } from '../data';
import { Config, Language, Plugin } from '../data/types';
import {
EarthlyBranchKey,
EarthlyBranchName,
GenderName,
HeavenlyStemKey,
HeavenlyStemName,
StarKey,
StarName,
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[]>> = {};

/**
* 批量加载插件
*
* @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);
};

export const config = ({ mutagens }: Config) => {
if (mutagens) {
// 由于key和value都有可能是不同语言传进来的
// 所以需要将key和value转化为对应的i18n key
const result = {} as Record<keyof typeof heavenlyStems, string[]>;

for (const key in mutagens) {
result[kot<HeavenlyStemKey>(key)] = mutagens[key as HeavenlyStemName]?.map((item) => kot<StarName>(item)) ?? [];
}

Object.assign(_mutagens, result);
}
};

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

/**
* 通过阳历获取星盘信息
*
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 @@ -153,3 +153,9 @@ export type Astrolabe = {

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

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

export type Config = {
mutagens?: ConfigMutagens;
};
20 changes: 18 additions & 2 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 @@ -65,14 +79,16 @@ export const getBrightness = (starName: StarName, index: number): Brightness =>
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

0 comments on commit 3a40867

Please sign in to comment.