tiny-ja-date 0.6.0 → 0.7.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/README.md CHANGED
@@ -85,7 +85,8 @@ formatRelativeJaDate(new Date(2026, 4, 15), new Date(2026, 4, 12))
85
85
 
86
86
  ### `isWeekendJP(date: DateInput, options?: DateOptions): boolean`
87
87
 
88
- Checks whether the date is Saturday or Sunday.
88
+ Checks whether the date is Saturday or Sunday, regardless of the weekly
89
+ closures used by the business-day functions.
89
90
 
90
91
  ```ts
91
92
  isWeekendJP(new Date(2026, 4, 16))
@@ -95,9 +96,10 @@ isWeekendJP(new Date(2026, 4, 16))
95
96
  ### `isBusinessDay(date: DateInput, options?: BusinessDayOptions): boolean`
96
97
 
97
98
  Checks the input date itself in the selected time zone. Returns `false` for
98
- Saturdays, Sundays, dates in `options.holidays`, and dates skipped entirely by
99
- the time zone. A weekday that starts after midnight can still be a business day.
100
- Invalid holiday inputs throw a `TypeError`, even when the input falls on a weekend.
99
+ weekly closures, dates in `options.holidays`, and dates skipped entirely by
100
+ the time zone. A date that starts after midnight can still be a business day.
101
+ Invalid holiday inputs throw a `TypeError`, even when the input falls on a
102
+ weekly closure.
101
103
 
102
104
  ```ts
103
105
  const options = { timeZone: "Asia/Tokyo", holidays: ["2026-11-23"] }
@@ -115,9 +117,9 @@ Returns a `Date` for the start of the next business day in the selected time
115
117
  zone. If midnight is skipped, the result is the first valid time on that date.
116
118
  If a time zone skipped an entire date, the search continues to the following date.
117
119
 
118
- The search starts the day after the input and skips Saturdays, Sundays, and
119
- any local calendar dates supplied in `options.holidays`. Each holiday accepts
120
- the same `DateInput` type as the input date.
120
+ The search starts the day after the input and skips weekly closures and dates
121
+ in `options.holidays`. Each holiday accepts the same `DateInput` type as the
122
+ input date.
121
123
 
122
124
  ```ts
123
125
  nextBusinessDay(new Date(2026, 4, 15))
@@ -130,7 +132,7 @@ nextBusinessDay("2026-05-15", { holidays: ["2026-05-18"] })
130
132
  ### `addBusinessDays(date: DateInput, amount: number, options?: BusinessDayOptions): Date`
131
133
 
132
134
  Counts forward for a positive amount and backward for a negative amount. Counting
133
- starts the day after or before the input and skips Saturdays, Sundays, dates in
135
+ starts the day after or before the input and skips weekly closures, dates in
134
136
  `options.holidays`, and dates skipped entirely by the time zone.
135
137
  `addBusinessDays(date, 1, options)` is equivalent to `nextBusinessDay(date, options)`.
136
138
 
@@ -157,7 +159,7 @@ zone. The starting date is excluded, and the destination counts if it is a
157
159
  business day. Counts are positive going forward and negative going backward.
158
160
  The same calendar date or a range with no business days returns `0`.
159
161
 
160
- Weekends, dates in `options.holidays`, and dates skipped entirely by the time
162
+ Weekly closures, dates in `options.holidays`, and dates skipped entirely by the time
161
163
  zone do not count. Both endpoints and all holiday inputs are validated even
162
164
  when the result is `0`.
163
165
 
@@ -171,6 +173,35 @@ countBusinessDays("2026-11-20", "2026-11-22", options) // 0
171
173
  countBusinessDays("2026-11-22", "2026-11-20", options) // -1
172
174
  ```
173
175
 
176
+ ## Weekly closures
177
+
178
+ `isBusinessDay`, `nextBusinessDay`, `addBusinessDays`, and `countBusinessDays`
179
+ accept `weekendDays?: readonly number[]` in `BusinessDayOptions`. Use integers
180
+ from `0` (Sunday) through `6` (Saturday).
181
+
182
+ - Omitting `weekendDays` closes Saturday and Sunday, equivalent to `[0, 6]`.
183
+ - An explicit array replaces the default. An empty array clears all weekly
184
+ closures. `holidays` still applies to individual dates.
185
+ - Duplicate weekdays count once. Values outside the integer range throw a
186
+ `RangeError`. A non-array value throws a `TypeError`. Validation also applies
187
+ to zero-day moves and same-date counts.
188
+ - Closing all seven days makes `isBusinessDay` return `false` and
189
+ `countBusinessDays` return `0`. `nextBusinessDay` and nonzero
190
+ `addBusinessDays` calls throw a `TypeError`. A zero-day move still returns the
191
+ start of the input date.
192
+
193
+ ```ts
194
+ const options = {
195
+ timeZone: "Asia/Tokyo",
196
+ weekendDays: [0],
197
+ holidays: ["2026-11-23"],
198
+ }
199
+
200
+ isBusinessDay("2026-11-21", options) // true (Saturday)
201
+ nextBusinessDay("2026-11-20", options) // Start of 2026-11-21 in Tokyo
202
+ countBusinessDays("2026-11-20", "2026-11-24", options) // 2 (Saturday and Tuesday)
203
+ ```
204
+
174
205
  ## Time zones
175
206
 
176
207
  All public functions accept an optional `timeZone` such as `"Asia/Tokyo"`.
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export interface DateOptions {
4
4
  }
5
5
  export interface BusinessDayOptions extends DateOptions {
6
6
  holidays?: readonly DateInput[];
7
+ weekendDays?: readonly number[];
7
8
  }
8
9
  export declare function formatJaDate(input: DateInput, options?: DateOptions): string;
9
10
  export declare function formatRelativeJaDate(input: DateInput, baseInput?: DateInput, options?: DateOptions): string;
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const WEEKDAYS_JA = ["日", "月", "火", "水", "木", "金", "土"];
2
+ const DEFAULT_WEEKEND_DAYS = new Set([0, 6]);
2
3
  const ONE_DAY_MS = 86400000;
3
4
  const MAX_DATE_MS = 8640000000000000;
4
5
  const GREGORIAN_CYCLE_MS = 146097 * ONE_DAY_MS;
@@ -140,12 +141,25 @@ function weekdayOf(day) {
140
141
  const weekday = (day / ONE_DAY_MS + 4) % 7;
141
142
  return (weekday + 7) % 7;
142
143
  }
143
- function isWeekend(day) {
144
- const weekday = weekdayOf(day);
145
- return weekday === 0 || weekday === 6;
144
+ function weekendDaySet(input) {
145
+ if (input === undefined)
146
+ return DEFAULT_WEEKEND_DAYS;
147
+ if (!Array.isArray(input))
148
+ throw new TypeError("Expected a weekendDays array");
149
+ const days = new Set();
150
+ for (const day of input) {
151
+ if (!Number.isInteger(day) || day < 0 || day >= WEEKDAYS_JA.length) {
152
+ throw new RangeError("Expected a weekend day from 0 to 6");
153
+ }
154
+ days.add(day);
155
+ }
156
+ return days;
146
157
  }
147
- function holidayDays(holidays, formatter) {
148
- return new Set(holidays?.map((holiday) => calendarDay(holiday, formatter)));
158
+ function businessCalendar(options, formatter) {
159
+ return {
160
+ holidays: new Set(options.holidays?.map((holiday) => calendarDay(holiday, formatter))),
161
+ weekendDays: weekendDaySet(options.weekendDays),
162
+ };
149
163
  }
150
164
  function dayStart(day, formatter) {
151
165
  if (formatter)
@@ -156,8 +170,8 @@ function dayStart(day, formatter) {
156
170
  local.setFullYear(year, month - 1, dayOfMonth);
157
171
  return local.getDate() === dayOfMonth ? local : undefined;
158
172
  }
159
- function businessDayStart(candidate, holidays, formatter) {
160
- if (isWeekend(candidate) || holidays.has(candidate))
173
+ function businessDayStart(candidate, calendar, formatter) {
174
+ if (calendar.weekendDays.has(weekdayOf(candidate)) || calendar.holidays.has(candidate))
161
175
  return undefined;
162
176
  return dayStart(candidate, formatter);
163
177
  }
@@ -182,26 +196,29 @@ export function formatRelativeJaDate(input, baseInput = new Date(), options = {}
182
196
  return `${diffDays}日後`;
183
197
  }
184
198
  export function isWeekendJP(input, options = {}) {
185
- return isWeekend(calendarDay(input, createFormatter(options.timeZone)));
199
+ const day = calendarDay(input, createFormatter(options.timeZone));
200
+ return DEFAULT_WEEKEND_DAYS.has(weekdayOf(day));
186
201
  }
187
202
  export function isBusinessDay(input, options = {}) {
188
203
  const formatter = createFormatter(options.timeZone);
189
204
  const day = dateOnlyDay(input) ?? calendarDay(input, formatter);
190
- const holidays = holidayDays(options.holidays, formatter);
191
- return businessDayStart(day, holidays, formatter) !== undefined;
205
+ const calendar = businessCalendar(options, formatter);
206
+ return businessDayStart(day, calendar, formatter) !== undefined;
192
207
  }
193
208
  export function addBusinessDays(input, amount, options = {}) {
194
209
  if (!Number.isSafeInteger(amount))
195
210
  throw new RangeError("Expected a safe integer business-day amount");
196
211
  const formatter = createFormatter(options.timeZone);
197
212
  const day = calendarDay(input, formatter);
198
- const holidays = holidayDays(options.holidays, formatter);
213
+ const calendar = businessCalendar(options, formatter);
199
214
  if (amount === 0) {
200
215
  const start = dayStart(day, formatter);
201
216
  if (!start)
202
217
  throw new TypeError("Invalid date");
203
218
  return start;
204
219
  }
220
+ if (calendar.weekendDays.size === WEEKDAYS_JA.length)
221
+ throw new TypeError("No business days");
205
222
  const direction = Math.sign(amount);
206
223
  const step = direction * ONE_DAY_MS;
207
224
  // A local calendar date can extend one day beyond the UTC Date limits.
@@ -210,7 +227,7 @@ export function addBusinessDays(input, amount, options = {}) {
210
227
  if (remaining > (limit - direction * day) / ONE_DAY_MS)
211
228
  throw new TypeError("Invalid date");
212
229
  for (let candidate = day + step; Math.abs(candidate) <= limit; candidate += step) {
213
- const start = businessDayStart(candidate, holidays, formatter);
230
+ const start = businessDayStart(candidate, calendar, formatter);
214
231
  if (!start)
215
232
  continue;
216
233
  remaining -= 1;
@@ -226,14 +243,16 @@ export function countBusinessDays(from, to, options = {}) {
226
243
  const formatter = createFormatter(options.timeZone);
227
244
  const start = dateOnlyDay(from) ?? calendarDay(from, formatter);
228
245
  const end = dateOnlyDay(to) ?? calendarDay(to, formatter);
229
- const holidays = holidayDays(options.holidays, formatter);
246
+ const calendar = businessCalendar(options, formatter);
247
+ if (calendar.weekendDays.size === WEEKDAYS_JA.length)
248
+ return 0;
230
249
  const direction = Math.sign(end - start);
231
250
  const step = direction * ONE_DAY_MS;
232
251
  let count = 0;
233
252
  let day = start;
234
253
  while (day !== end) {
235
254
  day += step;
236
- if (businessDayStart(day, holidays, formatter))
255
+ if (businessDayStart(day, calendar, formatter))
237
256
  count += direction;
238
257
  }
239
258
  return count;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiny-ja-date",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Tiny TypeScript date utilities for Japanese UI.",
5
5
  "type": "module",
6
6
  "exports": {