1
0
mirror of synced 2025-12-17 18:48:02 +08:00

v1.2.1 修复1-99年日期错乱的问题;增加单元测试。

This commit is contained in:
6tail
2021-04-13 22:20:23 +08:00
parent 8718348cd7
commit 0c000c99a7
20 changed files with 1028 additions and 7 deletions

141
__tests__/EightChar.test.js Normal file
View File

@@ -0,0 +1,141 @@
var {Solar} = require('../lunar');
test('test1', () => {
const solar = Solar.fromYmdHms(2005, 12, 23, 8, 37, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
expect(eightChar.getYear()).toBe('乙酉');
expect(eightChar.getMonth()).toBe('戊子');
expect(eightChar.getDay()).toBe('辛巳');
expect(eightChar.getTime()).toBe('壬辰');
});
test('test2', () => {
const solar = Solar.fromYmdHms(1988, 2, 15, 23, 30, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
expect(eightChar.getYear()).toBe('戊辰');
expect(eightChar.getMonth()).toBe('甲寅');
expect(eightChar.getDay()).toBe('庚子');
expect(eightChar.getTime()).toBe('戊子');
});
test('test2a', () => {
const solar = Solar.fromYmdHms(1988, 2, 15, 23, 30, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
eightChar.setSect(1);
expect(eightChar.getYear()).toBe('戊辰');
expect(eightChar.getMonth()).toBe('甲寅');
expect(eightChar.getDay()).toBe('辛丑');
expect(eightChar.getTime()).toBe('戊子');
});
test('test3', () => {
const solar = Solar.fromYmdHms(1988, 2, 15, 22, 30, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
expect(eightChar.getYear()).toBe('戊辰');
expect(eightChar.getMonth()).toBe('甲寅');
expect(eightChar.getDay()).toBe('庚子');
expect(eightChar.getTime()).toBe('丁亥');
});
test('test4', () => {
const solar = Solar.fromYmdHms(1988, 2, 2, 22, 30, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
expect(eightChar.getYear()).toBe('丁卯');
expect(eightChar.getMonth()).toBe('癸丑');
expect(eightChar.getDay()).toBe('丁亥');
expect(eightChar.getTime()).toBe('辛亥');
});
test('地支藏干', () => {
const solar = Solar.fromYmdHms(2005, 12, 23, 8, 37, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
expect(eightChar.getYearHideGan() + '').toBe('辛');
expect(eightChar.getMonthHideGan() + '').toBe('癸');
expect(eightChar.getDayHideGan() + '').toBe('丙,庚,戊');
expect(eightChar.getTimeHideGan() + '').toBe('戊,乙,癸');
});
test('天干十神', () => {
const solar = Solar.fromYmdHms(2005, 12, 23, 8, 37, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
expect(eightChar.getYearShiShenGan()).toBe('偏财');
expect(eightChar.getMonthShiShenGan()).toBe('正印');
expect(eightChar.getDayShiShenGan()).toBe('日主');
expect(eightChar.getTimeShiShenGan()).toBe('伤官');
});
test('地支十神', () => {
const solar = Solar.fromYmdHms(2005, 12, 23, 8, 37, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
expect(eightChar.getYearShiShenZhi() + '').toBe('比肩');
expect(eightChar.getMonthShiShenZhi() + '').toBe('食神');
expect(eightChar.getDayShiShenZhi() + '').toBe('正官,劫财,正印');
expect(eightChar.getTimeShiShenZhi() + '').toBe('正印,偏财,食神');
});
test('地势', () => {
const solar = Solar.fromYmdHms(2005, 12, 23, 8, 37, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
expect(eightChar.getYearDiShi()).toBe('临官');
expect(eightChar.getMonthDiShi()).toBe('长生');
expect(eightChar.getDayDiShi()).toBe('死');
expect(eightChar.getTimeDiShi()).toBe('墓');
});
test('纳音', () => {
const solar = Solar.fromYmdHms(2005, 12, 23, 8, 37, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
expect(eightChar.getYearNaYin()).toBe('泉中水');
expect(eightChar.getMonthNaYin()).toBe('霹雳火');
expect(eightChar.getDayNaYin()).toBe('白蜡金');
expect(eightChar.getTimeNaYin()).toBe('长流水');
});
test('胎元', () => {
let solar = Solar.fromYmdHms(2005, 12, 23, 8, 37, 0);
let lunar = solar.getLunar();
let eightChar = lunar.getEightChar();
expect(eightChar.getTaiYuan()).toBe('己卯');
solar = Solar.fromYmdHms(1995, 12, 18, 10, 28, 0);
lunar = solar.getLunar();
eightChar = lunar.getEightChar();
expect(eightChar.getTaiYuan()).toBe('己卯');
});
test('命宫', () => {
let solar = Solar.fromYmdHms(2005, 12, 23, 8, 37, 0);
let lunar = solar.getLunar();
let eightChar = lunar.getEightChar();
expect(eightChar.getMingGong()).toBe('己丑');
solar = Solar.fromYmdHms(1998, 6, 11, 4, 28, 0);
lunar = solar.getLunar();
eightChar = lunar.getEightChar();
expect(eightChar.getMingGong()).toBe('辛酉');
solar = Solar.fromYmdHms(1995, 12, 18, 10, 28, 0);
lunar = solar.getLunar();
eightChar = lunar.getEightChar();
expect(eightChar.getMingGong()).toBe('戊子');
});
test('身宫', () => {
const solar = Solar.fromYmdHms(1995, 12, 18, 10, 28, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
expect(eightChar.getShenGong()).toBe('壬午');
});

147
__tests__/Fu.test.js Normal file
View File

@@ -0,0 +1,147 @@
var {Solar} = require('../lunar');
test('test1()', () => {
const solar = Solar.fromYmd(2011,7,14);
const lunar = solar.getLunar();
const fu = lunar.getFu();
if(null==fu){
throw solar.toYmd();
}
expect(fu.toString()).toBe('初伏');
expect(fu.toFullString()).toBe('初伏第1天');
});
test('test2()', () => {
const solar = Solar.fromYmd(2011,7,23);
const lunar = solar.getLunar();
const fu = lunar.getFu();
if(null==fu){
throw solar.toYmd();
}
expect(fu.toString()).toBe('初伏');
expect(fu.toFullString()).toBe('初伏第10天');
});
test('test3()', () => {
const solar = Solar.fromYmd(2011,7,24);
const lunar = solar.getLunar();
const fu = lunar.getFu();
if(null==fu){
throw solar.toYmd();
}
expect(fu.toString()).toBe('中伏');
expect(fu.toFullString()).toBe('中伏第1天');
});
test('test4()', () => {
const solar = Solar.fromYmd(2011,8,12);
const lunar = solar.getLunar();
const fu = lunar.getFu();
if(null==fu){
throw solar.toYmd();
}
expect(fu.toString()).toBe('中伏');
expect(fu.toFullString()).toBe('中伏第20天');
});
test('test5()', () => {
const solar = Solar.fromYmd(2011,8,13);
const lunar = solar.getLunar();
const fu = lunar.getFu();
if(null==fu){
throw solar.toYmd();
}
expect(fu.toString()).toBe('末伏');
expect(fu.toFullString()).toBe('末伏第1天');
});
test('test6()', () => {
const solar = Solar.fromYmd(2011,8,22);
const lunar = solar.getLunar();
const fu = lunar.getFu();
if(null==fu){
throw solar.toYmd();
}
expect(fu.toString()).toBe('末伏');
expect(fu.toFullString()).toBe('末伏第10天');
});
test('test7()', () => {
const solar = Solar.fromYmd(2011,7,13);
const lunar = solar.getLunar();
const fu = lunar.getFu();
expect(fu).toBe(null);
});
test('test8()', () => {
const solar = Solar.fromYmd(2011,8,23);
const lunar = solar.getLunar();
const fu = lunar.getFu();
expect(fu).toBe(null);
});
test('test9()', () => {
const solar = Solar.fromYmd(2012,7,18);
const lunar = solar.getLunar();
const fu = lunar.getFu();
if(null==fu){
throw solar.toYmd();
}
expect(fu.toString()).toBe('初伏');
expect(fu.toFullString()).toBe('初伏第1天');
});
test('test10()', () => {
const solar = Solar.fromYmd(2012,8,5);
const lunar = solar.getLunar();
const fu = lunar.getFu();
if(null==fu){
throw solar.toYmd();
}
expect(fu.toString()).toBe('中伏');
expect(fu.toFullString()).toBe('中伏第9天');
});
test('test11()', () => {
const solar = Solar.fromYmd(2012,8,8);
const lunar = solar.getLunar();
const fu = lunar.getFu();
if(null==fu){
throw solar.toYmd();
}
expect(fu.toString()).toBe('末伏');
expect(fu.toFullString()).toBe('末伏第2天');
});
test('test12()', () => {
const solar = Solar.fromYmd(2020,7,17);
const lunar = solar.getLunar();
const fu = lunar.getFu();
if(null==fu){
throw solar.toYmd();
}
expect(fu.toString()).toBe('初伏');
expect(fu.toFullString()).toBe('初伏第2天');
});
test('test13()', () => {
const solar = Solar.fromYmd(2020,7,26);
const lunar = solar.getLunar();
const fu = lunar.getFu();
if(null==fu){
throw solar.toYmd();
}
expect(fu.toString()).toBe('中伏');
expect(fu.toFullString()).toBe('中伏第1天');
});
test('test14()', () => {
const solar = Solar.fromYmd(2020,8,24);
const lunar = solar.getLunar();
const fu = lunar.getFu();
if(null==fu){
throw solar.toYmd();
}
expect(fu.toString()).toBe('末伏');
expect(fu.toFullString()).toBe('末伏第10天');
});

40
__tests__/Holiday.test.js Normal file
View File

@@ -0,0 +1,40 @@
var {HolidayUtil} = require('../lunar');
test('test1', () => {
expect(HolidayUtil.getHoliday('2020-01-01') + '').toBe('2020-01-01 元旦节 2020-01-01');
// 将2020-01-01修改为春节
HolidayUtil.fix('202001011120200101');
expect(HolidayUtil.getHoliday('2020-01-01') + '').toBe('2020-01-01 春节 2020-01-01');
// 追加2099-01-01为元旦节
HolidayUtil.fix('209901010120990101');
expect(HolidayUtil.getHoliday('2099-01-01') + '').toBe('2099-01-01 元旦节 2099-01-01');
// 将2020-01-01修改为春节并追加2099-01-01为元旦节
HolidayUtil.fix('202001011120200101209901010120990101');
expect(HolidayUtil.getHoliday('2020-01-01') + '').toBe('2020-01-01 春节 2020-01-01');
expect(HolidayUtil.getHoliday('2099-01-01') + '').toBe('2099-01-01 元旦节 2099-01-01');
// 更改节假日名称
let names = HolidayUtil.NAMES;
names[0] = '元旦';
names[1] = '大年初一';
HolidayUtil.fix(names, '');
expect(HolidayUtil.getHoliday('2020-01-01') + '').toBe('2020-01-01 大年初一 2020-01-01');
expect(HolidayUtil.getHoliday('2099-01-01') + '').toBe('2099-01-01 元旦 2099-01-01');
// 追加节假日名称和数据
names = [];
for (let i = 0, j = HolidayUtil.NAMES.length; i < j; i++) {
names[i] = HolidayUtil.NAMES[i];
}
names[9] = '我的生日';
names[10] = '结婚纪念日';
names[11] = '她的生日';
HolidayUtil.fix(names, '20210529912021052920211111:12021111120211201;120211201');
expect(HolidayUtil.getHoliday('2021-05-29') + '').toBe('2021-05-29 我的生日 2021-05-29');
expect(HolidayUtil.getHoliday('2021-11-11') + '').toBe('2021-11-11 结婚纪念日 2021-11-11');
expect(HolidayUtil.getHoliday('2021-12-01') + '').toBe('2021-12-01 她的生日 2021-12-01');
});

88
__tests__/JieQi.test.js Normal file
View File

@@ -0,0 +1,88 @@
var {Solar} = require('../lunar');
test('test1', () => {
const solar = Solar.fromYmd(1986, 1, 5);
const lunar = solar.getLunar();
expect(lunar.getJie()).toBe('小寒');
expect(lunar.getJieQi()).toBe('小寒');
expect(lunar.getCurrentJieQi() + '').toBe('小寒');
expect(lunar.getCurrentJie() + '').toBe('小寒');
expect(lunar.getCurrentQi()).toBe(null);
expect(lunar.getQi()).toBe('');
expect(lunar.getPrevJie().getName()).toBe('大雪');
expect(lunar.getPrevQi().getName()).toBe('冬至');
expect(lunar.getPrevJieQi().getName()).toBe('冬至');
});
test('test2', () => {
const solar = Solar.fromYmdHms(1986, 1, 20, 17, 0, 0);
const lunar = solar.getLunar();
expect(lunar.getQi()).toBe('大寒');
expect(lunar.getJieQi()).toBe('大寒');
expect(lunar.getCurrentJieQi() + '').toBe('大寒');
expect(lunar.getCurrentQi() + '').toBe('大寒');
expect(lunar.getCurrentJie()).toBe(null);
expect(lunar.getJie()).toBe('');
expect(lunar.getNextJie().getName()).toBe('立春');
expect(lunar.getNextQi().getName()).toBe('雨水');
expect(lunar.getNextJieQi().getName()).toBe('立春');
});
test('test3', () => {
const solar = Solar.fromYmdHms(1986, 1, 20, 14, 0, 0);
const lunar = solar.getLunar();
expect(lunar.getPrevJie().getName()).toBe('小寒');
expect(lunar.getPrevQi().getName()).toBe('冬至');
expect(lunar.getPrevJieQi().getName()).toBe('小寒');
});
test('test4', () => {
const solar = Solar.fromYmd(1986, 12, 7);
const lunar = solar.getLunar();
expect(lunar.getJie()).toBe('大雪');
expect(lunar.getJieQi()).toBe('大雪');
expect(lunar.getCurrentJieQi() + '').toBe('大雪');
expect(lunar.getCurrentJie() + '').toBe('大雪');
expect(lunar.getCurrentQi()).toBe(null);
expect(lunar.getQi()).toBe('');
expect(lunar.getNextJie().getName()).toBe('大雪');
expect(lunar.getNextQi().getName()).toBe('冬至');
expect(lunar.getNextJieQi().getName()).toBe('大雪');
});
test('test5', () => {
const solar = Solar.fromYmd(1986, 1, 1);
const lunar = solar.getLunar();
expect(lunar.getJie()).toBe('');
expect(lunar.getQi()).toBe('');
expect(lunar.getJieQi()).toBe('');
expect(lunar.getCurrentJieQi()).toBe(null);
expect(lunar.getCurrentJie()).toBe(null);
expect(lunar.getCurrentQi()).toBe(null);
expect(lunar.getPrevJie().getName()).toBe('大雪');
expect(lunar.getPrevQi().getName()).toBe('冬至');
expect(lunar.getPrevJieQi().getName()).toBe('冬至');
expect(lunar.getNextJie().getName()).toBe('小寒');
expect(lunar.getNextQi().getName()).toBe('大寒');
expect(lunar.getNextJieQi().getName()).toBe('小寒');
});
test('test6', () => {
const solar = Solar.fromYmd(2012, 12, 25);
const lunar = solar.getLunar();
expect(lunar.getJie()).toBe('');
expect(lunar.getQi()).toBe('');
expect(lunar.getJieQi()).toBe('');
expect(lunar.getCurrentJie()).toBe(null);
expect(lunar.getCurrentQi()).toBe(null);
expect(lunar.getCurrentJieQi()).toBe(null);
expect(lunar.getNextJie().getName()).toBe('小寒');
expect(lunar.getNextQi().getName()).toBe('大寒');
expect(lunar.getNextJieQi().getName()).toBe('小寒');
expect(lunar.getPrevJie().getName()).toBe('大雪');
expect(lunar.getPrevQi().getName()).toBe('冬至');
expect(lunar.getPrevJieQi().getName()).toBe('冬至');
});

49
__tests__/LiuYao.test.js Normal file
View File

@@ -0,0 +1,49 @@
var {Solar} = require('../lunar');
test('test1()', () => {
const solar = Solar.fromYmd(2020,4,23);
const lunar = solar.getLunar();
expect(lunar.getLiuYao()).toBe('佛灭');
});
test('test2()', () => {
const solar = Solar.fromYmd(2021,1,15);
const lunar = solar.getLunar();
expect(lunar.getLiuYao()).toBe('友引');
});
test('test3()', () => {
const solar = Solar.fromYmd(2017,1,5);
const lunar = solar.getLunar();
expect(lunar.getLiuYao()).toBe('先胜');
});
test('test4()', () => {
const solar = Solar.fromYmd(2020,4,10);
const lunar = solar.getLunar();
expect(lunar.getLiuYao()).toBe('友引');
});
test('test5()', () => {
const solar = Solar.fromYmd(2020,6,11);
const lunar = solar.getLunar();
expect(lunar.getLiuYao()).toBe('大安');
});
test('test6()', () => {
const solar = Solar.fromYmd(2020,6,1);
const lunar = solar.getLunar();
expect(lunar.getLiuYao()).toBe('先胜');
});
test('test7()', () => {
const solar = Solar.fromYmd(2020,12,8);
const lunar = solar.getLunar();
expect(lunar.getLiuYao()).toBe('先负');
});
test('test8()', () => {
const solar = Solar.fromYmd(2020,12,11);
const lunar = solar.getLunar();
expect(lunar.getLiuYao()).toBe('赤口');
});

184
__tests__/Lunar.test.js Normal file
View File

@@ -0,0 +1,184 @@
var {Solar} = require('../lunar');
test('干支', () => {
let solar = Solar.fromYmdHms(2020, 1, 1, 13, 22, 0);
let lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('己亥');
expect(lunar.getYearInGanZhiByLiChun()).toBe('己亥');
expect(lunar.getYearInGanZhiExact()).toBe('己亥');
expect(lunar.getMonthInGanZhi()).toBe('丙子');
expect(lunar.getMonthInGanZhiExact()).toBe('丙子');
//小寒
solar = Solar.fromYmdHms(2020, 1, 6, 13, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('己亥');
expect(lunar.getYearInGanZhiByLiChun()).toBe('己亥');
expect(lunar.getYearInGanZhiExact()).toBe('己亥');
expect(lunar.getMonthInGanZhi()).toBe('丁丑');
expect(lunar.getMonthInGanZhiExact()).toBe('丁丑');
solar = Solar.fromYmdHms(2020, 1, 20, 13, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('己亥');
expect(lunar.getYearInGanZhiByLiChun()).toBe('己亥');
expect(lunar.getYearInGanZhiExact()).toBe('己亥');
expect(lunar.getMonthInGanZhi()).toBe('丁丑');
expect(lunar.getMonthInGanZhiExact()).toBe('丁丑');
//春节
solar = Solar.fromYmdHms(2020, 1, 25, 13, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('庚子');
expect(lunar.getYearInGanZhiByLiChun()).toBe('己亥');
expect(lunar.getYearInGanZhiExact()).toBe('己亥');
expect(lunar.getMonthInGanZhi()).toBe('丁丑');
expect(lunar.getMonthInGanZhiExact()).toBe('丁丑');
solar = Solar.fromYmdHms(2020, 1, 30, 13, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('庚子');
expect(lunar.getYearInGanZhiByLiChun()).toBe('己亥');
expect(lunar.getYearInGanZhiExact()).toBe('己亥');
expect(lunar.getMonthInGanZhi()).toBe('丁丑');
expect(lunar.getMonthInGanZhiExact()).toBe('丁丑');
solar = Solar.fromYmdHms(2020, 2, 1, 13, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('庚子');
expect(lunar.getYearInGanZhiByLiChun()).toBe('己亥');
expect(lunar.getYearInGanZhiExact()).toBe('己亥');
expect(lunar.getMonthInGanZhi()).toBe('丁丑');
expect(lunar.getMonthInGanZhiExact()).toBe('丁丑');
solar = Solar.fromYmdHms(2020, 2, 4, 13, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('庚子');
expect(lunar.getYearInGanZhiByLiChun()).toBe('庚子');
expect(lunar.getYearInGanZhiExact()).toBe('己亥');
expect(lunar.getMonthInGanZhi()).toBe('戊寅');
expect(lunar.getMonthInGanZhiExact()).toBe('丁丑');
solar = Solar.fromYmdHms(2020, 2, 4, 18, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('庚子');
expect(lunar.getYearInGanZhiByLiChun()).toBe('庚子');
expect(lunar.getYearInGanZhiExact()).toBe('庚子');
expect(lunar.getMonthInGanZhi()).toBe('戊寅');
expect(lunar.getMonthInGanZhiExact()).toBe('戊寅');
solar = Solar.fromYmdHms(2020, 2, 5, 13, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('庚子');
expect(lunar.getYearInGanZhiByLiChun()).toBe('庚子');
expect(lunar.getYearInGanZhiExact()).toBe('庚子');
expect(lunar.getMonthInGanZhi()).toBe('戊寅');
expect(lunar.getMonthInGanZhiExact()).toBe('戊寅');
solar = Solar.fromYmdHms(2020, 5, 22, 13, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('庚子');
expect(lunar.getYearInGanZhiByLiChun()).toBe('庚子');
expect(lunar.getYearInGanZhiExact()).toBe('庚子');
expect(lunar.getMonthInGanZhi()).toBe('辛巳');
expect(lunar.getMonthInGanZhiExact()).toBe('辛巳');
solar = Solar.fromYmdHms(2020, 5, 23, 13, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('庚子');
expect(lunar.getYearInGanZhiByLiChun()).toBe('庚子');
expect(lunar.getYearInGanZhiExact()).toBe('庚子');
expect(lunar.getMonthInGanZhi()).toBe('辛巳');
expect(lunar.getMonthInGanZhiExact()).toBe('辛巳');
solar = Solar.fromYmdHms(2020, 5, 29, 13, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('庚子');
expect(lunar.getYearInGanZhiByLiChun()).toBe('庚子');
expect(lunar.getYearInGanZhiExact()).toBe('庚子');
expect(lunar.getMonthInGanZhi()).toBe('辛巳');
expect(lunar.getMonthInGanZhiExact()).toBe('辛巳');
solar = Solar.fromYmdHms(2020, 6, 1, 13, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('庚子');
expect(lunar.getYearInGanZhiByLiChun()).toBe('庚子');
expect(lunar.getYearInGanZhiExact()).toBe('庚子');
expect(lunar.getMonthInGanZhi()).toBe('辛巳');
expect(lunar.getMonthInGanZhiExact()).toBe('辛巳');
solar = Solar.fromYmdHms(2020, 6, 29, 13, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('庚子');
expect(lunar.getYearInGanZhiByLiChun()).toBe('庚子');
expect(lunar.getYearInGanZhiExact()).toBe('庚子');
expect(lunar.getMonthInGanZhi()).toBe('壬午');
expect(lunar.getMonthInGanZhiExact()).toBe('壬午');
solar = Solar.fromYmdHms(2019, 5, 1, 13, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('己亥');
expect(lunar.getYearInGanZhiByLiChun()).toBe('己亥');
expect(lunar.getYearInGanZhiExact()).toBe('己亥');
expect(lunar.getMonthInGanZhi()).toBe('戊辰');
expect(lunar.getMonthInGanZhiExact()).toBe('戊辰');
solar = Solar.fromYmdHms(1986, 5, 29, 13, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('丙寅');
expect(lunar.getYearInGanZhiByLiChun()).toBe('丙寅');
expect(lunar.getYearInGanZhiExact()).toBe('丙寅');
expect(lunar.getMonthInGanZhi()).toBe('癸巳');
expect(lunar.getMonthInGanZhiExact()).toBe('癸巳');
solar = Solar.fromYmdHms(1986, 5, 1, 1, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('丙寅');
expect(lunar.getYearInGanZhiByLiChun()).toBe('丙寅');
expect(lunar.getYearInGanZhiExact()).toBe('丙寅');
expect(lunar.getMonthInGanZhi()).toBe('壬辰');
expect(lunar.getMonthInGanZhiExact()).toBe('壬辰');
solar = Solar.fromYmdHms(1986, 5, 6, 1, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('丙寅');
expect(lunar.getYearInGanZhiByLiChun()).toBe('丙寅');
expect(lunar.getYearInGanZhiExact()).toBe('丙寅');
expect(lunar.getMonthInGanZhi()).toBe('癸巳');
expect(lunar.getMonthInGanZhiExact()).toBe('壬辰');
solar = Solar.fromYmdHms(1986, 5, 6, 23, 22, 0);
lunar = solar.getLunar();
expect(lunar.getYearInGanZhi()).toBe('丙寅');
expect(lunar.getYearInGanZhiByLiChun()).toBe('丙寅');
expect(lunar.getYearInGanZhiExact()).toBe('丙寅');
expect(lunar.getMonthInGanZhi()).toBe('癸巳');
expect(lunar.getMonthInGanZhiExact()).toBe('癸巳');
});

74
__tests__/ShuJiu.test.js Normal file
View File

@@ -0,0 +1,74 @@
var {Solar} = require('../lunar');
test('test1()', () => {
const solar = Solar.fromYmd(2020,12,21);
const lunar = solar.getLunar();
const shuJiu = lunar.getShuJiu();
if(null==shuJiu){
throw solar.toYmd();
}
expect(shuJiu.toString()).toBe('一九');
expect(shuJiu.toFullString()).toBe('一九第1天');
});
test('test2()', () => {
const solar = Solar.fromYmd(2020,12,22);
const lunar = solar.getLunar();
const shuJiu = lunar.getShuJiu();
if(null==shuJiu){
throw solar.toYmd();
}
expect(shuJiu.toString()).toBe('一九');
expect(shuJiu.toFullString()).toBe('一九第2天');
});
test('test3()', () => {
const solar = Solar.fromYmd(2020,1,7);
const lunar = solar.getLunar();
const shuJiu = lunar.getShuJiu();
if(null==shuJiu){
throw solar.toYmd();
}
expect(shuJiu.toString()).toBe('二九');
expect(shuJiu.toFullString()).toBe('二九第8天');
});
test('test4()', () => {
const solar = Solar.fromYmd(2021,1,6);
const lunar = solar.getLunar();
const shuJiu = lunar.getShuJiu();
if(null==shuJiu){
throw solar.toYmd();
}
expect(shuJiu.toString()).toBe('二九');
expect(shuJiu.toFullString()).toBe('二九第8天');
});
test('test5()', () => {
const solar = Solar.fromYmd(2021,1,8);
const lunar = solar.getLunar();
const shuJiu = lunar.getShuJiu();
if(null==shuJiu){
throw solar.toYmd();
}
expect(shuJiu.toString()).toBe('三九');
expect(shuJiu.toFullString()).toBe('三九第1天');
});
test('test6()', () => {
const solar = Solar.fromYmd(2021,3,5);
const lunar = solar.getLunar();
const shuJiu = lunar.getShuJiu();
if(null==shuJiu){
throw solar.toYmd();
}
expect(shuJiu.toString()).toBe('九九');
expect(shuJiu.toFullString()).toBe('九九第3天');
});
test('test7()', () => {
const solar = Solar.fromYmd(2021,7,5);
const lunar = solar.getLunar();
const shuJiu = lunar.getShuJiu();
expect(shuJiu).toBe(null);
});

29
__tests__/Solar.test.js Normal file
View File

@@ -0,0 +1,29 @@
var {Solar} = require('../lunar');
test('test', () => {
var solar = Solar.fromYmd(2019,5,1);
expect(solar.toString()).toBe('2019-05-01');
expect(solar.toFullString()).toBe('2019-05-01 00:00:00 星期三 (劳动节) 金牛座');
expect(solar.getLunar().toString()).toBe('二〇一九年三月廿七');
expect(solar.getLunar().toFullString()).toBe('二〇一九年三月廿七 己亥(猪)年 戊辰(龙)月 戊戌(狗)日 子(鼠)时 纳音[平地木 大林木 平地木 桑柘木] 星期三 (七殿泰山王诞) 西方白虎 星宿[参水猿](吉) 彭祖百忌[戊不受田田主不祥 戌不吃犬作怪上床] 喜神方位[巽](东南) 阳贵神方位[艮](东北) 阴贵神方位[坤](西南) 福神方位[坎](正北) 财神方位[坎](正北) 冲[(壬辰)龙] 煞[北]');
});
test('1', () => {
var solar = Solar.fromYmdHms(2020,5,24,13,0,0);
expect(solar.getLunar().toString()).toBe('二〇二〇年闰四月初二');
});
test('6', () => {
var solar = Solar.fromYmd(11,1,1);
expect(solar.getLunar().toString()).toBe('一〇年腊月初八');
});
test('7', () => {
var solar = Solar.fromYmd(11,3,1);
expect(solar.getLunar().toString()).toBe('一一年二月初八');
});
test('9', () => {
var solar = Solar.fromYmd(26,4,13);
expect(solar.getLunar().toString()).toBe('二六年三月初八');
});

View File

@@ -0,0 +1,13 @@
var {SolarHalfYear} = require('../lunar');
test('toString()', () => {
const halfYear = SolarHalfYear.fromYm(2019, 5);
expect(halfYear.toString()).toBe('2019.1');
expect(halfYear.next(1).toString()).toBe('2019.2');
});
test('toFullString()', () => {
const halfYear = SolarHalfYear.fromYm(2019, 5);
expect(halfYear.toFullString()).toBe('2019年上半年');
expect(halfYear.next(1).toFullString()).toBe('2019年下半年');
});

View File

@@ -0,0 +1,13 @@
var {SolarSeason} = require('../lunar');
test('toString()', () => {
const season = SolarSeason.fromYm(2019, 5);
expect(season.toString()).toBe('2019.2');
expect(season.next(1).toString()).toBe('2019.3');
});
test('toFullString()', () => {
const season = SolarSeason.fromYm(2019, 5);
expect(season.toFullString()).toBe('2019年2季度');
expect(season.next(1).toFullString()).toBe('2019年3季度');
});

View File

@@ -0,0 +1,13 @@
var {SolarMonth} = require('../lunar');
test('toString()', () => {
const month = SolarMonth.fromYm(2019, 5);
expect(month.toString()).toBe('2019-5');
expect(month.next(1).toString()).toBe('2019-6');
});
test('toFullString()', () => {
const month = SolarMonth.fromYm(2019, 5);
expect(month.toFullString()).toBe('2019年5月');
expect(month.next(1).toFullString()).toBe('2019年6月');
});

View File

@@ -0,0 +1,11 @@
var {SolarUtil} = require('../lunar');
test('isLeapYear()', () => {
expect(SolarUtil.isLeapYear(2020)).toBe(true);
expect(SolarUtil.isLeapYear(2021)).toBe(false);
});
test('getDaysOfMonth()', () => {
expect(SolarUtil.getDaysOfMonth(2020, 1)).toBe(31);
expect(SolarUtil.getDaysOfMonth(2021, 2)).toBe(28);
});

View File

@@ -0,0 +1,21 @@
var {SolarWeek,SolarUtil} = require('../lunar');
test('testFromMonday', () => {
const start = 1;
const week = SolarWeek.fromYmd(2019, 5, 1, start);
expect(week.toString()).toBe('2019.5.1');
expect(week.toFullString()).toBe('2019年5月第1周');
expect(SolarUtil.getWeeksOfMonth(week.getYear(), week.getMonth(), start)).toBe(5);
expect(week.getFirstDay().toString()).toBe('2019-04-29');
expect(week.getFirstDayInMonth().toString()).toBe('2019-05-01');
});
test('testFromSunday', () => {
const start = 0;
const week = SolarWeek.fromYmd(2019, 5, 1, start);
expect(week.toString()).toBe('2019.5.1');
expect(week.toFullString()).toBe('2019年5月第1周');
expect(SolarUtil.getWeeksOfMonth(week.getYear(), week.getMonth(), start)).toBe(5);
expect(week.getFirstDay().toString()).toBe('2019-04-28');
expect(week.getFirstDayInMonth().toString()).toBe('2019-05-01');
});

View File

@@ -0,0 +1,13 @@
var {SolarYear} = require('../lunar');
test('toString()', () => {
const year = SolarYear.fromYear(2019);
expect(year.toString()).toBe('2019');
expect(year.next(1).toString()).toBe('2020');
});
test('toFullString()', () => {
const year = SolarYear.fromYear(2019);
expect(year.toFullString()).toBe('2019年');
expect(year.next(1).toFullString()).toBe('2020年');
});

49
__tests__/WuHou.test.js Normal file
View File

@@ -0,0 +1,49 @@
var {Solar} = require('../lunar');
test('test1()', () => {
const solar = Solar.fromYmd(2020,4,23);
const lunar = solar.getLunar();
expect(lunar.getWuHou()).toBe('萍始生');
});
test('test2()', () => {
const solar = Solar.fromYmd(2021,1,15);
const lunar = solar.getLunar();
expect(lunar.getWuHou()).toBe('雉始雊');
});
test('test3()', () => {
const solar = Solar.fromYmd(2017,1,5);
const lunar = solar.getLunar();
expect(lunar.getWuHou()).toBe('雁北乡');
});
test('test4()', () => {
const solar = Solar.fromYmd(2020,4,10);
const lunar = solar.getLunar();
expect(lunar.getWuHou()).toBe('田鼠化为鴽');
});
test('test5()', () => {
const solar = Solar.fromYmd(2020,6,11);
const lunar = solar.getLunar();
expect(lunar.getWuHou()).toBe('鵙始鸣');
});
test('test6()', () => {
const solar = Solar.fromYmd(2020,6,1);
const lunar = solar.getLunar();
expect(lunar.getWuHou()).toBe('麦秋至');
});
test('test7()', () => {
const solar = Solar.fromYmd(2020,12,8);
const lunar = solar.getLunar();
expect(lunar.getWuHou()).toBe('鹖鴠不鸣');
});
test('test8()', () => {
const solar = Solar.fromYmd(2020,12,11);
const lunar = solar.getLunar();
expect(lunar.getWuHou()).toBe('鹖鴠不鸣');
});

85
__tests__/XingZuo.test.js Normal file
View File

@@ -0,0 +1,85 @@
var {Solar} = require('../lunar');
test('test1()', () => {
let solar = Solar.fromYmd(2020, 3, 21);
expect(solar.getXingZuo()).toBe('白羊');
solar = Solar.fromYmd(2020, 4, 19);
expect(solar.getXingZuo()).toBe('白羊');
});
test('test2()', () => {
let solar = Solar.fromYmd(2020, 4, 20);
expect(solar.getXingZuo()).toBe('金牛');
solar = Solar.fromYmd(2020, 5, 20);
expect(solar.getXingZuo()).toBe('金牛');
});
test('test3()', () => {
let solar = Solar.fromYmd(2020, 5, 21);
expect(solar.getXingZuo()).toBe('双子');
solar = Solar.fromYmd(2020, 6, 21);
expect(solar.getXingZuo()).toBe('双子');
});
test('test4()', () => {
let solar = Solar.fromYmd(2020, 6, 22);
expect(solar.getXingZuo()).toBe('巨蟹');
solar = Solar.fromYmd(2020, 7, 22);
expect(solar.getXingZuo()).toBe('巨蟹');
});
test('test5()', () => {
let solar = Solar.fromYmd(2020, 7, 23);
expect(solar.getXingZuo()).toBe('狮子');
solar = Solar.fromYmd(2020, 8, 22);
expect(solar.getXingZuo()).toBe('狮子');
});
test('test6()', () => {
let solar = Solar.fromYmd(2020, 8, 23);
expect(solar.getXingZuo()).toBe('处女');
solar = Solar.fromYmd(2020, 9, 22);
expect(solar.getXingZuo()).toBe('处女');
});
test('test7()', () => {
let solar = Solar.fromYmd(2020, 9, 23);
expect(solar.getXingZuo()).toBe('天秤');
solar = Solar.fromYmd(2020, 10, 23);
expect(solar.getXingZuo()).toBe('天秤');
});
test('test8()', () => {
let solar = Solar.fromYmd(2020, 10, 24);
expect(solar.getXingZuo()).toBe('天蝎');
solar = Solar.fromYmd(2020, 11, 22);
expect(solar.getXingZuo()).toBe('天蝎');
});
test('test9()', () => {
let solar = Solar.fromYmd(2020, 11, 23);
expect(solar.getXingZuo()).toBe('射手');
solar = Solar.fromYmd(2020, 12, 21);
expect(solar.getXingZuo()).toBe('射手');
});
test('test10()', () => {
let solar = Solar.fromYmd(2020, 12, 22);
expect(solar.getXingZuo()).toBe('摩羯');
solar = Solar.fromYmd(2021, 1, 19);
expect(solar.getXingZuo()).toBe('摩羯');
});
test('test11()', () => {
let solar = Solar.fromYmd(2021, 1, 20);
expect(solar.getXingZuo()).toBe('水瓶');
solar = Solar.fromYmd(2021, 2, 18);
expect(solar.getXingZuo()).toBe('水瓶');
});
test('test12()', () => {
let solar = Solar.fromYmd(2021, 2, 19);
expect(solar.getXingZuo()).toBe('双鱼');
solar = Solar.fromYmd(2021, 3, 20);
expect(solar.getXingZuo()).toBe('双鱼');
});

22
__tests__/Xun.test.js Normal file
View File

@@ -0,0 +1,22 @@
var {Solar} = require('../lunar');
test('旬', () => {
const solar = Solar.fromYmdHms(2020, 11, 19, 0, 0, 0);
const lunar = solar.getLunar();
expect(lunar.getYearXun()).toBe('甲午');
});
test('旬空', () => {
const solar = Solar.fromYmdHms(2020, 11, 19, 0, 0, 0);
const lunar = solar.getLunar();
expect(lunar.getYearXunKong()).toBe('辰巳');
expect(lunar.getMonthXunKong()).toBe('午未');
expect(lunar.getDayXunKong()).toBe('戌亥');
});
test('八字旬空', () => {
const solar = Solar.fromYmdHms(1990, 12, 23, 8, 37, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
expect(eightChar.getDayXunKong()).toBe('子丑');
});

12
__tests__/Yun.test.js Normal file
View File

@@ -0,0 +1,12 @@
var {Solar} = require('../lunar');
it('起运', () => {
const solar = Solar.fromYmdHms(1981, 1, 29, 23, 37, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
const yun = eightChar.getYun(0);
expect(yun.getStartYear()).toBe(8);
expect(yun.getStartMonth()).toBe(0);
expect(yun.getStartDay()).toBe(20);
expect(yun.getStartSolar().toYmd()).toBe('1989-02-18');
});

View File

@@ -49,6 +49,12 @@
return _fromYmdHms(year,month,day,hour,minute,second);
};
var _fromYmdHms = function(y,m,d,hour,minute,second){
var calendar = new Date(y+'/'+m+'/'+d+' '+hour+':'+minute+':'+second);
if(y<100) {
calendar.setFullYear(y);
calendar.setMonth(m - 1);
calendar.setDate(d);
}
return {
_p:{
year:y,
@@ -57,7 +63,7 @@
hour:hour,
minute:minute,
second:second,
calendar:new Date(y+'/'+m+'/'+d+' '+hour+':'+minute+':'+second)
calendar:calendar
},
getYear:function(){
return this._p.year;
@@ -479,7 +485,15 @@
return o;
};
var _fromDate = function(date){
var c = new Date(date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate() + ' 0:0:0');
var solarYear = date.getFullYear();
var solarMonth = date.getMonth();
var solarDay = date.getDate();
var c = new Date(solarYear + '/' + (solarMonth + 1) + '/' + solarDay + ' 0:0:0');
if(solarYear<100){
c.setFullYear(solarYear);
c.setMonth(solarMonth);
c.setDate(solarDay);
}
var solarTime = c.getTime();
var lunarYear = 0;
var lunarMonth = 0;
@@ -1603,7 +1617,7 @@
return l;
},
toString:function(){
return this.getYear()+'-'+this.getMonth()+'.'+this.getIndex();
return this.getYear()+'.'+this.getMonth()+'.'+this.getIndex();
},
toFullString:function(){
return this.getYear()+'年'+this.getMonth()+'月第'+this.getIndex()+'周';
@@ -1834,7 +1848,7 @@
return l;
},
toString:function(){
return this.getYear();
return this.getYear()+'';
},
toFullString:function(){
return this.getYear()+'年';

View File

@@ -1,10 +1,10 @@
{
"name": "lunar-javascript",
"version": "1.2.0",
"version": "1.2.1",
"description": "lunar is a calendar library for Solar and Chinese Lunar.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"repository": {
"type": "git",
@@ -47,5 +47,8 @@
"bugs": {
"url": "https://github.com/6tail/lunar-javascript/issues"
},
"homepage": "https://github.com/6tail/lunar-javascript"
"homepage": "https://github.com/6tail/lunar-javascript",
"devDependencies": {
"jest": "^26.6.3"
}
}