bizdays-toolkit 0.31.5 → 0.31.6

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
@@ -6,6 +6,19 @@ Entries from 0.31.0 are in English. Earlier entries remain in Japanese (their **
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.31.6] - 2026-11-12
10
+
11
+ This version fixes option diagnostics that returned an `error` for options that can be created, exceptions without a `code`, and the code for malformed strings in `closingPeriod`, and corrects documents. Results of calls that already succeeded do not change; some calls that threw an exception without a `code` now throw one with a `code`, and a calendar's `closingPeriod` now reports a malformed month string as `invalid-month`.
12
+
13
+ ### Fixed
14
+
15
+ - `diagnoseCalendar` and `diagnoseSchedule` returned an `error` (`out-of-range`, at `holidayProvider` even without a provider) for options with an effective period that starts or ends near the edge of the years 1000–9999 and is open on the other side (such as `effectiveFrom: "9999-12-31"` or `effectiveTo: "1000-06-01"`), although `createCalendar` and `createSchedule` accept such options. The diagnostics widened the period by the rule's cycle beyond those years. `diagnoseSchedule` returned the same `error` for a schedule with a `businessDayOfWeek` rule and a holiday provider whose supported years reach 1000 or 9999, because the first and last weeks run past those years. The diagnostics now keep the scan within 1000–9999; a day at the edge whose rule looks at a day outside those years does not match, and in a schedule the days of such a week are treated as days whose rules cannot be judged; the design decisions (17) now say so. An `out-of-range` thrown by a provider elsewhere is still reported as before
16
+ - `diagnoseSchedule` returned an `error` (`invalid-provider-data`, "Maximum call stack size exceeded") for a schedule whose values contain a cycle (such as an object that refers to itself), although `createSchedule` accepts it. Values are now compared with cycles taken into account (two cyclic values of the same shape are the same); values nested too deeply to compare are treated as different, so the diagnostics never report them as `redundant`
17
+ - A holiday provider that returned an array with holes, and `withSubstituteDays` given such an array, threw a `TypeError` without a `code` ("Cannot read properties of undefined"). A hole is now checked like an item that is not an object: evaluations throw `invalid-provider-data`, the diagnostics return it as an `error`, and `withSubstituteDays` throws `invalid-argument`, each as a `RangeError` (it was a `TypeError`); use `isBizdaysError` and `code` rather than `instanceof TypeError`
18
+ - An instant written as `{ epochMilliseconds }` beyond the range of `Date` (more than 8.64e15 milliseconds from 1970, such as `1e20`) threw `RangeError: Invalid time value` without a `code`; it now throws `out-of-range`. A Temporal-like date whose `calendarId` is not a string (such as a symbol) threw a `TypeError` without a `code`; it now throws `invalid-input` like other calendars (the message shows the value as other messages do)
19
+ - A calendar's `closingPeriod` read only `"YYYY-MM"` as a month and any other string as a date, so a malformed month (such as `"2026-1"`) threw `invalid-date`, while the function `closingPeriod` threw `invalid-month`. Both now read a string with three or more parts separated by hyphens as a date (or date-time) and any other string as a month, so `"2026-1"`, `"202611"` and `"2026/11"` throw `invalid-month` in both; the rule is now written in the design decisions (8). The function `closingPeriod` now throws `invalid-date` instead of `invalid-month` for a date-time string with an offset (such as `"2026-11-05T10:00-05:00"`), which it does not accept. Valid months, dates and date-times give the same results as before. If you branch on `invalid-date` for a malformed month passed to the method, branch on `invalid-month`
20
+ - Documentation: the provider guide, the description of `HolidayProvider` and the design decisions (24) now say that, within a run of holidays longer than 31 days, `"after-holiday"` rules are not moved and rules for the day before or after a holiday do not match (write a long closure with `closedDates` or `closedAnnualDates`); the guide already said that 31 days is the furthest a move reaches. The README now gives the bundle sizes measured for 0.31.6 (they were measured for 0.31.1). The guide and the design decisions (1) now say that evaluating a day inside the years 1000–9999 throws `out-of-range` when the evaluation needs a day outside them (the neighbouring day of a rule for the day before or after a holiday, the days an `"after-holiday"` rule looks back on, the week of `businessDayOfWeek`, the start of a closing period), with or without a holiday provider and with `outOfRangePolicy: "ignore"` too, because those days do not exist as dates; the guide described only searches that leave those years. Results are unchanged
21
+
9
22
  ## [0.31.5] - 2026-11-11
10
23
 
11
24
  This version fixes results and errors that did not keep the guide, the type descriptions and the design decisions, and corrects documents. Schedules with `value: undefined` in a specific date, an annual date or a recurring rule now return `undefined` on those days, a wrong weekday number in three places now throws `invalid-weekday` instead of `invalid-option`, a schedule with both `sameAs` and `value` in `holidayPolicy` now throws, and calls that took an instant in the years BC or a year outside 1000–9999 now throw `out-of-range`. Other results do not change.
@@ -916,7 +929,8 @@ This version rebuilds the `redundant` diagnostic so that the reported rules can
916
929
 
917
930
  - 日本: 2026 年 2 月の官報(2027 年の春分・秋分の日)まで反映(`dataVersion: "2026-02"`、`confirmedThrough: 2027`)
918
931
 
919
- [Unreleased]: https://github.com/inohan77/bizdays-toolkit/compare/v0.31.5...HEAD
932
+ [Unreleased]: https://github.com/inohan77/bizdays-toolkit/compare/v0.31.6...HEAD
933
+ [0.31.6]: https://github.com/inohan77/bizdays-toolkit/compare/v0.31.5...v0.31.6
920
934
  [0.31.5]: https://github.com/inohan77/bizdays-toolkit/compare/v0.31.4...v0.31.5
921
935
  [0.31.4]: https://github.com/inohan77/bizdays-toolkit/compare/v0.31.3...v0.31.4
922
936
  [0.31.3]: https://github.com/inohan77/bizdays-toolkit/compare/v0.31.2...v0.31.3
package/README.md CHANGED
@@ -23,7 +23,7 @@ A business-day calendar that combines public holidays with company or store clos
23
23
  - **Recurring rules**: "closed on the 3rd Wednesday", "every other Wednesday", "stocktaking on the last day of the month", "closed on Mondays, moved to the next non-holiday when Monday is a holiday", "the day after (or before) a holiday", "the last weekday before a bank holiday", shift cycles such as four days on and two days off, each with its own behaviour on holidays and an optional effective period
24
24
  - **Day-by-day values and combined calendars**: store hours that change by weekday, holiday and date; a business-day calendar made from such a schedule; and the days on which all (or any) of several calendars are open
25
25
  - **Option diagnostics**: `bizdays-toolkit/diagnose` lists rules that never apply, rules that are shadowed or redundant, rules that have ended, typos in option names and options with no business day at all, without throwing. One line in a test keeps options honest
26
- - **Lightweight, zero dependencies, time-zone safe**: about 13.6 KB min+gzip (about 15.4 KB with Japanese holidays, about 15.8 KB with UK bank holidays, about 14.5 KB with French holidays, about 14.6 KB with Singapore holidays; measured for 0.31.1). Standard JavaScript and `Intl` only, ESM with bundled types; tested on Node.js, Chrome, Firefox, Edge, Safari, Cloudflare Workers, Bun and Deno. Instants such as `Date` are converted to a civil date in the calendar's time zone before evaluation
26
+ - **Lightweight, zero dependencies, time-zone safe**: about 14.0 KB min+gzip (about 15.8 KB with Japanese holidays, about 16.2 KB with UK bank holidays, about 14.9 KB with French holidays, about 15.0 KB with Singapore holidays; measured for 0.31.6). Standard JavaScript and `Intl` only, ESM with bundled types; tested on Node.js, Chrome, Firefox, Edge, Safari, Cloudflare Workers, Bun and Deno. Instants such as `Date` are converted to a civil date in the calendar's time zone before evaluation
27
27
  - **Extensible to other countries and regions**: holiday providers are pluggable through the `HolidayProvider` interface, and `bizdays-toolkit/provider` exposes the calendar arithmetic (nth weekday of a month, Easter, substitute days for holidays on a weekend) that most countries' holidays are built from
28
28
 
29
29
  ## Installation
@@ -3,7 +3,7 @@ import { checkUnknownKeys, internalError, IssueCollector, describeValue, rangeEr
3
3
  import { isOptionsObject } from "./options.js";
4
4
  import { renameKeys, writtenKey } from "./renamed.js";
5
5
  import { parseTimeOfDaySeconds, toCivil } from "./input.js";
6
- import { checkClosingDay, closingDayIn, closingMonth as calendarClosingMonth, dayOfMonth, formatMonth, isDayOfMonthValue, parseMonth, shiftMonth, } from "./month.js";
6
+ import { checkClosingDay, closingDayIn, closingMonth as calendarClosingMonth, dayOfMonth, formatMonth, isDayOfMonthValue, parseMonth, readsAsDate, shiftMonth, } from "./month.js";
7
7
  import { buildSchedule, compileAnnualRules, compileDateRules, compileHolidaySource, compileRecurringRules, createHolidayLookup, infoOfSchedule, valuesOfSchedule, isWeekday, mapDays, RECURRING_RULE_KEYS, jpUsage, } from "./schedule.js";
8
8
  export const CALENDAR_OPTION_KEYS = [
9
9
  "holidayProvider",
@@ -303,10 +303,9 @@ function buildCalendar(lookup, isOpen, info, searchDayLimit) {
303
303
  closingPeriod(input, closingDayOfMonth, rawOptions) {
304
304
  checkClosingDay(closingDayOfMonth);
305
305
  const options = adjustOptions(rawOptions, "closingPeriod options");
306
- const isMonth = typeof input === "string" && /^\d{4}-\d{2}$/.test(input);
307
- const month = isMonth
308
- ? parseMonth(input)
309
- : closingMonthOf(dateOf(input), closingDayOfMonth, options);
306
+ const month = typeof input !== "string" || readsAsDate(input)
307
+ ? closingMonthOf(dateOf(input), closingDayOfMonth, options)
308
+ : parseMonth(input);
310
309
  const to = closingDate(month, closingDayOfMonth, options);
311
310
  const from = addDays(closingDate(shiftMonth(month, -1), closingDayOfMonth, options), 1);
312
311
  if (from > to) {
@@ -47,8 +47,12 @@ function closestOf(key, known) {
47
47
  }
48
48
  const PATTERN_FROM = toDayNumber("2001-01-01");
49
49
  const PATTERN_DAYS = toDayNumber("2028-12-31") - PATTERN_FROM + 1;
50
+ const MIN_DAY = toDayNumber("1000-01-01");
51
+ const MAX_DAY = toDayNumber("9999-12-31");
50
52
  const MAX_WINDOW_YEARS = 200;
51
53
  const MAX_DATE_RULE_DAYS = 366 * MAX_WINDOW_YEARS;
54
+ const atCalendarEdge = (n, error) => (n < MIN_DAY + MAX_HOLIDAY_RUN || n > MAX_DAY - MAX_HOLIDAY_RUN) &&
55
+ error?.code === "out-of-range";
52
56
  const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
53
57
  const lcm = (a, b) => (a / gcd(a, b)) * b;
54
58
  function patternCycle(rules) {
@@ -100,7 +104,7 @@ function scanWindow(source, rules, cycle) {
100
104
  continue;
101
105
  const from = rule.effectiveFrom ?? rule.effectiveTo - cycle + 1;
102
106
  const to = Math.min(rule.effectiveTo ?? from + cycle - 1, from + 366 * MAX_WINDOW_YEARS - 1);
103
- ranges.push({ from, to });
107
+ ranges.push({ from: Math.max(from, MIN_DAY), to: Math.min(to, MAX_DAY) });
104
108
  }
105
109
  }
106
110
  ranges.sort((a, b) => a.from - b.from);
@@ -136,8 +140,9 @@ function patternBits(rule) {
136
140
  return bits;
137
141
  }
138
142
  function matchesAnyDay(rule, cycle) {
139
- const from = rule.effectiveFrom ?? (rule.effectiveTo === null ? PATTERN_FROM : rule.effectiveTo - cycle + 1);
140
- const to = Math.min(rule.effectiveTo ?? Infinity, from + cycle - 1);
143
+ const start = rule.effectiveFrom ?? (rule.effectiveTo === null ? PATTERN_FROM : rule.effectiveTo - cycle + 1);
144
+ const from = Math.max(start, MIN_DAY);
145
+ const to = Math.min(rule.effectiveTo ?? Infinity, start + cycle - 1, MAX_DAY);
141
146
  for (let n = from; n <= to; n++)
142
147
  if (matchPattern(rule, fromDayNumber(n)))
143
148
  return true;
@@ -194,15 +199,32 @@ function canCover(earlier, later) {
194
199
  }
195
200
  }
196
201
  function sameValue(a, b) {
202
+ try {
203
+ return sameValueIn(a, b, new Map());
204
+ }
205
+ catch (error) {
206
+ if (error instanceof RangeError)
207
+ return false;
208
+ throw error;
209
+ }
210
+ }
211
+ function sameValueIn(a, b, seen) {
197
212
  if (Object.is(a, b))
198
213
  return true;
199
214
  if (typeof a !== "object" || typeof b !== "object" || a === null || b === null)
200
215
  return false;
216
+ const pairs = seen.get(a);
217
+ if (pairs?.has(b))
218
+ return true;
219
+ if (pairs)
220
+ pairs.add(b);
221
+ else
222
+ seen.set(a, new Set([b]));
201
223
  if (Array.isArray(a) || Array.isArray(b)) {
202
224
  return (Array.isArray(a) &&
203
225
  Array.isArray(b) &&
204
226
  a.length === b.length &&
205
- a.every((v, i) => sameValue(v, b[i])));
227
+ a.every((v, i) => sameValueIn(v, b[i], seen)));
206
228
  }
207
229
  const plain = (o) => {
208
230
  const proto = Object.getPrototypeOf(o);
@@ -214,7 +236,7 @@ function sameValue(a, b) {
214
236
  const kb = Object.keys(b);
215
237
  return (ka.length === kb.length &&
216
238
  ka.every((k) => Object.hasOwn(b, k) &&
217
- sameValue(a[k], b[k])));
239
+ sameValueIn(a[k], b[k], seen)));
218
240
  }
219
241
  const yearOf = (dayNumber) => parseDate(fromDayNumber(dayNumber)).year;
220
242
  function without(compiled, rule, dropDates) {
@@ -319,10 +341,16 @@ export function unjudgeableDays(compiled, window) {
319
341
  }
320
342
  const start = from - ((weekdayOf(fromDayNumber(from)) - r.weekStart + 7) % 7);
321
343
  for (let w = start; w <= to; w += 7) {
322
- const week = [0, 1, 2, 3, 4, 5, 6].map((i) => w + i);
344
+ const all = [0, 1, 2, 3, 4, 5, 6].map((i) => w + i);
345
+ const week = all.filter((n) => n >= MIN_DAY && n <= MAX_DAY);
323
346
  const weekMonths = [...new Set(week.map(monthOf))];
324
347
  if (!weekMonths.some(inMonths))
325
348
  continue;
349
+ if (week.length < 7) {
350
+ for (const n of week)
351
+ days.add(n);
352
+ continue;
353
+ }
326
354
  if (weekMonths.map(throws).some(Boolean))
327
355
  for (const n of week)
328
356
  days.add(n);
@@ -459,9 +487,19 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
459
487
  if (source.providers.length === 0 || !window)
460
488
  return false;
461
489
  let found = false;
462
- eachDay(window, (n) => (found =
463
- inPeriod(rule, n) &&
464
- matchPattern(rule, fromDayNumber(n), holidayOfSafe, undefined, lookup.fromJp)));
490
+ eachDay(window, (n) => {
491
+ if (!inPeriod(rule, n))
492
+ return false;
493
+ try {
494
+ found = matchPattern(rule, fromDayNumber(n), holidayOfSafe, undefined, lookup.fromJp);
495
+ }
496
+ catch (error) {
497
+ if (!atCalendarEdge(n, error))
498
+ throw error;
499
+ found = false;
500
+ }
501
+ return found;
502
+ });
465
503
  return found;
466
504
  };
467
505
  const businessDayRuleMatches = (rule) => {
@@ -604,7 +642,17 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
604
642
  if (isCalendar && window && !dates.some((r) => r.value === true)) {
605
643
  const resolve = createResolver(without(compiled, null, true), lookup);
606
644
  let found = false;
607
- eachDay(window, (n) => (found = resolve(fromDayNumber(n)).day.value === true));
645
+ eachDay(window, (n) => {
646
+ try {
647
+ found = resolve(fromDayNumber(n)).day.value === true;
648
+ }
649
+ catch (error) {
650
+ if (!atCalendarEdge(n, error))
651
+ throw error;
652
+ found = false;
653
+ }
654
+ return found;
655
+ });
608
656
  if (!found) {
609
657
  c.warning("no-business-day", "", `No day is a business day with these options (checked ${window.label}); ` +
610
658
  "searching for a business day throws");
@@ -48,6 +48,9 @@ function wallParts(epochMilliseconds, timeZone) {
48
48
  export function instantToCivil(epochMilliseconds, timeZone) {
49
49
  if (!Number.isFinite(epochMilliseconds))
50
50
  throw rangeError("invalid-time", "Invalid instant");
51
+ if (Math.abs(epochMilliseconds) > 8.64e15) {
52
+ throw rangeError("out-of-range", "Instant out of range (1000-9999)");
53
+ }
51
54
  if (new Date(epochMilliseconds).getUTCFullYear() < 999) {
52
55
  throw rangeError("out-of-range", "Instant out of range (1000-9999)");
53
56
  }
@@ -111,7 +114,7 @@ export function toCivil(input, timeZone) {
111
114
  return instantToCivil(input.epochMilliseconds, timeZone);
112
115
  if (isDateLike(input)) {
113
116
  if (input.calendarId !== undefined && input.calendarId !== "iso8601") {
114
- throw typeError("invalid-input", `Unsupported calendar: ${input.calendarId} (only iso8601)`);
117
+ throw typeError("invalid-input", `Unsupported calendar: ${describeValue(input.calendarId)} (only iso8601)`);
115
118
  }
116
119
  if (!Number.isInteger(input.year) || input.year < 1000 || input.year > 9999) {
117
120
  throw rangeError("invalid-date", `Invalid date: year must be an integer from 1000 to 9999: ${describeValue(input.year)}`);
@@ -48,9 +48,10 @@ export function closingDayIn({ year, month }, closingDayOfMonth) {
48
48
  const last = lengthOfMonth(year, month);
49
49
  return closingDayOfMonth === -1 || closingDayOfMonth > last ? last : closingDayOfMonth;
50
50
  }
51
+ export const readsAsDate = (input) => input.split("-").length >= 3;
51
52
  export function closingPeriod(input, closingDayOfMonth) {
52
53
  checkClosingDay(closingDayOfMonth);
53
- const month = typeof input === "string" && input.split("-").length === 3
54
+ const month = typeof input === "string" && readsAsDate(input)
54
55
  ? parseMonth(closingMonth(input, closingDayOfMonth))
55
56
  : parseMonth(input);
56
57
  const previous = shiftMonth(month, -1);
@@ -759,7 +759,7 @@ function loadProviderYear(provider, year) {
759
759
  if (!Array.isArray(list))
760
760
  throw providerDataError(provider, year, "holidaysInYear must return an array");
761
761
  const seen = new Set();
762
- return list.map((h, i) => {
762
+ return Array.from(list, (h, i) => {
763
763
  if (!isObject(h))
764
764
  throw providerDataError(provider, year, `item ${i} is not an object`);
765
765
  const { date, name, nameEn, kind, confirmed } = h;
@@ -38,6 +38,8 @@ export interface Holiday<Kind extends string = string> {
38
38
  * holidaysInYear が返すデータは、年ごとに初めて使うときに検証され、誤りがあれば
39
39
  * code: "invalid-provider-data" の例外になる。返した祝日は、契約の項目(date・name・nameEn・names・
40
40
  * kind・confirmed・key)だけを持つ変更できないオブジェクトに複製される。
41
+ * 31 日を超えて続く祝日の期間では、"after-holiday" の振替と祝日の前後の規則は当てはまらない(前後を調べるのは 31 日まで。
42
+ * 長い休業は closedDates・closedAnnualDates で書く。decisions.md の 24 番)。
41
43
  *
42
44
  * 項目を足すときのバージョンは RELEASING.md の区分の表による。1.0.0 以降、マイナーバージョンで足す項目は省略可能なものに限る
43
45
  * (必須にすると、自作の提供元が型エラーになるため)。
@@ -54,7 +54,7 @@ function checkHolidays(holidays) {
54
54
  }
55
55
  let year;
56
56
  const seen = new Set();
57
- holidays.forEach((h, i) => {
57
+ Array.from(holidays).forEach((h, i) => {
58
58
  if (typeof h !== "object" || h === null) {
59
59
  throw rangeError("invalid-argument", `holidays[${i}] is not an object`);
60
60
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bizdays-toolkit",
3
- "version": "0.31.5",
3
+ "version": "0.31.6",
4
4
  "description": "Holidays, business-day calendar and day-by-day schedules (e.g. store hours) with time zone support. Japanese holidays verified against the Cabinet Office data; UK bank holidays (England, Wales, Scotland, Northern Ireland) verified against GOV.UK and The Gazette; French public holidays (metropole, Alsace-Moselle) verified against data.gouv.fr; Singapore public holidays verified against data.gov.sg. / 祝日と営業日のカレンダー(タイムゾーン対応・日本の祝日は内閣府、英国の bank holidays は GOV.UK と The Gazette、フランスの祝日は data.gouv.fr、シンガポールの祝日は data.gov.sg のデータで全日照合)",
5
5
  "keywords": [
6
6
  "bank-holidays",