Skip to content

Commit

Permalink
Merge pull request #10 from NghiaCaNgao/refactor
Browse files Browse the repository at this point in the history
Fix lunar.init() auto set leap_month = true
  • Loading branch information
nacana22 authored Jun 22, 2023
2 parents 3594cfe + f0f2ab0 commit 48d778a
Show file tree
Hide file tree
Showing 19 changed files with 655 additions and 252 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ jobs:
name: "Release v${{ steps.package-version.outputs.current-version }}"
body: |
${{ steps.extract-release-notes.outputs.release_notes }}
######## GITHUB RELEASE LATEST #########
- name: Create latest GITHUB release
uses: marvinpinto/action-automatic-releases@latest
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
automatic_release_tag: "latest"
prerelease: true
title: "Development Build"

############# NPM RELEASE ##############
- name: Publish the release to NPM
run: npm publish
Expand Down
33 changes: 0 additions & 33 deletions .github/workflows/release.yml

This file was deleted.

9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## [2.0.0] - 2023-06-22
### Added
- Thêm interfaces: list **`ILunarDateEx`**, **`ILunarDateLeap`**
### Fixed
- Sửa lỗi `init` luôn nhận tháng nhuận đối với tháng có tháng nhuận. Fixed #9
- SolarDate.jdn đã có kiểm tra ngày tháng nhập có đúng không
### Removed
- constructor không còn attrs: `leap_year``jd`

## [1.0.11] - 2023-06-20
### Added
- function `setDate`
Expand Down
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Remake từ <b><a href="https://www.informatik.uni-leipzig.de/~duc/amlich/calrul
- [lunar.toSolarDate()](#lunartosolardate)
- [lunar.setDate()](#lunarsetdate)
- [lunar.setDate()](#lunarsetdate)

## Features

- Chuyển đổi lịch dương sang lịch âm (của Việt Nam) và ngược lại.
Expand Down Expand Up @@ -95,10 +96,8 @@ const calendar = require("@nghiavuive/lunar_date_vi/dist/index.cjs");

Sử dụng qua jsDelivr

<!-- FIXME: Sửa lại theo tag -->

```bash
<script src="https://cdn.jsdelivr.net/gh/NghiaCaNgao/lunarDate@v1.0.11/dist/index.umd.js"></script>
<script src="https://cdn.jsdelivr.net/gh/NghiaCaNgao/lunarDate@latest/dist/index.umd.js"></script>
```

## Examples
Expand Down Expand Up @@ -443,7 +442,10 @@ console.log(dl.get());
#### Lunar constructor

Tạo thực thể [**`LunarDate`**](#lunardate) từ [**`ILunarDate`**](#ilunardate)
> **Note** Hàm này chưa chuẩn hóa dữ liệu vào

> **Note** Để nhập tháng nhuận, sử dụng thêm attr `leap_month = true`. Nếu sử dụng `leap_month = true` với tháng không thể nhuận, tư động chuyển về `leap_month = false`.
> **Note** Nếu nhập sai ngày tháng sẽ trả về lỗi [**`Invalid date`**](https://github.com/NghiaCaNgao/LunarDate/wiki/Error-message)
> **Note** Khi khởi tạo cần điền đầy đủ `day`, `month`, `year`. Nếu không điền các thông tin khác (`leap_year`, ...) thì mặc định là `undefined`. Sau khi khởi tạo có thể sử dụng hàm [**`lunar.init()`**](#lunarinit) để tự động điền các thông tin còn thiếu. Nếu các thông tin (`leap_year`, `jd`,...) là `undefined` thì sẽ không thể sử dụng được các hàm khác trong thực thể.
Expand All @@ -468,6 +470,19 @@ console.log(al);
// leap_year: undefined,
// leap_month: undefined
// }

const al = new LunarDate({ day: 1, month: 2, year: 2023, leap_month: true });
al.init();
console.log(al.toSolarDate());

// SolarDate {
// day: 22,
// month: 3,
// year: 2023,
// name: 'solar_calendar',
// jd: 2460026,
// leap_year: false
// }
```

#### `SolarDate.fromSolarDate()`
Expand Down
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"type": "module", add in package.json
"type": "module", remove to run npx ts-node
133 changes: 101 additions & 32 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,22 @@ class SolarDate extends Calendar {
return new SolarDate({ day, month, year });
}
static jdn(date) {
const day = date instanceof Date ? date.getDate() : date.day;
const month = date instanceof Date ? date.getMonth() + 1 : date.month;
const year = date instanceof Date ? date.getFullYear() : date.year;
const a = INT((14 - month) / 12);
const y = year + 4800 - a;
const m = month + 12 * a - 3;
var jd = day + INT((153 * m + 2) / 5) + 365 * y + INT(y / 4) - INT(y / 100) + INT(y / 400) - 32045;
if (jd < 2299161) {
jd = day + INT((153 * m + 2) / 5) + 365 * y + INT(y / 4) - 32083;
if (SolarDate.isValidDate(date)) {
const day = date instanceof Date ? date.getDate() : date.day;
const month = date instanceof Date ? date.getMonth() + 1 : date.month;
const year = date instanceof Date ? date.getFullYear() : date.year;
const a = INT((14 - month) / 12);
const y = year + 4800 - a;
const m = month + 12 * a - 3;
var jd = day + INT((153 * m + 2) / 5) + 365 * y + INT(y / 4) - INT(y / 100) + INT(y / 400) - 32045;
if (jd < 2299161) {
jd = day + INT((153 * m + 2) / 5) + 365 * y + INT(y / 4) - 32083;
}
return jd;
}
else {
throw new Error("Invalid date");
}
return jd;
}
toDate() {
const { day, month, year } = this;
Expand Down Expand Up @@ -187,38 +192,83 @@ class SolarDate extends Calendar {

class LunarDate extends Calendar {
leap_month;
length;
constructor(date) {
super(date, "lunar_calendar");
this.leap_month = date.leap_month;
this.leap_year = date.leap_year;
this.jd = date.jd;
}
setExAttribute(date) {
this.leap_year = this.leap_year || date.leap_year;
this.jd = this.jd || date.jd;
this.length = this.length || date.length;
}
init(force_change = false) {
const recommendation = LunarDate.getRecommended({ day: this.day, month: this.month, year: this.year });
if (!LunarDate.isValidDate({ day: this.day, month: this.month, year: this.year }))
throw new Error("Invalid date");
const recommendation = LunarDate.getRecommended({
day: this.day, month: this.month, year: this.year,
leap_month: this.leap_month
});
if (force_change) {
this.leap_month = recommendation.leap_month;
this.leap_year = recommendation.leap_year;
this.jd = recommendation.jd;
this.length = recommendation.length;
}
else {
this.leap_month = this.leap_month || recommendation.leap_month;
this.leap_year = this.leap_year || recommendation.leap_year;
this.jd = this.jd || recommendation.jd;
this.length = this.length || recommendation.length;
}
}
static isValidDate(date) {
if (date.day <= 0 || date.day > 30)
return false;
if (date.month <= 0 || date.month > 12)
return false;
if (date.year === 1200) {
if (date.month === 1) {
if (date.day < 14) {
return false;
}
}
}
else if (date.year < 1200)
return false;
if (date.year === 2199) {
if (date.month === 11) {
if (date.day > 14) {
return false;
}
}
}
else if (date.year > 2199)
return false;
return true;
}
static getRecommended(date) {
const year_code = LunarDate.getYearCode(date.year);
const lunar_months = LunarDate.decodeLunarYear(date.year, year_code);
const rcm = {
jd: 0,
leap_month: false,
leap_year: false
leap_year: false,
length: 0
};
for (let i = 0; i < lunar_months.length; i++) {
if (lunar_months[i].month === date.month) {
rcm.jd = lunar_months[i].jd + date.day - 1;
rcm.leap_month = lunar_months[i].leap_month;
rcm.leap_year = lunar_months[i].leap_year;
let ref_months = (date.leap_month && lunar_months[i + 1] != undefined &&
lunar_months[i + 1].month === date.month)
? lunar_months[i + 1]
: lunar_months[i];
if (date.day > ref_months.length)
throw new Error("Invalid date");
rcm.jd = ref_months.jd + date.day - 1;
rcm.leap_month = ref_months.leap_month;
rcm.leap_year = ref_months.leap_year;
rcm.length = ref_months.length;
break;
}
}
return {
Expand Down Expand Up @@ -279,20 +329,22 @@ class LunarDate extends Calendar {
}
for (let month = 1; month <= 12; month++) {
const date = { day: 1, month, year };
lunar_months.push(new LunarDate({
...date,
leap_month: false,
let normal_lunar = new LunarDate({ ...date, leap_month: false });
normal_lunar.setExAttribute({
leap_year: leapMonth !== 0,
jd: currentJD
}));
jd: currentJD,
length: reg_month_lens[month - 1]
});
lunar_months.push(normal_lunar);
currentJD += reg_month_lens[month - 1];
if (leapMonth === month) {
lunar_months.push(new LunarDate({
...date,
leap_month: true,
let leap_lunar = new LunarDate({ ...date, leap_month: true });
leap_lunar.setExAttribute({
leap_year: leapMonth !== 0,
jd: currentJD
}));
jd: currentJD,
length: leapMonthLength
});
lunar_months.push(leap_lunar);
currentJD += leapMonthLength;
}
}
Expand All @@ -307,14 +359,18 @@ class LunarDate extends Calendar {
index--;
}
let offset = jd - lunar_months[index].jd;
return new LunarDate({
let lunar = new LunarDate({
day: lunar_months[index].day + offset,
month: lunar_months[index].month,
year: lunar_months[index].year,
leap_month: lunar_months[index].leap_month,
leap_month: lunar_months[index].leap_month
});
lunar.setExAttribute({
jd: jd,
leap_year: lunar_months[index].leap_year,
jd: jd
length: lunar_months[index].length
});
return lunar;
}
static getSunLongitudeByJd(jd) {
const T = (jd - 2451545.0) / 36525;
Expand Down Expand Up @@ -388,8 +444,21 @@ class LunarDate extends Calendar {
return SolarDate.fromJd(this.jd);
}
setDate(date) {
this.set(date);
this.init(true);
let backupDate = {
day: this.day, month: this.month, year: this.year,
leap_month: this.leap_month
};
try {
if (!LunarDate.isValidDate(date))
throw new Error("Invalid date");
this.set(date);
this.leap_month = date.leap_month;
this.init(true);
}
catch (error) {
this.setDate(backupDate);
throw new Error("Invalid date");
}
}
get() {
return {
Expand Down
5 changes: 3 additions & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,19 @@ declare class SolarDate extends Calendar {
}

interface ILunarDate extends ICalendarDate {
jd?: number;
leap_month?: boolean;
leap_year?: boolean;
}
interface ILuckyHour {
name: string;
time: number[];
}
declare class LunarDate extends Calendar {
private leap_month?;
private length?;
constructor(date: ILunarDate);
private setExAttribute;
init(force_change?: boolean): void;
private static isValidDate;
private static getRecommended;
private static getYearCode;
private static generateJdOfNewYear;
Expand Down
Loading

0 comments on commit 48d778a

Please sign in to comment.