Skip to content

Commit

Permalink
v0.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
SylarLong committed May 4, 2024
1 parent 032c87b commit ce3cf6a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lunar-lite",
"version": "0.2.1",
"version": "0.2.2",
"description": "精简版的农历和阳历日期转换库。",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
22 changes: 6 additions & 16 deletions src/__tests__/ganzhi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,14 @@ describe("calendar/heavenlyStemAndEarthlyBranch", () => {

data.forEach(({ date, timeIndex, isLeap, result, result2 }) => {
expect(
getHeavenlyStemAndEarthlyBranchByLunarDate(
date,
timeIndex,
isLeap,
{
year: 'exact'
}
).toString(),
getHeavenlyStemAndEarthlyBranchByLunarDate(date, timeIndex, isLeap, {
year: "exact",
}).toString(),
).toBe(result);
expect(
getHeavenlyStemAndEarthlyBranchByLunarDate(
date,
timeIndex,
isLeap,
{
year: 'normal'
}
).toString(),
getHeavenlyStemAndEarthlyBranchByLunarDate(date, timeIndex, isLeap, {
year: "normal",
}).toString(),
).toBe(result2 ?? result);
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/convertor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const normalizeDateStr = (date: string | Date) => {
.split(/[ ]+/)
.map((item) => item.split(/[-:/.]/))
.reduce((prev, next) => prev.concat(next), [])
.map((item) => +item);
.map((item) => Math.abs(+item));
};

/**
Expand Down
16 changes: 11 additions & 5 deletions src/ganzhi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ export const getHeavenlyStemAndEarthlyBranchByLunarDate = (
dateStr: string,
timeIndex: number,
isLeap?: boolean,
options: Options = { year: 'normal' }
options: Options = { year: "normal" },
): HeavenlyStemAndEarthlyBranchDate => {
const solarDate = lunar2solar(dateStr, isLeap);

return getHeavenlyStemAndEarthlyBranchBySolarDate(
solarDate.toString(),
timeIndex,
options
options,
);
};

Expand All @@ -41,7 +41,7 @@ export const getHeavenlyStemAndEarthlyBranchByLunarDate = (
export const getHeavenlyStemAndEarthlyBranchBySolarDate = (
dateStr: string | Date,
timeIndex: number,
options: Options = { year: 'exact' }
options: Options = { year: "exact" },
): HeavenlyStemAndEarthlyBranchDate => {
const [year, month, date] = normalizeDateStr(dateStr);
const solar = Solar.fromYmdHms(
Expand All @@ -54,8 +54,14 @@ export const getHeavenlyStemAndEarthlyBranchBySolarDate = (
);
const lunar = solar.getLunar();

const yearlyGan = options?.year === 'normal' ? lunar.getYearGan() : lunar.getYearGanByLiChun();
const yearlyZhi = options?.year === 'normal' ? lunar.getYearZhi() : lunar.getYearZhiByLiChun();
const yearlyGan =
options?.year === "normal"
? lunar.getYearGan()
: lunar.getYearGanByLiChun();
const yearlyZhi =
options?.year === "normal"
? lunar.getYearZhi()
: lunar.getYearZhiByLiChun();

const yearly: HeavenlyStemAndEarthlyBranch = [
yearlyGan as HeavenlyStem,
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ export type HeavenlyStemAndEarthlyBranchDate = {

export type Options = {
/** exact:立春分界,normal:除夕分界 */
year: 'exact' | 'normal',
}
year: "exact" | "normal";
};

0 comments on commit ce3cf6a

Please sign in to comment.