lunar-typescript
Version:
lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)、佛历和道历工具,支持星座、儒略日、干支、生肖、节气、节日、彭祖百忌、每日宜忌、吉神宜趋、凶煞宜忌、吉神(喜神/福神/财神/阳贵神/阴贵神)方位、胎神方位、冲煞、纳音、星宿、八字、五行、十神、建除十二值星、青龙名堂等十二神、黄道日及吉凶等。lunar is a calendar library for Solar and Chinese Lunar.
89 lines (88 loc) • 2.11 kB
JavaScript
import { LunarUtil } from "./LunarUtil.mjs";
import { LiuNian } from "./LiuNian.mjs";
import { XiaoYun } from "./XiaoYun.mjs";
export class DaYun {
constructor(yun, index) {
const lunar = yun.getLunar();
const birthYear = lunar.getSolar().getYear();
const year = yun.getStartSolar().getYear();
let startYear, startAge, endYear, endAge;
if (index < 1) {
startYear = birthYear;
startAge = 1;
endYear = year - 1;
endAge = year - birthYear;
} else {
startYear = year + (index - 1) * 10;
startAge = startYear - birthYear + 1;
endYear = startYear + 9;
endAge = startAge + 9;
}
this._startYear = startYear;
this._endYear = endYear;
this._startAge = startAge;
this._endAge = endAge;
this._index = index;
this._yun = yun;
this._lunar = lunar;
}
getStartYear() {
return this._startYear;
}
getEndYear() {
return this._endYear;
}
getStartAge() {
return this._startAge;
}
getEndAge() {
return this._endAge;
}
getIndex() {
return this._index;
}
getLunar() {
return this._lunar;
}
getGanZhi() {
if (this._index < 1) {
return "";
}
let offset = LunarUtil.getJiaZiIndex(this._lunar.getMonthInGanZhiExact());
offset += this._yun.isForward() ? this._index : -this._index;
const size = LunarUtil.JIA_ZI.length;
if (offset >= size) {
offset -= size;
}
if (offset < 0) {
offset += size;
}
return LunarUtil.JIA_ZI[offset];
}
getXun() {
return LunarUtil.getXun(this.getGanZhi());
}
getXunKong() {
return LunarUtil.getXunKong(this.getGanZhi());
}
getLiuNian(n = 10) {
if (this._index < 1) {
n = this._endYear - this._startYear + 1;
}
const l = [];
for (let i = 0; i < n; i++) {
l.push(new LiuNian(this, i));
}
return l;
}
getXiaoYun(n = 10) {
if (this._index < 1) {
n = this._endYear - this._startYear + 1;
}
const l = [];
for (let i = 0; i < n; i++) {
l.push(new XiaoYun(this, i, this._yun.isForward()));
}
return l;
}
}