Skip to content

Commit

Permalink
✨ upgrade lunar-lite
Browse files Browse the repository at this point in the history
  • Loading branch information
SylarLong committed Jan 2, 2024
1 parent 43f47f0 commit 9ad69a7
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 12,740 deletions.
12,669 changes: 0 additions & 12,669 deletions package-lock.json

This file was deleted.

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.1",
"version": "2.2.2",
"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.0.3"
"lunar-lite": "^0.1.1"
}
}
6 changes: 0 additions & 6 deletions src/__tests__/astro/astro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
getMajorStarBySolarDate,
getSignByLunarDate,
getSignBySolarDate,
getZodiacByLunarYear,
getZodiacBySolarDate,
} from '../../astro';
import { setLanguage } from '../../i18n';
Expand Down Expand Up @@ -660,11 +659,6 @@ describe('Astrolabe', () => {
expect(getZodiacBySolarDate('2023-2-20', 'en-US')).toEqual('rabbit');
});

test('getZodiacByLunarYear()', () => {
expect(getZodiacByLunarYear(2023)).toEqual('兔');
expect(getZodiacByLunarYear(2023, 'en-US')).toEqual('rabbit');
});

test('getSignBySolarDate()', () => {
expect(getSignBySolarDate('2023-9-5')).toEqual('处女座');
expect(getSignBySolarDate('2023-9-5', 'en-US')).toEqual('virgo');
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/astro/palace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('astro/palace', () => {
result: {
soulIndex: 7,
bodyIndex: 5,
heavenlyStemOfSoul: '',
heavenlyStemOfSoul: '',
earthlyBranchOfSoul: '酉',
},
},
Expand All @@ -22,7 +22,7 @@ describe('astro/palace', () => {
result: {
soulIndex: 6,
bodyIndex: 6,
heavenlyStemOfSoul: '',
heavenlyStemOfSoul: '',
earthlyBranchOfSoul: '申',
},
},
Expand Down
29 changes: 2 additions & 27 deletions src/astro/astro.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
getHeavenlyStemAndEarthlyBranchBySolarDate,
getSign,
getZodiac,
heavenlyStemAndEarthlyBranchOfYear,
lunar2solar,
solar2lunar,
} from 'lunar-lite';
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';
Expand Down Expand Up @@ -191,25 +184,7 @@ export const byLunar = (
export const getZodiacBySolarDate = (solarDateStr: string, language?: Language): string => {
language && setLanguage(language);

const { lunarYear } = solar2lunar(solarDateStr);
const yearly = heavenlyStemAndEarthlyBranchOfYear(lunarYear);

return t(kot(getZodiac(yearly[1])));
};

/**
* 通过农历年份获取十二生肖
*
* @version v1.2.1
*
* @param year 农历年份
* @param language 输出语言,默认为中文
* @returns 十二生肖
*/
export const getZodiacByLunarYear = (year: number, language?: Language) => {
language && setLanguage(language);

const yearly = heavenlyStemAndEarthlyBranchOfYear(year);
const { yearly } = getHeavenlyStemAndEarthlyBranchBySolarDate(solarDateStr, 0);

return t(kot(getZodiac(yearly[1])));
};
Expand Down
4 changes: 2 additions & 2 deletions src/star/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ import { fixEarthlyBranchIndex, fixIndex, fixLunarDayIndex, fixLunarMonthIndex }
*/
export const getStartIndex = (solarDateStr: string, timeIndex: number, fixLeap?: boolean) => {
const { heavenlyStemOfSoul, earthlyBranchOfSoul } = getSoulAndBody(solarDateStr, timeIndex, fixLeap);
const { lunarYear, lunarMonth, lunarDay } = solar2lunar(solarDateStr);
const { lunarDay } = solar2lunar(solarDateStr);
const fiveElements = kot<FiveElementsClassKey>(getFiveElementsClass(heavenlyStemOfSoul, earthlyBranchOfSoul));
let remainder = -1; // 余数
let quotient; // 商
let offset = -1; // 循环次数

// 获取当月最大天数
const maxDays = getTotalDaysOfLunarMonth(lunarYear, lunarMonth);
const maxDays = getTotalDaysOfLunarMonth(solarDateStr);

// 如果timeIndex等于12说明是晚子时,需要加一天
let _day = timeIndex === 12 ? lunarDay + 1 : lunarDay;
Expand Down
32 changes: 4 additions & 28 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { solar2lunar, getTotalDaysOfLunarMonth } from 'lunar-lite';
import { EARTHLY_BRANCHES, heavenlyStems, MUTAGEN, STARS_INFO } from '../data';
import { initStars } from '../star';
import {
Expand All @@ -13,9 +12,9 @@ import {
t,
StarKey,
} from '../i18n';
import dayjs from 'dayjs';
import FunctionalStar from '../star/FunctionalStar';
import { HeavenlyStemAndEarthlyBranchDate } from 'lunar-lite/lib/types';
import { solar2lunar } from 'lunar-lite';

/**
* 用于处理索引,将索引锁定在 0~max 范围内
Expand Down Expand Up @@ -88,29 +87,6 @@ export const fixEarthlyBranchIndex = (earthlyBranchName: EarthlyBranchName): num
return fixIndex(EARTHLY_BRANCHES.indexOf(earthlyBranch) - EARTHLY_BRANCHES.indexOf('yinEarthly'));
};

/**
* 处理晚子时日期
*
* @param {string} solarDateStr 阳历日期
* @param {number} numbertimeIndex 时辰序号【0~12】,12代表晚子时
* @returns {LunarDate} LunarDate
*/
export const fixLunarDate = (solarDateStr: string, timeIndex: number) => {
let lunarDate = solar2lunar(solarDateStr);
// 获取当月的天数
const totalDaysOfLunarMonth = getTotalDaysOfLunarMonth(lunarDate.lunarYear, lunarDate.lunarMonth);

if (timeIndex >= 12 && lunarDate.lunarDay >= totalDaysOfLunarMonth) {
// 假如是晚子时并且日期是农历月的最后一天时,月份需要加1
const dt = dayjs(solarDateStr);

dt.add(1, 'day');
lunarDate = solar2lunar(new Date(dt.format()));
}

return lunarDate;
};

/**
* 调整农历月份的索引
*
Expand All @@ -125,12 +101,12 @@ export const fixLunarDate = (solarDateStr: string, timeIndex: number) => {
* @returns {number} 月份索引
*/
export const fixLunarMonthIndex = (solarDateStr: string, timeIndex: number, fixLeap?: boolean) => {
const lunarDate = fixLunarDate(solarDateStr, timeIndex);
const { lunarMonth, lunarDay, isLeap } = lunarDate;
const { lunarMonth, lunarDay, isLeap } = solar2lunar(solarDateStr);
// 紫微斗数以`寅`宫为第一个宫位
const firstIndex = EARTHLY_BRANCHES.indexOf('yinEarthly');
const needToAdd = isLeap && fixLeap && lunarDay > 15 && timeIndex !== 12;

return fixIndex(lunarMonth + 1 - firstIndex + (isLeap && fixLeap && lunarDay > 15 ? 1 : 0));
return fixIndex(lunarMonth + 1 - firstIndex + (needToAdd ? 1 : 0));
};

/**
Expand Down
15 changes: 11 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3870,10 +3870,17 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"

lunar-lite@^0.0.3:
version "0.0.3"
resolved "https://registry.npmjs.org/lunar-lite/-/lunar-lite-0.0.3.tgz#c0a053fb6d585a91c6496352208e28d6ff2c2dde"
integrity sha512-J6K/MU8CLTqBPfijc5u+wnYlxvZAS3k5knjfUakZoz2XTkOB0HerUgXNHyZS2be389J4GSYaUKvDk8mps0sNdQ==
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==
dependencies:
lunar-typescript "^1.6.13"

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==

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

0 comments on commit 9ad69a7

Please sign in to comment.