vietnamese-lunar 2.1.0 → 2.1.1-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,52 @@
2
2
 
3
3
  All notable changes to this package are documented here. Versions follow [Semantic Versioning](https://semver.org/): a **major** bump (1.x → 2.x) means you may need to change your code.
4
4
 
5
+ ## 2.1.1 — Unreleased
6
+
7
+ ### ⚠️ Behaviour changes (relative to 2.1.0)
8
+
9
+ Checked by running 2.1.0 and this version side by side on 20,863 dates (every 7th day, 1800–2199): the only differing `dayInfo` / `lunarDay` values are `clash` (below) and the new fields; `getHolidays`, `monthInfo`, `getSolarTerms` (apart from the new `startLocal`) and `nextHolidays` with `{day, month, year}` are identical.
10
+
11
+ - **Invalid dates throw `RangeError`** in `dayInfo`, `lunarDay`, `getHourInfo`, `nextHolidays` and `getLunarDate(date)`, with a message naming the function and the bad value.
12
+ - `dayInfo` / `lunarDay`: returned the string `"Invalid date input."` (also in 2.0.x). **Code that checks `typeof result === "string"` must switch to `try/catch`.** The return types change from `DayInfo | string` / `LunarDayInfo | string` to `DayInfo` / `LunarDayInfo`.
13
+ - `getHourInfo`: returned `null` for a bad date; still returns `null` for an `hour` that is not an integer 0..23.
14
+ - `nextHolidays`: see Fixed.
15
+ - Unchanged and still non-throwing: `getLunarDate(day, month, year)` (zeroed `LunarDate` outside 1800–2199), `getSolarDate`, `getLunarMonthDays`, `getLeapMonth` (`null` / `0`), `monthInfo` (`null`).
16
+ - **`clash.canChi` uses a new rule**: tuổi xung = Can Chi with the opposite Chi whose Nạp âm element *khắc* the day's Nạp âm element (either direction). Always exactly 2 items, sorted by Can. `clash.chi` is unchanged.
17
+
18
+ | Day | 2.1.0 | 2.1.1 |
19
+ |---|---|---|
20
+ | Quý Dậu (26/10/2026) | `["Đinh Mão"]` | `["Đinh Mão", "Tân Mão"]` |
21
+ | Kỷ Mão (1/11/2026) | `[]` | `["Ất Dậu", "Tân Dậu"]` |
22
+ | Kỷ Mùi (17/10/2025) | `[]` | `["Ất Sửu", "Đinh Sửu"]` |
23
+
24
+ The values changed for all 20,863 sampled days. Cross-checked against the commonly published almanac listing (e.g. Giáp Tý → Mậu Ngọ, Nhâm Ngọ).
25
+
26
+ ### Fixed
27
+
28
+ - `nextHolidays` returned `[]` when `from` was a `Date` or a string; only `{day, month, year}` worked. It now accepts any `DateInput` (`Date`, `{day, month, year}`, `'d/m/yyyy'`, `'dd/mm/yyyy'`) through the same normaliser as every other function.
29
+
30
+ ### Added
31
+
32
+ - `getSolarTermByIndex(year, index)`.
33
+ - `startLocal: {day, month, year, hour, minute}` (Vietnam time, UTC+7) on `solarTerm.current`, `solarTerm.next` and every item of `getSolarTerms()`.
34
+ - `nextHolidays(from, count, options)`: `groupConsecutive` (merges consecutive same-name days; adds `endDate` and `days`), `types: HolidayType[]`, `dayOffOnly`. Defaults keep the 2.1.0 behaviour (no grouping, no filtering).
35
+ - `getLunarDate(date: DateInput)` overload.
36
+ - `lunarDay()` returns `dateText` (camelCase, same value as `date_text`). **`date_text` is deprecated** but unchanged.
37
+ - New exported types: `LocalDateTime`, `NextHolidaysOptions`.
38
+ - `scripts/readme-examples.mjs`: README examples are generated by running the built code (`node scripts/readme-examples.mjs --write`).
39
+
40
+ ### Documented
41
+
42
+ - `getSolarTerms(year)` is ordered by time from Tiểu Hàn; `index` is the `SOLAR_TERMS` numbering (0 = Xuân Phân), the same numbering as `dayInfo().solarTerm.*.index`. `getSolarTerms(2026)[17]` is Thu Phân, `index: 12`.
43
+ - `solarTerm.*.start` is an absolute instant (prints as UTC); Vietnam time is UTC+7 — use `startLocal`.
44
+ - `daysUntil` counts calendar days in UTC+7 and does not depend on the time of day or the machine's time zone (tested under `Asia/Ho_Chi_Minh`, `America/New_York`, `Pacific/Auckland`).
45
+ - README: Interface table now lists every `DayInfo` field; 2.1.0 and 2.1.1 sections separated; corrected the `monthDays` example (17/10/2025 is a 29-day month).
46
+
47
+ ### Not in this release
48
+
49
+ - `monthBasis: 'solar-term' | 'lunar-month'` for `dayStar` (optional item C of the request) — deferred to 2.2.0.
50
+
5
51
  ## 2.1.0 — 2026-11-01
6
52
 
7
53
  Backward compatible with 2.0.x: no existing field changed name or type; everything below is additive.
@@ -1,4 +1,5 @@
1
- export declare const getClash: (dayCan: number, dayChi: number) => {
1
+ import { Element } from "./type.js";
2
+ export declare const getClash: (dayElement: Element | null, dayChi: number) => {
2
3
  canChi: string[];
3
4
  chi: string[];
4
5
  };
package/lib/cjs/compat.js CHANGED
@@ -2,19 +2,31 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getCompatible = exports.getClash = void 0;
4
4
  const constants_js_1 = require("./constants.js");
5
+ // Quy tắc tuổi xung (khớp cách liệt kê của lịch vạn niên phổ biến):
6
+ // tuổi xung với ngày = tuổi có Chi ĐỐI (lục xung, cách 6) với Chi của ngày
7
+ // VÀ hành Nạp âm của tuổi tương khắc với hành Nạp âm của ngày (khắc một trong hai chiều).
8
+ // Ví dụ: ngày Giáp Tý (Hải Trung Kim) xung Mậu Ngọ (Hỏa khắc Kim) và Nhâm Ngọ (Kim khắc Mộc);
9
+ // ngày Kỷ Mão (Thành Đầu Thổ) xung Tân Dậu (Mộc khắc Thổ) và Ất Dậu (Thổ khắc Thủy).
10
+ // 5 tuổi cùng một Chi mang đủ 5 hành, nên mỗi ngày luôn có đúng 2 tuổi xung.
5
11
  // Tam hợp: Thân-Tý-Thìn, Hợi-Mão-Mùi, Dần-Ngọ-Tuất, Tỵ-Dậu-Sửu (Chi cách nhau 4).
6
12
  // Lục hợp: Tý-Sửu, Dần-Hợi, Mão-Tuất, Thìn-Dậu, Tỵ-Thân, Ngọ-Mùi (tổng chỉ số ≡ 1 mod 12).
7
- // Lục xung: Chi đối nhau (cách 6).
8
- // Thiên khắc địa xung: Chi đối nhau + Can tương xung (Giáp-Canh, Ất-Tân, Bính-Nhâm, Đinh-Quý).
9
- // Mậu và Kỷ không có Can xung, nên với ngày Mậu/Kỷ `canChi` rỗng (vẫn có `chi`).
10
- const getClashCan = (can) => (can <= 3 ? can + 6 : can >= 6 ? can - 6 : -1);
11
- const getClash = (dayCan, dayChi) => {
13
+ const KHAC = { 'Kim': 'Mộc', 'Mộc': 'Thổ', 'Thổ': 'Thủy', 'Thủy': 'Hỏa', 'Hỏa': 'Kim' };
14
+ const elementOf = (canChiText) => {
15
+ const napAm = constants_js_1.HEAVENLY_EARTHLY_ELEMENT_MAP[canChiText];
16
+ return napAm ? napAm.split(' ').pop() : null;
17
+ };
18
+ const getClash = (dayElement, dayChi) => {
12
19
  const oppositeChi = (dayChi + 6) % 12;
13
- const clashCan = getClashCan(dayCan);
14
- return {
15
- canChi: clashCan === -1 ? [] : [`${constants_js_1.HEAVENLY_STEMS[clashCan]} ${constants_js_1.EARTHLY_BRANCHES[oppositeChi]}`],
16
- chi: [constants_js_1.EARTHLY_BRANCHES[oppositeChi]],
17
- };
20
+ const canChi = [];
21
+ if (dayElement) {
22
+ for (let can = oppositeChi % 2; can < 10; can += 2) { // Can và Chi cùng chẵn/lẻ mới có trong 60 Hoa Giáp
23
+ const text = `${constants_js_1.HEAVENLY_STEMS[can]} ${constants_js_1.EARTHLY_BRANCHES[oppositeChi]}`;
24
+ const el = elementOf(text);
25
+ if (el && (KHAC[el] === dayElement || KHAC[dayElement] === el))
26
+ canChi.push(text);
27
+ }
28
+ }
29
+ return { canChi, chi: [constants_js_1.EARTHLY_BRANCHES[oppositeChi]] };
18
30
  };
19
31
  exports.getClash = getClash;
20
32
  const getCompatible = (dayChi) => ({
@@ -1,10 +1,11 @@
1
- import { Holiday, HolidayEvent, LunarDate } from "./type.js";
1
+ import { DateInput, Holiday, HolidayEvent, LunarDate, NextHolidaysOptions } from "./type.js";
2
2
  export declare const getVietnameseHolidays: (solarDay: number, solarMonth: number, solarYear: number, lunarDate: LunarDate, nextLunarDate: LunarDate) => Holiday[];
3
3
  /** Mọi ngày lễ rơi vào năm dương `year`, sắp theo ngày tăng dần (lễ dương trước lễ âm nếu trùng ngày). Rỗng nếu ngoài 1800–2199. */
4
4
  export declare const getHolidays: (year: number) => HolidayEvent[];
5
- /** `count` ngày lễ tiếp theo kể từ `from` (tính cả chính ngày `from`), theo ngày tăng dần. */
6
- export declare const nextHolidays: (from: {
7
- day: number;
8
- month: number;
9
- year: number;
10
- }, count: number) => HolidayEvent[];
5
+ /**
6
+ * `count` ngày lễ tiếp theo kể từ `from` (tính cả chính ngày `from`), theo ngày tăng dần.
7
+ * `from` là `DateInput`; throw `RangeError` nếu không hợp lệ. `count` đếm theo phần tử SAU khi lọc và gộp.
8
+ * Với `groupConsecutive`, một chuỗi ngày nghỉ đang diễn ra tại `from` được trả với `date` = ngày đầu thực của chuỗi
9
+ * nếu ngày đó nằm trong cùng năm dương, nên có thể sớm hơn `from`.
10
+ */
11
+ export declare const nextHolidays: (from: DateInput, count: number, options?: NextHolidaysOptions) => HolidayEvent[];
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.nextHolidays = exports.getHolidays = exports.getVietnameseHolidays = void 0;
4
+ const input_js_1 = require("./input.js");
4
5
  const lunar_js_1 = require("./lunar.js");
5
6
  const utils_js_1 = require("./utils.js");
6
7
  const typeOf = (def, calendar) => { var _a; return def.isPublicHoliday ? 'public' : (_a = def.type) !== null && _a !== void 0 ? _a : (calendar === 'lunar' ? 'traditional' : 'commemorative'); };
@@ -111,16 +112,54 @@ const getHolidays = (year) => {
111
112
  return events.sort(byDate);
112
113
  };
113
114
  exports.getHolidays = getHolidays;
114
- /** `count` ngày lễ tiếp theo kể từ `from` (tính cả chính ngày `from`), theo ngày tăng dần. */
115
- const nextHolidays = (from, count) => {
115
+ const dayNumber = (d) => (0, utils_js_1.getJulianDayNumber)(d.day, d.month, d.year);
116
+ const groupConsecutiveEvents = (events) => {
117
+ const grouped = [];
118
+ for (const e of events) {
119
+ const last = grouped[grouped.length - 1];
120
+ if (last && last.name === e.name && last.calendar === e.calendar && dayNumber(last.endDate) + 1 === dayNumber(e.date)) {
121
+ last.endDate = e.date;
122
+ last.days = dayNumber(e.date) - dayNumber(last.date) + 1;
123
+ }
124
+ else {
125
+ grouped.push({ ...e, endDate: e.date, days: 1 });
126
+ }
127
+ }
128
+ return grouped;
129
+ };
130
+ /**
131
+ * `count` ngày lễ tiếp theo kể từ `from` (tính cả chính ngày `from`), theo ngày tăng dần.
132
+ * `from` là `DateInput`; throw `RangeError` nếu không hợp lệ. `count` đếm theo phần tử SAU khi lọc và gộp.
133
+ * Với `groupConsecutive`, một chuỗi ngày nghỉ đang diễn ra tại `from` được trả với `date` = ngày đầu thực của chuỗi
134
+ * nếu ngày đó nằm trong cùng năm dương, nên có thể sớm hơn `from`.
135
+ */
136
+ const nextHolidays = (from, count, options = {}) => {
137
+ const start = (0, input_js_1.requireDateInput)(from, 'nextHolidays');
138
+ if (!Number.isInteger(count) || count <= 0)
139
+ return [];
140
+ const startJd = dayNumber(start);
141
+ const accept = (e) => (!options.types || options.types.indexOf(e.type) !== -1) && (!options.dayOffOnly || e.isDayOff);
116
142
  const result = [];
117
- const start = (0, utils_js_1.getJulianDayNumber)(from.day, from.month, from.year);
118
- for (let year = from.year; year <= 2199 && result.length < count; year++) {
119
- for (const e of (0, exports.getHolidays)(year)) {
120
- if ((0, utils_js_1.getJulianDayNumber)(e.date.day, e.date.month, e.date.year) >= start)
121
- result.push(e);
143
+ let pending = []; // các ngày (đã lọc) chưa chắc đã đủ chuỗi liền nhau
144
+ for (let year = start.year; year <= 2199; year++) {
145
+ // Lấy cả năm, kể cả các ngày trước `from`, để chuỗi liền nhau không bị cắt đôi ở `from`.
146
+ const events = (0, exports.getHolidays)(year).filter(accept);
147
+ if (!options.groupConsecutive) {
148
+ for (const e of events)
149
+ if (dayNumber(e.date) >= startJd)
150
+ result.push(e);
151
+ if (result.length >= count)
152
+ break;
153
+ continue;
154
+ }
155
+ pending = pending.concat(events);
156
+ const groups = groupConsecutiveEvents(pending);
157
+ // Chuỗi cuối có thể kéo sang năm sau (vd Tết cuối tháng 12 dương) — giữ lại đến khi biết chắc.
158
+ const upcoming = groups.filter(g => dayNumber(g.endDate) >= startJd);
159
+ if (upcoming.length >= count + 1 || year === 2199) {
160
+ return upcoming.slice(0, count);
122
161
  }
123
162
  }
124
- return result.slice(0, Math.max(0, count));
163
+ return result.slice(0, count);
125
164
  };
126
165
  exports.nextHolidays = nextHolidays;
@@ -3,7 +3,7 @@ export { getLunarDate, getSolarDate, getLeapMonth, getLunarMonthDays } from './l
3
3
  export { getJulianDayNumber, getSolarTermIndex } from './utils.js';
4
4
  export { getHeavenlyEarthly, getDayStemIndex, getDayBranchIndex, getMonthBranchIndex, getCanChi, } from './heavenly_earthly.js';
5
5
  export { getDayStarInfo } from './dayStar.js';
6
- export { getSolarTerms } from './solarTerms.js';
6
+ export { getSolarTerms, getSolarTermByIndex } from './solarTerms.js';
7
7
  export { getHolidays, nextHolidays } from './holidays.js';
8
8
  export { HEAVENLY_STEMS, EARTHLY_BRANCHES, SOLAR_TERMS } from './constants.js';
9
- export type { LunarDate, DayInfo, LunarDayInfo, DayStarInfo, Holiday, dateFormat, StarSystem, StarCategory, HolidayType, HolidayEvent, HourInfo, SolarTermItem, SolarTermInfo, CanChiInfo, CanChiPart, Element, DayInfoOptions, DateInput, SolarDateObject, SolarDateResult, } from './type.js';
9
+ export type { LunarDate, DayInfo, LunarDayInfo, DayStarInfo, Holiday, dateFormat, StarSystem, StarCategory, HolidayType, HolidayEvent, HourInfo, SolarTermItem, SolarTermInfo, CanChiInfo, CanChiPart, Element, DayInfoOptions, DateInput, LocalDateTime, NextHolidaysOptions, SolarDateObject, SolarDateResult, } from './type.js';
package/lib/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SOLAR_TERMS = exports.EARTHLY_BRANCHES = exports.HEAVENLY_STEMS = exports.nextHolidays = exports.getHolidays = exports.getSolarTerms = exports.getDayStarInfo = exports.getCanChi = exports.getMonthBranchIndex = exports.getDayBranchIndex = exports.getDayStemIndex = exports.getHeavenlyEarthly = exports.getSolarTermIndex = exports.getJulianDayNumber = exports.getLunarMonthDays = exports.getLeapMonth = exports.getSolarDate = exports.getLunarDate = exports.monthInfo = exports.getHourInfo = exports.lunarDay = exports.dayInfo = void 0;
3
+ exports.SOLAR_TERMS = exports.EARTHLY_BRANCHES = exports.HEAVENLY_STEMS = exports.nextHolidays = exports.getHolidays = exports.getSolarTermByIndex = exports.getSolarTerms = exports.getDayStarInfo = exports.getCanChi = exports.getMonthBranchIndex = exports.getDayBranchIndex = exports.getDayStemIndex = exports.getHeavenlyEarthly = exports.getSolarTermIndex = exports.getJulianDayNumber = exports.getLunarMonthDays = exports.getLeapMonth = exports.getSolarDate = exports.getLunarDate = exports.monthInfo = exports.getHourInfo = exports.lunarDay = exports.dayInfo = void 0;
4
4
  var lunarConverter_js_1 = require("./lunarConverter.js");
5
5
  Object.defineProperty(exports, "dayInfo", { enumerable: true, get: function () { return lunarConverter_js_1.dayInfo; } });
6
6
  Object.defineProperty(exports, "lunarDay", { enumerable: true, get: function () { return lunarConverter_js_1.lunarDay; } });
@@ -24,6 +24,7 @@ var dayStar_js_1 = require("./dayStar.js");
24
24
  Object.defineProperty(exports, "getDayStarInfo", { enumerable: true, get: function () { return dayStar_js_1.getDayStarInfo; } });
25
25
  var solarTerms_js_1 = require("./solarTerms.js");
26
26
  Object.defineProperty(exports, "getSolarTerms", { enumerable: true, get: function () { return solarTerms_js_1.getSolarTerms; } });
27
+ Object.defineProperty(exports, "getSolarTermByIndex", { enumerable: true, get: function () { return solarTerms_js_1.getSolarTermByIndex; } });
27
28
  var holidays_js_1 = require("./holidays.js");
28
29
  Object.defineProperty(exports, "getHolidays", { enumerable: true, get: function () { return holidays_js_1.getHolidays; } });
29
30
  Object.defineProperty(exports, "nextHolidays", { enumerable: true, get: function () { return holidays_js_1.nextHolidays; } });
@@ -3,3 +3,5 @@ export declare const MIN_YEAR = 1800;
3
3
  export declare const MAX_YEAR = 2199;
4
4
  /** Chuẩn hóa mọi dạng đầu vào về `{day, month, year}`; `null` nếu sai định dạng, không tồn tại hoặc ngoài 1800–2199. */
5
5
  export declare const parseDateInput: (input: unknown) => SolarDateObject | null;
6
+ /** Như `parseDateInput` nhưng throw `RangeError` (thông báo rõ ràng) khi đầu vào không hợp lệ. Dùng chung cho mọi hàm public nhận ngày dương. */
7
+ export declare const requireDateInput: (input: unknown, fnName: string) => SolarDateObject;
package/lib/cjs/input.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseDateInput = exports.MAX_YEAR = exports.MIN_YEAR = void 0;
3
+ exports.requireDateInput = exports.parseDateInput = exports.MAX_YEAR = exports.MIN_YEAR = void 0;
4
4
  const utils_js_1 = require("./utils.js");
5
5
  exports.MIN_YEAR = 1800;
6
6
  exports.MAX_YEAR = 2199;
@@ -37,3 +37,24 @@ const parseDateInput = (input) => {
37
37
  return isRealDate(day, month, year) ? { day, month, year } : null;
38
38
  };
39
39
  exports.parseDateInput = parseDateInput;
40
+ const describeInput = (input) => {
41
+ var _a;
42
+ if (input instanceof Date)
43
+ return isNaN(input.getTime()) ? 'Invalid Date' : input.toString();
44
+ try {
45
+ return (_a = JSON.stringify(input)) !== null && _a !== void 0 ? _a : String(input);
46
+ }
47
+ catch {
48
+ return String(input);
49
+ }
50
+ };
51
+ /** Như `parseDateInput` nhưng throw `RangeError` (thông báo rõ ràng) khi đầu vào không hợp lệ. Dùng chung cho mọi hàm public nhận ngày dương. */
52
+ const requireDateInput = (input, fnName) => {
53
+ const parsed = (0, exports.parseDateInput)(input);
54
+ if (!parsed) {
55
+ throw new RangeError(`${fnName}: invalid date ${describeInput(input)}. ` +
56
+ `Expected a Date, {day, month, year} or a 'd/m/yyyy' string that exists on the calendar, within years ${exports.MIN_YEAR}–${exports.MAX_YEAR}.`);
57
+ }
58
+ return parsed;
59
+ };
60
+ exports.requireDateInput = requireDateInput;
@@ -1,5 +1,11 @@
1
- import { LunarDate, SolarDateResult } from "./type.js";
2
- export declare const getLunarDate: (day: number, month: number, year: number) => LunarDate;
1
+ import { DateInput, LunarDate, SolarDateResult } from "./type.js";
2
+ /**
3
+ * Solar → lunar. Hai dạng gọi:
4
+ * - `getLunarDate(day, month, year)`: số; ngoài 1800–2199 trả `LunarDate` có `date: 0` (không throw).
5
+ * - `getLunarDate(date)`: `DateInput` (Date | {day,month,year} | 'd/m/yyyy'); throw `RangeError` nếu không hợp lệ.
6
+ */
7
+ export declare function getLunarDate(date: DateInput): LunarDate;
8
+ export declare function getLunarDate(day: number, month: number, year: number): LunarDate;
3
9
  /** Tháng nhuận của năm âm (1–12), hoặc 0 nếu năm không nhuận / ngoài phạm vi 1800–2199. */
4
10
  export declare const getLeapMonth: (lunarYear: number) => number;
5
11
  /** Số ngày của tháng âm (29 hoặc 30); `null` nếu tháng/năm không tồn tại (kể cả tháng nhuận không có trong năm đó). */
package/lib/cjs/lunar.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDayMaster = exports.getSolarDate = exports.getLunarMonthDays = exports.getLeapMonth = exports.getLunarDate = void 0;
3
+ exports.getDayMaster = exports.getSolarDate = exports.getLunarMonthDays = exports.getLeapMonth = void 0;
4
+ exports.getLunarDate = getLunarDate;
4
5
  const utils_js_1 = require("./utils.js");
6
+ const input_js_1 = require("./input.js");
5
7
  const constants_js_1 = require("./constants.js");
6
8
  const MIN_YEAR = 1800;
7
9
  const MAX_YEAR = 2199;
@@ -64,7 +66,17 @@ const findLunarDate = (julianDay, lunarYear) => {
64
66
  monthDays: lunarYear[index].monthDays
65
67
  };
66
68
  };
67
- const getLunarDate = (day, month, year) => {
69
+ function getLunarDate(dayOrDate, month, year) {
70
+ if (typeof dayOrDate !== 'number') {
71
+ const d = (0, input_js_1.requireDateInput)(dayOrDate, 'getLunarDate');
72
+ return lunarFromParts(d.day, d.month, d.year);
73
+ }
74
+ if (month === undefined || year === undefined) {
75
+ throw new RangeError("getLunarDate: expected (day, month, year) or a single DateInput (Date | {day, month, year} | 'd/m/yyyy').");
76
+ }
77
+ return lunarFromParts(dayOrDate, month, year);
78
+ }
79
+ const lunarFromParts = (day, month, year) => {
68
80
  if (year < MIN_YEAR || year > MAX_YEAR) {
69
81
  return ZERO_DATE(0);
70
82
  }
@@ -75,7 +87,6 @@ const getLunarDate = (day, month, year) => {
75
87
  }
76
88
  return findLunarDate(julianDay, lunarYearData);
77
89
  };
78
- exports.getLunarDate = getLunarDate;
79
90
  const isLunarYear = (year) => Number.isInteger(year) && year >= MIN_YEAR && year <= MAX_YEAR;
80
91
  const findMonth = (lunarMonth, lunarYear, isLeap) => {
81
92
  if (!isLunarYear(lunarYear))
@@ -1,7 +1,9 @@
1
1
  import { DateInput, DayInfo, DayInfoOptions, HourInfo, LunarDayInfo } from "./type.js";
2
- export declare const dayInfo: (date: DateInput, options?: DayInfoOptions) => DayInfo | string;
3
- export declare const lunarDay: (date: DateInput) => LunarDayInfo | string;
4
- /** Thông tin một giờ (0..23) của ngày; `null` nếu ngày hoặc giờ không hợp lệ. */
2
+ /** Throw `RangeError` nếu `date` không hợp lệ (từ 2.1.1; trước đây trả chuỗi 'Invalid date input.'). */
3
+ export declare const dayInfo: (date: DateInput, options?: DayInfoOptions) => DayInfo;
4
+ /** Throw `RangeError` nếu `date` không hợp lệ. */
5
+ export declare const lunarDay: (date: DateInput) => LunarDayInfo;
6
+ /** Thông tin một giờ (0..23) của ngày. Throw `RangeError` nếu `date` không hợp lệ; trả `null` nếu `hour` không phải số nguyên 0..23. */
5
7
  export declare const getHourInfo: (date: DateInput, hour: number, options?: DayInfoOptions) => HourInfo | null;
6
8
  /** `dayInfo` cho mọi ngày của tháng dương (28–31 phần tử); `null` nếu tháng/năm không hợp lệ hoặc ngoài 1800–2199. */
7
9
  export declare const monthInfo: (year: number, month: number, options?: DayInfoOptions) => DayInfo[] | null;
@@ -11,7 +11,6 @@ const compat_js_1 = require("./compat.js");
11
11
  const solarTerms_js_1 = require("./solarTerms.js");
12
12
  const utils_js_1 = require("./utils.js");
13
13
  const input_js_1 = require("./input.js");
14
- const INVALID = 'Invalid date input.';
15
14
  const getDayName = (lunarDate) => {
16
15
  if (lunarDate.date === 0) {
17
16
  return { date: '', month: '', year: '' };
@@ -50,7 +49,7 @@ const buildDayInfo = (day, month, year, options) => {
50
49
  hours: (0, auspiciousHours_js_1.getHours)(jd, system),
51
50
  solarTerm: (0, solarTerms_js_1.getSolarTermInfo)(jd),
52
51
  canChi,
53
- clash: (0, compat_js_1.getClash)(canChi.day.can, canChi.day.chi),
52
+ clash: (0, compat_js_1.getClash)(getElement(napAm), canChi.day.chi),
54
53
  compatible: (0, compat_js_1.getCompatible)(canChi.day.chi),
55
54
  };
56
55
  };
@@ -58,30 +57,30 @@ const nextSolarParts = (day, month, year) => {
58
57
  const d = new Date(Date.UTC(year, month - 1, day + 1));
59
58
  return [d.getUTCDate(), d.getUTCMonth() + 1, d.getUTCFullYear()];
60
59
  };
60
+ /** Throw `RangeError` nếu `date` không hợp lệ (từ 2.1.1; trước đây trả chuỗi 'Invalid date input.'). */
61
61
  const dayInfo = (date, options) => {
62
- const parsed = (0, input_js_1.parseDateInput)(date);
63
- if (!parsed)
64
- return INVALID;
62
+ const parsed = (0, input_js_1.requireDateInput)(date, 'dayInfo');
65
63
  return buildDayInfo(parsed.day, parsed.month, parsed.year, options);
66
64
  };
67
65
  exports.dayInfo = dayInfo;
66
+ /** Throw `RangeError` nếu `date` không hợp lệ. */
68
67
  const lunarDay = (date) => {
69
- const parsed = (0, input_js_1.parseDateInput)(date);
70
- if (!parsed)
71
- return INVALID;
68
+ const parsed = (0, input_js_1.requireDateInput)(date, 'lunarDay');
72
69
  const lunar = (0, lunar_js_1.getLunarDate)(parsed.day, parsed.month, parsed.year);
70
+ const text = getDayName(lunar);
73
71
  return {
74
72
  date: lunar.date,
75
- date_text: getDayName(lunar),
73
+ date_text: text,
74
+ dateText: { ...text },
76
75
  month: lunar.month,
77
76
  isLeap: lunar.isLeap,
78
77
  };
79
78
  };
80
79
  exports.lunarDay = lunarDay;
81
- /** Thông tin một giờ (0..23) của ngày; `null` nếu ngày hoặc giờ không hợp lệ. */
80
+ /** Thông tin một giờ (0..23) của ngày. Throw `RangeError` nếu `date` không hợp lệ; trả `null` nếu `hour` không phải số nguyên 0..23. */
82
81
  const getHourInfo = (date, hour, options) => {
83
- const parsed = (0, input_js_1.parseDateInput)(date);
84
- if (!parsed || !Number.isInteger(hour) || hour < 0 || hour > 23)
82
+ const parsed = (0, input_js_1.requireDateInput)(date, 'getHourInfo');
83
+ if (!Number.isInteger(hour) || hour < 0 || hour > 23)
85
84
  return null;
86
85
  const system = (options === null || options === void 0 ? void 0 : options.starSystem) === 'ngoc-hap' ? 'ngoc-hap' : 'hiep-ky';
87
86
  const jd = (0, utils_js_1.getJulianDayNumber)(parsed.day, parsed.month, parsed.year);
@@ -90,7 +89,7 @@ const getHourInfo = (date, hour, options) => {
90
89
  exports.getHourInfo = getHourInfo;
91
90
  /** `dayInfo` cho mọi ngày của tháng dương (28–31 phần tử); `null` nếu tháng/năm không hợp lệ hoặc ngoài 1800–2199. */
92
91
  const monthInfo = (year, month, options) => {
93
- if (!Number.isInteger(month) || month < 1 || month > 12 || !(0, input_js_1.parseDateInput)({ day: 1, month, year }))
92
+ if (!Number.isInteger(month) || month < 1 || month > 12 || !Number.isInteger(year) || year < 1800 || year > 2199)
94
93
  return null;
95
94
  const days = new Date(Date.UTC(year, month, 0)).getUTCDate();
96
95
  const result = [];
@@ -1,5 +1,12 @@
1
1
  import { SolarTermInfo, SolarTermItem } from "./type.js";
2
2
  /** Tiết khí hiện tại và kế tiếp của ngày có JDN `julianDay` (múi giờ UTC+7). */
3
3
  export declare const getSolarTermInfo: (julianDay: number) => SolarTermInfo;
4
- /** 24 tiết khí của năm dương, sắp theo thời gian (Tiểu Hàn → Đông Chí). Sai số cỡ vài chục phút do công thức vị trí Mặt Trời rút gọn. */
4
+ /**
5
+ * 24 tiết khí của năm dương, sắp theo THỜI GIAN trong năm (bắt đầu từ Tiểu Hàn tháng 1, kết thúc ở Đông Chí).
6
+ * Vì vậy vị trí trong mảng KHÁC `index`: `index` là số thứ tự chuẩn trong `SOLAR_TERMS` (0 = Xuân Phân), cùng gốc với
7
+ * `dayInfo().solarTerm.*.index`. Muốn tra theo `index` dùng `getSolarTermByIndex`.
8
+ * Thời điểm tính theo công thức vị trí Mặt Trời rút gọn (sai số cỡ vài chục phút); `start` là instant, `startLocal` là giờ UTC+7.
9
+ */
5
10
  export declare const getSolarTerms: (year: number) => SolarTermItem[];
11
+ /** Tiết khí có số thứ tự chuẩn `index` (0 = Xuân Phân ... 23 = Kinh Trập) rơi vào năm dương `year`; `null` nếu `year`/`index` không hợp lệ. */
12
+ export declare const getSolarTermByIndex: (year: number, index: number) => SolarTermItem | null;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSolarTerms = exports.getSolarTermInfo = void 0;
3
+ exports.getSolarTermByIndex = exports.getSolarTerms = exports.getSolarTermInfo = void 0;
4
4
  const constants_js_1 = require("./constants.js");
5
5
  const utils_js_1 = require("./utils.js");
6
6
  const TWO_PI = Math.PI * 2;
@@ -49,7 +49,15 @@ const getTermRecord = (julianDay) => {
49
49
  }
50
50
  return rec;
51
51
  };
52
- const item = (index, jd) => ({ name: constants_js_1.SOLAR_TERMS[index], index, start: jdToDate(jd) });
52
+ const toLocal = (date) => {
53
+ const t = new Date(date.getTime() + TZ_VN * 3600000);
54
+ return { day: t.getUTCDate(), month: t.getUTCMonth() + 1, year: t.getUTCFullYear(), hour: t.getUTCHours(), minute: t.getUTCMinutes() };
55
+ };
56
+ const item = (index, jd) => {
57
+ const start = jdToDate(jd);
58
+ return { name: constants_js_1.SOLAR_TERMS[index], index, start, startLocal: toLocal(start) };
59
+ };
60
+ const copyItem = (t) => ({ ...t, start: new Date(t.start.getTime()), startLocal: { ...t.startLocal } });
53
61
  /** Tiết khí hiện tại và kế tiếp của ngày có JDN `julianDay` (múi giờ UTC+7). */
54
62
  const getSolarTermInfo = (julianDay) => {
55
63
  const rec = getTermRecord(julianDay);
@@ -60,7 +68,12 @@ const getSolarTermInfo = (julianDay) => {
60
68
  };
61
69
  exports.getSolarTermInfo = getSolarTermInfo;
62
70
  const yearCache = new Map();
63
- /** 24 tiết khí của năm dương, sắp theo thời gian (Tiểu Hàn → Đông Chí). Sai số cỡ vài chục phút do công thức vị trí Mặt Trời rút gọn. */
71
+ /**
72
+ * 24 tiết khí của năm dương, sắp theo THỜI GIAN trong năm (bắt đầu từ Tiểu Hàn tháng 1, kết thúc ở Đông Chí).
73
+ * Vì vậy vị trí trong mảng KHÁC `index`: `index` là số thứ tự chuẩn trong `SOLAR_TERMS` (0 = Xuân Phân), cùng gốc với
74
+ * `dayInfo().solarTerm.*.index`. Muốn tra theo `index` dùng `getSolarTermByIndex`.
75
+ * Thời điểm tính theo công thức vị trí Mặt Trời rút gọn (sai số cỡ vài chục phút); `start` là instant, `startLocal` là giờ UTC+7.
76
+ */
64
77
  const getSolarTerms = (year) => {
65
78
  if (!Number.isInteger(year) || year < 1800 || year > 2199)
66
79
  return [];
@@ -77,6 +90,14 @@ const getSolarTerms = (year) => {
77
90
  cached.sort((a, b) => a.start.getTime() - b.start.getTime());
78
91
  yearCache.set(year, cached);
79
92
  }
80
- return cached.map(t => ({ ...t, start: new Date(t.start.getTime()) }));
93
+ return cached.map(copyItem);
81
94
  };
82
95
  exports.getSolarTerms = getSolarTerms;
96
+ /** Tiết khí có số thứ tự chuẩn `index` (0 = Xuân Phân ... 23 = Kinh Trập) rơi vào năm dương `year`; `null` nếu `year`/`index` không hợp lệ. */
97
+ const getSolarTermByIndex = (year, index) => {
98
+ var _a;
99
+ if (!Number.isInteger(index) || index < 0 || index > 23)
100
+ return null;
101
+ return (_a = (0, exports.getSolarTerms)(year).find(t => t.index === index)) !== null && _a !== void 0 ? _a : null;
102
+ };
103
+ exports.getSolarTermByIndex = getSolarTermByIndex;
package/lib/cjs/type.d.ts CHANGED
@@ -47,6 +47,10 @@ export type SolarDateResult = {
47
47
  jd: number;
48
48
  };
49
49
  export type HolidayEvent = {
50
+ /** Chỉ có khi `groupConsecutive: true`: ngày cuối của chuỗi ngày nghỉ liền nhau cùng tên. */
51
+ endDate?: SolarDateObject;
52
+ /** Chỉ có khi `groupConsecutive: true`: số ngày của chuỗi (1 nếu không liền nhau). */
53
+ days?: number;
50
54
  name: string;
51
55
  date: SolarDateObject;
52
56
  calendar: 'solar' | 'lunar';
@@ -68,13 +72,26 @@ export type HourInfo = {
68
72
  star: string;
69
73
  category: StarCategory;
70
74
  };
75
+ /** Ngày giờ theo giờ Việt Nam (UTC+7). `month` 1..12, `hour` 0..23. */
76
+ export type LocalDateTime = {
77
+ day: number;
78
+ month: number;
79
+ year: number;
80
+ hour: number;
81
+ minute: number;
82
+ };
71
83
  export type SolarTermItem = {
72
84
  name: string;
85
+ /** Số thứ tự chuẩn trong `SOLAR_TERMS`: 0 = Xuân Phân, 1 = Thanh Minh, ... 18 = Đông Chí, 21 = Lập Xuân, 23 = Kinh Trập. */
73
86
  index: number;
87
+ /** Thời điểm tuyệt đối (instant). In ra bằng `toISOString()` là UTC; giờ Việt Nam = UTC + 7 — xem `startLocal`. */
74
88
  start: Date;
89
+ /** `start` đổi sang giờ Việt Nam (UTC+7), không phụ thuộc múi giờ máy chạy. */
90
+ startLocal: LocalDateTime;
75
91
  };
76
92
  export type SolarTermInfo = {
77
93
  current: SolarTermItem;
94
+ /** `daysUntil`: số ngày lịch (UTC+7) từ ngày truy vấn đến ngày tiết khí bắt đầu; không phụ thuộc giờ trong ngày hay múi giờ máy. */
78
95
  next: SolarTermItem & {
79
96
  daysUntil: number;
80
97
  };
@@ -91,6 +108,14 @@ export type CanChiInfo = {
91
108
  hour: CanChiPart;
92
109
  };
93
110
  export type Element = 'Kim' | 'Mộc' | 'Thủy' | 'Hỏa' | 'Thổ';
111
+ export type NextHolidaysOptions = {
112
+ /** Gộp các ngày liền nhau cùng `name` thành một phần tử (`date` = ngày đầu, thêm `endDate` và `days`). Mặc định false. */
113
+ groupConsecutive?: boolean;
114
+ /** Chỉ lấy các loại này. */
115
+ types?: HolidayType[];
116
+ /** Chỉ lấy ngày nghỉ hưởng lương (`isDayOff`). */
117
+ dayOffOnly?: boolean;
118
+ };
94
119
  export type DayInfoOptions = {
95
120
  starSystem?: StarSystem;
96
121
  };
@@ -118,6 +143,10 @@ export type DayInfo = {
118
143
  hours: HourInfo[];
119
144
  solarTerm: SolarTermInfo;
120
145
  canChi: CanChiInfo;
146
+ /**
147
+ * `chi`: Chi xung (lục xung, luôn 1 phần tử). `canChi`: các tuổi xung = Chi đối + Nạp âm tương khắc với Nạp âm của ngày
148
+ * (luôn 2 phần tử, sắp theo Can). Dùng `chi` làm dự phòng nếu `canChi` rỗng (chỉ xảy ra khi không xác định được Nạp âm).
149
+ */
121
150
  clash: {
122
151
  canChi: string[];
123
152
  chi: string[];
@@ -129,7 +158,10 @@ export type DayInfo = {
129
158
  };
130
159
  export type LunarDayInfo = {
131
160
  date: number;
161
+ /** @deprecated snake_case giữ cho tương thích; dùng `dateText` (cùng giá trị). */
132
162
  date_text: dateFormat;
163
+ /** Can Chi của ngày/tháng/năm âm. Cùng giá trị với `date_text`. */
164
+ dateText: dateFormat;
133
165
  month: number;
134
166
  isLeap: boolean;
135
167
  };
@@ -1,4 +1,5 @@
1
- export declare const getClash: (dayCan: number, dayChi: number) => {
1
+ import { Element } from "./type.js";
2
+ export declare const getClash: (dayElement: Element | null, dayChi: number) => {
2
3
  canChi: string[];
3
4
  chi: string[];
4
5
  };
package/lib/esm/compat.js CHANGED
@@ -1,17 +1,29 @@
1
- import { EARTHLY_BRANCHES, HEAVENLY_STEMS } from "./constants.js";
1
+ import { EARTHLY_BRANCHES, HEAVENLY_EARTHLY_ELEMENT_MAP, HEAVENLY_STEMS } from "./constants.js";
2
+ // Quy tắc tuổi xung (khớp cách liệt kê của lịch vạn niên phổ biến):
3
+ // tuổi xung với ngày = tuổi có Chi ĐỐI (lục xung, cách 6) với Chi của ngày
4
+ // VÀ hành Nạp âm của tuổi tương khắc với hành Nạp âm của ngày (khắc một trong hai chiều).
5
+ // Ví dụ: ngày Giáp Tý (Hải Trung Kim) xung Mậu Ngọ (Hỏa khắc Kim) và Nhâm Ngọ (Kim khắc Mộc);
6
+ // ngày Kỷ Mão (Thành Đầu Thổ) xung Tân Dậu (Mộc khắc Thổ) và Ất Dậu (Thổ khắc Thủy).
7
+ // 5 tuổi cùng một Chi mang đủ 5 hành, nên mỗi ngày luôn có đúng 2 tuổi xung.
2
8
  // Tam hợp: Thân-Tý-Thìn, Hợi-Mão-Mùi, Dần-Ngọ-Tuất, Tỵ-Dậu-Sửu (Chi cách nhau 4).
3
9
  // Lục hợp: Tý-Sửu, Dần-Hợi, Mão-Tuất, Thìn-Dậu, Tỵ-Thân, Ngọ-Mùi (tổng chỉ số ≡ 1 mod 12).
4
- // Lục xung: Chi đối nhau (cách 6).
5
- // Thiên khắc địa xung: Chi đối nhau + Can tương xung (Giáp-Canh, Ất-Tân, Bính-Nhâm, Đinh-Quý).
6
- // Mậu và Kỷ không có Can xung, nên với ngày Mậu/Kỷ `canChi` rỗng (vẫn có `chi`).
7
- const getClashCan = (can) => (can <= 3 ? can + 6 : can >= 6 ? can - 6 : -1);
8
- export const getClash = (dayCan, dayChi) => {
10
+ const KHAC = { 'Kim': 'Mộc', 'Mộc': 'Thổ', 'Thổ': 'Thủy', 'Thủy': 'Hỏa', 'Hỏa': 'Kim' };
11
+ const elementOf = (canChiText) => {
12
+ const napAm = HEAVENLY_EARTHLY_ELEMENT_MAP[canChiText];
13
+ return napAm ? napAm.split(' ').pop() : null;
14
+ };
15
+ export const getClash = (dayElement, dayChi) => {
9
16
  const oppositeChi = (dayChi + 6) % 12;
10
- const clashCan = getClashCan(dayCan);
11
- return {
12
- canChi: clashCan === -1 ? [] : [`${HEAVENLY_STEMS[clashCan]} ${EARTHLY_BRANCHES[oppositeChi]}`],
13
- chi: [EARTHLY_BRANCHES[oppositeChi]],
14
- };
17
+ const canChi = [];
18
+ if (dayElement) {
19
+ for (let can = oppositeChi % 2; can < 10; can += 2) { // Can và Chi cùng chẵn/lẻ mới có trong 60 Hoa Giáp
20
+ const text = `${HEAVENLY_STEMS[can]} ${EARTHLY_BRANCHES[oppositeChi]}`;
21
+ const el = elementOf(text);
22
+ if (el && (KHAC[el] === dayElement || KHAC[dayElement] === el))
23
+ canChi.push(text);
24
+ }
25
+ }
26
+ return { canChi, chi: [EARTHLY_BRANCHES[oppositeChi]] };
15
27
  };
16
28
  export const getCompatible = (dayChi) => ({
17
29
  tamHop: [EARTHLY_BRANCHES[(dayChi + 4) % 12], EARTHLY_BRANCHES[(dayChi + 8) % 12]],
@@ -1,10 +1,11 @@
1
- import { Holiday, HolidayEvent, LunarDate } from "./type.js";
1
+ import { DateInput, Holiday, HolidayEvent, LunarDate, NextHolidaysOptions } from "./type.js";
2
2
  export declare const getVietnameseHolidays: (solarDay: number, solarMonth: number, solarYear: number, lunarDate: LunarDate, nextLunarDate: LunarDate) => Holiday[];
3
3
  /** Mọi ngày lễ rơi vào năm dương `year`, sắp theo ngày tăng dần (lễ dương trước lễ âm nếu trùng ngày). Rỗng nếu ngoài 1800–2199. */
4
4
  export declare const getHolidays: (year: number) => HolidayEvent[];
5
- /** `count` ngày lễ tiếp theo kể từ `from` (tính cả chính ngày `from`), theo ngày tăng dần. */
6
- export declare const nextHolidays: (from: {
7
- day: number;
8
- month: number;
9
- year: number;
10
- }, count: number) => HolidayEvent[];
5
+ /**
6
+ * `count` ngày lễ tiếp theo kể từ `from` (tính cả chính ngày `from`), theo ngày tăng dần.
7
+ * `from` là `DateInput`; throw `RangeError` nếu không hợp lệ. `count` đếm theo phần tử SAU khi lọc và gộp.
8
+ * Với `groupConsecutive`, một chuỗi ngày nghỉ đang diễn ra tại `from` được trả với `date` = ngày đầu thực của chuỗi
9
+ * nếu ngày đó nằm trong cùng năm dương, nên có thể sớm hơn `from`.
10
+ */
11
+ export declare const nextHolidays: (from: DateInput, count: number, options?: NextHolidaysOptions) => HolidayEvent[];