From b255f929bb6fef80762b025284ebb25d814801ce Mon Sep 17 00:00:00 2001 From: Sylar Date: Sat, 24 Feb 2024 00:36:06 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=F0=9F=AA=84=20support=20global=20plugin=20?= =?UTF-8?q?#165?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 12 ++++++++++++ package.json | 4 ++-- src/__tests__/astro/plugin.test.ts | 24 ++++++++++++++++++++++++ src/astro/FunctionalAstrolabe.ts | 16 +++++++++++++++- src/astro/astro.ts | 18 +++++++++++++----- src/data/types/astro.ts | 3 +++ yarn.lock | 18 +++++++++--------- 7 files changed, 78 insertions(+), 17 deletions(-) create mode 100644 src/__tests__/astro/plugin.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cd7a456..517f0995 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,18 @@ - 🛠️ 修复(fix) - 🧹 琐事(Chore) +## v2.3.0 + +- 🪄 功能(feature) + + 🇨🇳 + + - 支持全局插件 #165 + + 🇺🇸 + + - support global plugin #165 + ## v2.2.3 - 🛠️ 修复(fix) diff --git a/package.json b/package.json index 6655507b..984482f8 100644 --- a/package.json +++ b/package.json @@ -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", @@ -71,6 +71,6 @@ "dependencies": { "dayjs": "^1.11.10", "i18next": "^23.5.1", - "lunar-lite": "^0.1.1" + "lunar-lite": "^0.1.2" } } diff --git a/src/__tests__/astro/plugin.test.ts b/src/__tests__/astro/plugin.test.ts new file mode 100644 index 00000000..438100a8 --- /dev/null +++ b/src/__tests__/astro/plugin.test.ts @@ -0,0 +1,24 @@ +import { astro } from '../..'; +import FunctionalAstrolabe from '../../astro/FunctionalAstrolabe'; + +export interface IAstrolabe extends FunctionalAstrolabe { + myNewFunc: () => string; +} + +// 创建一个插件函数 +export function myTestPlugin(this: IAstrolabe): void { + // 实现插件应用逻辑 + this.myNewFunc = () => { + return this.fiveElementsClass; + }; +} + +astro.loadPlugins([myTestPlugin]); + +describe('plugin test', () => { + test('plugin', () => { + const astrolabe = astro.bySolar('2023-10-18', 4, 'female'); + + expect(astrolabe.myNewFunc()).toEqual('火六局'); + }); +}); diff --git a/src/astro/FunctionalAstrolabe.ts b/src/astro/FunctionalAstrolabe.ts index 32baefb0..686892ad 100644 --- a/src/astro/FunctionalAstrolabe.ts +++ b/src/astro/FunctionalAstrolabe.ts @@ -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'; @@ -192,6 +192,12 @@ const _getHoroscopeBySolarDate = ( * 文档地址:https://docs.iztro.com/posts/astrolabe.html#functionalastrolabe */ export interface IFunctionalAstrolabe extends Astrolabe { + /** + * 插件注入方法 + * + * @param plugin 插件函数 + */ + use(plugin: Plugin): void; /** * 获取运限数据 * @@ -292,6 +298,9 @@ export default class FunctionalAstrolabe implements IFunctionalAstrolabe { fiveElementsClass; palaces; + // 保存插件列表 + private plugins: Plugin[] = []; + constructor(data: Astrolabe) { this.gender = data.gender; this.solarDate = data.solarDate; @@ -312,6 +321,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; diff --git a/src/astro/astro.ts b/src/astro/astro.ts index 7814e52c..11081379 100644 --- a/src/astro/astro.ts +++ b/src/astro/astro.ts @@ -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 { Language } from '../data/types'; +import { Language, Plugin } from '../data/types'; import { EarthlyBranchKey, EarthlyBranchName, GenderName, HeavenlyStemKey, kot, setLanguage, t } from '../i18n'; import { getAdjectiveStar, getBoShi12, getchangsheng12, getMajorStar, getMinorStar, getYearly12 } from '../star'; import { fixIndex, translateChineseDate } from '../utils'; @@ -8,6 +8,12 @@ import FunctionalAstrolabe from './FunctionalAstrolabe'; import FunctionalPalace, { IFunctionalPalace } from './FunctionalPalace'; import { getPalaceNames, getSoulAndBody, getHoroscope, getFiveElementsClass } from './palace'; +const _plugins = [] as Plugin[]; + +export const loadPlugins = (plugins: Plugin[]) => { + Array.prototype.push.apply(_plugins, plugins); +}; + /** * 通过阳历获取星盘信息 * @@ -40,13 +46,13 @@ export const astrolabeBySolarDate = ( * @param language 输出语言 * @returns 星盘信息 */ -export const bySolar = ( +export function bySolar( solarDateStr: string, timeIndex: number, gender: GenderName, fixLeap: boolean = true, language?: Language, -) => { +): T { language && setLanguage(language); const palaces: IFunctionalPalace[] = []; @@ -121,8 +127,10 @@ export const bySolar = ( palaces, }); - return result; -}; + _plugins.map((plugin) => result.use(plugin)); + + return result as T; +} /** * 通过农历获取星盘信息 diff --git a/src/data/types/astro.ts b/src/data/types/astro.ts index 6e862e2d..680d72d3 100644 --- a/src/data/types/astro.ts +++ b/src/data/types/astro.ts @@ -150,3 +150,6 @@ export type Astrolabe = { /** 十二宫数据 */ palaces: IFunctionalPalace[]; }; + +// 定义一个接口,表示插件函数的类型 +export type Plugin = () => void; diff --git a/yarn.lock b/yarn.lock index 8922be87..278e2c93 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" From 7d1cf398db0f6aac3b6a511efb96b30f0c015350 Mon Sep 17 00:00:00 2001 From: Sylar Date: Sat, 24 Feb 2024 11:21:39 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=AA=84=20support=20global=20plugin=20?= =?UTF-8?q?#165?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__tests__/astro/plugin.test.ts | 23 +++++++++++++++++++++++ src/astro/astro.ts | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/src/__tests__/astro/plugin.test.ts b/src/__tests__/astro/plugin.test.ts index 438100a8..1ef7bb9f 100644 --- a/src/__tests__/astro/plugin.test.ts +++ b/src/__tests__/astro/plugin.test.ts @@ -3,6 +3,7 @@ import FunctionalAstrolabe from '../../astro/FunctionalAstrolabe'; export interface IAstrolabe extends FunctionalAstrolabe { myNewFunc: () => string; + majorStar: () => string; } // 创建一个插件函数 @@ -13,12 +14,34 @@ export function myTestPlugin(this: IAstrolabe): void { }; } +// 创建二个插件函数 +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); describe('plugin test', () => { test('plugin', () => { const astrolabe = astro.bySolar('2023-10-18', 4, 'female'); expect(astrolabe.myNewFunc()).toEqual('火六局'); + expect(astrolabe.majorStar()).toEqual('七杀'); }); }); diff --git a/src/astro/astro.ts b/src/astro/astro.ts index 11081379..468d5af5 100644 --- a/src/astro/astro.ts +++ b/src/astro/astro.ts @@ -14,6 +14,10 @@ export const loadPlugins = (plugins: Plugin[]) => { Array.prototype.push.apply(_plugins, plugins); }; +export const loadPlugin = (plugin: Plugin) => { + _plugins.push(plugin); +}; + /** * 通过阳历获取星盘信息 * From 3a40867984f64c22f69f8ec7522954b2228a42c9 Mon Sep 17 00:00:00 2001 From: Sylar Date: Sat, 24 Feb 2024 19:07:15 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=AA=84=20support=20customized=20mutag?= =?UTF-8?q?ens?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/__tests__/astro/plugin.test.ts | 20 +++++++++++++ src/astro/FunctionalAstrolabe.ts | 2 ++ src/astro/astro.ts | 48 ++++++++++++++++++++++++++++-- src/data/types/astro.ts | 6 ++++ src/utils/index.ts | 20 +++++++++++-- 6 files changed, 92 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 984482f8..f0c09f62 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/astro/plugin.test.ts b/src/__tests__/astro/plugin.test.ts index 1ef7bb9f..abbbca33 100644 --- a/src/__tests__/astro/plugin.test.ts +++ b/src/__tests__/astro/plugin.test.ts @@ -37,6 +37,8 @@ export function myTestPlugin2(this: IAstrolabe): void { astro.loadPlugins([myTestPlugin]); astro.loadPlugin(myTestPlugin2); +astro.config({ mutagens: { 庚: ['太阳', '武曲', '天同', '天相'] } }); + describe('plugin test', () => { test('plugin', () => { const astrolabe = astro.bySolar('2023-10-18', 4, 'female'); @@ -44,4 +46,22 @@ describe('plugin test', () => { expect(astrolabe.myNewFunc()).toEqual('火六局'); expect(astrolabe.majorStar()).toEqual('七杀'); }); + + test('changed configuration', () => { + const astrolabe = astro.bySolar('2010-10-18', 4, 'female'); + + expect(astrolabe.palace('命宫')?.hasMutagen('忌')).toBeFalsy(); + expect(astrolabe.palace('夫妻')?.hasMutagen('忌')).toBeTruthy(); + }); + + test('not changed configuration', () => { + const astrolabe = astro.bySolar('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(); + }); }); diff --git a/src/astro/FunctionalAstrolabe.ts b/src/astro/FunctionalAstrolabe.ts index 686892ad..0b9ab7ff 100644 --- a/src/astro/FunctionalAstrolabe.ts +++ b/src/astro/FunctionalAstrolabe.ts @@ -195,6 +195,8 @@ export interface IFunctionalAstrolabe extends Astrolabe { /** * 插件注入方法 * + * @version v2.3.0 + * * @param plugin 插件函数 */ use(plugin: Plugin): void; diff --git a/src/astro/astro.ts b/src/astro/astro.ts index 468d5af5..eadf8693 100644 --- a/src/astro/astro.ts +++ b/src/astro/astro.ts @@ -1,7 +1,18 @@ 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'; @@ -9,15 +20,46 @@ import FunctionalPalace, { IFunctionalPalace } from './FunctionalPalace'; import { getPalaceNames, getSoulAndBody, getHoroscope, getFiveElementsClass } from './palace'; const _plugins = [] as Plugin[]; +const _mutagens: Partial> = {}; +/** + * 批量加载插件 + * + * @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; + + for (const key in mutagens) { + result[kot(key)] = mutagens[key as HeavenlyStemName]?.map((item) => kot(item)) ?? []; + } + + Object.assign(_mutagens, result); + } +}; + +export const getConfig = () => ({ mutagens: _mutagens }); + /** * 通过阳历获取星盘信息 * diff --git a/src/data/types/astro.ts b/src/data/types/astro.ts index 680d72d3..1933dc25 100644 --- a/src/data/types/astro.ts +++ b/src/data/types/astro.ts @@ -153,3 +153,9 @@ export type Astrolabe = { // 定义一个接口,表示插件函数的类型 export type Plugin = () => void; + +export type ConfigMutagens = Partial>; + +export type Config = { + mutagens?: ConfigMutagens; +}; diff --git a/src/utils/index.ts b/src/utils/index.ts index 55014f8e..339d5d77 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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 范围内 @@ -65,14 +79,16 @@ export const getBrightness = (starName: StarName, index: number): Brightness => export const getMutagen = (starName: StarName, heavenlyStemName: HeavenlyStemName): Mutagen => { const heavenlyStem = kot(heavenlyStemName, 'Heavenly'); const starKey = kot(starName); + const target = getTargetMutagens(heavenlyStem); - return t(MUTAGEN[heavenlyStems[heavenlyStem].mutagen.indexOf(starKey as never)]); + return t(MUTAGEN[target.indexOf(starKey as never)]); }; export const getMutagensByHeavenlyStem = (heavenlyStemName: HeavenlyStemName): StarName[] => { const heavenlyStem = kot(heavenlyStemName, 'Heavenly'); + const target = getTargetMutagens(heavenlyStem); - return heavenlyStems[heavenlyStem].mutagen.map((star) => t(star)); + return target.map((star) => t(star)); }; /** From 5a04649870e1418ee2e07309a89281c27b010d17 Mon Sep 17 00:00:00 2001 From: Sylar Date: Sat, 24 Feb 2024 19:07:51 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=F0=9F=A7=B9=20restore=20package.json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index f0c09f62..984482f8 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,6 @@ "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", From 0ddcb4326cf65892f4197d4c9837cac698c238ca Mon Sep 17 00:00:00 2001 From: Sylar Date: Sat, 24 Feb 2024 19:12:38 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=F0=9F=A7=B9=20update=20changelog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 517f0995..a1d2b2f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,10 +14,12 @@ 🇨🇳 - 支持全局插件 #165 + - 支持全局配置 #165 🇺🇸 - support global plugin #165 + - support global configuration #165 ## v2.2.3 From 13fcae0e106f24d767679838f47a7910a0c446aa Mon Sep 17 00:00:00 2001 From: Sylar Date: Sun, 25 Feb 2024 21:35:40 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=F0=9F=AA=84=20support=20brightness=20confi?= =?UTF-8?q?guration=20#165?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__tests__/astro/plugin.test.ts | 9 ++++++++- src/astro/astro.ts | 24 ++++++++++++++++++++---- src/data/types/astro.ts | 15 +++++++++++++-- src/utils/index.ts | 8 +++++++- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/__tests__/astro/plugin.test.ts b/src/__tests__/astro/plugin.test.ts index abbbca33..4fdc7bd5 100644 --- a/src/__tests__/astro/plugin.test.ts +++ b/src/__tests__/astro/plugin.test.ts @@ -37,7 +37,12 @@ export function myTestPlugin2(this: IAstrolabe): void { astro.loadPlugins([myTestPlugin]); astro.loadPlugin(myTestPlugin2); -astro.config({ mutagens: { 庚: ['太阳', '武曲', '天同', '天相'] } }); +astro.config({ + mutagens: { 庚: ['太阳', '武曲', '天同', '天相'] }, + brightness: { + 贪狼: ['旺', '旺', '旺', '旺', '旺', '旺', '旺', '旺', '旺', '旺', '旺', '旺'], + }, +}); describe('plugin test', () => { test('plugin', () => { @@ -52,6 +57,7 @@ describe('plugin test', () => { expect(astrolabe.palace('命宫')?.hasMutagen('忌')).toBeFalsy(); expect(astrolabe.palace('夫妻')?.hasMutagen('忌')).toBeTruthy(); + expect(astrolabe.star('贪狼').withBrightness('旺')).toBeTruthy(); }); test('not changed configuration', () => { @@ -63,5 +69,6 @@ describe('plugin test', () => { expect(astrolabe.palace('田宅')?.hasMutagen('禄')).toBeFalsy(); expect(astrolabe.palace('财帛')?.fliesTo('夫妻', '科')).toBeTruthy(); expect(astrolabe.palace('财帛')?.fliesTo('仆役', '忌')).toBeTruthy(); + expect(astrolabe.star('紫微').withBrightness('旺')).toBeTruthy(); }); }); diff --git a/src/astro/astro.ts b/src/astro/astro.ts index eadf8693..d435b41a 100644 --- a/src/astro/astro.ts +++ b/src/astro/astro.ts @@ -2,6 +2,7 @@ import { getHeavenlyStemAndEarthlyBranchBySolarDate, getSign, getZodiac, lunar2s import { CHINESE_TIME, EARTHLY_BRANCHES, HEAVENLY_STEMS, TIME_RANGE, earthlyBranches, heavenlyStems } from '../data'; import { Config, Language, Plugin } from '../data/types'; import { + BrightnessKey, EarthlyBranchKey, EarthlyBranchName, GenderName, @@ -21,6 +22,7 @@ import { getPalaceNames, getSoulAndBody, getHoroscope, getFiveElementsClass } fr const _plugins = [] as Plugin[]; const _mutagens: Partial> = {}; +const _brightness: Partial> = {}; /** * 批量加载插件 @@ -44,10 +46,14 @@ export const loadPlugin = (plugin: Plugin) => { _plugins.push(plugin); }; -export const config = ({ mutagens }: Config) => { +/** + * + * @param {Config} param0 自定义配置 + */ +export const config = ({ mutagens, brightness }: Config) => { + // 由于key和value都有可能是不同语言传进来的 + // 所以需要将key和value转化为对应的i18n key if (mutagens) { - // 由于key和value都有可能是不同语言传进来的 - // 所以需要将key和value转化为对应的i18n key const result = {} as Record; for (const key in mutagens) { @@ -56,9 +62,19 @@ export const config = ({ mutagens }: Config) => { Object.assign(_mutagens, result); } + + if (brightness) { + const result = {} as Record; + + for (const key in brightness) { + result[kot(key)] = brightness[key as StarName]?.map((item) => kot(item)) ?? []; + } + + Object.assign(_brightness, result); + } }; -export const getConfig = () => ({ mutagens: _mutagens }); +export const getConfig = () => ({ mutagens: _mutagens, brightness: _brightness }); /** * 通过阳历获取星盘信息 diff --git a/src/data/types/astro.ts b/src/data/types/astro.ts index 1933dc25..ad42c3a9 100644 --- a/src/data/types/astro.ts +++ b/src/data/types/astro.ts @@ -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'; @@ -151,11 +158,15 @@ export type Astrolabe = { palaces: IFunctionalPalace[]; }; -// 定义一个接口,表示插件函数的类型 +/** + * 定义一个接口,表示插件函数的类型 + * */ export type Plugin = () => void; export type ConfigMutagens = Partial>; +export type ConfigBrightness = Partial>; export type Config = { mutagens?: ConfigMutagens; + brightness?: ConfigBrightness; }; diff --git a/src/utils/index.ts b/src/utils/index.ts index 339d5d77..9d4d230f 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -72,8 +72,14 @@ export const earthlyBranchIndexToPalaceIndex = (earthlyBranchName: EarthlyBranch */ export const getBrightness = (starName: StarName, index: number): Brightness => { const star = kot(starName); + const { brightness } = getConfig(); + const targetBrightness = brightness[star] ? brightness[star] : STARS_INFO[star]?.brightness; - return t(STARS_INFO[star]?.brightness[fixIndex(index)]); + if (!targetBrightness) { + return ''; + } + + return t(targetBrightness[fixIndex(index)]); }; export const getMutagen = (starName: StarName, heavenlyStemName: HeavenlyStemName): Mutagen => { From 2f7bfda9d88db5d593c3cb8d97407c1636b2aa94 Mon Sep 17 00:00:00 2001 From: Sylar Date: Sun, 25 Feb 2024 22:01:59 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20fix=20Cognitive=20C?= =?UTF-8?q?omplexity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/astro/astro.ts | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/astro/astro.ts b/src/astro/astro.ts index d435b41a..fadc9c06 100644 --- a/src/astro/astro.ts +++ b/src/astro/astro.ts @@ -1,5 +1,5 @@ import { getHeavenlyStemAndEarthlyBranchBySolarDate, getSign, getZodiac, lunar2solar, solar2lunar } from 'lunar-lite'; -import { CHINESE_TIME, EARTHLY_BRANCHES, HEAVENLY_STEMS, TIME_RANGE, earthlyBranches, heavenlyStems } from '../data'; +import { CHINESE_TIME, EARTHLY_BRANCHES, HEAVENLY_STEMS, TIME_RANGE, earthlyBranches } from '../data'; import { Config, Language, Plugin } from '../data/types'; import { BrightnessKey, @@ -7,9 +7,7 @@ import { EarthlyBranchName, GenderName, HeavenlyStemKey, - HeavenlyStemName, StarKey, - StarName, kot, setLanguage, t, @@ -47,30 +45,26 @@ export const loadPlugin = (plugin: Plugin) => { }; /** + * 全局配置四化和亮度 + * + * 由于key和value都有可能是不同语言传进来的, + * 所以需会将key和value转化为对应的i18n key。 + * + * @version 2.3.0 * * @param {Config} param0 自定义配置 */ export const config = ({ mutagens, brightness }: Config) => { - // 由于key和value都有可能是不同语言传进来的 - // 所以需要将key和value转化为对应的i18n key if (mutagens) { - const result = {} as Record; - - for (const key in mutagens) { - result[kot(key)] = mutagens[key as HeavenlyStemName]?.map((item) => kot(item)) ?? []; - } - - Object.assign(_mutagens, result); + Object.entries(mutagens).forEach(([key, value]) => { + _mutagens[kot(key)] = value.map((item) => kot(item)) ?? []; + }); } if (brightness) { - const result = {} as Record; - - for (const key in brightness) { - result[kot(key)] = brightness[key as StarName]?.map((item) => kot(item)) ?? []; - } - - Object.assign(_brightness, result); + Object.entries(brightness).forEach(([key, value]) => { + _brightness[kot(key)] = value.map((item) => kot(item)) ?? []; + }); } };